简体   繁体   中英

Python auto-indentation in ScintillaNET

i am working on a Winforms application with ScintillaNET. I realized that ScintillaNET itself has no auto-indentation feature. You would have to create one yourself. I have searched online and found a solution to auto-indenting with curly brackets: Auto-indenting with curly brackets .

I decided to make an auto indentation feature for python in ScintillaNET. Since Python's syntax does not use curly brackets, but instead a : , the referenced code does not apply. So to i tried to make use of the InsertChecked feature to check for auto-indenting triggers before a new line. Basically if the user types a : and afterwards adds a new line \n , that is an indication that a condition/class or definition is defined.

To make sure that we don't misinterpret what the user is trying to do, say that in Python, you do string[1:2] to get a substring, then this feature will not apply. We can make sure by doing the following.

if (caretPosition != 0 && caretPosition != ScintillaControl.Text.Length) //if not at the end or start
{

}

But so far, i have a function that just auto indents after: but does not increment that indent by 4 per last line . It's weird because it should be able to get the last line length, then add by 4 (indent) . It's very hard to explain, i have provided a GIF image below:

在此处输入图像描述

在此处输入图像描述

So, anyone have a better implementation of what i'm trying to figure out? Or a function that takes the last line length then adds auto indents once a trigger char appears? Here's my code:

private void textarea_InsertCheck(object sender, InsertCheckEventArgs e)
    {            
        if ((e.Text.EndsWith("\r") || e.Text.EndsWith("\n")))
        {
            var curLine = TextArea.LineFromPosition(e.Position);
            var curLineText = TextArea.Lines[curLine].Text;

            var indent = Regex.Match(curLineText, @"");

            if (Regex.IsMatch(curLineText, @":"))
                e.Text += '\t';
        }            
    }

Help me on this one guys.

Alright, i see that no one has an idea to implement this, but THANK GOD i've been doing some research and found this old thread invisible to search engines that explains what i'm looking for: https://github.com/jacobslusser/ScintillaNET/issues/137

Whew.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM