// This method is called after the string has been committed to the source buffer. /// <summary> /// Called after the declaration has been committed to the source file. When implemented in a derived class, it provides a completion character which may itself be a trigger for another round of IntelliSense. /// </summary> /// <param name="textView">[in] An <see cref="T:Microsoft.VisualStudio.TextManager.Interop.IVsTextView"/> object representing the view that displays the source file.</param> /// <param name="committedText">[in] A string containing the text that was inserted as part of the completion process.</param> /// <param name="commitCharacter">[in] The character that was used to commit the text to the source file.</param> /// <param name="index">[in] The index of the item that was committed to the source file.</param> /// <returns> /// Returns a character to be inserted after the committed text. If nothing is to be inserted, returns 0. /// </returns> public override char OnAutoComplete(IVsTextView textView, string committedText, char commitCharacter, int index) { /* * // Do not replace when committed with \13 (enter) * if (commitCharacter == 13) * return '\0'; */ if (index < 0) { return('\0'); } Declaration declaration = declarations[index]; if (declaration != null) { // In this example, MyDeclaration identifies types with a string. // You can choose a different approach. if (declaration.DeclarationType == DeclarationType.Snippet) { Source source = languageService.GetSource(textView); if (source != null) { ExpansionProvider expansionProvider = source.GetExpansionProvider(); if (expansionProvider != null) { string title; string path; int commitLength = commitSpan.iEndIndex - commitSpan.iStartIndex; if (commitLength < committedText.Length) { // Replace everything that was inserted // so calculate the span of the full // insertion, taking into account what // was inserted when the commitSpan // was obtained in the first place. commitSpan.iEndIndex += (committedText.Length - commitLength); } if (expansionProvider.FindExpansionByShortcut(textView, committedText, commitSpan, true, out title, out path) == 0) { expansionProvider.InsertNamedExpansion(textView, title, path, commitSpan, false); } } } } } return('\0'); }
// This method is called after the string has been committed to the source buffer. public override char OnAutoComplete(IVsTextView textView, string committedText, char commitCharacter, int index) { LuaDeclarations item = declarations[index] as LuaDeclarations; if (item != null) { // In this example, TestDeclaration identifies types with a string. // You can choose a different approach. if (item.GetType().Name == "snippet") { Source src = languageService.GetSource(textView); if (src != null) { ExpansionProvider ep = src.GetExpansionProvider(); if (ep != null) { string title; string path; int commitLength = commitSpan.iEndIndex - commitSpan.iStartIndex; if (commitLength < committedText.Length) { // Replace everything that was inserted // so calculate the span of the full // insertion, taking into account what // was inserted when the commitSpan // was obtained in the first place. commitSpan.iEndIndex += (committedText.Length - commitLength); } if (ep.FindExpansionByShortcut(textView, committedText, commitSpan, true, out title, out path) != 0) { ep.InsertNamedExpansion(textView, title, path, commitSpan, false); } } } } } return('\0'); }
// This method is called after the string has been committed to the source buffer. public override char OnAutoComplete(IVsTextView textView, string committedText, char commitCharacter, int index) { const char defaultReturnValue = '\0'; Declaration item = declarations[index] as Declaration; if (item == null) { return(defaultReturnValue); } // In this example, PythonDeclaration identifies types with an enum. // You can choose a different approach. if (item.Type != Declaration.DeclarationType.Snippet) { return(defaultReturnValue); } Source src = languageService.GetSource(textView); if (src == null) { return(defaultReturnValue); } ExpansionProvider ep = src.GetExpansionProvider(); if (ep == null) { return(defaultReturnValue); } string title; string path; int commitLength = commitSpan.iEndIndex - commitSpan.iStartIndex; if (commitLength < committedText.Length) { // Replace everything that was inserted so calculate the span of the full // insertion, taking into account what was inserted when the commitSpan // was obtained in the first place. commitSpan.iEndIndex += (committedText.Length - commitLength); } if (ep.FindExpansionByShortcut(textView, committedText, commitSpan, true, out title, out path) >= 0) { ep.InsertNamedExpansion(textView, title, path, commitSpan, false); } return(defaultReturnValue); }
public override bool HandlePreExec(ref Guid guidCmdGroup, uint nCmdId, uint nCmdexecopt, IntPtr pvaIn, IntPtr pvaOut) { if (guidCmdGroup == VSConstants.VSStd2K) { if (nCmdId == (uint)VSConstants.VSStd2KCmdID.INSERTSNIPPET) { // Handle the Insert Snippet command by showing the UI to insert a snippet. ExpansionProvider ep = GetExpansionProvider(); if (ep != null && TextView != null) { ep.DisplayExpansionBrowser(TextView, CMakeStrings.CMakeSnippet, null, false, null, false); } return(true); } else if (nCmdId == (uint)VSConstants.VSStd2KCmdID.OPENFILE) { // Handle the Open File by opening the file specified by the current // token. string extraSearchPath; string fileName = GetCurrentTokenFileName(out extraSearchPath); if (fileName == null) { return(false); } string curFileDir = Path.GetDirectoryName(Source.GetFilePath()); string filePath = Path.Combine(curFileDir, fileName); if (!File.Exists(filePath)) { filePath = null; if (extraSearchPath != null) { filePath = Path.Combine(extraSearchPath, fileName); if (!File.Exists(filePath)) { filePath = null; } } } if (filePath != null) { VsShellUtilities.OpenDocument(ServiceProvider.GlobalProvider, filePath); } else { MessageBox.Show(string.Format(CMakeStrings.FileNotFound, fileName), CMakeStrings.MessageBoxTitle, MessageBoxButtons.OK, MessageBoxIcon.Stop); } return(true); } else if (nCmdId == (uint)VSConstants.VSStd2KCmdID.TAB) { // Handle the tab key when the caret is positioned immediately after // the name of a snippet by inserting that snippet. ExpansionProvider ep = GetExpansionProvider(); if (ep == null || TextView == null || ep.InTemplateEditingMode) { return(false); } int line; int col; if (TextView.GetCaretPos(out line, out col) != VSConstants.S_OK) { return(false); } TokenInfo tokenInfo = Source.GetTokenInfo(line, col); if (tokenInfo.StartIndex == tokenInfo.EndIndex) { return(false); } TextSpan span = tokenInfo.ToTextSpan(line); string shortcut = Source.GetText(span); if (string.IsNullOrEmpty(shortcut)) { return(false); } string title; string path; if (ep.FindExpansionByShortcut(TextView, shortcut, span, true, out title, out path) != VSConstants.S_OK) { return(false); } return(ep.InsertNamedExpansion(TextView, title, path, span, false)); } else if (nCmdId == (uint)VSConstants.VSStd2KCmdID.RETURN) { // Dismiss method tips when the user presses enter. They interfere // with the proper functioning of smart indentation. if (Source.IsCompletorActive && !Source.CompletionSet.IsDisplayed) { Source.DismissCompletor(); } } } else if (guidCmdGroup == VSConstants.GUID_VSStandardCommandSet97) { if (nCmdId == (uint)VSConstants.VSStd97CmdID.F1Help) { DoContextHelp(); return(true); } } return(base.HandlePreExec(ref guidCmdGroup, nCmdId, nCmdexecopt, pvaIn, pvaOut)); }