diff options
author | Mickael Istria | 2019-06-24 13:04:05 +0000 |
---|---|---|
committer | Mickael Istria | 2019-09-16 07:42:13 +0000 |
commit | b706c681d2e1dedf9cff05eb1a366db762abc8be (patch) | |
tree | 14c8e47e2756a474ec2f6cdb0ff4c6cfb2b836d9 /org.eclipse.ui.genericeditor.tests | |
parent | ac2adad7a6d0be0fc28293d34900f45781810acd (diff) | |
download | eclipse.platform.text-b706c681d2e1dedf9cff05eb1a366db762abc8be.tar.gz eclipse.platform.text-b706c681d2e1dedf9cff05eb1a366db762abc8be.tar.xz eclipse.platform.text-b706c681d2e1dedf9cff05eb1a366db762abc8be.zip |
Bug 547683 - Add non-UI Thread access to last known selection
Adds ITextViewerExtension9.getLastKnownSelection() method that gives
access to
last known value for the selection without requiring UI Thread.
The new Generic Editor test case shows how this can be leveraged in
ContentAssistProcessors.
Change-Id: I8a4831c08f7ff1ade07f2a12ad8d0d2c682a372c
Signed-off-by: Mickael Istria <mistria@redhat.com>
Diffstat (limited to 'org.eclipse.ui.genericeditor.tests')
5 files changed, 100 insertions, 7 deletions
diff --git a/org.eclipse.ui.genericeditor.tests/META-INF/MANIFEST.MF b/org.eclipse.ui.genericeditor.tests/META-INF/MANIFEST.MF index a9071960add..a2dcaad225e 100644 --- a/org.eclipse.ui.genericeditor.tests/META-INF/MANIFEST.MF +++ b/org.eclipse.ui.genericeditor.tests/META-INF/MANIFEST.MF @@ -2,7 +2,7 @@ Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: %Plugin.name Bundle-SymbolicName: org.eclipse.ui.genericeditor.tests;singleton:=true -Bundle-Version: 1.1.400.qualifier +Bundle-Version: 1.1.500.qualifier Bundle-Vendor: %Plugin.providerName Bundle-Localization: plugin Export-Package: org.eclipse.ui.genericeditor.tests, @@ -13,7 +13,7 @@ Require-Bundle: org.eclipse.core.runtime;bundle-version="[3.6.0,4.0.0)", org.eclipse.text;bundle-version="[3.5.0,4.0.0)", org.eclipse.ui.genericeditor, org.eclipse.core.resources;bundle-version="3.11.0", - org.eclipse.jface.text;bundle-version="3.11.0", + org.eclipse.jface.text;bundle-version="3.16.0", org.eclipse.ui;bundle-version="3.108.0", org.eclipse.ui.workbench.texteditor;bundle-version="3.10.0", org.eclipse.ui.ide;bundle-version="3.11.0", diff --git a/org.eclipse.ui.genericeditor.tests/plugin.xml b/org.eclipse.ui.genericeditor.tests/plugin.xml index c2a259aa126..2bf886673d6 100644 --- a/org.eclipse.ui.genericeditor.tests/plugin.xml +++ b/org.eclipse.ui.genericeditor.tests/plugin.xml @@ -35,6 +35,10 @@ </test> </enabledWhen> </contentAssistProcessor> + <contentAssistProcessor + class="org.eclipse.ui.genericeditor.tests.contributions.ToUpperCaseContentAssistProcessor" + contentType="org.eclipse.ui.genericeditor.tests.content-type"> + </contentAssistProcessor> </extension> <extension point="org.eclipse.ui.genericeditor.hoverProviders"> diff --git a/org.eclipse.ui.genericeditor.tests/pom.xml b/org.eclipse.ui.genericeditor.tests/pom.xml index 030439183b3..a417d6d93d2 100644 --- a/org.eclipse.ui.genericeditor.tests/pom.xml +++ b/org.eclipse.ui.genericeditor.tests/pom.xml @@ -19,7 +19,7 @@ </parent> <groupId>org.eclipse.ui</groupId> <artifactId>org.eclipse.ui.genericeditor.tests</artifactId> - <version>1.1.400-SNAPSHOT</version> + <version>1.1.500-SNAPSHOT</version> <packaging>eclipse-test-plugin</packaging> <properties> <testSuite>${project.artifactId}</testSuite> diff --git a/org.eclipse.ui.genericeditor.tests/src/org/eclipse/ui/genericeditor/tests/CompletionTest.java b/org.eclipse.ui.genericeditor.tests/src/org/eclipse/ui/genericeditor/tests/CompletionTest.java index e296de61134..d6504cd6289 100644 --- a/org.eclipse.ui.genericeditor.tests/src/org/eclipse/ui/genericeditor/tests/CompletionTest.java +++ b/org.eclipse.ui.genericeditor.tests/src/org/eclipse/ui/genericeditor/tests/CompletionTest.java @@ -56,6 +56,7 @@ public class CompletionTest extends AbstratGenericEditorTest { @Test public void testCompletion() throws Exception { final Set<Shell> beforeShells = Arrays.stream(editor.getSite().getShell().getDisplay().getShells()).filter(Shell::isVisible).collect(Collectors.toSet()); + editor.selectAndReveal(3, 0); openConentAssist(); this.completionShell= findNewShell(beforeShells); final Table completionProposalList = findCompletionSelectionControl(completionShell); @@ -65,11 +66,28 @@ public class CompletionTest extends AbstratGenericEditorTest { } @Test + public void testCompletionUsingViewerSelection() throws Exception { + final Set<Shell> beforeShells = Arrays.stream(editor.getSite().getShell().getDisplay().getShells()).filter(Shell::isVisible).collect(Collectors.toSet()); + editor.getDocumentProvider().getDocument(editor.getEditorInput()).set("abc"); + editor.selectAndReveal(0, 3); + openConentAssist(); + this.completionShell= findNewShell(beforeShells); + final Table completionProposalList = findCompletionSelectionControl(completionShell); + assertTrue(new DisplayHelper() { + @Override + protected boolean condition() { + return Arrays.stream(completionProposalList.getItems()).map(TableItem::getText).anyMatch("ABC"::equals); + } + }.waitForCondition(completionProposalList.getDisplay(), 200)); + } + + @Test public void testEnabledWhenCompletion() throws Exception { // Confirm that when disabled, a completion shell is present EnabledPropertyTester.setEnabled(false); createAndOpenFile("enabledWhen.txt", "bar 'bar'"); final Set<Shell> beforeShells = Arrays.stream(editor.getSite().getShell().getDisplay().getShells()).filter(Shell::isVisible).collect(Collectors.toSet()); + editor.selectAndReveal(3, 0); openConentAssist(); Shell[] afterShells = Arrays.stream(editor.getSite().getShell().getDisplay().getShells()) .filter(Shell::isVisible) @@ -82,12 +100,12 @@ public class CompletionTest extends AbstratGenericEditorTest { EnabledPropertyTester.setEnabled(true); createAndOpenFile("enabledWhen.txt", "bar 'bar'"); final Set<Shell> beforeEnabledShells = Arrays.stream(editor.getSite().getShell().getDisplay().getShells()).filter(Shell::isVisible).collect(Collectors.toSet()); + editor.selectAndReveal(3, 0); openConentAssist(); assertNotNull(findNewShell(beforeEnabledShells)); } private void openConentAssist() { - editor.selectAndReveal(3, 0); ContentAssistAction action = (ContentAssistAction) editor.getAction(ITextEditorActionConstants.CONTENT_ASSIST); action.update(); action.run(); @@ -103,13 +121,12 @@ public class CompletionTest extends AbstratGenericEditorTest { */ private void checkCompletionContent(final Table completionProposalList) { // should be instantaneous, but happens to go asynchronous on CI so let's allow a wait - new DisplayHelper() { + assertTrue(new DisplayHelper() { @Override protected boolean condition() { return completionProposalList.getItemCount() == 2; } - }.waitForCondition(completionProposalList.getDisplay(), 200); - assertEquals(2, completionProposalList.getItemCount()); + }.waitForCondition(completionProposalList.getDisplay(), 200)); final TableItem computingItem = completionProposalList.getItem(0); assertTrue("Missing computing info entry", computingItem.getText().contains("Computing")); //$NON-NLS-1$ //$NON-NLS-2$ TableItem completionProposalItem = completionProposalList.getItem(1); @@ -142,6 +159,7 @@ public class CompletionTest extends AbstratGenericEditorTest { @Test public void testCompletionFreeze_bug521484() throws Exception { final Set<Shell> beforeShells = Arrays.stream(editor.getSite().getShell().getDisplay().getShells()).filter(Shell::isVisible).collect(Collectors.toSet()); + editor.selectAndReveal(3, 0); openConentAssist(); this.completionShell= findNewShell(beforeShells); final Table completionProposalList = findCompletionSelectionControl(this.completionShell); diff --git a/org.eclipse.ui.genericeditor.tests/src/org/eclipse/ui/genericeditor/tests/contributions/ToUpperCaseContentAssistProcessor.java b/org.eclipse.ui.genericeditor.tests/src/org/eclipse/ui/genericeditor/tests/contributions/ToUpperCaseContentAssistProcessor.java new file mode 100644 index 00000000000..9c2b8e56a80 --- /dev/null +++ b/org.eclipse.ui.genericeditor.tests/src/org/eclipse/ui/genericeditor/tests/contributions/ToUpperCaseContentAssistProcessor.java @@ -0,0 +1,71 @@ +/******************************************************************************* + * Copyright (c) 2019 Red Hat Inc. and others. + * + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License 2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + *******************************************************************************/ +package org.eclipse.ui.genericeditor.tests.contributions; + +import org.eclipse.jface.text.BadLocationException; +import org.eclipse.jface.text.ITextSelection; +import org.eclipse.jface.text.ITextViewer; +import org.eclipse.jface.text.ITextViewerExtension9; +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; + +public class ToUpperCaseContentAssistProcessor implements IContentAssistProcessor { + + @Override + public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int offset) { + if (!(viewer instanceof ITextViewerExtension9)) { + return new ICompletionProposal[0]; + } + ITextSelection selection= ((ITextViewerExtension9)viewer).getLastKnownSelection(); + if (selection.isEmpty() || selection.getLength() == 0) { + return new ICompletionProposal[0]; + } + String initialText; + try { + initialText= viewer.getDocument().get(selection.getOffset(), selection.getLength()); + return new ICompletionProposal[] { + new CompletionProposal(initialText.toUpperCase(), selection.getOffset(), initialText.length(), selection.getOffset() + initialText.length()) + }; + } catch (BadLocationException e) { + return new ICompletionProposal[0]; + } + + } + + @Override + public IContextInformation[] computeContextInformation(ITextViewer viewer, int offset) { + return new IContextInformation[0]; + } + + @Override + public char[] getCompletionProposalAutoActivationCharacters() { + return new char[0]; + } + + @Override + public char[] getContextInformationAutoActivationCharacters() { + return new char[0]; + } + + @Override + public String getErrorMessage() { + return getClass().getName(); + } + + @Override + public IContextInformationValidator getContextInformationValidator() { + return null; + } + +} |