Author Topic: Auto detect indent style  (Read 16362 times)

Offline Alpha

  • Developer
  • Lives here!
  • *****
  • Posts: 1513
Auto detect indent style
« 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?

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: Auto detect indent style
« Reply #1 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 :)
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline Alpha

  • Developer
  • Lives here!
  • *****
  • Posts: 1513
Re: Auto detect indent style
« Reply #2 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.

Offline Alpha

  • Developer
  • Lives here!
  • *****
  • Posts: 1513
Re: Auto detect indent style
« Reply #3 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).

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: Auto detect indent style
« Reply #4 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:)
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline Alpha

  • Developer
  • Lives here!
  • *****
  • Posts: 1513
Re: Auto detect indent style
« Reply #5 on: March 04, 2013, 10:01:20 pm »
Smart idea, thanks.  (Sometimes, it seems, I miss that which should be obvious :-\...)

Offline Alpha

  • Developer
  • Lives here!
  • *****
  • Posts: 1513
Re: Auto detect indent style
« Reply #6 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.

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 6035
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: Auto detect indent style
« Reply #7 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. :)
If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.

Offline Alpha

  • Developer
  • Lives here!
  • *****
  • Posts: 1513
Re: Auto detect indent style
« Reply #8 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.

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 6035
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: Auto detect indent style
« Reply #9 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. ;)
If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: Auto detect indent style
« Reply #10 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?
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline Jenna

  • Administrator
  • Lives here!
  • *****
  • Posts: 7256
Re: Auto detect indent style
« Reply #11 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.

Offline Alpha

  • Developer
  • Lives here!
  • *****
  • Posts: 1513
Re: Auto detect indent style
« Reply #12 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.