]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.document.ui/src/org/simantics/document/ui/CSSCompletionAssistProcessor.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.document.ui / src / org / simantics / document / ui / CSSCompletionAssistProcessor.java
diff --git a/bundles/org.simantics.document.ui/src/org/simantics/document/ui/CSSCompletionAssistProcessor.java b/bundles/org.simantics.document.ui/src/org/simantics/document/ui/CSSCompletionAssistProcessor.java
new file mode 100644 (file)
index 0000000..033b9c5
--- /dev/null
@@ -0,0 +1,77 @@
+package org.simantics.document.ui;\r
+\r
+import org.eclipse.jface.text.BadLocationException;\r
+import org.eclipse.jface.text.IDocument;\r
+import org.eclipse.jface.text.ITextSelection;\r
+import org.eclipse.jface.text.ITextViewer;\r
+import org.eclipse.jface.text.contentassist.ICompletionProposal;\r
+import org.eclipse.jface.text.contentassist.IContentAssistProcessor;\r
+import org.eclipse.jface.text.contentassist.IContextInformation;\r
+import org.eclipse.jface.text.contentassist.IContextInformationValidator;\r
+\r
+public class CSSCompletionAssistProcessor implements IContentAssistProcessor {\r
+    \r
+    private String lastError = "";\r
+    private CSSTextEditorEnvironment environment;\r
+    \r
+    public CSSCompletionAssistProcessor(CSSTextEditorEnvironment environment) {\r
+        this.environment = environment;\r
+    }\r
+\r
+    @Override\r
+    public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int tmpOffset) {\r
+        ITextSelection selection= (ITextSelection) viewer.getSelectionProvider().getSelection();\r
+        // adjust offset to end of normalized selection\r
+        if (selection.getOffset() == tmpOffset)\r
+            tmpOffset = selection.getOffset() + selection.getLength();\r
+        final int offset = tmpOffset;\r
+        IDocument document = viewer.getDocument();\r
+        String tmpPrefix = "";\r
+        try {\r
+            tmpPrefix = getPrefix(document, offset);\r
+        } catch (BadLocationException e) {\r
+            e.printStackTrace();\r
+        }\r
+        environment.updateEnvironment(document);\r
+        return environment.getCompletionProposals(tmpPrefix, offset);\r
+    }\r
+    \r
+    private static String getPrefix(IDocument doc, int offset) throws BadLocationException {\r
+        if (doc == null || offset >= doc.getLength())\r
+            return "";\r
+\r
+        int length= 0;\r
+        while (--offset >= 0 && Character.isJavaIdentifierPart(doc.getChar(offset)) || doc.getChar(offset) == '.')\r
+            length++;\r
+\r
+        return doc.get(offset + 1, length);\r
+    }\r
+    \r
+    @Override\r
+    public IContextInformation[] computeContextInformation(ITextViewer viewer, int offset) {\r
+        return null;\r
+    }\r
+\r
+    private static final char[] AUTO_ACTIVATION_CHARS = new char[] { '.', '(' };\r
+    \r
+    @Override\r
+    public char[] getCompletionProposalAutoActivationCharacters() {\r
+        return AUTO_ACTIVATION_CHARS;\r
+    }\r
+\r
+    @Override\r
+    public char[] getContextInformationAutoActivationCharacters() {\r
+        return null;\r
+    }\r
+\r
+    @Override\r
+    public String getErrorMessage() {\r
+        return lastError;\r
+    }\r
+\r
+    @Override\r
+    public IContextInformationValidator getContextInformationValidator() {\r
+        return null;\r
+    }\r
+\r
+}\r