示例#1
0
 private void Snippets_CloseMode()
 {
     if (snippetsMode != null)
     {
         snippetsMode.Close();
         snippetsMode = null;
     }
 }
示例#2
0
 private void Snippets_OnAutocompleteClose()
 {
     snippetsMode = null;
 }
示例#3
0
    private bool Snippets_Apply(Controller controller, Variant variant)
    {
        if (snippetsMode != null)
        {
            snippetsMode.NextEntry();
            return(true);
        }
        if (!controller.AllSelectionsEmpty)
        {
            return(false);
        }
        Buffer buffer = buffers.list.Selected;

        if (buffer != null && (buffer.settingsMode | SettingsMode.Normal) != 0)
        {
            Selection           selection = buffer.Controller.LastSelection;
            Place               place     = controller.Lines.PlaceOf(selection.anchor);
            SnippetFilesScanner scanner   = Nest.MainForm.SnippetFilesScanner;
            Line line = controller.Lines[place.iLine];
            {
                if (place.iChar <= line.GetFirstSpaces())
                {
                    return(false);
                }
            }
            scanner.TryRescan();
            string indent;
            int    tabsCount;
            line.GetFirstIntegerTabs(out indent, out tabsCount);
            List <SnippetInfo> infos       = scanner.GetInfos(buffer.Name);
            List <SnippetAtom> sortedAtoms = new List <SnippetAtom>();
            List <SnippetAtom> atoms       = new List <SnippetAtom>();
            scanner.LoadFiles(infos);
            foreach (SnippetInfo info in infos)
            {
                SnippetFile snippetFile = scanner.GetFile(info.path);
                if (snippetFile != null)
                {
                    sortedAtoms.AddRange(snippetFile.Atoms);
                }
            }
            sortedAtoms.Sort(SnippetAtom.Compare);
            int lastCount = -1;
            foreach (SnippetAtom atom in sortedAtoms)
            {
                bool matched = true;
                for (int i = 0; i < atom.key.Length; ++i)
                {
                    int iChar = place.iChar - atom.key.Length + i;
                    if (iChar < 0)
                    {
                        matched = false;
                        break;
                    }
                    if (atom.key[i] != line.chars[iChar].c)
                    {
                        matched = false;
                    }
                }
                if (matched)
                {
                    int iChar0 = place.iChar - atom.key.Length;
                    int iChar1 = iChar0 - 1;
                    if (iChar0 >= 0 && iChar1 >= 0)
                    {
                        char c0 = line.chars[iChar0].c;
                        char c1 = line.chars[iChar1].c;
                        if ((char.IsLetterOrDigit(c0) || c0 == '_') &&
                            (char.IsLetterOrDigit(c1) || c1 == '_'))
                        {
                            matched = false;
                        }
                    }
                }
                if (matched && (variant == null ||
                                atom.index == variant.Index && atom.GetCompletionText() == variant.DisplayText) &&
                    (lastCount == -1 || atom.key.Length == lastCount))
                {
                    atoms.Add(atom);
                    lastCount = atom.key.Length;
                }
            }
            if (atoms.Count > 1 && variant != null)
            {
                for (int i = atoms.Count; i-- > 0;)
                {
                    SnippetAtom atom = atoms[i];
                    if (i == variant.Index)
                    {
                        atoms.RemoveAt(i);
                    }
                }
            }
            if (atoms.Count == 1)
            {
                int position = selection.anchor;

                SnippetAtom atom = atoms[0];
                if (atom.desc != null && atom.desc.Trim().StartsWith("action:"))
                {
                    controller.ClearMinorSelections();
                    controller.LastSelection.anchor = position - atom.key.Length;
                    controller.LastSelection.caret  = position;
                    controller.EraseSelection();
                    CommandData   command = new CommandData("", atom.text.Trim());
                    StringBuilder errors  = new StringBuilder();
                    List <MacrosExecutor.Action> actions = command.GetActions(errors);
                    if (errors.Length == 0)
                    {
                        MulticaretTextBox.initMacrosExecutor.ExecuteSequence(actions);
                    }
                    else
                    {
                        controller.InsertText("ERROR: " + errors.ToString());
                    }
                }
                else
                {
                    Snippet snippet = new Snippet(
                        atom.GetIndentedText(indent, controller.Lines.TabSettings),
                        settings,
                        new SnippetReplaceValue(this, controller.Lines, position).ReplaceValue);

                    controller.ClearMinorSelections();
                    controller.LastSelection.anchor = position - atom.key.Length;
                    controller.LastSelection.caret  = position;
                    controller.InsertText(snippet.StartText);

                    snippetsMode = new SnippetMode(
                        textBox, controller, snippet, position - atom.key.Length, Snippets_OnAutocompleteClose);
                    snippetsMode.Show();
                }
                return(true);
            }
            if (atoms.Count > 1)
            {
                Snippets_ShowAutocomplete(atoms, true);
                return(true);
            }
        }
        return(false);
    }