Code::Blocks Forums

Developer forums (C::B DEVELOPMENT STRICTLY!) => Development => Topic started by: Alpha on March 01, 2013, 09:19:17 pm

Title: Auto detect indent style
Post by: Alpha on March 01, 2013, 09:19:17 pm
I am working on providing the option for indentation style to be automatically detected and set.  Probably the most logical method would be to call my detection routine from cbEditor::InternalSetEditorStyleAfterFileOpen() (similar to how DetectLineEnds(cbStyledTextCtrl* control) is called).  There is a problem, however.  I want to call GetLineIndentString(), but cannot because it is not a static method.

Program design question: would it be preferred that I convert GetLineIndentString() to a static method?  Or add a static counterpart?  Or move it to cbStyledTextCtrl?  Or something else?
Title: Re: Auto detect indent style
Post by: oBFusCATed on March 01, 2013, 10:24:48 pm
Program design question: would it be preferred that I convert GetLineIndentString() to a static method?  Or add a static counterpart?  Or move it to cbStyledTextCtrl?  Or something else?
If it doesn't access and class data, I'd vote for external/global function :)
Title: Re: Auto detect indent style
Post by: Alpha on March 02, 2013, 03:58:22 am
It does not access class data (well not from cbEditor, it does use from cbStyledTextCtrl).  Of note, I believe the SmartIndent plugins (and maybe Abbreviations) use this function.
Title: Re: Auto detect indent style
Post by: Alpha on March 04, 2013, 04:51:30 pm
Detecting tabs vs. spaces is easy, however, detecting 2-space indentation vs 4-space indentation (or any other arbitrary number of spaces) is a little more difficult.

I plan to create a threshold to determine the indentation style (and fall back to defaults if it is indeterminate).  To tune this to greater accuracy, I need to collect statistics on samples of multiple styles of code.

If anyone could post samples/links to different styles of code, I can run this test on them.
If you have time to test yourself, apply the attached patch, and post the numbers printed in the Code::Blocks log (along with what the indentation style of the file(s) actually is).
Title: Re: Auto detect indent style
Post by: oBFusCATed on March 04, 2013, 05:07:07 pm
If anyone could post samples/links to different styles of code, I can run this test on them.
Search github or code.google.com for examples, I suppose you can find all the possible styles know to man:)
Title: Re: Auto detect indent style
Post by: Alpha on March 04, 2013, 10:01:20 pm
Smart idea, thanks.  (Sometimes, it seems, I miss that which should be obvious :-\...)
Title: Re: Auto detect indent style
Post by: Alpha on March 16, 2013, 01:14:07 am
The attached patch should correctly identify (and set) the indentation style per editor, or, if identification is not strong enough, fallback to user defaults.
Title: Re: Auto detect indent style
Post by: ollydbg on March 16, 2013, 10:49:10 am
I see some source code which have both spaces and tabs in the same line. But I see that your patch handle either line begin with pure tabs or lines begin with pure spaces, right?

For example, the GDB's source code
a tab length = 8 space
but GDB's indent length = 2 space

I'm not sure your algorithm can detect this. :)
Title: Re: Auto detect indent style
Post by: Alpha on March 16, 2013, 04:04:03 pm
I'm not sure your algorithm can detect this. :)
Correct.  During my testing, I had no idea what the correct settings even would be in this situation.  I decided that in the case of mixed spaces/tabs, just allow the tab key to emit actual tabs, letting the programmer decide when/where they want to use tabs and spaces.
Title: Re: Auto detect indent style
Post by: ollydbg on March 16, 2013, 04:48:37 pm
My idea is:
1, firstly, guess the tab's length
2, secondly, guess the indent's length
I'm not thinking details. ;)
Title: Re: Auto detect indent style
Post by: oBFusCATed on April 06, 2013, 10:52:17 pm
Alpha:
Why this option is on by default?
I suppose many users will be annoyed if the function guesses wrong?
Title: Re: Auto detect indent style
Post by: Jenna on April 06, 2013, 11:06:51 pm
I would prefer for all such changesm that the former behaviour is not changed by default.
Even if some users will not notice the new functions immediately.
Title: Re: Auto detect indent style
Post by: Alpha on April 07, 2013, 12:54:22 am
I wrote the function to fail fast, so if it is not very certain, the default values are used.  That is why I put it on by default.  However, I can go turn it off.