1 package org.simantics.scl.ui.editor.completion;
\r
3 import org.eclipse.jface.text.BadLocationException;
\r
4 import org.eclipse.jface.text.IDocument;
\r
5 import org.eclipse.jface.text.ITextSelection;
\r
6 import org.eclipse.jface.text.ITextViewer;
\r
7 import org.eclipse.jface.text.contentassist.ICompletionProposal;
\r
8 import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
\r
9 import org.eclipse.jface.text.contentassist.IContextInformation;
\r
10 import org.eclipse.jface.text.contentassist.IContextInformationValidator;
\r
12 public class SCLCompletionAssistProcessor implements IContentAssistProcessor {
\r
14 // private String lastPrefix = null;
\r
15 private String lastError = "";
\r
16 private SCLTextEditorEnvironment sclTextEditorEnvironment;
\r
18 public SCLCompletionAssistProcessor(SCLTextEditorEnvironment sclTextEditorEnvironment) {
\r
19 this.sclTextEditorEnvironment = sclTextEditorEnvironment;
\r
23 public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int tmpOffset) {
\r
24 ITextSelection selection= (ITextSelection) viewer.getSelectionProvider().getSelection();
\r
25 // adjust offset to end of normalized selection
\r
26 if (selection.getOffset() == tmpOffset)
\r
27 tmpOffset = selection.getOffset() + selection.getLength();
\r
28 final int offset = tmpOffset;
\r
29 IDocument document = viewer.getDocument();
\r
30 String tmpPrefix = "";
\r
32 tmpPrefix = getPrefix(document, offset);
\r
33 } catch (BadLocationException e) {
\r
34 e.printStackTrace();
\r
36 sclTextEditorEnvironment.updateEnvironment(document);
\r
37 return sclTextEditorEnvironment.getCompletionProposals(tmpPrefix, offset);
\r
40 private static String getPrefix(IDocument doc, int offset) throws BadLocationException {
\r
41 int docLen = doc.getLength();
\r
42 if (doc == null || offset > docLen || docLen == 0)
\r
46 while (--offset >= 0 && Character.isJavaIdentifierPart(doc.getChar(offset)) || doc.getChar(offset) == '.')
\r
49 return doc.get(offset + 1, length);
\r
53 public IContextInformation[] computeContextInformation(ITextViewer viewer, int offset) {
\r
57 private static final char[] AUTO_ACTIVATION_CHARS = new char[] { '.', '(' };
\r
60 public char[] getCompletionProposalAutoActivationCharacters() {
\r
61 return AUTO_ACTIVATION_CHARS;
\r
65 public char[] getContextInformationAutoActivationCharacters() {
\r
70 public String getErrorMessage() {
\r
75 public IContextInformationValidator getContextInformationValidator() {
\r