public Declaration(VsExpansion expansion) { this.Type = DeclarationType.Snippet; this.shortcut = expansion.shortcut; this.title = expansion.title; this.description = expansion.description; }
/// <summary> /// Constructor used by IPy snippets retrieved from the expansion manager /// </summary> internal PyCompletion(VsExpansion vsExpansion, IGlyphService glyphService) : base(vsExpansion.title) { this.InsertionText = vsExpansion.title; this.Description = vsExpansion.description; this.IconSource = glyphService.GetGlyph(StandardGlyphGroup.GlyphCSharpExpansion, StandardGlyphItem.GlyphItemPublic); this.VsExpansion = vsExpansion; }
internal SpringCompletion(IGlyphService glyphService, VsExpansion vsExpansion) : base(vsExpansion.title) { this.DisplayText = vsExpansion.shortcut; this.InsertionText = vsExpansion.title; this.Description = vsExpansion.description; this.IconSource = this.GetIconSource(glyphService, SpringCompletionType.Snippet); this.VsExpansion = vsExpansion; }
/// <summary> /// Caches expansions returned by IVsExpansionManager for a given language services. /// </summary> private void CacheLanguageExpansionStructs(IVsExpansionManager expansionManager) { if (_expansions.Keys.Count > 0) { return; } IVsExpansionEnumeration expansionEnumerator = null; int hr = expansionManager.EnumerateExpansions( RGuidList.RLanguageServiceGuid, 0, // return all info null, // return all types 0, // return all types 0, // do not return NULL type 0, // do not return duplicates out expansionEnumerator ); ErrorHandler.ThrowOnFailure(hr); if (expansionEnumerator != null) { VsExpansion expansion = new VsExpansion(); IntPtr[] pExpansionInfo = new IntPtr[1]; try { // Allocate enough memory for one VSExpansion structure. // This memory is filled in by the Next method. pExpansionInfo[0] = Marshal.AllocCoTaskMem(Marshal.SizeOf(expansion)); uint count = 0; expansionEnumerator.GetCount(out count); for (uint i = 0; i < count; i++) { uint fetched = 0; expansionEnumerator.Next(1, pExpansionInfo, out fetched); if (fetched > 0) { // Convert the returned blob of data into a structure. expansion = (VsExpansion)Marshal.PtrToStructure(pExpansionInfo[0], typeof(VsExpansion)); if (!string.IsNullOrEmpty(expansion.shortcut)) { _expansions[expansion.shortcut] = expansion; } } } } finally { if (pExpansionInfo[0] != null) { Marshal.FreeCoTaskMem(pExpansionInfo[0]); } } } }
private static VsExpansion ConvertToVsExpansionAndFree(ExpansionBuffer buffer) { VsExpansion expansion = new VsExpansion(); ConvertToStringAndFree(ref buffer.descriptionPtr, ref expansion.description); ConvertToStringAndFree(ref buffer.pathPtr, ref expansion.path); ConvertToStringAndFree(ref buffer.shortcutPtr, ref expansion.shortcut); ConvertToStringAndFree(ref buffer.titlePtr, ref expansion.title); return(expansion); }
private static VsExpansion ConvertToVsExpansionAndFree(IntPtr expansionPtr) { var buffer = (VsExpansionWithIntPtrs)Marshal.PtrToStructure(expansionPtr, typeof(VsExpansionWithIntPtrs)); var expansion = new VsExpansion(); ConvertToStringAndFree(ref buffer.DescriptionPtr, ref expansion.description); ConvertToStringAndFree(ref buffer.PathPtr, ref expansion.path); ConvertToStringAndFree(ref buffer.ShortcutPtr, ref expansion.shortcut); ConvertToStringAndFree(ref buffer.TitlePtr, ref expansion.title); return(expansion); }
protected virtual Completion CreateSnippetCompletion(VsExpansion expansion, ImageSource defaultExpansionGlyph, IconDescription defaultIconDescription) { if (string.IsNullOrEmpty(expansion.shortcut)) { return(null); } string displayText = expansion.shortcut; string insertionText = expansion.shortcut; string description = expansion.description; return(new Completion(displayText, insertionText, description, defaultExpansionGlyph, defaultIconDescription.ToString())); }
internal void AddSnippets(ref TestAuthoringScope scope) { if (this.expansionsList != null && this.expansionsList.Count > 0) { int count = this.expansionsList.Count; for (int i = 0; i < count; i++) { VsExpansion expansionInfo = (VsExpansion)this.expansionsList[i]; scope.AddDeclarations(new TestDeclaration(expansionInfo.title, expansionInfo.description, "snippet")); } } }
public static VsExpansion[] EnumerateExpansions(this IVsExpansionManager expansionManager, Guid language, string[] snippetTypes, bool shortcutsOnly) { Contract.Requires <ArgumentNullException>(expansionManager != null, "expansionManager"); Contract.Ensures(Contract.Result <VsExpansion[]>() != null); bool includeNullType = false; bool includeDuplicates = false; IVsExpansionEnumeration expEnum = null; if (ErrorHandler.Succeeded(ErrorHandler.CallWithCOMConvention(() => expansionManager.EnumerateExpansions(language, shortcutsOnly ? 1 : 0, snippetTypes, snippetTypes.Length, includeNullType ? 1 : 0, includeDuplicates ? 1 : 0, out expEnum)))) { uint count; ErrorHandler.ThrowOnFailure(expEnum.GetCount(out count)); IntPtr[] raw = new IntPtr[count]; try { for (int i = 0; i < raw.Length; i++) { raw[i] = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(VsExpansion))); } uint fetched; ErrorHandler.ThrowOnFailure(expEnum.Next(count, raw, out fetched)); VsExpansion[] results = new VsExpansion[fetched]; for (int i = 0; i < results.Length; i++) { if (raw[i] != IntPtr.Zero) { results[i] = (VsExpansion)Marshal.PtrToStructure(raw[i], typeof(VsExpansion)); } } return(results); } finally { foreach (IntPtr p in raw) { Marshal.FreeCoTaskMem(p); } } } return(new VsExpansion[0]); }
public static VsExpansion[] EnumerateExpansions(this IVsExpansionManager expansionManager, Guid language, string[] snippetTypes, bool shortcutsOnly) { Contract.Requires<ArgumentNullException>(expansionManager != null, "expansionManager"); Contract.Ensures(Contract.Result<VsExpansion[]>() != null); bool includeNullType = false; bool includeDuplicates = false; IVsExpansionEnumeration expEnum; if (ErrorHandler.Succeeded(expansionManager.EnumerateExpansions(language, shortcutsOnly ? 1 : 0, snippetTypes, snippetTypes.Length, includeNullType ? 1 : 0, includeDuplicates ? 1 : 0, out expEnum))) { uint count; ErrorHandler.ThrowOnFailure(expEnum.GetCount(out count)); IntPtr[] raw = new IntPtr[count]; try { for (int i = 0; i < raw.Length; i++) raw[i] = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(VsExpansion))); uint fetched; ErrorHandler.ThrowOnFailure(expEnum.Next(count, raw, out fetched)); VsExpansion[] results = new VsExpansion[fetched]; for (int i = 0; i < results.Length; i++) { if (raw[i] != IntPtr.Zero) { results[i] = (VsExpansion)Marshal.PtrToStructure(raw[i], typeof(VsExpansion)); } } return results; } finally { foreach (IntPtr p in raw) { Marshal.FreeCoTaskMem(p); } } } return new VsExpansion[0]; }
private ImmutableArray <SnippetInfo> ExtractSnippetInfo(IVsExpansionEnumeration expansionEnumerator) { AssertIsForeground(); var snippetListBuilder = ImmutableArray.CreateBuilder <SnippetInfo>(); uint count = 0; uint fetched = 0; VsExpansion snippetInfo = new VsExpansion(); IntPtr[] pSnippetInfo = new IntPtr[1]; try { // Allocate enough memory for one VSExpansion structure. This memory is filled in by the Next method. pSnippetInfo[0] = Marshal.AllocCoTaskMem(Marshal.SizeOf(snippetInfo)); expansionEnumerator.GetCount(out count); for (uint i = 0; i < count; i++) { expansionEnumerator.Next(1, pSnippetInfo, out fetched); if (fetched > 0) { // Convert the returned blob of data into a structure that can be read in managed code. snippetInfo = ConvertToVsExpansionAndFree(pSnippetInfo[0]); if (!string.IsNullOrEmpty(snippetInfo.shortcut)) { snippetListBuilder.Add(new SnippetInfo(snippetInfo.shortcut, snippetInfo.title, snippetInfo.description, snippetInfo.path)); } } } } finally { Marshal.FreeCoTaskMem(pSnippetInfo[0]); } return(snippetListBuilder.ToImmutable()); }
private void InsertCodeExpansion(VsExpansion expansion, int startLine, int startColumn, int endLine, int endColumn) { // Insert the selected code snippet and start an expansion session IVsTextLines buffer; vsTextView.GetBuffer(out buffer); // Get the IVsExpansion from the current IVsTextLines IVsExpansion vsExpansion = (IVsExpansion)buffer; // Call the actual method that performs the snippet insertion vsExpansion.InsertNamedExpansion( expansion.title, expansion.path, new TextSpan { iStartIndex = startColumn, iEndIndex = endColumn, iEndLine = endLine, iStartLine = startLine }, null, GuidList.guidSpringLanguage, 0, out expansionSession); }
private void InsertCodeExpansion(VsExpansion expansion) { int startLine, startColumn, endLine, endColumn; if (completionSession != null) { // if there is an active completion session we need to use the trigger point of that session int position = completionSession.GetTriggerPoint(completionSession.TextView.TextBuffer).GetPosition(textView.TextBuffer.CurrentSnapshot); startLine = textView.TextBuffer.CurrentSnapshot.GetLineNumberFromPosition(position); startColumn = position - textView.TextBuffer.CurrentSnapshot.GetLineFromPosition(position).Start.Position; this.vsTextView.GetCaretPos(out endLine, out endColumn); } else { // there is no active completion session so we would use the caret position of the view instead this.vsTextView.GetCaretPos(out startLine, out startColumn); endColumn = startColumn; endLine = startLine; } InsertCodeExpansion(expansion, startLine, startColumn, endLine, endColumn); }
private static VsExpansion ConvertToVsExpansionAndFree(IntPtr expansionPtr) { var buffer = (VsExpansionWithIntPtrs)Marshal.PtrToStructure(expansionPtr, typeof(VsExpansionWithIntPtrs)); var expansion = new VsExpansion(); ConvertToStringAndFree(ref buffer.DescriptionPtr, ref expansion.description); ConvertToStringAndFree(ref buffer.PathPtr, ref expansion.path); ConvertToStringAndFree(ref buffer.ShortcutPtr, ref expansion.shortcut); ConvertToStringAndFree(ref buffer.TitlePtr, ref expansion.title); return expansion; }
private ImmutableArray<SnippetInfo> ExtractSnippetInfo(IVsExpansionEnumeration expansionEnumerator) { AssertIsForeground(); var snippetListBuilder = ImmutableArray.CreateBuilder<SnippetInfo>(); uint count = 0; uint fetched = 0; VsExpansion snippetInfo = new VsExpansion(); IntPtr[] pSnippetInfo = new IntPtr[1]; try { // Allocate enough memory for one VSExpansion structure. This memory is filled in by the Next method. pSnippetInfo[0] = Marshal.AllocCoTaskMem(Marshal.SizeOf(snippetInfo)); expansionEnumerator.GetCount(out count); for (uint i = 0; i < count; i++) { expansionEnumerator.Next(1, pSnippetInfo, out fetched); if (fetched > 0) { // Convert the returned blob of data into a structure that can be read in managed code. snippetInfo = ConvertToVsExpansionAndFree(pSnippetInfo[0]); if (!string.IsNullOrEmpty(snippetInfo.shortcut)) { snippetListBuilder.Add(new SnippetInfo(snippetInfo.shortcut, snippetInfo.title, snippetInfo.description, snippetInfo.path)); } } } } finally { Marshal.FreeCoTaskMem(pSnippetInfo[0]); } return snippetListBuilder.ToImmutable(); }
public int Exec(ref Guid pguidCmdGroup, uint nCmdID, uint nCmdexecopt, IntPtr pvaIn, IntPtr pvaOut) { if (pguidCmdGroup == VSConstants.VSStd2K) { if (nCmdID == (uint)VSConstants.VSStd2KCmdID.INSERTSNIPPET || nCmdID == (uint)VSConstants.VSStd2KCmdID.SURROUNDWITH) { IVsTextManager2 textManager = (IVsTextManager2)this.serviceProvider.GetService(typeof(SVsTextManager)); IVsExpansionManager expansionManager; if (VSConstants.S_OK == textManager.GetExpansionManager(out expansionManager)) { expansionManager.InvokeInsertionUI( vsTextView, this, GuidList.guidSpringLanguage, new string[] { "Expansion" }, 1, 0, null, 0, 1, "Insert Snippet", string.Empty); } return(VSConstants.S_OK); } if (this.expansionSession != null) { // Handle VS Expansion (Code Snippets) keys if ((nCmdID == (uint)VSConstants.VSStd2KCmdID.TAB)) { if (expansionSession.GoToNextExpansionField(0) == VSConstants.S_OK) { return(VSConstants.S_OK); } } else if ((nCmdID == (uint)VSConstants.VSStd2KCmdID.BACKTAB)) { if (expansionSession.GoToPreviousExpansionField() == VSConstants.S_OK) { return(VSConstants.S_OK); } } else if ((nCmdID == (uint)VSConstants.VSStd2KCmdID.RETURN || nCmdID == (uint)VSConstants.VSStd2KCmdID.CANCEL)) { if (expansionSession.EndCurrentExpansion(0) == VSConstants.S_OK) { expansionSession = null; return(VSConstants.S_OK); } } } // Handle Edit.ListMembers or Edit.CompleteWord commands if ((nCmdID == (uint)VSConstants.VSStd2KCmdID.SHOWMEMBERLIST || nCmdID == (uint)VSConstants.VSStd2KCmdID.COMPLETEWORD)) { if (completionSession != null) { completionSession.Dismiss(); } ShowCompletion(); return(VSConstants.S_OK); } // Handle Enter/Tab commit keys if (completionSession != null && (nCmdID == (uint)VSConstants.VSStd2KCmdID.RETURN || nCmdID == (uint)VSConstants.VSStd2KCmdID.TAB)) { if (completionSession.SelectedCompletionSet.SelectionStatus.IsSelected) { completionSession.Commit(); } else { completionSession.Dismiss(); } return(VSConstants.S_OK); } // Handle Code Snippets after pressing the Tab key without completion if (completionSession == null && (nCmdID == (uint)VSConstants.VSStd2KCmdID.TAB)) { IVsTextManager2 expansionManager = (IVsTextManager2)this.serviceProvider.GetService(typeof(SVsTextManager)); SnippetsEnumerable snippetsEnumerator = new SnippetsEnumerable(expansionManager, GuidList.guidSpringLanguage); SnapshotPoint currentPoint = (this.textView.Caret.Position.BufferPosition) - 1; ITextStructureNavigator navigator = this.textStructureNavigatorSelectorService.GetTextStructureNavigator(this.textView.TextBuffer); TextExtent extent = navigator.GetExtentOfWord(currentPoint); string shortcut = this.textView.TextSnapshot.GetText(extent.Span); // Search a snippet that matched the token text VsExpansion expansion = snippetsEnumerator.FirstOrDefault(e => e.title == shortcut); if (expansion.title != null) { // Set the location where the snippet will be inserted int startLine, startColumn, endLine, endColumn; this.vsTextView.GetCaretPos(out startLine, out endColumn); startColumn = endColumn - expansion.title.Length; endLine = startLine; // Insert the snippet InsertCodeExpansion(expansion, startLine, startColumn, endLine, endColumn); return(VSConstants.S_OK); } } // Hanlde other keys if ((nCmdID == (uint)VSConstants.VSStd2KCmdID.TYPECHAR)) { char typedChar = (char)(ushort)Marshal.GetObjectForNativeVariant(pvaIn); if (completionSession == null) { // Handle trigger keys // Check if the typed char is a trigger if (IsTriggerKey(typedChar)) { var result = this.nextCommandTarget.Exec(pguidCmdGroup, nCmdID, nCmdexecopt, pvaIn, pvaOut); ShowCompletion(); return(result); } } else { // Handle commit keys // Check if the typed char is a commit key if (IsCommitKey(typedChar)) { SpringCompletion selectedCompletion = completionSession.SelectedCompletionSet.SelectionStatus.Completion as SpringCompletion; if (completionSession.SelectedCompletionSet.SelectionStatus.IsSelected && selectedCompletion != null && selectedCompletion.Type != null && selectedCompletion.Type.Value == SpringCompletionType.Namespace) { completionSession.Commit(); } else { completionSession.Dismiss(); } var result = this.nextCommandTarget.Exec(pguidCmdGroup, nCmdID, nCmdexecopt, pvaIn, pvaOut); // Check we should trigger completion after comitting the previous session (for example, after typing dot '.') if (IsTriggerKey(typedChar)) { ShowCompletion(); } return(result); } else { var result = this.nextCommandTarget.Exec(pguidCmdGroup, nCmdID, nCmdexecopt, pvaIn, pvaOut); completionSession.Filter(); return(result); } } } // redo the filter if there is a deletion if (nCmdID == (uint)VSConstants.VSStd2KCmdID.BACKSPACE || nCmdID == (uint)VSConstants.VSStd2KCmdID.DELETE) { var result = this.nextCommandTarget.Exec(pguidCmdGroup, nCmdID, nCmdexecopt, pvaIn, pvaOut); if (completionSession != null && !completionSession.IsDismissed) { completionSession.Filter(); } return(result); } } // we haven't handled this command so pass it onto the next target return(this.nextCommandTarget.Exec(pguidCmdGroup, nCmdID, nCmdexecopt, pvaIn, pvaOut)); }
protected virtual Completion CreateSnippetCompletion(VsExpansion expansion, ImageSource defaultExpansionGlyph, IconDescription defaultIconDescription) { if (string.IsNullOrEmpty(expansion.shortcut)) return null; string displayText = expansion.shortcut; string insertionText = expansion.shortcut; string description = expansion.description; return new Completion(displayText, insertionText, description, defaultExpansionGlyph, defaultIconDescription.ToString()); }
/// <summary> /// Gets the snippets. /// </summary> /// <returns></returns> private List <VsExpansion> GetSnippets() { var snippets = new List <VsExpansion>(); var textManager = (IVsTextManager)languageService.GetService(typeof(SVsTextManager)); if (textManager != null) { var textManager2 = (IVsTextManager2)textManager; if (textManager2 != null) { IVsExpansionManager expansionManager = null; textManager2.GetExpansionManager(out expansionManager); if (expansionManager != null) { // Tell the environment to fetch all of our snippets. IVsExpansionEnumeration expansionEnumerator = null; int ret = expansionManager.EnumerateExpansions(Guids.LuaLanguageService, 0, // return all info null, // return all types 0, // return all types 1, // include snippets without types 0, // do not include duplicates out expansionEnumerator); if (expansionEnumerator != null) { // Cache our expansions in an array of // VSExpansion structures. uint count = 0; uint fetched = 0; VsExpansion expansionInfo = new VsExpansion(); IntPtr[] pExpansionInfo = new IntPtr[1]; // Allocate enough memory for one VSExpansion structure. // This memory is filled in by the Next method. pExpansionInfo[0] = Marshal.AllocCoTaskMem(Marshal.SizeOf(expansionInfo)); expansionEnumerator.GetCount(out count); for (uint i = 0; i < count; i++) { expansionEnumerator.Next(1, pExpansionInfo, out fetched); if (fetched > 0) { // Convert the returned blob of data into a // structure that can be read in managed code. expansionInfo = (VsExpansion) Marshal.PtrToStructure(pExpansionInfo[0], typeof(VsExpansion)); if (!String.IsNullOrEmpty(expansionInfo.shortcut)) { snippets.Add(expansionInfo); } } } Marshal.FreeCoTaskMem(pExpansionInfo[0]); } } } } return(snippets); }
/// <summary> /// Gets the snippets. /// </summary> /// <returns></returns> private List<VsExpansion> GetSnippets() { var snippets = new List<VsExpansion>(); var textManager = (IVsTextManager)languageService.GetService(typeof(SVsTextManager)); if (textManager != null) { var textManager2 = (IVsTextManager2)textManager; if (textManager2 != null) { IVsExpansionManager expansionManager = null; textManager2.GetExpansionManager(out expansionManager); if (expansionManager != null) { // Tell the environment to fetch all of our snippets. IVsExpansionEnumeration expansionEnumerator = null; int ret = expansionManager.EnumerateExpansions(Guids.LuaLanguageService, 0, // return all info null, // return all types 0, // return all types 1, // include snippets without types 0, // do not include duplicates out expansionEnumerator); if (expansionEnumerator != null) { // Cache our expansions in an array of // VSExpansion structures. uint count = 0; uint fetched = 0; VsExpansion expansionInfo = new VsExpansion(); IntPtr[] pExpansionInfo = new IntPtr[1]; // Allocate enough memory for one VSExpansion structure. // This memory is filled in by the Next method. pExpansionInfo[0] = Marshal.AllocCoTaskMem(Marshal.SizeOf(expansionInfo)); expansionEnumerator.GetCount(out count); for (uint i = 0; i < count; i++) { expansionEnumerator.Next(1, pExpansionInfo, out fetched); if (fetched > 0) { // Convert the returned blob of data into a // structure that can be read in managed code. expansionInfo = (VsExpansion) Marshal.PtrToStructure(pExpansionInfo[0], typeof(VsExpansion)); if (!String.IsNullOrEmpty(expansionInfo.shortcut)) { snippets.Add(expansionInfo); } } } Marshal.FreeCoTaskMem(pExpansionInfo[0]); } } } } return snippets; }
private static VsExpansion ConvertToVsExpansionAndFree(ExpansionBuffer buffer) { VsExpansion expansion = new VsExpansion(); ConvertToStringAndFree(ref buffer.descriptionPtr, ref expansion.description); ConvertToStringAndFree(ref buffer.pathPtr, ref expansion.path); ConvertToStringAndFree(ref buffer.shortcutPtr, ref expansion.shortcut); ConvertToStringAndFree(ref buffer.titlePtr, ref expansion.title); return expansion; }