Autocompletion with indentation not working
まだ誰も着手していません。
評価
調査の方向性
提供されている TextArea_PreviewKeyDown の実装から始め、特に document.Insert と、その後のキャレットの位置決めを確認してください。示されている関数宣言を使って Enter キーの動作を再現し、実際のカーソルのインデントを要求された出力と比較してください。この issue では、リポジトリ内のファイルも実行するテストも指定されていません。
索引モデルが issue の本文から書いたものです。
説明
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
function name()
and press enter i want to have
function name()
|
end function
instead i am getting
function name()
|
end function
| 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;
public class AutocompleteHandler
{
private TextEditor editor;
private const string TriggerWord = "function";
private const string ClosingBlock = "end function";
public AutocompleteHandler(TextEditor editor)
{
this.editor = editor;
this.editor.TextArea.PreviewKeyDown += TextArea_PreviewKeyDown;
}
private void TextArea_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
int currentOffset = editor.CaretOffset;
TextDocument document = editor.Document;
// Get the current line
DocumentLine currentLine = document.GetLineByOffset(currentOffset);
int lineOffset = currentLine.Offset;
string lineText = document.GetText(currentLine);
// Check if the line contains the trigger word followed by a valid function declaration
if (IsValidFunctionDeclaration(lineText))
{
// Insert the closing block
string indentation = GetLineIndentation(lineText);
string closingBlockText = $"{indentation}{ClosingBlock}";
// Update the content of the text in the editor
document.Insert(currentOffset, $"\n\n{closingBlockText}");
// Adjust cursor position
int cursorLine = currentLine.LineNumber + 1; // Line number of the inserted closing block
int cursorColumn = 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;
}
}
}
private bool IsValidFunctionDeclaration(string lineText)
{
// Remove leading and trailing whitespaces
lineText = lineText.Trim();
// Check if the line starts with the trigger word "function"
if (!lineText.StartsWith(TriggerWord, StringComparison.OrdinalIgnoreCase))
return false;
// Check if the line ends with either an empty parameter list "()"
// or a parameter list with at least one character inside
return lineText.EndsWith("()") || lineText.EndsWith(")");
}
private string GetLineIndentation(string lineText)
{
int indentationLength = lineText.TakeWhile(char.IsWhiteSpace).Count();
return lineText.Substring(0, indentationLength);
}
}
- 主要言語
- C#
- スター
- 2.1k
- フォーク
- 496
- PR マージ指標
- 30日以内にマージされた PR はありません
環境構築
このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
icsharpcode/AvalonEdit のほかの issue
-
難易度 3/5 1〜2日 初心者へのやさしさ 76/100
icsharpcode/AvalonEdit#454 ·
-
難易度 4/5 3〜5日 初心者へのやさしさ 48/100
icsharpcode/AvalonEdit#449 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 48/100
icsharpcode/AvalonEdit#446 ·
-
難易度 4/5 3〜5日 初心者へのやさしさ 28/100
icsharpcode/AvalonEdit#445 · コメント 1 件 ·
-
Context menu ApplicationCommands.Cut still enabled when IsReadOnly = true対応中かも @JyslaFancy が 162 日前に担当しました。 オープン
難易度 3/5 1〜2日 初心者へのやさしさ 35/100
icsharpcode/AvalonEdit#444 ·
icsharpcode/AvalonEdit の issue をすべて見る
似ている issue
-
type:task ux
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
elsa-workflows/elsa-foundation#5253 ·
メンテナーはふだん 1 日以内に返信