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.