Avoid indentation in section titles


I'm using section titles not just for dividing the file into sections, but also (and mostly) as a great tool for bookmarking --- i.e. having links to various places in my code, often inside functions, that allows me to just to particular places in the function, etc. I find it as an extremely powerful tool for code navigation -- I even use it for links to "ToDo" items, etc.
My problem, though, is indentation. It seems that I have no control of it, and I do not even understand the logic beyond it. I can have a header in a if clause , and then one in the else clause, and the latter will be further indented. Then one that follows the if statement will be indented even one lever in , even though I would have expected it to be at a outer level of indentation.

Can someone please explain to me the indentation rules, and maybe provide some tips of how to control it? I see it constantly but in quite long and complex files, but I think I managed to boil it down to a simpler example:

# where things go wrong
foo <- function(x) {
  #:: Here is a first bookmark --- 
  if (x > 100) {
    # op for high x ----
    x <- x+1
  } else {
    # op for small x ----
    x <- x/2
  # Done exemining x ----

and that's how the outline looks like:

    op for high x
        op for small x
            done with examining x 

(please avoid syntax highlights, couldn't find a way to for the blog to show prefixed white spaces without using a code block; the indentation level is what matters)

Here it's not that bad, but with more complex code it gets to the point that I cannot see the "bookmarks" / titles in the outline because of over indentation. I just don't understand how the indentation system works, so cannot work around it.

Please advise.
Thank you

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.