Author Topic: Strange Indenting Request  (Read 1848 times)

pvonkaenel

  • Senior Community Member
  • Posts: 211
  • Hero Points: 6
Strange Indenting Request
« on: December 31, 2007, 08:20:32 pm »
Hi,

I have Slickedit configured to indent with tabs instead of spaces.  This allows different developers to view the code in a way they are use to by changing the number of spaces a tab represents.  This tends to work except for one case I keep bumping into.  Concider the following code fragment:

Code: [Select]
void MyRoutine(int param1,
               int param2,
               int param3)
{
  for (int i = 0; i < param1; i++)
    printf("%d\n", i);
}

If this code was constructed with 2 space tabs it looks good with 2 space tabs.  If the spacing is changes to 4, then it looks good except for the lines that define param2 and param3: they end up getting pushed out much farther than necessary.

Would it be possible to make a hybrid indenting mode that uses both tabs and spaces?  I think it should work if tabs are used only as far as they were used on the previous line, and then the rest of the alignment space is filled with spaces.

Thanks,
Peter

ScottW, VP of Dev

  • Senior Community Member
  • Posts: 1471
  • Hero Points: 64
Re: Strange Indenting Request
« Reply #1 on: January 03, 2008, 02:55:04 pm »
This is a common problem with using tabs for indenting and why most people recommend that you use spaces instead. Using spaces is the only way to ensure correct, consistent indenting that properly aligns all elements.

For the sample shown, there is no way to use tabs on the lines containing param2 and param3 and still have them align. Maybe that still meets your definition since the line above contains no tabs.

So, the approach you're offering would look at the line above it each time a tab is typed. If the corresponding column also contains a tab, then insert a tab. Otherwise insert spaces. The problem with this is that each new indentation level will always, by definition, have non-tab characters above it. Even if we have a special rule for column 0 that always inserts a tab, the for loop in your sample the printf line would use spaces for the second indent (below the "for" keyword). So we would only use a tab for the first indent level.

Or we could say that as soon as you have a line with mixed tabs and spaces, convert the whole line to spaces. The theory, here, is that the spaces are only being type when you are aligning things. But you will also have cases where the alignment works without having to type spaces, so those lines will not be converted and will look wrong with different values for tabs.

What would really be needed is a way to *know* when someone is aligning elements and provide some kind of marker between them so that if the lines are reformatted that alignemnt is preserved. That would also help in the case where someone renames an identifier. This would be pretty tough to implement and would require extensive anlaysis of a parse tree to recognize simliar semantic elements that fit predefined alignment patterns.

So, we're back to everyone using the same value for their indentation. Since you cannot guarantee that with tabs, converting tabs to spaces is the best solution.  Please let me know if I've missed something. I've tried to provide a thorough analysis so you can see that we've thought this through.

pvonkaenel

  • Senior Community Member
  • Posts: 211
  • Hero Points: 6
Re: Strange Indenting Request
« Reply #2 on: January 04, 2008, 07:16:56 pm »
I can see your point.  I hoping there was a simple solution like differentiating between syntax indenting where the tab would be used, and switch to spaces for other cases.  Thinking about it, however, the system would need to maintain that information line to line, and there would be a fair chance of messing it up.

Peter