【JfaceTextFramework学习笔记之二】内容提示

来源:互联网 发布:开源的数据库防火墙 编辑:程序博客网 时间:2024/05/01 22:40
Content assist1-在org.eclipse.jface.text.contentassist包中有实现,会弹出窗口来提示可能的文本选择
来完成一段句子。也可以用来对特定的文本提供额外的相关信息。

2-实现内容提示是可选的,默认情况下SourceViewerConfiguration 并不提供内容提示,因为并不清楚要编辑的具体内容。


3- 要实现内容提示,编辑器的source viewer configuration 必须要被配置. 

在Java例子中的JavaSourceViewerConfiguration中是这么做的:

public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) {

ContentAssistant assistant= new ContentAssistant();
assistant.setContentAssistProcessor(new JavaCompletionProcessor(), IDocument.DEFAULT_CONTENT_TYPE);
assistant.setContentAssistProcessor(new JavaDocCompletionProcessor(), JavaPartitionScanner.JAVA_DOC);
...
return assistant;
}

4-IContentAssistant接口,建立内容提示content assistant和语法高亮很相似. 


assistant对象要被针对不同的内容类型配置对应的phrase completion strategies


5-IContentAssistProcessor 接口定义了完成策略,提示完成信息,和环境信息,根据一个特定的内容类型中的位移(因为在同一类型的分区中不同的位移可能有不同的提示)


6-并不是所有的分区都需要提示,
Java例子中:JavaCompletionProcessor 对关键字进行提示,
所以关键字被定义在一个属性变量中,

JavaCompletionProcessor :
public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int documentOffset) {
ICompletionProposal[] result= new ICompletionProposal[fgProposals.length];
for (int i= 0; i < fgProposals.length; i++) {
IContextInformation info= new ContextInformation(fgProposals[i], MessageFormat.format(JavaEditorMessages.getString("CompletionProcessor.Proposal.ContextInfo.pattern"), new Object[] { fgProposals[i] })); //$NON-NLS-1$
result[i]= new CompletionProposal(fgProposals[i], documentOffset, 0, fgProposals[i].length(), null, fgProposals[i], info, MessageFormat.format(JavaEditorMessages.getString("CompletionProcessor.Proposal.hoverinfo.pattern"), new Object[] { fgProposals[i]})); //$NON-NLS-1$
}
return result;
}
7- 内容提示的激发,用户主动请求(alt+/)或者碰到.或者(
public char[] getCompletionProposalAutoActivationCharacters() {return new char[] { '.', '(' };}

8-内容提示之外JavaCompletionProcessor 还定义可以被用户请求的环境信息

In the Java editor example, the information is not really contextual.  An array containing five similar context information objects is computed for the current offset when the user requests context info. All of these context information objects define a context that contains the five characters in front of the offset and the five after the offset. If any one of these five proposals is selected, the detailed information will appear near the cursor and will stay as long as the cursor is within the context of the five characters around the offset.

public IContextInformation[] computeContextInformation(ITextViewer viewer, int documentOffset) {IContextInformation[] result= new IContextInformation[5];for (int i= 0; i < result.length; i++)result[i]= new ContextInformation(MessageFormat.format(JavaEditorMessages.getString("CompletionProcessor.ContextInfo.display.pattern"), new Object[] { new Integer(i), new Integer(documentOffset) }),MessageFormat.format(JavaEditorMessages.getString("CompletionProcessor.ContextInfo.value.pattern"), new Object[] { new Integer(i), new Integer(documentOffset - 5), new Integer(documentOffset + 5)}));return result;}
当输入#时显示环境信息
public char[] getContextInformationAutoActivationCharacters() {return new char[] { '#' };}


9-可以使用IContentAssistant来配置内容提示的外观和行为。


比如,可以配置自动激发的时间,提示方向,和信息弹出的颜色


public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) {

ContentAssistant assistant= new ContentAssistant();
...
assistant.enableAutoActivation(true);
assistant.setAutoActivationDelay(500);
assistant.setProposalPopupOrientation(IContentAssistant.PROPOSAL_OVERLAY);
assistant.setContextInformationPopupOrientation(IContentAssistant.CONTEXT_INFO_ABOVE);
assistant.setContextInformationPopupBackground(JavaEditorEnvironment.getJavaColorProvider().getColor(new RGB(150, 150, 0)));

return assistant;
}

10-为了允许用户调用内容提示,必须创建一个Action


一般在子类重写的 AbstractTextEditor.createActions()中进行创建:

protected void createActions() {...IAction action= new ContentAssistAction(aResourceBundle, "ContentAssistProposal.", this); //$NON-NLS-1$action.setActionDefinitionId(ITextEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS);setAction(actionId, action); //$NON-NLS-1$markAsStateDependentAction(actionId, true); //$NON-NLS-1$PlatformUI.getWorkbench().getHelpSystem().setHelp(action, helpContextId);...}在编辑器的主菜单中显示该Action
private RetargetTextEditorAction fContentAssist;public MyEditorActionContributor() {fContentAssist= new RetargetTextEditorAction();String commandId= ITextEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS;fContentAssist.setActionDefinitionId(commandId);}public void contributeToMenu(IMenuManager menu) {IMenuManager editMenu= menu.findMenuUsingPath(M_EDIT);editMenu.appendToGroup(MB_ADDITIONS, fContentAssist);}public void setActiveEditor(IEditorPart part) {IAction editorAction= getAction(part, "ContentAssist");fContentAssist.setAction(editorAction);}...
原创粉丝点击