]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/editor/completion/SCLCompletionAssistProcessor.java
Externalize strings in org.simantics.scl.ui
[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;
2
3 import org.eclipse.jface.text.BadLocationException;
4 import org.eclipse.jface.text.IDocument;
5 import org.eclipse.jface.text.ITextSelection;
6 import org.eclipse.jface.text.ITextViewer;
7 import org.eclipse.jface.text.contentassist.ICompletionProposal;
8 import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
9 import org.eclipse.jface.text.contentassist.IContextInformation;
10 import org.eclipse.jface.text.contentassist.IContextInformationValidator;
11
12 public class SCLCompletionAssistProcessor implements IContentAssistProcessor {
13     
14 //    private String lastPrefix = null;
15     private String lastError = ""; //$NON-NLS-1$
16     private SCLTextEditorEnvironment sclTextEditorEnvironment;
17     
18     public SCLCompletionAssistProcessor(SCLTextEditorEnvironment sclTextEditorEnvironment) {
19         this.sclTextEditorEnvironment = sclTextEditorEnvironment;
20     }
21
22     @Override
23     public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int tmpOffset) {
24         ITextSelection selection= (ITextSelection) viewer.getSelectionProvider().getSelection();
25         // adjust offset to end of normalized selection
26         if (selection.getOffset() == tmpOffset)
27             tmpOffset = selection.getOffset() + selection.getLength();
28         final int offset = tmpOffset;
29         IDocument document = viewer.getDocument();
30         String tmpPrefix = ""; //$NON-NLS-1$
31         try {
32             tmpPrefix = getPrefix(document, offset);
33         } catch (BadLocationException e) {
34             e.printStackTrace();
35         }
36         sclTextEditorEnvironment.updateEnvironment(document);
37         return sclTextEditorEnvironment.getCompletionProposals(tmpPrefix, offset);
38     }
39     
40     private static String getPrefix(IDocument doc, int offset) throws BadLocationException {
41         int docLen = doc.getLength();
42         if (doc == null || offset > docLen || docLen == 0)
43             return ""; //$NON-NLS-1$
44
45         int length= 0;
46         while (--offset >= 0 && Character.isJavaIdentifierPart(doc.getChar(offset)) || doc.getChar(offset) == '.')
47             length++;
48
49         return doc.get(offset + 1, length);
50     }
51     
52     @Override
53     public IContextInformation[] computeContextInformation(ITextViewer viewer, int offset) {
54         return null;
55     }
56
57     private static final char[] AUTO_ACTIVATION_CHARS = new char[] { '.', '(' };
58     
59     @Override
60     public char[] getCompletionProposalAutoActivationCharacters() {
61         return AUTO_ACTIVATION_CHARS;
62     }
63
64     @Override
65     public char[] getContextInformationAutoActivationCharacters() {
66         return null;
67     }
68
69     @Override
70     public String getErrorMessage() {
71         return lastError;
72     }
73
74     @Override
75     public IContextInformationValidator getContextInformationValidator() {
76         return null;
77     }
78
79 }