컨텐츠 지원을 사용하여 요청 시 컨텍스트 컨텐츠 완료를 제공할 수 있습니다. 이 기능은 org.eclipse.jface.text.contentassist의 플랫폼 텍스트 프레임워크를 통해 구현됩니다. 팝업 창(정보 팝업)은 문구를 완료하기 위해 사용 가능한 텍스트 선택사항을 제안하는 데 사용됩니다. 사용자는 텍스트에 삽입할 선택사항을 선택할 수 있습니다. 또한 컨텐츠 지원은 문서의 현재 위치와 관련된 정보를 사용자에게 제공할 수 있도록 컨텍스트 정보 팝업을 지원합니다.
컨텐츠 지원 구현은 선택적입니다. 기본적으로 SourceViewerConfiguration은 특정 편집기에 사용된 문서 모델을 모르고 컨텐츠 지원에 대한 일반 작동이 없으므로 컨텐츠 지원을 설치하지 않습니다.
컨텐츠 지원을 구현하려면 컨텐츠 지원을 정의하도록 편집기의 소스 표시기 구성을 구성해야 합니다. JavaSourceViewerConfiguration 내부의 Java 편집기 예제에서 구성됩니다.
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; }
컨텐츠 지원 작동은 인터페이스 IContentAssistant에서 정의됩니다. 컨텐츠 지원을 설정하는 것은 구문 강조표시를 설정하는 것과 다소 유사합니다. 지원은 서로 다른 문서 컨텐츠 유형과 관련하여 상이한 문구 완료 계획으로 구성되어야 합니다. 완료 계획은 IContentAssistProcessor를 사용하여 구현됩니다. 프로세서는 완료를 제안하고 특정 컨텐츠 유형 내 오프셋에 대한 컨텍스트 정보를 계산합니다.
모든 컨텐츠 유형에 컨텐츠 지원이 있어야 할 필요는 없습니다. Java 예제 편집기에서 컨텐츠 지원 프로세서는 기본 컨텐츠 유형 및 javadoc에 제공되지만, 복수행 주석에는 제공되지 않습니다. 각각의 프로세서를 살펴봅시다.
JavaCompletionProcessor는 매우 간단합니다. 이 키워드는 완료 후보로서만 키워드를
제안할 수 있습니다. 키워드는 필드 fgProposals
에 정의되며 이러한 키워드는 후보로 항상 제안됩니다.
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; }
완료는 사용자 요청을 통해 트리거되거나 "(" 또는 "." 문자 입력 시 자동으로 트리거될 수 있습니다.
public char[] getCompletionProposalAutoActivationCharacters() { return new char[] { '.', '(' }; }
완료 제안 외에 JavaCompletionProcessor는 사용자가 요청할 수 있는 컨텍스트 정보를 정의합니다. 컨텍스트 정보에는 제공된 컨텍스트 및 자세한 정보 메시지에서 사용 가능한 정보에 대한 설명이 포함됩니다.
Java 편집기 예제에서 정보는 실제 컨텍스트가 아닙니다. 사용자가 컨텍스트 정보를 요청할 때 다섯 개의 비슷한 컨텍스트 정보 오브젝트가 포함된 배열이 현재 오프셋으로 계산됩니다. 이런 컨텍스트 정보 오브젝트는 모두 오프셋 앞에 5자와 오프셋 뒤에 5자를 포함하는 컨텍스트를 정의합니다. 다섯 가지 제안 중 하나가 선택되면 자세한 정보가 커서 근처에 나타나며 커서가 오프셋 주위의 5자 컨텍스트에 있는 한 정보가 유지됩니다.
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[] { '#' }; }
컨텐츠 지원의 모양 및 작동은 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; }