protected override void OnSyntaxEditorSmartIndent(SyntaxEditor syntaxEditor, SmartIndentEventArgs e) { TokenStream tokenStream = syntaxEditor.Document.GetTokenStream(syntaxEditor.SelectedView.GetCurrentToken()); int nesting = 0; while (nesting != -1) { tokenStream.Seek(-1); if (tokenStream.Position < 0) { return; } switch (tokenStream.Peek().Key) { case "OpenCurlyBraceToken": nesting--; break; case "CloseCurlyBraceToken": nesting++; break; } } int indent = 0; if (nesting == -1) { int startIndex; IToken indentToken = GetLineIndent(tokenStream, out startIndex); indent = GetAbsoluteWhitespaceLength(indentToken, syntaxEditor.Document) + syntaxEditor.Document.TabSize; } e.IndentAmount = indent; }
/// <summary> /// Occurs when smart indent is to be performed. /// </summary> private void EditorSmartIndent(object sender, SmartIndentEventArgs e) { var editor = sender as ActiproSoftware.SyntaxEditor.SyntaxEditor; if (editor == null) return; // By default, SyntaxEditor initializes the e.IndentAmount properly for Block formatting. // You can change e.IndentAmount to provide "smart" indenting based on language context. if (!(editor.Document.Language is DynamicSyntaxLanguage)) return; // Increment indent if pressing ENTER after a curly brace switch (editor.Document.Language.Key) { case "C#": case "Java": case "JScript": case "PHP": { var stream = editor.Document.GetTextStream(editor.SelectedView.Selection.FirstOffset); var exitLoop = false; while (stream.Offset > 0) { stream.GoToPreviousToken(); switch (stream.Token.Key) { case "WhitespaceToken": // Ignore whitespace break; case "OpenCurlyBraceToken": e.IndentAmount++; exitLoop = true; break; default: exitLoop = true; break; } if (exitLoop) break; } break; } } // Add status message // this.WriteLine("SmartIndent: " + e.IndentAmount + " character(s)"); }
private void EditorSmartIndent(object sender, SmartIndentEventArgs e) { var editor = sender as SyntaxEditor; if (editor == null) return; if (!(editor.Document.Language is DynamicSyntaxLanguage)) return; // Increment indent if pressing ENTER after a curly brace switch (editor.Document.Language.Key) { case "C#": case "Java": case "JScript": case "PHP": { TextStream stream = editor.Document.GetTextStream(editor.SelectedView.Selection.FirstOffset); bool exitLoop = false; while (stream.Offset > 0) { stream.GoToPreviousToken(); switch (stream.Token.Key) { case "WhitespaceToken": // Ignore whitespace break; case "OpenCurlyBraceToken": e.IndentAmount++; exitLoop = true; break; default: exitLoop = true; break; } if (exitLoop) break; } } break; } }