Skip to content
This repository has been archived by the owner on Jul 10, 2024. It is now read-only.

Commit

Permalink
autocaret
Browse files Browse the repository at this point in the history
  • Loading branch information
HendrikMennen committed Apr 29, 2024
1 parent ce2e2a8 commit b8d44c8
Showing 1 changed file with 38 additions and 23 deletions.
61 changes: 38 additions & 23 deletions src/OneWare.Essentials/LanguageService/TypeAssistanceBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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; }
Expand All @@ -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; }

Expand All @@ -40,7 +43,7 @@ protected TypeAssistanceBase(IEditor editor)
{
Editor = editor;
}

protected virtual void OnAssistanceActivated()
{
AssistanceActivated?.Invoke(this, EventArgs.Empty);
Expand All @@ -60,7 +63,7 @@ public virtual void Close()
{
IsOpen = false;
}

public virtual void Attach()
{
IsAttached = true;
Expand All @@ -76,19 +79,31 @@ protected virtual Task TextEnteredAsync(TextInputEventArgs e)
{
if (ContainerLocator.Container.Resolve<ISettingsService>().GetSettingValue<bool>("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;
}

Expand Down Expand Up @@ -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)
{
Expand All @@ -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)
{
Expand Down

0 comments on commit b8d44c8

Please sign in to comment.