summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorszarnekow2009-04-24 11:45:52 (EDT)
committer sefftinge2009-04-24 11:45:52 (EDT)
commitc7e18f1bb989ad61d4d18bf9c21a9d355b5e52b1 (patch)
tree7da5b3c47b2dc55c9dd27d86ea02813e712400d7
parentbde6ddd2f5ab2c3ecfb2b3ca0d2a9d8006899cc0 (diff)
downloadorg.eclipse.xtext-c7e18f1bb989ad61d4d18bf9c21a9d355b5e52b1.zip
org.eclipse.xtext-c7e18f1bb989ad61d4d18bf9c21a9d355b5e52b1.tar.gz
org.eclipse.xtext-c7e18f1bb989ad61d4d18bf9c21a9d355b5e52b1.tar.bz2
Feature: Support the Ctrl-Modifier for ContentProposals, provide more helpful behavior for cases with only one proposal
-rwxr-xr-xplugins/org.eclipse.xtext.ui.common/src/org/eclipse/xtext/ui/common/editor/contentassist/impl/AbstractJavaBasedContentProposalProvider.java1
-rw-r--r--plugins/org.eclipse.xtext.ui.common/src/org/eclipse/xtext/ui/common/editor/contentassist/impl/ConfigurableCompletionProposal.java45
-rw-r--r--plugins/org.eclipse.xtext.ui.common/src/org/eclipse/xtext/ui/common/editor/contentassist/impl/DefaultCompletionProposalPostProcessor.java31
-rw-r--r--plugins/org.eclipse.xtext.ui.common/src/org/eclipse/xtext/ui/common/service/DefaultUIModule.java6
-rw-r--r--plugins/org.eclipse.xtext.ui.core/src/org/eclipse/xtext/ui/core/editor/contentassist/ICompletionProposalPostProcessor.java29
-rw-r--r--plugins/org.eclipse.xtext.ui.core/src/org/eclipse/xtext/ui/core/editor/contentassist/XtextContentAssistProcessor.java12
6 files changed, 121 insertions, 3 deletions
diff --git a/plugins/org.eclipse.xtext.ui.common/src/org/eclipse/xtext/ui/common/editor/contentassist/impl/AbstractJavaBasedContentProposalProvider.java b/plugins/org.eclipse.xtext.ui.common/src/org/eclipse/xtext/ui/common/editor/contentassist/impl/AbstractJavaBasedContentProposalProvider.java
index 9fe57df..2ed1176 100755
--- a/plugins/org.eclipse.xtext.ui.common/src/org/eclipse/xtext/ui/common/editor/contentassist/impl/AbstractJavaBasedContentProposalProvider.java
+++ b/plugins/org.eclipse.xtext.ui.common/src/org/eclipse/xtext/ui/common/editor/contentassist/impl/AbstractJavaBasedContentProposalProvider.java
@@ -177,6 +177,7 @@ public abstract class AbstractJavaBasedContentProposalProvider extends AbstractC
int replacementOffset, int replacementLength, ContentAssistContext context) {
ConfigurableCompletionProposal result = new ConfigurableCompletionProposal(name, replacementOffset, replacementLength, name.length(), image, displayString, null, null);
result.setMatcher(context.getMatcher());
+ result.setReplaceContextLength(context.getCurrentNode().getLength());
return result;
}
diff --git a/plugins/org.eclipse.xtext.ui.common/src/org/eclipse/xtext/ui/common/editor/contentassist/impl/ConfigurableCompletionProposal.java b/plugins/org.eclipse.xtext.ui.common/src/org/eclipse/xtext/ui/common/editor/contentassist/impl/ConfigurableCompletionProposal.java
index 9b49f6a..cc2a970 100644
--- a/plugins/org.eclipse.xtext.ui.common/src/org/eclipse/xtext/ui/common/editor/contentassist/impl/ConfigurableCompletionProposal.java
+++ b/plugins/org.eclipse.xtext.ui.common/src/org/eclipse/xtext/ui/common/editor/contentassist/impl/ConfigurableCompletionProposal.java
@@ -176,6 +176,7 @@ public class ConfigurableCompletionProposal implements ICompletionProposal, ICom
private ITextViewer viewer;
private char[] exitChars;
private PrefixMatcher matcher;
+ private int replaceContextLength;
public boolean isAutoInsertable() {
return autoInsertable;
@@ -257,18 +258,48 @@ public class ConfigurableCompletionProposal implements ICompletionProposal, ICom
public void apply(ITextViewer viewer, char trigger, int stateMask, int offset) {
this.setReplacementLength(offset - getReplacementOffset() + viewer.getSelectedRange().y);
+ boolean replaceRight = (stateMask & SWT.CTRL) != 0;
+ if (replaceRight) {
+ setReplacementLength(getReplaceContextLength());
+ }
apply(viewer.getDocument());
}
+
+ private Point rememberedSelection;
+
+ private void updateSelection(ITextViewer viewer) {
+ rememberedSelection = viewer.getSelectedRange();
+ int offset = rememberedSelection.x;
+ int length= getReplaceContextLength() - (offset - getReplacementOffset());
+
+ viewer.setSelectedRange(offset, length);
+ }
+
+ private void restoreSelection(ITextViewer viewer) {
+ if (rememberedSelection != null)
+ viewer.setSelectedRange(rememberedSelection.x, rememberedSelection.y);
+ }
public void selected(ITextViewer viewer, boolean smartToggle) {
- // TODO implement highlighting depending on smartToggle
+ if (smartToggle)
+ updateSelection(viewer);
+ else {
+ restoreSelection(viewer);
+ rememberedSelection= null;
+ }
}
public void unselected(ITextViewer viewer) {
- // TODO implement highlighting depending on smartToggle
+ restoreSelection(viewer);
+ rememberedSelection= null;
}
public boolean validate(IDocument document, int offset, DocumentEvent event) {
+ if (event != null) {
+ int oldReplaceContextLength = getReplaceContextLength();
+ int diff = event.getText().length() - event.getLength();
+ setReplaceContextLength(oldReplaceContextLength + diff);
+ }
try {
String prefix = document.get(replacementOffset, offset - replacementOffset);
return matcher.isCandidateMatchingPrefix(replacementString, prefix);
@@ -287,6 +318,14 @@ public class ConfigurableCompletionProposal implements ICompletionProposal, ICom
return matcher;
}
+ public void setReplaceContextLength(int replaceContextLength) {
+ this.replaceContextLength = replaceContextLength;
+ }
+
+ public int getReplaceContextLength() {
+ return replaceContextLength;
+ }
+
// copied from AbstractJavaCompletionProposal
/**
* Sets up a simple linked mode at {@link #getCursorPosition()} and an exit policy that will
@@ -346,5 +385,5 @@ public class ConfigurableCompletionProposal implements ICompletionProposal, ICom
}
}
-
+
}
diff --git a/plugins/org.eclipse.xtext.ui.common/src/org/eclipse/xtext/ui/common/editor/contentassist/impl/DefaultCompletionProposalPostProcessor.java b/plugins/org.eclipse.xtext.ui.common/src/org/eclipse/xtext/ui/common/editor/contentassist/impl/DefaultCompletionProposalPostProcessor.java
new file mode 100644
index 0000000..1bf998c
--- /dev/null
+++ b/plugins/org.eclipse.xtext.ui.common/src/org/eclipse/xtext/ui/common/editor/contentassist/impl/DefaultCompletionProposalPostProcessor.java
@@ -0,0 +1,31 @@
+/*******************************************************************************
+ * Copyright (c) 2009 itemis AG (http://www.itemis.eu) 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
+ *******************************************************************************/
+package org.eclipse.xtext.ui.common.editor.contentassist.impl;
+
+import org.eclipse.jface.text.contentassist.ICompletionProposal;
+import org.eclipse.xtext.ui.core.editor.contentassist.ICompletionProposalPostProcessor;
+
+/**
+ * @author Sebastian Zarnekow - Initial contribution and API
+ */
+public class DefaultCompletionProposalPostProcessor implements ICompletionProposalPostProcessor {
+
+ public ICompletionProposal[] postProcess(ICompletionProposal[] proposals) {
+ if (proposals.length == 1) {
+ if (proposals[0] instanceof ConfigurableCompletionProposal) {
+ ConfigurableCompletionProposal proposal = (ConfigurableCompletionProposal) proposals[0];
+ if (proposal.isAutoInsertable() && proposal.getReplaceContextLength() > proposal.getReplacementLength()) {
+ proposal.setAutoInsertable(false);
+ }
+ }
+ }
+ return proposals;
+ }
+
+
+}
diff --git a/plugins/org.eclipse.xtext.ui.common/src/org/eclipse/xtext/ui/common/service/DefaultUIModule.java b/plugins/org.eclipse.xtext.ui.common/src/org/eclipse/xtext/ui/common/service/DefaultUIModule.java
index 7b9b761..49cca10 100644
--- a/plugins/org.eclipse.xtext.ui.common/src/org/eclipse/xtext/ui/common/service/DefaultUIModule.java
+++ b/plugins/org.eclipse.xtext.ui.common/src/org/eclipse/xtext/ui/common/service/DefaultUIModule.java
@@ -17,6 +17,7 @@ import org.eclipse.jface.text.rules.ITokenScanner;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.ui.views.contentoutline.IContentOutlinePage;
import org.eclipse.xtext.service.AbstractGenericModule;
+import org.eclipse.xtext.ui.common.editor.contentassist.impl.DefaultCompletionProposalPostProcessor;
import org.eclipse.xtext.ui.common.editor.contentassist.impl.DefaultContentAssistContextFactory;
import org.eclipse.xtext.ui.common.editor.contentassist.impl.DefaultContentAssistantFactory;
import org.eclipse.xtext.ui.common.editor.contentassist.impl.DefaultFollowElementCalculator;
@@ -34,6 +35,7 @@ import org.eclipse.xtext.ui.core.InjectableAdapterFactoryLabelProvider;
import org.eclipse.xtext.ui.core.editor.IDamagerRepairer;
import org.eclipse.xtext.ui.core.editor.XtextDamagerRepairer;
import org.eclipse.xtext.ui.core.editor.contentassist.ContentAssistContext;
+import org.eclipse.xtext.ui.core.editor.contentassist.ICompletionProposalPostProcessor;
import org.eclipse.xtext.ui.core.editor.contentassist.IContentAssistantFactory;
import org.eclipse.xtext.ui.core.editor.contentassist.IFollowElementCalculator;
import org.eclipse.xtext.ui.core.editor.contentassist.ITemplateProposalProvider;
@@ -105,6 +107,10 @@ public abstract class DefaultUIModule extends AbstractGenericModule {
return DefaultContentAssistContextFactory.class;
}
+ public Class<? extends ICompletionProposalPostProcessor> bindICompletionProposalPostProcessor() {
+ return DefaultCompletionProposalPostProcessor.class;
+ }
+
public Class<? extends IFollowElementCalculator> bindIFollowElementCalculator() {
return DefaultFollowElementCalculator.class;
}
diff --git a/plugins/org.eclipse.xtext.ui.core/src/org/eclipse/xtext/ui/core/editor/contentassist/ICompletionProposalPostProcessor.java b/plugins/org.eclipse.xtext.ui.core/src/org/eclipse/xtext/ui/core/editor/contentassist/ICompletionProposalPostProcessor.java
new file mode 100644
index 0000000..21a9516
--- /dev/null
+++ b/plugins/org.eclipse.xtext.ui.core/src/org/eclipse/xtext/ui/core/editor/contentassist/ICompletionProposalPostProcessor.java
@@ -0,0 +1,29 @@
+/*******************************************************************************
+ * Copyright (c) 2009 itemis AG (http://www.itemis.eu) 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
+ *******************************************************************************/
+package org.eclipse.xtext.ui.core.editor.contentassist;
+
+import org.eclipse.jface.text.contentassist.ICompletionProposal;
+
+import com.google.inject.ImplementedBy;
+
+/**
+ * @author Sebastian Zarnekow - Initial contribution and API
+ */
+@ImplementedBy(ICompletionProposalPostProcessor.NullProcessor.class)
+public interface ICompletionProposalPostProcessor {
+
+ ICompletionProposal[] postProcess(ICompletionProposal[] proposals);
+
+ public class NullProcessor implements ICompletionProposalPostProcessor {
+
+ public ICompletionProposal[] postProcess(ICompletionProposal[] proposals) {
+ return proposals;
+ }
+
+ }
+}
diff --git a/plugins/org.eclipse.xtext.ui.core/src/org/eclipse/xtext/ui/core/editor/contentassist/XtextContentAssistProcessor.java b/plugins/org.eclipse.xtext.ui.core/src/org/eclipse/xtext/ui/core/editor/contentassist/XtextContentAssistProcessor.java
index 709735d..4807967 100644
--- a/plugins/org.eclipse.xtext.ui.core/src/org/eclipse/xtext/ui/core/editor/contentassist/XtextContentAssistProcessor.java
+++ b/plugins/org.eclipse.xtext.ui.core/src/org/eclipse/xtext/ui/core/editor/contentassist/XtextContentAssistProcessor.java
@@ -56,6 +56,9 @@ public class XtextContentAssistProcessor implements IContentAssistProcessor, Com
@Inject
private ICompletionProposalComparator completionProposalComparator;
+ @Inject
+ private ICompletionProposalPostProcessor completionProposalPostProcessor;
+
@Inject(optional = true)
@Named(value=COMPLETION_AUTO_ACTIVATION_CHARS)
private String completionProposalAutoActivationCharacters = null;
@@ -75,6 +78,7 @@ public class XtextContentAssistProcessor implements IContentAssistProcessor, Com
IXtextDocument document = (IXtextDocument) viewer.getDocument();
ICompletionProposal[] result = document.readOnly(createCompletionProposalComputer(viewer, offset));
Arrays.sort(result, completionProposalComparator);
+ result = completionProposalPostProcessor.postProcess(result);
return result;
}
@@ -172,5 +176,13 @@ public class XtextContentAssistProcessor implements IContentAssistProcessor, Com
return result;
}
+ public void setCompletionProposalPostProcessor(ICompletionProposalPostProcessor completionProposalPostProcessor) {
+ this.completionProposalPostProcessor = completionProposalPostProcessor;
+ }
+
+ public ICompletionProposalPostProcessor getCompletionProposalPostProcessor() {
+ return completionProposalPostProcessor;
+ }
+
}