Fold Based Indentation

112 views
Skip to first unread message

stewart81

unread,
Apr 13, 2012, 1:17:45 PM4/13/12
to scintilla-interest
Hi,

This is more just a check on something I'm doing to see if there are
any downsides that I am missing. I've been grappling with adding
intelligent auto indentation for various languages and I had
successfully implemented it for a couple of languages using a token
couple of custom token parsing scripts which works fine but I wanted
to extend it more rapidly as it's a bit time consuming to write a
token parsing class for each language so I decided to try a different
approach. The different approach is to look at the fold parent of the
new line when adding a new line. If the fold parent matches the
previous parent then I have it add in the extra indentation. To close
it off it checks the fold parent of the next line if available and if
the next line has a fold parent that is less than the current line
then it matches the indentation to the fold parent. Basically the
logic is to use the folding which already in theory does a good job of
recognizing the structure of the code instead of writing a token
parser to recognize the structure of the code. It appears to work well
enough but I can't help but wonder if I am missing a serious potential
problem with this method since I couldn't find any reference to
someone taking a similar course to achieve the result.

Thanks for any input on this.

Stewart

Lex Trotman

unread,
Apr 13, 2012, 9:06:57 PM4/13/12
to scintilla...@googlegroups.com

Well I can think of some things you need to watch out for:

1. won't work for languages that use indenting for the fold structure
or if fold by indentation is on (that's an egg laying a chicken)

2. will likely get it wrong when fold comments is on (without extra logic)

3. will likely get it wrong if explicit fold is on (without extra logic)

4. won't work when fold is off (make sure it doesn't accidentally
remove all indentation from the file)

5. won't work for languages without syntax based folding

Cheers
Lex


>
> Stewart
>
> --
> You received this message because you are subscribed to the Google Groups "scintilla-interest" group.
> To post to this group, send email to scintilla...@googlegroups.com.
> To unsubscribe from this group, send email to scintilla-inter...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/scintilla-interest?hl=en.
>

stewart81

unread,
Apr 14, 2012, 9:08:12 AM4/14/12
to scintilla-interest
Hi,

Thanks for the thoughts. I am only dealing with 8 languages
specifically and they all have folding support available. It's just so
time consuming writing a class to handle each one so I am kind of
looking for at least a temporary shortcut. I have considered the
possibility of modifying the applicable lexers and recompiling
scintilla but I haven't looked at this to determine how difficult this
process would be yet. I'm just thinking if they already have logic in
them to decide when a fold should be handled then it's possible they
may be able to be modified to also tell when an indentation should be
added or removed.

Thanks,

Stewart

On Apr 13, 9:06 pm, Lex Trotman <ele...@gmail.com> wrote:

Dave Brotherstone

unread,
Apr 14, 2012, 10:00:00 AM4/14/12
to scintilla...@googlegroups.com

Notepad++ has a plugin that does exactly this, and it works fantastically for every language I've tried.  I've not got a link, but if you have a look on the notepad++ wiki, there's a link on the plugin central page. I think it's called indentByFold.

Dave

stewart81

unread,
Apr 14, 2012, 10:46:09 AM4/14/12
to scintilla-interest
Hi Dave,

Thanks for the heads up. It works well for indenting but doesn't seem
to offer any kind of unindenting. I have considered using this logic
for auto indenting and just auto adding the closing tag but the big
problem I have with this route is that this method would strong arm
you into a specific structure. The application is a PHP editor with
project management. Not so much an IDE since I have never put in any
kind of built in syntax testing or anything of that nature but it
supports PHP, JavaScript, HTML, ASP, Perl and a few other web specific
languages and my problem with the one idea is it would force all tags
into breaking lines by default which I would personally find very
annoying. Tags such as <p>, <div>, etc I tend to break the line on for
clarity but tags such as <title> I never break the line on. I know
some people do and some don't. The idea I have seems to be working
quite well and since I know I am limited to specific language support
I think it should meet my needs. The biggest complaint I have with it
is completing a tag and I am trying to think of a way to deal with it.
Right now if you hit enter it ads the extra tab which is to be
expected and normal behavior but then when I close the current tag 2
or 3 lines down I have to hit enter again before it will push that
line back. I would prefer if I could find a way to detect that the
fold end had been achieved as soon as I type the last character. IE if
I am closing a body tag with </body> when I enter the last > it would
be nice if it could detect this and adjust. The closest so far I have
gotten is it works if there is an extra line because I can check the
fold parent against the fold parent of the last line.

stewart81

unread,
Apr 14, 2012, 11:31:23 AM4/14/12
to scintilla-interest
After some more experimentation I got it to work almost perfectly as
long as there is it is not on the last line. I am going to look into
the logic some more and see what I can come up with in that regard. I
am using SCI_GETLASTCHILD to get the last child of the parent fold if
applicable and then if the last child matches the current line
matching the indentation to the parent. This seems to work quite well
with the exception that if there is no line after the current position
it fails. From the documentation I get that this occurs because the
getlastchild finds the next line with a lower fold level than the
current line so as long as there is an extra line it's fine but if
there is no extra line then it has nothing to check against. Is there
anything that I'm missing in Scintilla that would allow me to have it
calculate past the end of the document?

Thank You,

Stewart

Neil Hodgson

unread,
Apr 15, 2012, 7:11:03 PM4/15/12
to scintilla...@googlegroups.com
stewart81:

> Is there
> anything that I'm missing in Scintilla that would allow me to have it
> calculate past the end of the document?

The lines end at the end of the document.

Neil

stewart81

unread,
Apr 17, 2012, 2:16:26 PM4/17/12
to scintilla-interest
Hi Neil,

I figured as much. I found a work around which was to basically tag on
an extra line when looking to determine if an indentation should be
closed up and if the current position is the end of the document. This
has worked well enough but it strikes me as a bit of a bad way to do
it.

Thank You,

Stewart
Reply all
Reply to author
Forward
0 new messages