Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSopot Cela2017-04-05 09:46:48 +0000
committerSopot Cela2017-04-05 10:10:23 +0000
commit8b0b0d544e1f4a70aafef0266f2f43e3a6c69c68 (patch)
treecc160b2409b2128ef07eb1ea3368ca799646fc2c
parentd8543e64c954fa678badd2d76c6e8bdd0883abca (diff)
downloadeclipse.platform.text-I20170405-2000.tar.gz
eclipse.platform.text-I20170405-2000.tar.xz
eclipse.platform.text-I20170405-2000.zip
Bug 514397 - Provide default autocompletion for generic editorY20170406-1000I20170405-2000
Change-Id: I113fe5ec4faf1fde579dc3400d42041d9e77c9a4 Signed-off-by: Sopot Cela <scela@redhat.com>
-rw-r--r--org.eclipse.ui.genericeditor/src/org/eclipse/ui/internal/genericeditor/DefaultContentAssistProcessor.java109
-rw-r--r--org.eclipse.ui.genericeditor/src/org/eclipse/ui/internal/genericeditor/ExtensionBasedTextViewerConfiguration.java3
2 files changed, 112 insertions, 0 deletions
diff --git a/org.eclipse.ui.genericeditor/src/org/eclipse/ui/internal/genericeditor/DefaultContentAssistProcessor.java b/org.eclipse.ui.genericeditor/src/org/eclipse/ui/internal/genericeditor/DefaultContentAssistProcessor.java
new file mode 100644
index 00000000000..9e47220531e
--- /dev/null
+++ b/org.eclipse.ui.genericeditor/src/org/eclipse/ui/internal/genericeditor/DefaultContentAssistProcessor.java
@@ -0,0 +1,109 @@
+/*******************************************************************************
+ * Copyright (c) 2017 Red Hat Inc. and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * - Sopot Cela (Red Hat Inc.)
+ *******************************************************************************/
+package org.eclipse.ui.internal.genericeditor;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.eclipse.jface.text.ITextViewer;
+import org.eclipse.jface.text.contentassist.CompletionProposal;
+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;
+
+/**
+ *
+ * This content assist processor is a default processor which will be present if
+ * no other auto-completion is registered for a given content-type. It splits
+ * the text into 'words' (which are defined as anything in-between
+ * non-alphanumeric characters) and offers them as auto-complete alternatives to
+ * the matching prefix.
+ *
+ * E.g. if your file contains "this is a t^" and you ask for auto-completion at
+ * ^ you will get 'this' as an alternative.
+ *
+ */
+public class DefaultContentAssistProcessor implements IContentAssistProcessor {
+
+ private static final String NON_ALPHANUMERIC_LAST_REGEXP = "[^\\w](?!.*[^\\w])"; //$NON-NLS-1$
+ private static final String NON_ALPHANUMERIC_REGEXP = "[^a-zA-Z0-9]+"; //$NON-NLS-1$
+ private static final Pattern NON_ALPHANUMERIC_LAST_PATTERN = Pattern.compile(NON_ALPHANUMERIC_LAST_REGEXP);
+
+
+ @Override
+ public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int offset) {
+ String text = viewer.getDocument().get();
+ String[] tokens = text.split(NON_ALPHANUMERIC_REGEXP);
+
+ //remove duplicates
+ Set<String> tokenSet = new HashSet<String>(Arrays.asList(tokens));
+
+ //wordStartIndex is the index of the last non-alphanumeric before 'offset' in text 'text'
+ int wordStartIndex = findStartingPoint(text, offset);
+ String prefix = text.substring(wordStartIndex, offset);
+
+ List<ICompletionProposal> proposals = new ArrayList<ICompletionProposal>();
+ for (String token : tokenSet) {
+ if ((token==null)||(token.isEmpty())) {
+ continue;
+ }
+ if (token.equals(prefix)) {
+ continue;
+ }
+ if (token.startsWith(prefix)) {
+ String completion = token.substring(prefix.length());
+ CompletionProposal proposal = new CompletionProposal(completion, offset, 0,
+ completion.length(), null, token, null, null);
+ proposals.add(proposal);
+ }
+ }
+ return proposals.toArray(new ICompletionProposal[proposals.size()]);
+ }
+
+ private int findStartingPoint(String text, int offset) {
+ String substring = text.substring(0, offset);
+ Matcher m = NON_ALPHANUMERIC_LAST_PATTERN.matcher(substring);
+ m.find();
+ return m.end();
+ }
+
+ @Override
+ public IContextInformation[] computeContextInformation(ITextViewer viewer, int offset) {
+ return null;
+ }
+
+ @Override
+ public char[] getCompletionProposalAutoActivationCharacters() {
+ return null;
+ }
+
+ @Override
+ public char[] getContextInformationAutoActivationCharacters() {
+ return null;
+ }
+
+ @Override
+ public String getErrorMessage() {
+ return null;
+ }
+
+ @Override
+ public IContextInformationValidator getContextInformationValidator() {
+ return null;
+ }
+
+}
diff --git a/org.eclipse.ui.genericeditor/src/org/eclipse/ui/internal/genericeditor/ExtensionBasedTextViewerConfiguration.java b/org.eclipse.ui.genericeditor/src/org/eclipse/ui/internal/genericeditor/ExtensionBasedTextViewerConfiguration.java
index 1fe651b32a1..0992d14609b 100644
--- a/org.eclipse.ui.genericeditor/src/org/eclipse/ui/internal/genericeditor/ExtensionBasedTextViewerConfiguration.java
+++ b/org.eclipse.ui.genericeditor/src/org/eclipse/ui/internal/genericeditor/ExtensionBasedTextViewerConfiguration.java
@@ -110,6 +110,9 @@ public final class ExtensionBasedTextViewerConfiguration extends TextSourceViewe
contentAssistant.enableColoredLabels(true);
contentAssistant.enableAutoActivation(true);
this.processors = registry.getContentAssistProcessors(sourceViewer, getContentTypes());
+ if (this.processors.isEmpty()) {
+ this.processors.add(new DefaultContentAssistProcessor());
+ }
for (IContentAssistProcessor processor : this.processors) {
contentAssistant.addContentAssistProcessor(processor, IDocument.DEFAULT_CONTENT_TYPE);
}

Back to the top