Example #1
0
    private bool Snippets_DoAutocomplete(Controller controller)
    {
        Buffer buffer = buffers.list.Selected;

        if (buffer != null && (buffer.settingsMode | SettingsMode.Normal) != 0)
        {
            SnippetFilesScanner scanner = MainForm.SnippetFilesScanner;
            scanner.TryRescan();
            List <SnippetInfo> infos = scanner.GetInfos(buffer.Name);
            List <SnippetAtom> atoms = new List <SnippetAtom>();
            if (infos != null && infos.Count > 0)
            {
                scanner.LoadFiles(infos);
                foreach (SnippetInfo info in infos)
                {
                    SnippetFile file = scanner.GetFile(info.path);
                    if (file != null)
                    {
                        foreach (SnippetAtom atom in file.Atoms)
                        {
                            atoms.Add(atom);
                        }
                    }
                }
            }
            if (atoms.Count > 0)
            {
                Snippets_ShowAutocomplete(atoms, false);
                return(true);
            }
        }
        return(false);
    }
        /// <summary>
        /// Load the snippet
        ///
        /// throws IOExcpetion if load failure
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public bool LoadSnippet(string fileName)
        {
            try
            {
                //load the snipept into memory
                snippetFile = new SnippetFile(fileName);

                snippetIndex = 0;

                //set this snippet as the active snippet
                ActiveSnippet = snippetFile.Snippets[snippetIndex];

                //populate the gui with this snippets information
                PullFieldsFromActiveSnippet();
                //indicate that this snippet is not dirty
                IsFormDirty = false;

                FormLoadFinished = true;
            }
            catch (IOException) //abort loading snippet, fail program
            {
                //since an io error occured
                throw;
            }

            return(true);
        }
Example #3
0
        /// <summary>
        /// Loads the data for this index item from a snippet file
        /// </summary>
        /// <param name="filePath">the path of the file</param>
        private bool AddOrUpdateSnippetsToIndexFromSnippetFile(string filePath)
        {
            try
            {
                SnippetFile snippetFile = new SnippetFile(filePath);
                foreach (Snippet currentSnippet in snippetFile.Snippets)
                {
                    SnippetIndexItem existingItem = null;
                    indexedSnippets.TryGetValue(GetDictionaryKey(filePath, currentSnippet.Title), out existingItem);
                    if (existingItem == null)
                    {
                        //add the item to the collection
                        CreateIndexItemDataFromSnippet(currentSnippet, filePath);
                    }
                    else
                    {
                        UpdateIndexItemData(existingItem, currentSnippet);
                    }
                }
            }
            catch (IOException e)
            {
                logger.Log("Unable to open snippet file at path: " + filePath, "SnippetIndex", e);
                return(false);
            }

            return(true);
        }
 public bool UpdateSnippetFile(SnippetFile snippetFile) => _liteRepository.Update <SnippetFile>(snippetFile);
 public void InsertSnippetFile(SnippetFile snippetFile) => _liteRepository.Insert <SnippetFile>(snippetFile);
Example #6
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);
    }
Example #7
0
        /// <summary>
        /// Update a  snippet item in the collection based upon the current filepath
        /// then swap the item with the new one
        /// </summary>
        /// <param name="updatedSnippet">The updated snippet.</param>
        /// <returns></returns>
        public bool UpdateSnippetFile(SnippetFile updatedSnippetFile)
        {
            // Find keys to remove
            List <string> keysToRemove = new List <string>();
            // Keys we found and updated
            List <string> foundKeys = new List <string>();

            // These have title changes to we need to create a new key for them
            List <Snippet> snippetsToAdd = new List <Snippet>();

            // Update snippets that have not changed titles
            foreach (Snippet snippet in updatedSnippetFile.Snippets)
            {
                SnippetIndexItem item = null;
                string           key  = GetDictionaryKey(updatedSnippetFile.FileName, snippet.Title);
                indexedSnippets.TryGetValue(key, out item);
                if (item != null)
                {
                    UpdateIndexItemData(item, snippet);
                    foundKeys.Add(key);
                }
                else
                {
                    snippetsToAdd.Add(snippet);
                }
            }


            if (snippetsToAdd.Count > 0)
            {
                // Figure out which keys are no longer valid
                foreach (string key in indexedSnippets.Keys)
                {
                    if (key.Contains(updatedSnippetFile.FileName.ToUpperInvariant()) &&
                        !foundKeys.Contains(key))
                    {
                        keysToRemove.Add(key);
                    }
                }

                // Since this file only has one snippet we know the one to update
                // so we don't need to re-add it
                if (updatedSnippetFile.Snippets.Count == 1 && keysToRemove.Count == 1)
                {
                    SnippetIndexItem item = null;
                    indexedSnippets.TryGetValue(keysToRemove[0], out item);
                    if (item != null)
                    {
                        UpdateIndexItemData(item, updatedSnippetFile.Snippets[0]);
                    }
                }
                else
                {
                    // Remove those keys
                    foreach (string key in keysToRemove)
                    {
                        lock (indexedSnippets)
                        {
                            indexedSnippets.Remove(key);
                        }
                    }

                    // Add update snippet items
                    foreach (Snippet snippet in snippetsToAdd)
                    {
                        CreateIndexItemDataFromSnippet(snippet, updatedSnippetFile.FileName);
                    }
                }
            }

            return(SaveIndexFile());
        }