【发布时间】:2020-06-19 10:27:56
【问题描述】:
我正在使用 ScintillaNET 开发 Winforms 应用程序。我意识到 ScintillaNET 本身没有自动缩进功能。您必须自己创建一个。我在网上搜索并找到了使用大括号自动缩进的解决方案:Auto-indenting with curly brackets。
我决定在 ScintillaNET 中为 python 制作一个自动缩进功能。由于 Python 的语法不使用大括号,而是使用 :,因此引用的代码不适用。所以我尝试使用InsertChecked 功能在新行之前检查自动缩进触发器。基本上,如果用户键入 :,然后添加新行 \n,则表示已定义条件/类或定义。
为了确保我们不会误解用户尝试执行的操作,假设在 Python 中,您使用 string[1:2] 来获取子字符串,那么此功能将不适用。我们可以通过执行以下操作来确保。
if (caretPosition != 0 && caretPosition != ScintillaControl.Text.Length) //if not at the end or start
{
}
但到目前为止,我只有一个函数 auto indents after : but does not increment that indent by 4 per last line。这很奇怪,因为它应该能够得到最后一行的长度,然后加 4(缩进)。很难解释,我在下面提供了一个 GIF 图片:
那么,任何人都可以更好地实现我想要弄清楚的内容吗?或者一个函数,它采用最后一行长度,然后在出现触发字符时添加自动缩进?这是我的代码:
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';
}
}
帮我解决这个问题。
【问题讨论】:
标签: python c# .net winforms scintillanet