大家在使用Eclipse編寫Java代碼的時候,一定被Java代碼編輯器的強大功能所吸引:出色的錯誤提示,準確的內容幫助,文本的折疊等等。今天我以Eclipse插件中的XML Editor例子作為模板,為XML文本編輯器加入內容幫助(Content Assis)。
1.內容幫助簡介
在目前流行的IDE中,內容提示幫助是必不可少的功能,可以說,如果沒有了內容幫助,那IDE就不能稱為IDE。有了內容幫助提示,能大大提高代碼編寫速度。
請看下圖:當我們在Java編輯器中輸入‘.’的時候,就會彈出一個菜單,里面列出了類所具有的方法以及屬性,并且在我們繼續輸入字符的時候,彈出的內容會隨著我們的輸入進行過濾。

下面我們以Eclipse的XML Editor Example為例,介紹一下內容幫助如何實現的。
2.創建XML Editor
我們首先需要建立一個Plugin工程,然后在向導頁中選擇我們要生成的XML Editor例子:
點Finish完成,這時候我們的工程便生成了,并且向導還為我們生成了XML Editor所需要的一些類,以及為我們的Plugin.xml實現了org.eclipse.ui.editors擴展點:
3.簡單的內容幫助
在我們生成的類中,有一個名為XMLConfiguration的類,該類對XML Editor進行了一些設置,包括如何去為不同的文本區域顯示不同的顏色等,TextEditor所維護的SourceViewer就是通過它來進行設置的,但這不是我們所要討論的范圍,這里簡單地介紹一下即可。
接下來我們需要復寫XMLConfiguration的一個方法:getContentAssistant。這個方法便是告訴我們的編輯器,我們所具有的內容幫助是什么,在創建XML Editor的時候,默認是不為我們生成這方面代碼的,所以我們需要自己復寫:
????
public
?IContentAssistant?getContentAssistant(ISourceViewer?sourceViewer)?{
????????
//
?生成一個ContentAssistant
????????ContentAssistant?assistant?
=
??
new
?ContentAssistant();
?
????????
//
?設置幫組內容彈出響應時間
????????assistant.setAutoActivationDelay(
200
);
????????assistant.enableAutoActivation(
true
);
????????
return
?assistant;
????}
ContentAssistant并不是內容幫助的提供者,它只是維護我們的內容幫助,幫我們彈出菜單以及幫助內容信息等作用。
真正告訴ContentAssistant要顯示那些幫助內容的,是IContentAssistProcessor接口類。讓我們創建一個名為StrutsContentAssisProcessor的類,并讓它實現IContentAssistProcessor接口:
public?class?StrutsContentAssisProcessor?implements?IContentAssistProcessor?{
????public?ICompletionProposal[]?computeCompletionProposals(ITextViewer?viewer,
????????????int?offset)?{
????????return?null;
????}
????public?IContextInformation[]?computeContextInformation(ITextViewer?viewer,
????????????int?offset)?{
????????return?null;
????}
????public?char[]?getCompletionProposalAutoActivationCharacters()?{
????????return?null;
????}
????public?char[]?getContextInformationAutoActivationCharacters()?{
????????return?null;
????}
????public?String?getErrorMessage()?{
????????return?null;
????}
????public?IContextInformationValidator?getContextInformationValidator()?{
????????return?null;
????}
}
大家注意下computeCompletionProposals方法,這個方法便是返回我們的具體內容幫助。所以我們需要為我們的編輯器創建所需要的內容幫助:CompletionProposal
先看一下這個類的構造函數各個參數的含義:
???? *?@param replacementString?:選擇幫助信息后所要替代的文本內容
?????*?@param?replacementOffset?:替代內容輸入的位置
?????*?@param?replacementLength?:替代文本覆蓋原來文本的長度???
?? ??*?@param?cursorPosition?:完成內容幫助的文本替代后,光標所在位置
?????*?@param?image?:幫助內容顯示的圖標
?????*?@param?displayString?:幫助內容的顯示字符串
?????*?@param?contextInformation?:幫助內容的信息描述
?????*?@param?additionalProposalInfo?:附加信息
在這幾個參數中image 、contextInformation、additionalProposalInfo我們可以設置為空。現在讓我們在computeCompletionProposals生成我們的幫助內容:
?public?ICompletionProposal[]?computeCompletionProposals(ITextViewer?viewer,
????????????int?offset)?{
????????ICompletionProposal[]?proposals?=?new?ICompletionProposal[2];
????????
????????proposals[0]?=?new?CompletionProposal("替換文本1",?offset,?0,?new?String("替換文本1").length(),?null,?"幫組內容1",?null,null)?;
????????proposals[1]?=?new?CompletionProposal("替換文本2",?offset,?0,?new?String("替換文本2").length(),?null,?"幫組內容2",?null,null)?;
????????
????????return?proposals;
????}
computeCompletionProposals輸入的參數中 offset是指當內容幫助彈出的時候,文本編輯器光標所在位置。
大家都知道,幫助內容彈出的時候是需要一定條件的,也就是當我們輸入了激活內容幫助的字符的時候,它便會彈出來。IContentAssistProcessor?的
getCompletionProposalAutoActivationCharacters方法便是讓我們返回激活幫助內容字符的,假設當我們輸入了‘<’時,彈出幫助內容:
???public?char[]?getCompletionProposalAutoActivationCharacters()?{
????????return?new?String("<").toCharArray();
????}
好了,我們的第一步已經完成了,接下來就是在ContentAssis對象中設置我們所要返回的內容幫助。
返回到XMLConfiguration的getContentAssistant方法:
????public?IContentAssistant?getContentAssistant(ISourceViewer?sourceViewer)?{
????????//?生成一個ContentAssistant
????????ContentAssistant?assistant?=??new?ContentAssistant();
????????//?讓幫助內容在XML的Tag標簽范圍內激%