PavelTorgashov / FastColoredTextBox

Fast Colored TextBox for Syntax Highlighting. The text editor component for .NET.
Other
1.21k stars 463 forks source link

Autocomplete after dot does not replace ^ with cursor position #171

Closed jaskaran0001 closed 5 years ago

jaskaran0001 commented 5 years ago

I am using the autocomplete after dot method as below to generate popup menu, It is working fine. The only issue I am facing is that it is not replacing ^ with the cursor position.

How can I automatically set the cursor position after a popup menu item is inserted in IDE?

Eg. com.examples() instead of com.examples(^) Using the below code:

` namespace Tester { public partial class AutocompleteSample4 : Form { AutocompleteMenu popupMenu;

    static readonly string[] sources = new string[]{
        "com",            
        "com.examples(^)"
    };

    public AutocompleteSample4()
    {
        InitializeComponent();

        //create autocomplete popup menu
        popupMenu = new AutocompleteMenu(fctb);
        popupMenu.SearchPattern = @"[\w\.]";

        //
        var items = new List<AutocompleteItem>();
        foreach (var item in sources)
            items.Add(new MethodAutocompleteItem2(item));

        popupMenu.Items.SetAutocompleteItems(items);
    }
}

/// <summary>
/// This autocomplete item appears after dot
/// </summary>
public class MethodAutocompleteItem2 : MethodAutocompleteItem
{
    string firstPart;
    string lastPart;

    public MethodAutocompleteItem2(string text)
        : base(text)
    {
        var i = text.LastIndexOf('.');
        if (i < 0)
            firstPart = text;
        else
        {
            firstPart = text.Substring(0, i);
            lastPart = text.Substring(i + 1);
        }
    }

    public override CompareResult Compare(string fragmentText)
    {
        int i = fragmentText.LastIndexOf('.');

        if (i < 0)
        {
            if (firstPart.StartsWith(fragmentText) && string.IsNullOrEmpty(lastPart))
                return CompareResult.VisibleAndSelected;
            //if (firstPart.ToLower().Contains(fragmentText.ToLower()))
              //  return CompareResult.Visible;
        }
        else
        {
            var fragmentFirstPart = fragmentText.Substring(0, i);
            var fragmentLastPart = fragmentText.Substring(i + 1);

            if (firstPart != fragmentFirstPart)
                return CompareResult.Hidden;

            if (lastPart != null && lastPart.StartsWith(fragmentLastPart))
                return CompareResult.VisibleAndSelected;

            if (lastPart != null && lastPart.ToLower().Contains(fragmentLastPart.ToLower()))
                return CompareResult.Visible;

        }

        return CompareResult.Hidden;
    }

    public override string GetTextForReplace()
    {
        if (lastPart == null)
            return firstPart;

        return firstPart + "." + lastPart;
    }

    public override string ToString()
    {
        if (lastPart == null)
            return firstPart;

        return lastPart;
    }

    class DeclarationSnippet : SnippetAutocompleteItem
    {
        public DeclarationSnippet(string snippet)
            : base(snippet)
        {
        }

        public override CompareResult Compare(string fragmentText)
        {
            var pattern = Regex.Escape(fragmentText);
            if (Regex.IsMatch(Text, "\\b" + pattern, RegexOptions.IgnoreCase))
                return CompareResult.Visible;

            return CompareResult.Hidden;
        }
    }
}

} `

Hexman768 commented 5 years ago

Can you perhaps make a gif of the issue so that I can better understand what's going on? This repository is no longer being maintained btw, so any code changes will most likely just be merged on my fork of the project.

jaskaran0001 commented 5 years ago

In the below situation want to set the cursor position inside the brackets instead of the caret. How can I auto-set the cursor position upon inserting a value from popup? Using the above mentioned code.

1661C778-DB13-451A-BAD4-9755B4D0AB92

jaskaran0001 commented 5 years ago

Following method needs to be added in MethodAutocompleteItem class I can add it the repository. Do you allow users to contribute? public override void OnSelected(AutocompleteMenu popupMenu, SelectedEventArgs e) { e.Tb.BeginUpdate(); e.Tb.Selection.BeginUpdate(); //remember places var p1 = popupMenu.Fragment.Start; var p2 = e.Tb.Selection.Start; //do auto indent if (e.Tb.AutoIndent) { for (int iLine = p1.iLine + 1; iLine <= p2.iLine; iLine++) { e.Tb.Selection.Start = new Place(0, iLine); e.Tb.DoAutoIndent(iLine); } } e.Tb.Selection.Start = p1; //move caret position right and find char ^ while (e.Tb.Selection.CharBeforeStart != '^') if (!e.Tb.Selection.GoRightThroughFolded()) break; //remove char ^ e.Tb.Selection.GoLeft(true); if (e.Tb.SelectedText == "^") { e.Tb.InsertText(""); } else { e.Tb.Selection.GoRight(true); } // e.Tb.Selection.EndUpdate(); e.Tb.EndUpdate();

Hexman768 commented 5 years ago

Sorry for the late response, yes, users can create a PR which I will then test and review. Here is a link to my fork: https://github.com/Hexman768/FastColoredTextBox