Notice: This Wiki is now read only and edits are no longer possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.
Difference between revisions of "FAQ How do I add Content Assist to my language editor?"
(4 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
− | In[[ | + | == If you're using the Generic and Extensible editor == |
− | we describe how Content Assist is installed through our configuration | + | |
− | class, as follows: | + | Add hover support via the <tt>org.eclipse.ui.genericeditor.contentAssistProcessors</tt> extension point, providing an implemention <tt>IContentAssistProcessor</tt>. |
− | < | + | |
+ | == If you're extensing the StructuredTextEditor == | ||
+ | |||
+ | TODO | ||
+ | |||
+ | == If it's your own editor == | ||
+ | |||
+ | In [[FAQ How do I write an editor for my own language?]] we describe how Content Assist is installed through our configuration class, as follows: | ||
+ | |||
+ | <source lang="java"> | ||
class Configuration extends SourceViewerConfiguration { | class Configuration extends SourceViewerConfiguration { | ||
− | + | public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) { | |
− | public IContentAssistant getContentAssistant(ISourceViewer | + | |
− | + | ||
ContentAssistant ca = new ContentAssistant(); | ContentAssistant ca = new ContentAssistant(); | ||
− | IContentAssistProcessor cap = | + | IContentAssistProcessor cap = new EScriptCompletionProcessor(); // <-- See reference to IContentAssistProcess implementation here |
− | + | ca.setContentAssistProcessor(cap, IDocument.DEFAULT_CONTENT_TYPE); | |
− | ca.setContentAssistProcessor(cap, | + | ca.setInformationControlCreator(getInformationControlCreator(sourceViewer)); |
− | + | ||
− | ca.setInformationControlCreator( | + | |
− | + | ||
return ca; | return ca; | ||
− | |||
− | |||
} | } | ||
− | </ | + | </source> |
− | + | == Example of IContentAssistProcessor == | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | public ICompletionProposal[] computeCompletionProposals( | + | A completion processor takes the current insertion point in the editor and figures out a list of continuation proposals for the user to choose from. Our completion processor looks something like this: |
− | + | <source lang="java"> | |
+ | class EScriptCompletionProcessor implements IContentAssistProcessor { | ||
+ | private final IContextInformation[] NO_CONTEXTS = { }; | ||
+ | private final char[] PROPOSAL_ACTIVATION_CHARS = { 's','f','p','n','m', }; | ||
+ | private ICompletionProposal[] NO_COMPLETIONS = { }; | ||
+ | |||
+ | public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int offset) { | ||
try { | try { | ||
IDocument document = viewer.getDocument(); | IDocument document = viewer.getDocument(); | ||
Line 40: | Line 37: | ||
String prefix = lastWord(document, offset); | String prefix = lastWord(document, offset); | ||
String indent = lastIndent(document, offset); | String indent = lastIndent(document, offset); | ||
− | EscriptModel model = | + | EscriptModel model = EscriptModel.getModel(document, null); |
− | + | model.getContentProposals(prefix, indent, offset, result); | |
− | model.getContentProposals(prefix, indent, | + | return (ICompletionProposal[]) result.toArray(new ICompletionProposal[result.size()]); |
− | + | ||
− | return (ICompletionProposal[]) result.toArray( | + | |
− | + | ||
} catch (Exception e) { | } catch (Exception e) { | ||
// ... log the exception ... | // ... log the exception ... | ||
Line 66: | Line 60: | ||
try { | try { | ||
int start = offset-1; | int start = offset-1; | ||
− | while (start >= 0 && | + | while (start >= 0 && doc.getChar(start)!= '\n') start--; |
− | + | ||
int end = start; | int end = start; | ||
− | while (end < offset && | + | while (end < offset && Character.isSpaceChar(doc.getChar(end))) end++; |
− | + | ||
return doc.get(start+1, end-start-1); | return doc.get(start+1, end-start-1); | ||
} catch (BadLocationException e) { | } catch (BadLocationException e) { | ||
Line 77: | Line 69: | ||
return ""; | return ""; | ||
} | } | ||
− | public IContextInformation[] computeContextInformation( | + | public IContextInformation[] computeContextInformation(ITextViewer viewer, int offset) { |
− | + | ||
return NO_CONTEXTS; | return NO_CONTEXTS; | ||
} | } | ||
Line 86: | Line 77: | ||
// ... remaining methods are optional ... | // ... remaining methods are optional ... | ||
} | } | ||
− | </ | + | </source> |
− | Basically, Content Assist completion has three steps. First, we | + | Basically, Content Assist completion has three steps. First, we have to figure out what string has already been started by the user (see <tt>lastWord</tt>). Second, we have to find appropriate completions. Third, we have to return strings so that when they are inserted, they lay out acceptably (see the use of <tt>lastIndent</tt>). |
− | have to figure out what string has already been started by the user | + | |
− | (see <tt>lastWord</tt>). Second, we have to find appropriate completions. | + | |
− | Third, we have to return strings so that when they are inserted, | + | |
− | they lay out acceptably (see the use of <tt>lastIndent</tt>). | + | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | [[ | + | == See Also == |
+ | *[[FAQ How do I add Content Assist to my editor?]] | ||
+ | *[[FAQ How do I add hover support to my text editor?]] | ||
− | + | {{Template:FAQ_Tagline}} |
Latest revision as of 08:38, 9 February 2024
Contents
If you're using the Generic and Extensible editor
Add hover support via the org.eclipse.ui.genericeditor.contentAssistProcessors extension point, providing an implemention IContentAssistProcessor.
If you're extensing the StructuredTextEditor
TODO
If it's your own editor
In FAQ How do I write an editor for my own language? we describe how Content Assist is installed through our configuration class, as follows:
class Configuration extends SourceViewerConfiguration { public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) { ContentAssistant ca = new ContentAssistant(); IContentAssistProcessor cap = new EScriptCompletionProcessor(); // <-- See reference to IContentAssistProcess implementation here ca.setContentAssistProcessor(cap, IDocument.DEFAULT_CONTENT_TYPE); ca.setInformationControlCreator(getInformationControlCreator(sourceViewer)); return ca; }
Example of IContentAssistProcessor
A completion processor takes the current insertion point in the editor and figures out a list of continuation proposals for the user to choose from. Our completion processor looks something like this:
class EScriptCompletionProcessor implements IContentAssistProcessor { private final IContextInformation[] NO_CONTEXTS = { }; private final char[] PROPOSAL_ACTIVATION_CHARS = { 's','f','p','n','m', }; private ICompletionProposal[] NO_COMPLETIONS = { }; public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int offset) { try { IDocument document = viewer.getDocument(); ArrayList result = new ArrayList(); String prefix = lastWord(document, offset); String indent = lastIndent(document, offset); EscriptModel model = EscriptModel.getModel(document, null); model.getContentProposals(prefix, indent, offset, result); return (ICompletionProposal[]) result.toArray(new ICompletionProposal[result.size()]); } catch (Exception e) { // ... log the exception ... return NO_COMPLETIONS; } } private String lastWord(IDocument doc, int offset) { try { for (int n = offset-1; n >= 0; n--) { char c = doc.getChar(n); if (!Character.isJavaIdentifierPart(c)) return doc.get(n + 1, offset-n-1); } } catch (BadLocationException e) { // ... log the exception ... } return ""; } private String lastIndent(IDocument doc, int offset) { try { int start = offset-1; while (start >= 0 && doc.getChar(start)!= '\n') start--; int end = start; while (end < offset && Character.isSpaceChar(doc.getChar(end))) end++; return doc.get(start+1, end-start-1); } catch (BadLocationException e) { e.printStackTrace(); } return ""; } public IContextInformation[] computeContextInformation(ITextViewer viewer, int offset) { return NO_CONTEXTS; } char[] getCompletionProposalAutoActivationCharacters() { return PROPOSAL_ACTIVATION_CHARS; } // ... remaining methods are optional ... }
Basically, Content Assist completion has three steps. First, we have to figure out what string has already been started by the user (see lastWord). Second, we have to find appropriate completions. Third, we have to return strings so that when they are inserted, they lay out acceptably (see the use of lastIndent).
See Also
This FAQ was originally published in Official Eclipse 3.0 FAQs. Copyright 2004, Pearson Education, Inc. All rights reserved. This text is made available here under the terms of the Eclipse Public License v1.0.