X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;ds=sidebyside;f=bundles%2Forg.simantics.document.ui%2Fsrc%2Forg%2Fsimantics%2Fdocument%2Fui%2FCSSCompletionAssistProcessor.java;fp=bundles%2Forg.simantics.document.ui%2Fsrc%2Forg%2Fsimantics%2Fdocument%2Fui%2FCSSCompletionAssistProcessor.java;h=033b9c5763c1525d02527c9491c32278fb86ae4b;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git 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 index 000000000..033b9c576 --- /dev/null +++ b/bundles/org.simantics.document.ui/src/org/simantics/document/ui/CSSCompletionAssistProcessor.java @@ -0,0 +1,77 @@ +package org.simantics.document.ui; + +import org.eclipse.jface.text.BadLocationException; +import org.eclipse.jface.text.IDocument; +import org.eclipse.jface.text.ITextSelection; +import org.eclipse.jface.text.ITextViewer; +import org.eclipse.jface.text.contentassist.ICompletionProposal; +import org.eclipse.jface.text.contentassist.IContentAssistProcessor; +import org.eclipse.jface.text.contentassist.IContextInformation; +import org.eclipse.jface.text.contentassist.IContextInformationValidator; + +public class CSSCompletionAssistProcessor implements IContentAssistProcessor { + + private String lastError = ""; + private CSSTextEditorEnvironment environment; + + public CSSCompletionAssistProcessor(CSSTextEditorEnvironment environment) { + this.environment = environment; + } + + @Override + public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int tmpOffset) { + ITextSelection selection= (ITextSelection) viewer.getSelectionProvider().getSelection(); + // adjust offset to end of normalized selection + if (selection.getOffset() == tmpOffset) + tmpOffset = selection.getOffset() + selection.getLength(); + final int offset = tmpOffset; + IDocument document = viewer.getDocument(); + String tmpPrefix = ""; + try { + tmpPrefix = getPrefix(document, offset); + } catch (BadLocationException e) { + e.printStackTrace(); + } + environment.updateEnvironment(document); + return environment.getCompletionProposals(tmpPrefix, offset); + } + + private static String getPrefix(IDocument doc, int offset) throws BadLocationException { + if (doc == null || offset >= doc.getLength()) + return ""; + + int length= 0; + while (--offset >= 0 && Character.isJavaIdentifierPart(doc.getChar(offset)) || doc.getChar(offset) == '.') + length++; + + return doc.get(offset + 1, length); + } + + @Override + public IContextInformation[] computeContextInformation(ITextViewer viewer, int offset) { + return null; + } + + private static final char[] AUTO_ACTIVATION_CHARS = new char[] { '.', '(' }; + + @Override + public char[] getCompletionProposalAutoActivationCharacters() { + return AUTO_ACTIVATION_CHARS; + } + + @Override + public char[] getContextInformationAutoActivationCharacters() { + return null; + } + + @Override + public String getErrorMessage() { + return lastError; + } + + @Override + public IContextInformationValidator getContextInformationValidator() { + return null; + } + +}