From b8d44c856933804ca434c762dd50365f7639f95f Mon Sep 17 00:00:00 2001 From: Hendrik Mennen Date: Mon, 29 Apr 2024 18:54:00 +0200 Subject: [PATCH] autocaret --- .../LanguageService/TypeAssistanceBase.cs | 61 ++++++++++++------- 1 file changed, 38 insertions(+), 23 deletions(-) diff --git a/src/OneWare.Essentials/LanguageService/TypeAssistanceBase.cs b/src/OneWare.Essentials/LanguageService/TypeAssistanceBase.cs index 737b795..af4d560 100644 --- a/src/OneWare.Essentials/LanguageService/TypeAssistanceBase.cs +++ b/src/OneWare.Essentials/LanguageService/TypeAssistanceBase.cs @@ -2,6 +2,7 @@ using AvaloniaEdit; using AvaloniaEdit.CodeCompletion; using AvaloniaEdit.Indentation; +using ImTools; using OneWare.Essentials.EditorExtensions; using OneWare.Essentials.Models; using OneWare.Essentials.Services; @@ -16,10 +17,12 @@ public abstract class TypeAssistanceBase : ITypeAssistance public string? LineCommentSequence { get; protected init; } protected IEditor Editor { get; } protected TextEditor CodeBox => Editor.Editor; - protected IFile CurrentFile => Editor.CurrentFile ?? throw new NullReferenceException(nameof(Editor.CurrentFile)); - + + protected IFile CurrentFile => + Editor.CurrentFile ?? throw new NullReferenceException(nameof(Editor.CurrentFile)); + protected IIndentationStrategy? IndentationStrategy { get; init; } - + protected IFormattingStrategy? FormattingStrategy { get; init; } protected CompletionWindow? Completion { get; set; } @@ -29,7 +32,7 @@ public abstract class TypeAssistanceBase : ITypeAssistance protected TextInputWindow? TextInput { get; set; } public IFoldingStrategy? FoldingStrategy { get; protected init; } - + protected bool IsOpen { get; private set; } protected bool IsAttached { get; private set; } @@ -40,7 +43,7 @@ protected TypeAssistanceBase(IEditor editor) { Editor = editor; } - + protected virtual void OnAssistanceActivated() { AssistanceActivated?.Invoke(this, EventArgs.Empty); @@ -60,7 +63,7 @@ public virtual void Close() { IsOpen = false; } - + public virtual void Attach() { IsAttached = true; @@ -76,19 +79,31 @@ protected virtual Task TextEnteredAsync(TextInputEventArgs e) { if (ContainerLocator.Container.Resolve().GetSettingValue("Editor_UseAutoBracket")) { - var autoBracket = e.Text switch - { - "{" => "}", - "(" => ")", - _ => null - }; - - if (autoBracket == null) return Task.CompletedTask; - - CodeBox.TextArea.Document.Insert(CodeBox.TextArea.Caret.Offset, autoBracket); - CodeBox.CaretOffset -= autoBracket.Length; + if (CodeBox.CaretOffset > CodeBox.Document.TextLength || CodeBox.CaretOffset < 2) return Task.CompletedTask; + + var lastChar = CodeBox.Document.Text[CodeBox.CaretOffset - 2]; + + switch (e.Text) + { + case "{": + CodeBox.TextArea.Document.Insert(CodeBox.TextArea.Caret.Offset, "}"); + CodeBox.CaretOffset--; + break; + case "(": + CodeBox.TextArea.Document.Insert(CodeBox.TextArea.Caret.Offset, ")"); + CodeBox.CaretOffset--; + break; + case ")" when lastChar is '(': + CodeBox.TextArea.Document.Remove(CodeBox.TextArea.Caret.Offset-1, 1); + CodeBox.CaretOffset++; + break; + case "}" when lastChar is '}': + CodeBox.TextArea.Document.Remove(CodeBox.TextArea.Caret.Offset-1, 1); + CodeBox.CaretOffset++; + break; + } } - + return Task.CompletedTask; } @@ -135,16 +150,16 @@ public virtual void AutoIndent(int startLine, int endLine) public virtual void Format() { - if(FormattingStrategy != null) FormattingStrategy.Format(CodeBox.Document); + if (FormattingStrategy != null) FormattingStrategy.Format(CodeBox.Document); else IndentationStrategy?.IndentLines(CodeBox.Document, 1, CodeBox.Document.LineCount); } #region Comment - + public virtual void Comment() { if (LineCommentSequence is null) return; - + int startLine, endLine; if (!CodeBox.TextArea.Selection.IsEmpty) { @@ -164,11 +179,11 @@ public virtual void Comment() CodeBox.Document.Replace(CodeBox.Document.Lines[i - 1].Offset, 0, LineCommentSequence); CodeBox.Document.EndUpdate(); } - + public virtual void Uncomment() { if (LineCommentSequence is null) return; - + int startLine, endLine; if (!CodeBox.TextArea.Selection.IsEmpty) {