]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/editor/completion/SCLCompletionAssistProcessor.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.scl.ui / src / org / simantics / scl / ui / editor / completion / SCLCompletionAssistProcessor.java
1 package org.simantics.scl.ui.editor.completion;\r
2 \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
11 \r
12 public class SCLCompletionAssistProcessor implements IContentAssistProcessor {\r
13     \r
14 //    private String lastPrefix = null;\r
15     private String lastError = "";\r
16     private SCLTextEditorEnvironment sclTextEditorEnvironment;\r
17     \r
18     public SCLCompletionAssistProcessor(SCLTextEditorEnvironment sclTextEditorEnvironment) {\r
19         this.sclTextEditorEnvironment = sclTextEditorEnvironment;\r
20     }\r
21 \r
22     @Override\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
31         try {\r
32             tmpPrefix = getPrefix(document, offset);\r
33         } catch (BadLocationException e) {\r
34             e.printStackTrace();\r
35         }\r
36         sclTextEditorEnvironment.updateEnvironment(document);\r
37         return sclTextEditorEnvironment.getCompletionProposals(tmpPrefix, offset);\r
38     }\r
39     \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
43             return "";\r
44 \r
45         int length= 0;\r
46         while (--offset >= 0 && Character.isJavaIdentifierPart(doc.getChar(offset)) || doc.getChar(offset) == '.')\r
47             length++;\r
48 \r
49         return doc.get(offset + 1, length);\r
50     }\r
51     \r
52     @Override\r
53     public IContextInformation[] computeContextInformation(ITextViewer viewer, int offset) {\r
54         return null;\r
55     }\r
56 \r
57     private static final char[] AUTO_ACTIVATION_CHARS = new char[] { '.', '(' };\r
58     \r
59     @Override\r
60     public char[] getCompletionProposalAutoActivationCharacters() {\r
61         return AUTO_ACTIVATION_CHARS;\r
62     }\r
63 \r
64     @Override\r
65     public char[] getContextInformationAutoActivationCharacters() {\r
66         return null;\r
67     }\r
68 \r
69     @Override\r
70     public String getErrorMessage() {\r
71         return lastError;\r
72     }\r
73 \r
74     @Override\r
75     public IContextInformationValidator getContextInformationValidator() {\r
76         return null;\r
77     }\r
78 \r
79 }\r