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