Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Autocompletion with indentation not working #403

Open
Dangujba opened this issue Jun 29, 2023 · 0 comments
Open

Autocompletion with indentation not working #403

Dangujba opened this issue Jun 29, 2023 · 0 comments

Comments

@Dangujba
Copy link

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);
    }

} 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant