逐步解說: 顯示簽章說明
簽章說明 (也就是參數資訊) 在 [工具提示顯示方法的簽章,當使用者輸入的參數清單開始字元 (通常是左括號)。 輸入參數和參數分隔符號 (通常是逗點),工具提示會更新以顯示下一個參數以粗體顯示。 您可以定義語言服務的內容中的 「 簽章說明或您可以定義您自己的檔案名稱副檔名和內容類型,並顯示簽章說明的只是該型別,或者可以顯示簽章說明現有的內容類型 (例如,"text")。 這個逐步解說會示範如何顯示 「 文字 」 內容類型的簽章幫助。
簽章說明通常會觸發輸入特定字元,例如,"("(左括號),並重新輸入另一個字元,例如,")"(右括號)。 可以使用按鍵輸入的命令處理常式實作 IntelliSense 所觸發的輸入字元之外的功能 ( IOleCommandTarget介面) 和處理常式提供者實作IVsTextViewCreationListener介面。 若要建立簽章說明來源,也就是加入簽章的 [說明] 中的簽章清單,實作ISignatureHelpSource介面 」 與 「 來源提供者實作ISignatureHelpSourceProvider介面。 提供者管理擴充性架構 (MEF) 元件組件,而負責匯出的來源和控制器類別和匯入的服務和經紀人,比方說, ITextStructureNavigatorSelectorService,可讓您瀏覽文字緩衝區中,以及ISignatureHelpBroker,而觸發的簽章說明工作階段。
這個逐步解說會示範如何實作硬式編碼集的識別項的 [簽章幫助。 完整的實作中的語言是負責提供該內容。
必要條件
若要完成這個逐步解說中,您必須安裝Visual Studio 2010 SDK。
注意事項 |
---|
如需有關 Visual Studio 的 SDK 的詳細資訊,請參閱擴充 Visual Studio 的概觀。若要了解如何下載 Visual Studio 的 SDK,請參閱Visual Studio 擴充性開發人員中心 MSDN 網站上。 |
建立 MEF 專案
若要建立 MEF 專案
建立編輯器類別器的專案。 為方案命名 SignatureHelpTest。
VSIX 資訊清單編輯器中開啟 Source.extension.vsixmanifest 檔案。
請確定Content名包含 MEF 元件的內容類型,並Path設定為 SignatureHelpTest.dll。
儲存並關閉 Source.extension.vsixmanifest。
將下列參考加入至專案,並設定 CopyLocal 到false:
Microsoft.VisualStudio.Editor
Microsoft.VisualStudio.Language.Intellisense
Microsoft.VisualStudio.OLE.Interop
Microsoft.VisualStudio.Shell
Microsoft.VisualStudio.TextManager.Interop
刪除現有的類別檔案。
實作簽章有助於簽章和參數
簽章說明來源為基礎實作的簽章ISignature,其中每一個包含實作之參數的IParameter。 在完整的實作中,這項資訊可以取自語言文件,但在這個範例中,簽章是硬式編碼。
若要實作的協助簽章的簽章和參數
將類別檔案,並命名為 SignatureHelpSource。
加入下列匯入。
Imports System Imports System.Collections.Generic Imports System.Linq Imports System.Text Imports System.Collections.ObjectModel Imports System.ComponentModel.Composition Imports System.Runtime.InteropServices Imports Microsoft.VisualStudio.Language.Intellisense Imports Microsoft.VisualStudio.Text Imports Microsoft.VisualStudio.Text.Editor Imports Microsoft.VisualStudio.Utilities Imports Microsoft.VisualStudio.Editor Imports Microsoft.VisualStudio.Text.Operations Imports Microsoft.VisualStudio Imports Microsoft.VisualStudio.TextManager.Interop Imports Microsoft.VisualStudio.OLE.Interop
using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel.Composition; using System.Runtime.InteropServices; using Microsoft.VisualStudio.Language.Intellisense; using Microsoft.VisualStudio.Text; using Microsoft.VisualStudio.Text.Editor; using Microsoft.VisualStudio.Utilities; using Microsoft.VisualStudio.Editor; using Microsoft.VisualStudio.Text.Operations; using Microsoft.VisualStudio; using Microsoft.VisualStudio.TextManager.Interop; using Microsoft.VisualStudio.OLE.Interop;
新增類別,名為TestParameter實作IParameter。
Friend Class TestParameter Implements IParameter
internal class TestParameter : IParameter
加入建構函式所設定的所有屬性。
Public Sub New(ByVal documentation As String, ByVal locus As Span, ByVal name As String, ByVal signature As ISignature) Me.privateDocumentation = documentation Me.privateLocus = locus Me.privateName = name Me.privateSignature = signature End Sub
public TestParameter(string documentation, Span locus, string name, ISignature signature) { Documentation = documentation; Locus = locus; Name = name; Signature = signature; }
新增內容的IParameter。
Private privateDocumentation As String ReadOnly Property Documentation() As String Implements IParameter.Documentation Get Return privateDocumentation End Get End Property Private privateLocus As Span ReadOnly Property Locus() As Span Implements IParameter.Locus Get Return privateLocus End Get End Property Private privateName As String ReadOnly Property Name() As String Implements IParameter.Name Get Return privateName End Get End Property Private privateSignature As ISignature ReadOnly Property Signature() As ISignature Implements IParameter.Signature Get Return privateSignature End Get End Property Private privatePrettyPrintedLocus As Span ReadOnly Property PrettyPrintedLocus() As Span Implements IParameter.PrettyPrintedLocus Get Return privatePrettyPrintedLocus End Get End Property
public string Documentation { get; private set; } public Span Locus { get; private set; } public string Name { get; private set; } public ISignature Signature { get; private set; } public Span PrettyPrintedLocus { get; private set; }
新增類別,名為TestSignature實作ISignature。
Friend Class TestSignature Implements ISignature
internal class TestSignature : ISignature
加入一些私用欄位。
Private m_subjectBuffer As ITextBuffer Private m_currentParameter As IParameter Private m_content As String Private m_documentation As String Friend m_applicableToSpan As ITrackingSpan Friend m_parameters As ReadOnlyCollection(Of IParameter) Private m_printContent As String
private ITextBuffer m_subjectBuffer; private IParameter m_currentParameter; private string m_content; private string m_documentation; private ITrackingSpan m_applicableToSpan; private ReadOnlyCollection<IParameter> m_parameters; private string m_printContent;
新增的建構函式會設定欄位,然後訂閱Changed事件。
Friend Sub New(ByVal subjectBuffer As ITextBuffer, ByVal content As String, ByVal doc As String, ByVal parameters As ReadOnlyCollection(Of IParameter)) m_subjectBuffer = subjectBuffer m_content = content m_documentation = doc m_parameters = parameters AddHandler m_subjectBuffer.Changed, AddressOf OnSubjectBufferChanged End Sub
internal TestSignature(ITextBuffer subjectBuffer, string content, string doc, ReadOnlyCollection<IParameter> parameters) { m_subjectBuffer = subjectBuffer; m_content = content; m_documentation = doc; m_parameters = parameters; m_subjectBuffer.Changed += new EventHandler<TextContentChangedEventArgs>(OnSubjectBufferChanged); }
宣告CurrentParameterChanged事件。 當使用者填入簽章中的參數之一時,會引發這個事件。
Public Event CurrentParameterChanged As EventHandler(Of CurrentParameterChangedEventArgs) Implements ISignature.CurrentParameterChanged
public event EventHandler<CurrentParameterChangedEventArgs> CurrentParameterChanged;
實作CurrentParameter屬性,因此它會引發CurrentParameterChanged的屬性值變更時的事件。
ReadOnly Property CurrentParameter() As IParameter Implements ISignature.CurrentParameter Get Return m_currentParameter End Get End Property
public IParameter CurrentParameter { get { return m_currentParameter; } internal set { if (m_currentParameter != value) { IParameter prevCurrentParameter = m_currentParameter; m_currentParameter = value; this.RaiseCurrentParameterChanged(prevCurrentParameter, m_currentParameter); } } }
加入的方法,會引發CurrentParameterChanged事件。
Private Sub RaiseCurrentParameterChanged(ByVal prevCurrentParameter As IParameter, ByVal newCurrentParameter As IParameter) Dim tempHandler As EventHandler(Of CurrentParameterChangedEventArgs) = Me.CurrentParameterChangedEvent If tempHandler IsNot Nothing Then tempHandler(Me, New CurrentParameterChangedEventArgs(prevCurrentParameter, newCurrentParameter)) End If End Sub
private void RaiseCurrentParameterChanged(IParameter prevCurrentParameter, IParameter newCurrentParameter) { EventHandler<CurrentParameterChangedEventArgs> tempHandler = this.CurrentParameterChanged; if (tempHandler != null) { tempHandler(this, new CurrentParameterChangedEventArgs(prevCurrentParameter, newCurrentParameter)); } }
加入藉由比較在逗號的數目來計算目前參數的方法ApplicableToSpan的簽章中的逗點數目。
Friend Sub ComputeCurrentParameter() If Parameters.Count = 0 Then Me.m_currentParameter = Nothing Return End If 'the number of commas in the string is the index of the current parameter Dim sigText As String = ApplicableToSpan.GetText(m_subjectBuffer.CurrentSnapshot) Dim currentIndex As Integer = 0 Dim commaCount As Integer = 0 Do While currentIndex < sigText.Length Dim commaIndex As Integer = sigText.IndexOf(","c, currentIndex) If commaIndex = -1 Then Exit Do End If commaCount += 1 currentIndex = commaIndex + 1 Loop If commaCount < Parameters.Count Then Me.m_currentParameter = Parameters(commaCount) Else 'too many commas, so use the last parameter as the current one. Me.m_currentParameter = Parameters(Parameters.Count - 1) End If End Sub
internal void ComputeCurrentParameter() { if (Parameters.Count == 0) { this.CurrentParameter = null; return; } //the number of commas in the string is the index of the current parameter string sigText = ApplicableToSpan.GetText(m_subjectBuffer.CurrentSnapshot); int currentIndex = 0; int commaCount = 0; while (currentIndex < sigText.Length) { int commaIndex = sigText.IndexOf(',', currentIndex); if (commaIndex == -1) { break; } commaCount++; currentIndex = commaIndex + 1; } if (commaCount < Parameters.Count) { this.CurrentParameter = Parameters[commaCount]; } else { //too many commas, so use the last parameter as the current one. this.CurrentParameter = Parameters[Parameters.Count - 1]; } }
加入事件處理常式Changed呼叫的事件**ComputeCurrentParameter()**方法。
Friend Sub OnSubjectBufferChanged(ByVal sender As Object, ByVal e As TextContentChangedEventArgs) Me.ComputeCurrentParameter() End Sub
internal void OnSubjectBufferChanged(object sender, TextContentChangedEventArgs e) { this.ComputeCurrentParameter(); }
實作 ApplicableToSpan 屬性。 這個屬性存有ITrackingSpan對應的簽章套用至緩衝區中的文字範圍。
ReadOnly Property ApplicableToSpan() As ITrackingSpan Implements ISignature.ApplicableToSpan Get Return (m_applicableToSpan) End Get End Property
public ITrackingSpan ApplicableToSpan { get { return (m_applicableToSpan); } internal set { m_applicableToSpan = value; } }
實作其他參數。
ReadOnly Property Content() As String Implements ISignature.Content Get Return (m_content) End Get End Property ReadOnly Property Documentation() As String Implements ISignature.Documentation Get Return (m_documentation) End Get End Property ReadOnly Property Parameters() As ReadOnlyCollection(Of IParameter) Implements ISignature.Parameters Get Return (m_parameters) End Get End Property ReadOnly Property PrettyPrintedContent() As String Implements ISignature.PrettyPrintedContent Get Return (m_printContent) End Get End Property
public string Content { get { return (m_content); } internal set { m_content = value; } } public string Documentation { get { return (m_documentation); } internal set { m_documentation = value; } } public ReadOnlyCollection<IParameter> Parameters { get { return (m_parameters); } internal set { m_parameters = value; } } public string PrettyPrintedContent { get { return (m_printContent); } internal set { m_printContent = value; } }
實作簽章的說明來源
簽章說明來源是您提供資訊的簽章的集合。
若要實作的簽章說明來源
新增類別,名為TestSignatureHelpSource實作ISignatureHelpSource。
Friend Class TestSignatureHelpSource Implements ISignatureHelpSource
internal class TestSignatureHelpSource : ISignatureHelpSource
加入文字緩衝區的參考。
Private m_textBuffer As ITextBuffer
private ITextBuffer m_textBuffer;
加入建構函式設定文字緩衝區和簽章說明來源提供者。
Public Sub New(ByVal textBuffer As ITextBuffer) m_textBuffer = textBuffer End Sub
public TestSignatureHelpSource(ITextBuffer textBuffer) { m_textBuffer = textBuffer; }
實作 AugmentSignatureHelpSession 方法。 在這個範例中,簽名碼是硬式編碼,但完整的實作中您會取得這項資訊從語言文件。
Public Sub AugmentSignatureHelpSession(ByVal session As ISignatureHelpSession, ByVal signatures As IList(Of ISignature)) Implements ISignatureHelpSource.AugmentSignatureHelpSession Dim snapshot As ITextSnapshot = m_textBuffer.CurrentSnapshot Dim position As Integer = session.GetTriggerPoint(m_textBuffer).GetPosition(snapshot) Dim applicableToSpan As ITrackingSpan = m_textBuffer.CurrentSnapshot.CreateTrackingSpan(New Span(position, 0), SpanTrackingMode.EdgeInclusive, 0) signatures.Add(CreateSignature(m_textBuffer, "add(int firstInt, int secondInt)", "Documentation for adding integers.", applicableToSpan)) signatures.Add(CreateSignature(m_textBuffer, "add(double firstDouble, double secondDouble)", "Documentation for adding doubles.", applicableToSpan)) End Sub
public void AugmentSignatureHelpSession(ISignatureHelpSession session, IList<ISignature> signatures) { ITextSnapshot snapshot = m_textBuffer.CurrentSnapshot; int position = session.GetTriggerPoint(m_textBuffer).GetPosition(snapshot); ITrackingSpan applicableToSpan = m_textBuffer.CurrentSnapshot.CreateTrackingSpan( new Span(position, 0), SpanTrackingMode.EdgeInclusive, 0); signatures.Add(CreateSignature(m_textBuffer, "add(int firstInt, int secondInt)", "Documentation for adding integers.", applicableToSpan)); signatures.Add(CreateSignature(m_textBuffer, "add(double firstDouble, double secondDouble)", "Documentation for adding doubles.", applicableToSpan)); }
Helper 方法**CreateSignature()**提供只供說明。
Private Function CreateSignature(ByVal textBuffer As ITextBuffer, ByVal methodSig As String, ByVal methodDoc As String, ByVal span As ITrackingSpan) As TestSignature Dim sig As New TestSignature(textBuffer, methodSig, methodDoc, Nothing) AddHandler textBuffer.Changed, AddressOf sig.OnSubjectBufferChanged 'find the parameters in the method signature (expect methodname(one, two) Dim pars() As String = methodSig.Split(New Char() {"("c, ","c, ")"c}) Dim paramList As New List(Of IParameter)() Dim locusSearchStart As Integer = 0 For i As Integer = 1 To pars.Length - 1 Dim param As String = pars(i).Trim() If String.IsNullOrEmpty(param) Then Continue For End If 'find where this parameter is located in the method signature Dim locusStart As Integer = methodSig.IndexOf(param, locusSearchStart) If locusStart >= 0 Then Dim locus As New Span(locusStart, param.Length) locusSearchStart = locusStart + param.Length paramList.Add(New TestParameter("Documentation for the parameter.", locus, param, sig)) End If Next i sig.m_Parameters = New ReadOnlyCollection(Of IParameter)(paramList) sig.m_ApplicableToSpan = span sig.ComputeCurrentParameter() Return sig End Function
private TestSignature CreateSignature(ITextBuffer textBuffer, string methodSig, string methodDoc, ITrackingSpan span) { TestSignature sig = new TestSignature(textBuffer, methodSig, methodDoc, null); textBuffer.Changed += new EventHandler<TextContentChangedEventArgs>(sig.OnSubjectBufferChanged); //find the parameters in the method signature (expect methodname(one, two) string[] pars = methodSig.Split(new char[] { '(', ',', ')' }); List<IParameter> paramList = new List<IParameter>(); int locusSearchStart = 0; for (int i = 1; i < pars.Length; i++) { string param = pars[i].Trim(); if (string.IsNullOrEmpty(param)) continue; //find where this parameter is located in the method signature int locusStart = methodSig.IndexOf(param, locusSearchStart); if (locusStart >= 0) { Span locus = new Span(locusStart, param.Length); locusSearchStart = locusStart + param.Length; paramList.Add(new TestParameter("Documentation for the parameter.", locus, param, sig)); } } sig.Parameters = new ReadOnlyCollection<IParameter>(paramList); sig.ApplicableToSpan = span; sig.ComputeCurrentParameter(); return sig; }
實作 GetBestMatch 方法。 在這個範例中,有兩個簽章的情況,每一個都有兩個參數。 因此,這個方法不是必要的。 在簡的實作中,多個簽章說明來源是可用的這個方法用來決定最高優先權的簽章說明來源是否可以提供相符的簽章。 若無法移除,方法會傳回 null 下, 一步] 最高優先權的來源必須提供符合項目。
Public Function GetBestMatch(ByVal session As ISignatureHelpSession) As ISignature Implements ISignatureHelpSource.GetBestMatch If session.Signatures.Count > 0 Then Dim applicableToSpan As ITrackingSpan = session.Signatures(0).ApplicableToSpan Dim text As String = applicableToSpan.GetText(applicableToSpan.TextBuffer.CurrentSnapshot) If text.Trim().Equals("add") Then 'get only "add" Return session.Signatures(0) End If End If Return Nothing End Function
public ISignature GetBestMatch(ISignatureHelpSession session) { if (session.Signatures.Count > 0) { ITrackingSpan applicableToSpan = session.Signatures[0].ApplicableToSpan; string text = applicableToSpan.GetText(applicableToSpan.TextBuffer.CurrentSnapshot); if (text.Trim().Equals("add")) //get only "add" return session.Signatures[0]; } return null; }
實作 dispose () 方法:
Private m_isDisposed As Boolean Public Sub Dispose() Implements IDisposable.Dispose If Not m_isDisposed Then GC.SuppressFinalize(Me) m_isDisposed = True End If End Sub
private bool m_isDisposed; public void Dispose() { if (!m_isDisposed) { GC.SuppressFinalize(this); m_isDisposed = true; } }
實作簽章說明來源提供者
簽章說明來源提供者負責匯出管理擴充性架構 (MEF) 元件組件,以及執行個體化的簽章說明來源。
若要實作的簽章說明來源提供者
新增類別,名為TestSignatureHelpSourceProvider實作ISignatureHelpSourceProvider,並將其與匯出NameAttribute、 ContentTypeAttribute的 「 文字 」,以及OrderAttribute的早於 ="default"。
<Export(GetType(ISignatureHelpSourceProvider)), Name("Signature Help source"), Order(Before:="default"), ContentType("text")> Friend Class TestSignatureHelpSourceProvider Implements ISignatureHelpSourceProvider
[Export(typeof(ISignatureHelpSourceProvider))] [Name("Signature Help source")] [Order(Before = "default")] [ContentType("text")] internal class TestSignatureHelpSourceProvider : ISignatureHelpSourceProvider
實作TryCreateSignatureHelpSource具現化TestSignatureHelpSource。
Public Function TryCreateSignatureHelpSource(ByVal textBuffer As ITextBuffer) As ISignatureHelpSource Implements ISignatureHelpSourceProvider.TryCreateSignatureHelpSource Return New TestSignatureHelpSource(textBuffer) End Function
public ISignatureHelpSource TryCreateSignatureHelpSource(ITextBuffer textBuffer) { return new TestSignatureHelpSource(textBuffer); }
實作命令處理常式
簽章說明通常由所觸發 (字元和 dismissed 的) 字元。 您可以藉由實作來處理這些按鍵IOleCommandTarget ,這樣它會觸發一旦收到的簽章說明工作階段 (字元加上一個已知的方法的名稱,並關閉工作階段一旦收到) 字元。
若要實作命令處理常式
新增類別,名為TestSignatureHelpCommand實作IOleCommandTarget。
Friend NotInheritable Class TestSignatureHelpCommandHandler Implements IOleCommandTarget
internal sealed class TestSignatureHelpCommandHandler : IOleCommandTarget
加入私用欄位IVsTextView介面卡 (這可讓您將命令處理常式加入至鏈結的命令處理常式)、 文字檢視、 簽章有助於代理人和工作階段, ITextStructureNavigator,和下一個IOleCommandTarget。
Private m_nextCommandHandler As IOleCommandTarget Private m_textView As ITextView Private m_broker As ISignatureHelpBroker Private m_session As ISignatureHelpSession Private m_navigator As ITextStructureNavigator
IOleCommandTarget m_nextCommandHandler; ITextView m_textView; ISignatureHelpBroker m_broker; ISignatureHelpSession m_session; ITextStructureNavigator m_navigator;
加入建構函式來初始化這些欄位,並將 [命令] 篩選器新增至鏈結的命令的篩選器。
Friend Sub New(ByVal textViewAdapter As IVsTextView, ByVal textView As ITextView, ByVal nav As ITextStructureNavigator, ByVal broker As ISignatureHelpBroker) Me.m_textView = textView Me.m_broker = broker Me.m_navigator = nav 'add this to the filter chain textViewAdapter.AddCommandFilter(Me, m_nextCommandHandler) End Sub
internal TestSignatureHelpCommandHandler(IVsTextView textViewAdapter, ITextView textView, ITextStructureNavigator nav, ISignatureHelpBroker broker) { this.m_textView = textView; this.m_broker = broker; this.m_navigator = nav; //add this to the filter chain textViewAdapter.AddCommandFilter(this, out m_nextCommandHandler); }
實作Exec方法,以觸發命令過濾器的接收到的簽章說明工作階段 (字元之後的已知的方法名稱,並對此關閉工作階段一旦收到) 工作階段時仍在作用中的字元。 在每一個案例中,會轉送命令。
Public Function Exec(ByRef pguidCmdGroup As Guid, ByVal nCmdID As UInteger, ByVal nCmdexecopt As UInteger, ByVal pvaIn As IntPtr, ByVal pvaOut As IntPtr) As Integer Implements IOleCommandTarget.Exec Dim typedChar As Char = Char.MinValue If pguidCmdGroup = VSConstants.VSStd2K AndAlso nCmdID = CUInt(VSConstants.VSStd2KCmdID.TYPECHAR) Then typedChar = CChar(ChrW(CUShort(Marshal.GetObjectForNativeVariant(pvaIn)))) If typedChar.Equals("("c) Then 'move the point back so it's in the preceding word Dim point As SnapshotPoint = m_textView.Caret.Position.BufferPosition - 1 Dim extent As TextExtent = m_navigator.GetExtentOfWord(point) Dim word As String = extent.Span.GetText() If word.Equals("add") Then m_session = m_broker.TriggerSignatureHelp(m_textView) End If ElseIf typedChar.Equals(")"c) AndAlso m_session IsNot Nothing Then m_session.Dismiss() m_session = Nothing End If End If Return m_nextCommandHandler.Exec(pguidCmdGroup, nCmdID, nCmdexecopt, pvaIn, pvaOut) End Function
public int Exec(ref Guid pguidCmdGroup, uint nCmdID, uint nCmdexecopt, IntPtr pvaIn, IntPtr pvaOut) { char typedChar = char.MinValue; if (pguidCmdGroup == VSConstants.VSStd2K && nCmdID == (uint)VSConstants.VSStd2KCmdID.TYPECHAR) { typedChar = (char)(ushort)Marshal.GetObjectForNativeVariant(pvaIn); if (typedChar.Equals('(')) { //move the point back so it's in the preceding word SnapshotPoint point = m_textView.Caret.Position.BufferPosition - 1; TextExtent extent = m_navigator.GetExtentOfWord(point); string word = extent.Span.GetText(); if (word.Equals("add")) m_session = m_broker.TriggerSignatureHelp(m_textView); } else if (typedChar.Equals(')') && m_session != null) { m_session.Dismiss(); m_session = null; } } return m_nextCommandHandler.Exec(ref pguidCmdGroup, nCmdID, nCmdexecopt, pvaIn, pvaOut); }
實作QueryStatus方法,它永遠會轉送命令。
Public Function QueryStatus(ByRef pguidCmdGroup As Guid, ByVal cCmds As UInteger, ByVal prgCmds() As OLECMD, ByVal pCmdText As IntPtr) As Integer Implements IOleCommandTarget.QueryStatus Return m_nextCommandHandler.QueryStatus(pguidCmdGroup, cCmds, prgCmds, pCmdText) End Function
public int QueryStatus(ref Guid pguidCmdGroup, uint cCmds, OLECMD[] prgCmds, IntPtr pCmdText) { return m_nextCommandHandler.QueryStatus(ref pguidCmdGroup, cCmds, prgCmds, pCmdText); }
實作簽章說明命令提供者
您可以藉由實作提供簽章 Help 命令IVsTextViewCreationListener來建立文字檢視時,具現化命令處理常式。
若要實作的簽章說明命令提供者
新增類別,名為TestSignatureHelpController實作IVsTextViewCreationListener ,並將其與匯出NameAttribute, ContentTypeAttribute,以及TextViewRoleAttribute。
<Export(GetType(IVsTextViewCreationListener)), Name("Signature Help controller"), TextViewRole(PredefinedTextViewRoles.Editable), ContentType("text")> Friend Class TestSignatureHelpCommandProvider Implements IVsTextViewCreationListener
[Export(typeof(IVsTextViewCreationListener))] [Name("Signature Help controller")] [TextViewRole(PredefinedTextViewRoles.Editable)] [ContentType("text")] internal class TestSignatureHelpCommandProvider : IVsTextViewCreationListener
匯入IVsEditorAdaptersFactoryService (用來取得ITextView、 指定IVsTextView物件)、 ITextStructureNavigatorSelectorService (用來尋找目前的文字),以及ISignatureHelpBroker (如果您要觸發簽章說明工作階段)。
<Import()> Friend AdapterService As IVsEditorAdaptersFactoryService <Import()> Friend Property NavigatorService() As ITextStructureNavigatorSelectorService <Import()> Friend SignatureHelpBroker As ISignatureHelpBroker
[Import] internal IVsEditorAdaptersFactoryService AdapterService; [Import] internal ITextStructureNavigatorSelectorService NavigatorService { get; set; } [Import] internal ISignatureHelpBroker SignatureHelpBroker;
實作VsTextViewCreated具現化的方法TestSignatureCommandHandler。
Public Sub VsTextViewCreated(ByVal textViewAdapter As IVsTextView) Implements IVsTextViewCreationListener.VsTextViewCreated Dim textView As ITextView = AdapterService.GetWpfTextView(textViewAdapter) If textView Is Nothing Then Return End If textView.Properties.GetOrCreateSingletonProperty(Function() New TestSignatureHelpCommandHandler(textViewAdapter, textView, NavigatorService.GetTextStructureNavigator(textView.TextBuffer), SignatureHelpBroker)) End Sub
public void VsTextViewCreated(IVsTextView textViewAdapter) { ITextView textView = AdapterService.GetWpfTextView(textViewAdapter); if (textView == null) return; textView.Properties.GetOrCreateSingletonProperty( () => new TestSignatureHelpCommandHandler(textViewAdapter, textView, NavigatorService.GetTextStructureNavigator(textView.TextBuffer), SignatureHelpBroker)); }
建置和測試程式碼
若要測試這段程式碼,建置 SignatureHelpTest 方案並執行它實驗性的執行個體中。
若要建置和測試 SignatureHelpTest 的解決方案
建置方案。
當您執行此專案在偵錯工具時,會執行個體化 Visual Studio 的第二個執行個體。
建立文字檔案,並輸入一些文字,其中包含文字"新增"再加上左括號。
輸入左括號之後,您應該會看到工具提示],顯示一份針對兩個簽章**add()**方法。