IntelliSense 成員完成 (受管理的封裝架構)
IntelliSense 的成員完成時顯示一份特定的範圍,例如類別、 結構、 列舉型別或命名空間的可能隸屬的工具提示。 比方說,在 C# 中,如果使用者輸入"this"加上句點,一份該類別或結構,在目前的範圍內的所有成員都會收到使用者可從中選取的清單中。
受管理的封裝架構 (MPF) 提供的支援工具提示 」 和 「 管理清單中,在 [工具提示中。 所需要的全部是由剖析器,以便提供出現在清單中的資料之間的合作關係。
如何運作
在會員清單會顯示使用 MPF 類別的兩種方式如下:
識別項 (含) 之後的成員完成字元,放置插入號,然後選取列出成員 的 IntelliSense 功能表。
IScanner掃瞄器偵測到成員完成字元,並設定語彙基元的觸發程序的MemberSelect ,該字元。
成員完成字元表示的類別、 結構或列舉型別成員所要遵循。 例如,C# 或 Visual Basic 中的成員完成字元是.,而 c + + 中的字元可能是.或->。 觸發程序設定值,則當成員選取字元會被掃描。
IntelliSense 成員的 [清單] 指令
SHOWMEMBERLIST命令會呼叫Completion上的方法Source類別和Completion方法,轉而呼叫ParseSource方法的剖析器使用剖析原因的DisplayMemberList。
剖析器會決定目前的位置,以及在或之前的目前位置的語彙基元的內容。 根據這個語彙基元,會看到一份宣告。 例如,在 C#,如果您在類別成員,然後選取插入號列出成員,您得到一份該類別的所有成員。 如果您有插入號後面的物件變數一段時間之後,您會取得類別的物件表示的所有成員的清單。 請注意是否插入號位於成員,而顯示的成員] 清單中的色彩時,會從清單中選取的成員取代插入號位於其中一種在清單中的成員。
語彙基元的觸發程序
MemberSelect引動程序發出呼叫Completion上的方法Source類別和Completion方法,轉而呼叫剖析原因的剖析器MemberSelect (如果語彙基元的觸發程序也包括MatchBraces旗標,剖析原因是MemberSelectAndHighlightBraces它結合了成員選取項目,並在大括號反白顯示)。
剖析器會決定目前的內容,以及具有已輸入的內容之前的成員選取字元的位置。 這項資訊,從剖析器會建立一份要求的範圍中的所有成員。 這份宣告清單會儲存在AuthoringScope會從傳回的物件ParseSource方法。 如果傳回任何宣告,則會顯示成員完成的工具提示。 工具提示由執行個體的CompletionSet類別。
啟用成員完成的支援
您必須具有CodeSense登錄項目設為 1,以支援 IntelliSense 的任何作業。 可以設定此登錄項目,具名參數,傳遞至ProvideLanguageServiceAttribute與語言套件相關聯的使用者屬性。 語言服務類別讀取無法讀取此登錄項目的值從EnableCodeSense屬性在LanguagePreferences類別。
如果您的掃瞄器傳回的語彙基元的觸發程序MemberSelect,並且您剖析器會傳回一份宣告,然後完成成員清單會顯示。
支援的成員完成掃描器
掃描器必須能夠偵測成員完成字元,並設定的語彙基元的觸發程序MemberSelect剖析該字元的時機。
範例
以下是簡單的範例偵測成員完成字元,並設定適當的TokenTriggers旗標。 這個範例是僅供說明。 它會假設您的掃描器包含方法GetNextToken的識別,並傳回從文字行的語彙基元。 範例程式碼時看到正確的字元種類,只需設定觸發程序。
using Microsoft.VisualStudio.Package;
using Microsoft.VisualStudio.TextManager.Interop;
namespace TestLanguagePackage
{
public class TestScanner : IScanner
{
private Lexer lex;
private const char memberSelectChar = '.';
public bool ScanTokenAndProvideInfoAboutIt(TokenInfo tokenInfo,
ref int state)
{
bool foundToken = false
string token = lex.GetNextToken();
if (token != null)
{
foundToken = true;
char c = token[0];
if (c == memberSelectChar)
{
tokenInfo.Trigger |= TokenTriggers.MemberSelect;
}
}
return foundToken;
}
}
}
剖析器支援的成員完成
成員完成的Source類別呼叫GetDeclarations方法。 您必須實作清單從衍生類別中Declarations類別。 請參閱Declarations如需詳細資訊,您必須實作的方法的相關的類別。
剖析器以呼叫MemberSelect或MemberSelectAndHighlightBraces當成員選取輸入字元。 給定的位置ParseRequest物件是之後的成員選取字元。 剖析器必須蒐集可以出現在 [從該特別的點,在原始程式碼中的 [成員] 清單中的所有成員的名稱。 然後,剖析器必須剖析目前的行,以判斷使用者想要的成員選取字元相關聯的範圍。
此範圍根據識別項的型別,成員選取字元之前。 例如,假設在 C# 中的 [成員變數languageService ,有一種LanguageService,輸入 languageService。 產生的所有成員的清單LanguageService類別。 也在 C# 中,鍵入此搜尋。 產生的類別,在目前範圍中的所有成員的清單。
範例
下列範例會示範一種方法來填入Declarations清單。 這段程式碼會假設剖析器建構宣告,並將它加入至清單中,藉由呼叫AddDeclaration上的方法TestAuthoringScope類別。
using System.Collections;
using Microsoft.VisualStudio.Package;
using Microsoft.VisualStudio.TextManager.Interop;
namespace TestLanguagePackage
{
internal class TestDeclaration
{
public string Name; // Name of declaration
public int TypeImageIndex; // Glyph index
public string Description; // Description of declaration
public TestDeclaration(string name, int typeImageIndex, string description)
{
this.Name = name;
this.TypeImageIndex = typeImageIndex;
this.Description = description;
}
}
//===================================================
internal class TestDeclarations : Declarations
{
private ArrayList declarations;
public TestDeclarations()
: base()
{
declarations = new ArrayList();
}
public void AddDeclaration(TestDeclaration declaration)
{
declarations.Add(declaration);
}
//////////////////////////////////////////////////////////////////////
// Declarations of class methods that must be implemented.
public override int GetCount()
{
// Return the number of declarations to show.
return declarations.Count;
}
public override string GetDescription(int index)
{
// Return the description of the specified item.
string description = "";
if (index >= 0 && index < declarations.Count)
{
description = ((TestDeclaration)declarations[index]).Description;
}
return description;
}
public override string GetDisplayText(int index)
{
// Determine what is displayed in the tool tip list.
string text = null;
if (index >= 0 && index < declarations.Count)
{
text = ((TestDeclaration)declarations[index]).Name;
}
return text;
}
public override int GetGlyph(int index)
{
// Return index of image to display next to the display text.
int imageIndex = -1;
if (index >= 0 && index < declarations.Count)
{
imageIndex = ((TestDeclaration)declarations[index]).TypeImageIndex;
}
return imageIndex;
}
public override string GetName(int index)
{
string name = null;
if (index >= 0 && index < declarations.Count)
{
name = ((TestDeclaration)declarations[index]).Name;
}
return name;
}
}
//===================================================
public class TestAuthoringScope : AuthoringScope
{
private TestDeclarations declarationsList;
public void AddDeclaration(TestDeclaration declaration)
{
if (declaration != null)
{
if (declarationsList == null)
{
declarationsList = new TestDeclarations();
}
declarationsList.AddDeclaration(declaration);
}
}
public override Declarations GetDeclarations(IVsTextView view,
int line,
int col,
TokenInfo info,
ParseReason reason)
{
return declarationsList;
}
/////////////////////////////////////////////////
// Remainder of AuthoringScope methods not shown.
/////////////////////////////////////////////////
}
//===================================================
class TestLanguageService : LanguageService
{
public override AuthoringScope ParseSource(ParseRequest req)
{
TestAuthoringScope scope = new TestAuthoringScope();
if (req.Reason == ParseReason.MemberSelect ||
req.Reason == ParseReason.MemberSelectAndHighlightBraces)
{
// Gather list of declarations based on what the user
// has typed so far. In this example, this list is an array of
// MemberDeclaration objects (a helper class you might implement
// to hold member declarations).
// How this list is gathered is dependent on the parser
// and is not shown here.
MemberDeclarations memberDeclarations;
memberDeclarations = GetDeclarationsForScope();
// Now populate the Declarations list in the authoring scope.
// GetImageIndexBasedOnType() is a helper method you
// might implement to convert a member type to an index into
// the image list returned from the language service.
foreach (MemberDeclaration dec in memberDeclarations)
{
scope.AddDeclaration(new TestDeclaration(
dec.Name,
GetImageIndexBasedOnType(dec.Type),
dec.Description));
}
}
return scope;
}
}
}