You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am to new avalonEdit and using it for my custom language, the lanuguage does not use { } for block instead it uses some keyword use as end. So i tried to implement feature that will automatically complete block for user and indent cursor, now the completion is working but indentation not working.
When user type in
functionname()
and press enter i want to have
functionname()
|
endfunction
instead i am getting
functionname()
|
endfunction
| representing cursor
Below is implementation the insertion working perfectly
using System;using System.Linq;using System.Windows;using System.Windows.Input;using ICSharpCode.AvalonEdit;using ICSharpCode.AvalonEdit.Document;publicclassAutocompleteHandler{privateTextEditoreditor;privateconststringTriggerWord="function";privateconststringClosingBlock="end function";publicAutocompleteHandler(TextEditoreditor){this.editor =editor;this.editor.TextArea.PreviewKeyDown +=TextArea_PreviewKeyDown;}privatevoidTextArea_PreviewKeyDown(objectsender,KeyEventArgse){if(e.Key == Key.Enter){intcurrentOffset= editor.CaretOffset;TextDocumentdocument= editor.Document;// Get the current lineDocumentLinecurrentLine= document.GetLineByOffset(currentOffset);intlineOffset= currentLine.Offset;stringlineText= document.GetText(currentLine);// Check if the line contains the trigger word followed by a valid function declarationif(IsValidFunctionDeclaration(lineText)){// Insert the closing blockstringindentation= GetLineIndentation(lineText);stringclosingBlockText=$"{indentation}{ClosingBlock}";// Update the content of the text in the editor
document.Insert(currentOffset,$"\n\n{closingBlockText}");// Adjust cursor positionintcursorLine= currentLine.LineNumber +1;// Line number of the inserted closing blockintcursorColumn= indentation.Length +4;// Cursor column position in the middle of the block
editor.TextArea.Caret.Line =cursorLine;
editor.TextArea.Caret.Column =cursorColumn;
editor.TextArea.Caret.BringCaretToView();// Mark the event as handled
e.Handled =true;}}}privateboolIsValidFunctionDeclaration(stringlineText){// Remove leading and trailing whitespaceslineText= lineText.Trim();// Check if the line starts with the trigger word "function"if(!lineText.StartsWith(TriggerWord, StringComparison.OrdinalIgnoreCase))returnfalse;// Check if the line ends with either an empty parameter list "()"// or a parameter list with at least one character insidereturn lineText.EndsWith("()")|| lineText.EndsWith(")");}privatestringGetLineIndentation(stringlineText){intindentationLength= lineText.TakeWhile(char.IsWhiteSpace).Count();return lineText.Substring(0, indentationLength);}}
The text was updated successfully, but these errors were encountered:
I am to new avalonEdit and using it for my custom language, the lanuguage does not use { } for block instead it uses some keyword use as end. So i tried to implement feature that will automatically complete block for user and indent cursor, now the completion is working but indentation not working.
When user type in
and press enter i want to have
instead i am getting
| representing cursor
Below is implementation the insertion working perfectly
The text was updated successfully, but these errors were encountered: