diff options
author | Sopot Cela | 2016-09-26 10:16:25 +0000 |
---|---|---|
committer | Dani Megert | 2016-09-26 10:30:36 +0000 |
commit | 5bcb7ac5045743f9ec17895a9a12318dd90ea4cb (patch) | |
tree | ea2d98cee208a040d2ee43fef458d3775a5dc90d /org.eclipse.ui.genericeditor.tests/src/org | |
parent | 8871738e5b595e7715499161b04980d9f16df820 (diff) | |
download | eclipse.platform.text-5bcb7ac5045743f9ec17895a9a12318dd90ea4cb.tar.gz eclipse.platform.text-5bcb7ac5045743f9ec17895a9a12318dd90ea4cb.tar.xz eclipse.platform.text-5bcb7ac5045743f9ec17895a9a12318dd90ea4cb.zip |
Bug 497871 - Generic and extensible text editor
This change creates a new extensible text editor, with extension points
for:
* contentAssist
* hover
* syntax highlighting
Some unit tests show examples of extensions. You can load an IDE with
the suggested change (including org.eclipse.ui.editors.tests) and get a
syntax highlighter, a hover support and some content assist on the plain
text editor.
Bug: 497871
Bug: 496117
Bug: 496115
Bug: 496300
Signed-off-by: Mickael Istria <mistria@redhat.com>
Signed-off-by: Sopot Cela <scela@redhat.com>
Change-Id: I2eec71e4620364aa11c500ec07e54c693863cf44
Diffstat (limited to 'org.eclipse.ui.genericeditor.tests/src/org')
8 files changed, 487 insertions, 0 deletions
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 new file mode 100644 index 00000000000..8f7e69a6d1f --- /dev/null +++ b/org.eclipse.ui.genericeditor.tests/src/org/eclipse/ui/genericeditor/tests/CompletionTest.java @@ -0,0 +1,106 @@ +/******************************************************************************* + * Copyright (c) 2016 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: + * Mickael Istria, Sopot Cela (Red Hat Inc.) + *******************************************************************************/ +package org.eclipse.ui.genericeditor.tests; + +import static org.junit.Assert.assertEquals; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Display; +import org.eclipse.swt.widgets.Shell; +import org.eclipse.swt.widgets.Table; +import org.eclipse.swt.widgets.TableItem; +import org.eclipse.swt.widgets.Widget; + +import org.eclipse.jface.text.contentassist.ICompletionProposal; + +import org.eclipse.ui.PlatformUI; +import org.eclipse.ui.part.FileEditorInput; + +import org.eclipse.ui.texteditor.AbstractTextEditor; +import org.eclipse.ui.texteditor.ContentAssistAction; +import org.eclipse.ui.texteditor.ITextEditorActionConstants; + +/** + * @since 3.11 + * + */ +public class CompletionTest { + + private AbstractTextEditor editor; + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + GenericEditorTestUtils.setUpBeforeClass(); + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + GenericEditorTestUtils.tearDownAfterClass(); + } + + @Before + public void setUp() throws Exception { + GenericEditorTestUtils.closeIntro(); + editor = (AbstractTextEditor) PlatformUI.getWorkbench().getActiveWorkbenchWindow() + .getActivePage().openEditor(new FileEditorInput(GenericEditorTestUtils.getFile()), "org.eclipse.ui.genericeditor.GenericEditor"); + } + + @After + public void tearDown() throws Exception { + editor.getSite().getPage().closeEditor(editor, false); + editor= null; + } + + @Test + public void testCompletion() throws Exception { + Set<Shell> beforeShell = new HashSet<>(Arrays.asList(Display.getDefault().getShells())); + editor.selectAndReveal(3, 0); + ContentAssistAction action = (ContentAssistAction) editor.getAction(ITextEditorActionConstants.CONTENT_ASSIST); + action.update(); + action.run(); + Set<Shell> afterShell = new HashSet<>(Arrays.asList(Display.getDefault().getShells())); + afterShell.removeAll(beforeShell); + assertEquals("No completion", 1, afterShell.size()); + Shell completionShell= afterShell.iterator().next(); + Table completionProposalList = findCompletionSelectionControl(completionShell); + assertEquals(1, completionProposalList.getItemCount()); + TableItem completionProposalItem = completionProposalList.getItem(0); + assertEquals("s are good for a beer.", ((ICompletionProposal)completionProposalItem.getData()).getDisplayString()); + // TODO find a way to actually trigger completion and verify result against Editor content + // Assert.assertEquals("Completion didn't complete", "bars are good for a beer.", ((StyledText)editor.getAdapter(Control.class)).getText()); + completionShell.close(); + } + + private Table findCompletionSelectionControl(Widget control) { + if (control instanceof Table) { + return (Table)control; + } else if (control instanceof Composite) { + for (Widget child : ((Composite)control).getChildren()) { + Table res = findCompletionSelectionControl(child); + if (res != null) { + return res; + } + } + } + return null; + } + +} diff --git a/org.eclipse.ui.genericeditor.tests/src/org/eclipse/ui/genericeditor/tests/GenericEditorTestSuite.java b/org.eclipse.ui.genericeditor.tests/src/org/eclipse/ui/genericeditor/tests/GenericEditorTestSuite.java new file mode 100644 index 00000000000..9e41f7e300c --- /dev/null +++ b/org.eclipse.ui.genericeditor.tests/src/org/eclipse/ui/genericeditor/tests/GenericEditorTestSuite.java @@ -0,0 +1,31 @@ +/******************************************************************************* + * Copyright (c) 2000, 2016 IBM Corporation 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: + * IBM Corporation - initial API and implementation + * Mickael Istria (Red Hat Inc.) - [484157] Add zoom test + *******************************************************************************/ +package org.eclipse.ui.genericeditor.tests; + +import org.junit.runner.RunWith; +import org.junit.runners.Suite; +import org.junit.runners.Suite.SuiteClasses; + +/** + * Test Suite for org.eclipse.ui.editors. + * + * @since 3.0 + */ +@RunWith(Suite.class) +@SuiteClasses({ + CompletionTest.class, + StylingTest.class, + HoverTest.class +}) +public class GenericEditorTestSuite { + // see @SuiteClasses +} diff --git a/org.eclipse.ui.genericeditor.tests/src/org/eclipse/ui/genericeditor/tests/GenericEditorTestUtils.java b/org.eclipse.ui.genericeditor.tests/src/org/eclipse/ui/genericeditor/tests/GenericEditorTestUtils.java new file mode 100644 index 00000000000..33caf13be52 --- /dev/null +++ b/org.eclipse.ui.genericeditor.tests/src/org/eclipse/ui/genericeditor/tests/GenericEditorTestUtils.java @@ -0,0 +1,51 @@ +/******************************************************************************* + * Copyright (c) 2016 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.genericeditor.tests; + +import java.io.ByteArrayInputStream; + +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IProject; +import org.eclipse.core.resources.ResourcesPlugin; + +import org.eclipse.ui.PlatformUI; +import org.eclipse.ui.intro.IIntroPart; + +public class GenericEditorTestUtils { + + private static IProject project; + private static IFile file; + + public static void setUpBeforeClass() throws Exception { + project = ResourcesPlugin.getWorkspace().getRoot().getProject("genericEditorTest"); + project.create(null); + project.open(null); + file = project.getFile("foo.txt"); + file.create(new ByteArrayInputStream("bar 'bar'".getBytes()), true, null); + } + + public static void tearDownAfterClass() throws Exception { + file.delete(true, null); + project.delete(true, null); + } + + public static void closeIntro() { + IIntroPart intro = PlatformUI.getWorkbench().getIntroManager().getIntro(); + if (intro != null) { + PlatformUI.getWorkbench().getIntroManager().closeIntro(intro); + } + } + + public static IFile getFile(){ + return file; + } + +} diff --git a/org.eclipse.ui.genericeditor.tests/src/org/eclipse/ui/genericeditor/tests/HoverTest.java b/org.eclipse.ui.genericeditor.tests/src/org/eclipse/ui/genericeditor/tests/HoverTest.java new file mode 100644 index 00000000000..37f4f1b092f --- /dev/null +++ b/org.eclipse.ui.genericeditor.tests/src/org/eclipse/ui/genericeditor/tests/HoverTest.java @@ -0,0 +1,108 @@ +/******************************************************************************* + * Copyright (c) 2016 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: + * Mickael Istria, Sopot Cela (Red Hat Inc.) + *******************************************************************************/ +package org.eclipse.ui.genericeditor.tests; + +import static org.junit.Assert.assertEquals; + +import java.lang.reflect.Field; +import java.lang.reflect.Method; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +import org.eclipse.swt.SWT; +import org.eclipse.swt.custom.StyledText; +import org.eclipse.swt.widgets.Control; +import org.eclipse.swt.widgets.Display; +import org.eclipse.swt.widgets.Event; + +import org.eclipse.jface.text.AbstractHoverInformationControlManager; +import org.eclipse.jface.text.AbstractInformationControlManager; +import org.eclipse.jface.text.ITextViewer; +import org.eclipse.jface.text.TextViewer; + +import org.eclipse.ui.PlatformUI; +import org.eclipse.ui.part.FileEditorInput; + +import org.eclipse.ui.texteditor.AbstractTextEditor; + +/** + * @since 3.11 + * + */ +public class HoverTest { + + private AbstractTextEditor editor; + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + GenericEditorTestUtils.setUpBeforeClass(); + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + GenericEditorTestUtils.tearDownAfterClass(); + } + + @Before + public void setUp() throws Exception { + GenericEditorTestUtils.closeIntro(); + editor = (AbstractTextEditor) PlatformUI.getWorkbench().getActiveWorkbenchWindow() + .getActivePage().openEditor(new FileEditorInput(GenericEditorTestUtils.getFile()), "org.eclipse.ui.genericeditor.GenericEditor"); + } + + @After + public void tearDown() throws Exception { + editor.getSite().getPage().closeEditor(editor, false); + editor= null; + } + + @Test + public void testHover() throws Exception { + this.editor.selectAndReveal(2, 0); + // Events need to be processed for hover listener to work correctly + long timeout = 1000; + long start = System.currentTimeMillis(); + while (start + timeout > System.currentTimeMillis()) { + Display.getDefault().readAndDispatch(); + } + // sending event to trigger hover computation + StyledText editorTextWidget = (StyledText) this.editor.getAdapter(Control.class); + editorTextWidget.getShell().forceActive(); + editorTextWidget.getShell().setActive(); + editorTextWidget.getShell().setFocus(); + editorTextWidget.getShell().getDisplay().wake(); + Event hoverEvent = new Event(); + hoverEvent.widget = editorTextWidget; + hoverEvent.type = SWT.MouseHover; + hoverEvent.x = editorTextWidget.getClientArea().x + 5; + hoverEvent.y = editorTextWidget.getClientArea().y + 5; + hoverEvent.display = editorTextWidget.getDisplay(); + hoverEvent.doit = true; + editorTextWidget.notifyListeners(SWT.MouseHover, hoverEvent); + // retrieving hover content + Method getSourceViewerMethod= AbstractTextEditor.class.getDeclaredMethod("getSourceViewer"); + getSourceViewerMethod.setAccessible(true); + ITextViewer viewer = (ITextViewer) getSourceViewerMethod.invoke(editor); + Field textHoverManagerField= TextViewer.class.getDeclaredField("fTextHoverManager"); + textHoverManagerField.setAccessible(true); + AbstractHoverInformationControlManager hover = (AbstractHoverInformationControlManager) textHoverManagerField.get(viewer); + Field informationField = AbstractInformationControlManager.class.getDeclaredField("fInformation"); + informationField.setAccessible(true); + Object hoverData = informationField.get(hover); + Thread.sleep(500); // hoverData populated asynchronously + assertEquals("Alrighty!", hoverData); + } + +} diff --git a/org.eclipse.ui.genericeditor.tests/src/org/eclipse/ui/genericeditor/tests/StylingTest.java b/org.eclipse.ui.genericeditor.tests/src/org/eclipse/ui/genericeditor/tests/StylingTest.java new file mode 100644 index 00000000000..4a5fa8b93ad --- /dev/null +++ b/org.eclipse.ui.genericeditor.tests/src/org/eclipse/ui/genericeditor/tests/StylingTest.java @@ -0,0 +1,68 @@ +/******************************************************************************* + * Copyright (c) 2016 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.genericeditor.tests; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +import org.eclipse.swt.custom.StyleRange; +import org.eclipse.swt.custom.StyledText; +import org.eclipse.swt.graphics.RGB; +import org.eclipse.swt.widgets.Control; + +import org.eclipse.ui.PlatformUI; +import org.eclipse.ui.part.FileEditorInput; + +import org.eclipse.ui.texteditor.AbstractTextEditor; + +public class StylingTest { + + private AbstractTextEditor editor; + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + GenericEditorTestUtils.setUpBeforeClass(); + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + GenericEditorTestUtils.tearDownAfterClass(); + } + + @Before + public void setUp() throws Exception { + GenericEditorTestUtils.closeIntro(); + + editor = (AbstractTextEditor) PlatformUI.getWorkbench().getActiveWorkbenchWindow() + .getActivePage().openEditor(new FileEditorInput(GenericEditorTestUtils.getFile()), "org.eclipse.ui.genericeditor.GenericEditor"); + } + + @After + public void tearDown() throws Exception { + editor.getSite().getPage().closeEditor(editor, false); + editor= null; + } + + @Test + public void testStyle() throws Exception { + + editor.selectAndReveal(4, 8); + StyledText widget = (StyledText) editor.getAdapter(Control.class); + StyleRange style= widget.getStyleRangeAtOffset(4);//get the style of first token + boolean isRed= style.foreground.getRGB().equals(new RGB(255, 0, 0));//is it Red? + Assert.assertTrue("Token is not of expected color", isRed); + } + +} diff --git a/org.eclipse.ui.genericeditor.tests/src/org/eclipse/ui/genericeditor/tests/contributions/BarContentAssistProcessor.java b/org.eclipse.ui.genericeditor.tests/src/org/eclipse/ui/genericeditor/tests/contributions/BarContentAssistProcessor.java new file mode 100644 index 00000000000..4d5f12ba448 --- /dev/null +++ b/org.eclipse.ui.genericeditor.tests/src/org/eclipse/ui/genericeditor/tests/contributions/BarContentAssistProcessor.java @@ -0,0 +1,58 @@ +/******************************************************************************* + * Copyright (c) 2016 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: + * - Mickael Istria (Red Hat Inc.) + *******************************************************************************/ +package org.eclipse.ui.genericeditor.tests.contributions; + +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; + +public class BarContentAssistProcessor implements IContentAssistProcessor { + + @Override + public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int offset) { + String text = viewer.getTextWidget().getText(); + if (text.length() >= 3 && text.substring(offset - 3, offset).equals("bar")) { + String message = "s are good for a beer."; + CompletionProposal proposal = new CompletionProposal(message, offset, 0, message.length()); + return new ICompletionProposal[] { proposal }; + } + return new ICompletionProposal[0]; + } + + @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.tests/src/org/eclipse/ui/genericeditor/tests/contributions/MagicHoverProvider.java b/org.eclipse.ui.genericeditor.tests/src/org/eclipse/ui/genericeditor/tests/contributions/MagicHoverProvider.java new file mode 100644 index 00000000000..d8f83117ea0 --- /dev/null +++ b/org.eclipse.ui.genericeditor.tests/src/org/eclipse/ui/genericeditor/tests/contributions/MagicHoverProvider.java @@ -0,0 +1,37 @@ +/******************************************************************************* + * Copyright (c) 2016 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: + * Mickael Istria (Red Hat Inc.) + *******************************************************************************/ +package org.eclipse.ui.genericeditor.tests.contributions; + +import org.eclipse.jface.text.IRegion; +import org.eclipse.jface.text.ITextHover; +import org.eclipse.jface.text.ITextHoverExtension2; +import org.eclipse.jface.text.ITextViewer; +import org.eclipse.jface.text.Region; + +public class MagicHoverProvider implements ITextHover,ITextHoverExtension2 { + + @Deprecated + @Override + public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) { + return null; + } + + @Override + public IRegion getHoverRegion(ITextViewer textViewer, int offset) { + return new Region(0, textViewer.getTextWidget().getText().length()); + } + + @Override + public Object getHoverInfo2(ITextViewer textViewer, IRegion hoverRegion) { + return "Alrighty!"; + } + +} diff --git a/org.eclipse.ui.genericeditor.tests/src/org/eclipse/ui/genericeditor/tests/contributions/ThePresentationReconciler.java b/org.eclipse.ui.genericeditor.tests/src/org/eclipse/ui/genericeditor/tests/contributions/ThePresentationReconciler.java new file mode 100644 index 00000000000..c7968f845d7 --- /dev/null +++ b/org.eclipse.ui.genericeditor.tests/src/org/eclipse/ui/genericeditor/tests/contributions/ThePresentationReconciler.java @@ -0,0 +1,28 @@ +package org.eclipse.ui.genericeditor.tests.contributions; + +import org.eclipse.swt.graphics.Color; +import org.eclipse.swt.graphics.RGB; +import org.eclipse.swt.widgets.Display; + +import org.eclipse.jface.text.IDocument; +import org.eclipse.jface.text.TextAttribute; +import org.eclipse.jface.text.presentation.PresentationReconciler; +import org.eclipse.jface.text.rules.DefaultDamagerRepairer; +import org.eclipse.jface.text.rules.IRule; +import org.eclipse.jface.text.rules.RuleBasedScanner; +import org.eclipse.jface.text.rules.SingleLineRule; +import org.eclipse.jface.text.rules.Token; + +public class ThePresentationReconciler extends PresentationReconciler { + + public ThePresentationReconciler() { + RuleBasedScanner scanner= new RuleBasedScanner(); + IRule[] rules = new IRule[1]; + rules[0]= new SingleLineRule("'", "'", new Token(new TextAttribute(new Color(Display.getCurrent(), new RGB(255, 0, 0))))); //$NON-NLS-1$ //$NON-NLS-2$ + scanner.setRules(rules); + DefaultDamagerRepairer dr= new DefaultDamagerRepairer(scanner); + this.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE); + this.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE); + } + +} |