Search in sources :

Example 21 with TextSelection

use of org.eclipse.jface.text.TextSelection in project eclipse.platform.text by eclipse.

the class GotoLastEditPositionAction method run.

@Override
public void run() {
    EditPosition editPosition = TextEditorPlugin.getDefault().getLastEditPosition();
    if (editPosition == null)
        return;
    final Position pos = editPosition.getPosition();
    if (pos == null || pos.isDeleted)
        return;
    IWorkbenchWindow window = getWindow();
    if (window == null)
        return;
    IWorkbenchPage page = window.getActivePage();
    IEditorPart editor;
    try {
        editor = page.openEditor(editPosition.getEditorInput(), editPosition.getEditorId());
    } catch (PartInitException ex) {
        // $NON-NLS-1$
        IStatus status = new Status(IStatus.ERROR, TextEditorPlugin.PLUGIN_ID, IStatus.OK, "Go to Last Edit Location failed", ex);
        TextEditorPlugin.getDefault().getLog().log(status);
        return;
    }
    // Optimization - could also use else branch
    if (editor instanceof ITextEditor) {
        ITextEditor textEditor = (ITextEditor) editor;
        textEditor.selectAndReveal(pos.offset, pos.length);
        return;
    }
    /*
		 * Workaround: send out a text selection
		 * XXX: Needs to be improved, see https://bugs.eclipse.org/bugs/show_bug.cgi?id=32214
		 */
    if (editor != null) {
        IEditorSite site = editor.getEditorSite();
        if (site == null)
            return;
        ISelectionProvider provider = editor.getEditorSite().getSelectionProvider();
        if (provider == null)
            return;
        provider.setSelection(new TextSelection(pos.offset, pos.length));
    }
}
Also used : Status(org.eclipse.core.runtime.Status) IStatus(org.eclipse.core.runtime.IStatus) IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) IStatus(org.eclipse.core.runtime.IStatus) ISelectionProvider(org.eclipse.jface.viewers.ISelectionProvider) EditPosition(org.eclipse.ui.internal.texteditor.EditPosition) EditPosition(org.eclipse.ui.internal.texteditor.EditPosition) Position(org.eclipse.jface.text.Position) TextSelection(org.eclipse.jface.text.TextSelection) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) IEditorPart(org.eclipse.ui.IEditorPart) PartInitException(org.eclipse.ui.PartInitException) IEditorSite(org.eclipse.ui.IEditorSite)

Example 22 with TextSelection

use of org.eclipse.jface.text.TextSelection in project linuxtools by eclipse.

the class DoubleClickTest method testDoubleClickFunction.

@Test
public void testDoubleClickFunction() throws Exception {
    ILaunchConfiguration config = createConfiguration(proj.getProject());
    // $NON-NLS-1$
    doLaunch(config, "testDoubleClickFunction");
    CachegrindViewPart view = (CachegrindViewPart) ValgrindUIPlugin.getDefault().getView().getDynamicView();
    CachegrindOutput output = view.getOutputs()[0];
    // $NON-NLS-1$
    CachegrindFile file = getFileByName(output, "cpptest.cpp");
    // $NON-NLS-1$
    CachegrindFunction func = getFunctionByName(file, "main");
    TreePath path = new TreePath(new Object[] { output, file, func });
    doDoubleClick(path);
    // check file in editor
    IEditorPart editor = checkFile(file);
    // check line number
    ITextEditor textEditor = (ITextEditor) editor;
    ISelection selection = textEditor.getSelectionProvider().getSelection();
    TextSelection textSelection = (TextSelection) selection;
    // zero-indexed
    int line = textSelection.getStartLine() + 1;
    int expectedLine = ((IFunction) func.getModel()).getSourceRange().getStartLine();
    assertEquals(expectedLine, line);
}
Also used : ILaunchConfiguration(org.eclipse.debug.core.ILaunchConfiguration) ITextEditor(org.eclipse.ui.texteditor.ITextEditor) TreePath(org.eclipse.jface.viewers.TreePath) CachegrindOutput(org.eclipse.linuxtools.internal.valgrind.cachegrind.model.CachegrindOutput) CachegrindFile(org.eclipse.linuxtools.internal.valgrind.cachegrind.model.CachegrindFile) CachegrindFunction(org.eclipse.linuxtools.internal.valgrind.cachegrind.model.CachegrindFunction) TextSelection(org.eclipse.jface.text.TextSelection) ISelection(org.eclipse.jface.viewers.ISelection) CachegrindViewPart(org.eclipse.linuxtools.internal.valgrind.cachegrind.CachegrindViewPart) IEditorPart(org.eclipse.ui.IEditorPart) Test(org.junit.Test)

Example 23 with TextSelection

use of org.eclipse.jface.text.TextSelection in project linuxtools by eclipse.

the class DoubleClickTest method testDoubleClickLine.

@Test
public void testDoubleClickLine() throws Exception {
    ILaunchConfiguration config = createConfiguration(proj.getProject());
    // $NON-NLS-1$
    doLaunch(config, "testDoubleClickFunction");
    CachegrindViewPart view = (CachegrindViewPart) ValgrindUIPlugin.getDefault().getView().getDynamicView();
    CachegrindOutput output = view.getOutputs()[0];
    // $NON-NLS-1$
    CachegrindFile file = getFileByName(output, "cpptest.cpp");
    // $NON-NLS-1$
    CachegrindFunction func = getFunctionByName(file, "main");
    CachegrindLine line = func.getLines()[0];
    TreePath path = new TreePath(new Object[] { output, file, func });
    doDoubleClick(path);
    // check file in editor
    IEditorPart editor = checkFile(file);
    // check line number
    ITextEditor textEditor = (ITextEditor) editor;
    ISelection selection = textEditor.getSelectionProvider().getSelection();
    TextSelection textSelection = (TextSelection) selection;
    // zero-indexed
    int actualLine = textSelection.getStartLine() + 1;
    assertEquals(line.getLine(), actualLine);
}
Also used : ILaunchConfiguration(org.eclipse.debug.core.ILaunchConfiguration) ITextEditor(org.eclipse.ui.texteditor.ITextEditor) TreePath(org.eclipse.jface.viewers.TreePath) CachegrindOutput(org.eclipse.linuxtools.internal.valgrind.cachegrind.model.CachegrindOutput) CachegrindFile(org.eclipse.linuxtools.internal.valgrind.cachegrind.model.CachegrindFile) CachegrindFunction(org.eclipse.linuxtools.internal.valgrind.cachegrind.model.CachegrindFunction) TextSelection(org.eclipse.jface.text.TextSelection) ISelection(org.eclipse.jface.viewers.ISelection) CachegrindViewPart(org.eclipse.linuxtools.internal.valgrind.cachegrind.CachegrindViewPart) CachegrindLine(org.eclipse.linuxtools.internal.valgrind.cachegrind.model.CachegrindLine) IEditorPart(org.eclipse.ui.IEditorPart) Test(org.junit.Test)

Example 24 with TextSelection

use of org.eclipse.jface.text.TextSelection in project linuxtools by eclipse.

the class CParserTest method canParseCurrentFunctionFromCFile.

/**
 * Given an IEditorPart we should be able to retrieve the currently
 * function active C function inside a C source file.
 *
 * @throws Exception
 */
@Test
public void canParseCurrentFunctionFromCFile() throws Exception {
    // make testproject a C project
    project.addCNature();
    final String expectedFunctionName = "doSomething";
    final String cSourceCode = "static int " + expectedFunctionName + "(char *test)\n" + "{\n" + "int index = 0;\n" + "// " + OFFSET_MARKER + "\n" + "return 0;\n" + "}\n";
    assertNull(project.getTestProject().findMember(new Path("/src/some_c_file.c")));
    // Add some_c_file.c to project
    InputStream newFileInputStream = new ByteArrayInputStream(cSourceCode.getBytes());
    IFile cSourceFile = project.addFileToProject("/src", "some_c_file.c", newFileInputStream);
    assertNotNull(project.getTestProject().findMember(new Path("/src/some_c_file.c")));
    // Open a source file and get the IEditorPart
    cppSourceEditorPart = openEditor(cSourceFile);
    assertEquals(cSourceCode, getContent(cppSourceEditorPart));
    // make sure we have the proper editor type
    assertTrue(cppSourceEditorPart instanceof AbstractTextEditor);
    // Select the snippet we want
    int selectionStart = cSourceCode.indexOf(OFFSET_MARKER);
    assertTrue(selectionStart >= 0);
    int selectionLength = OFFSET_MARKER.length();
    AbstractTextEditor cEditor = (AbstractTextEditor) cppSourceEditorPart;
    cEditor.getSelectionProvider().setSelection(new TextSelection(selectionStart, selectionLength));
    final String actualFunctionName = cParser.parseCurrentFunction(cppSourceEditorPart);
    assertEquals(expectedFunctionName, actualFunctionName);
}
Also used : Path(org.eclipse.core.runtime.Path) IFile(org.eclipse.core.resources.IFile) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) TextSelection(org.eclipse.jface.text.TextSelection) AbstractTextEditor(org.eclipse.ui.texteditor.AbstractTextEditor) Test(org.junit.Test)

Example 25 with TextSelection

use of org.eclipse.jface.text.TextSelection in project linuxtools by eclipse.

the class CParserTest method canParseClassNameIfNoVariableIdentifierSelectedInCppFile.

/**
 * Given an IEditorPart and not selected any variable identifier in a class, we should
 * get the class name as selected function name only.
 *
 * @throws Exception
 */
@Test
public void canParseClassNameIfNoVariableIdentifierSelectedInCppFile() throws Exception {
    // make test project a C++ project
    project.addCCNature();
    final String className = "shape";
    final String cppSourceCode = "class " + className + " {\n" + "int x;\n" + "int y;\n" + "// " + OFFSET_MARKER + "\n" + "private:\n" + "int color;\n" + "void set_color(int color);\n" + "}\n";
    assertNull(project.getTestProject().findMember(new Path("/src/shape.h")));
    // Add shape.h to project
    InputStream newFileInputStream = new ByteArrayInputStream(cppSourceCode.getBytes());
    IFile cppSourceFile = project.addFileToProject("/src", "shape.h", newFileInputStream);
    assertNotNull(project.getTestProject().findMember(new Path("/src/shape.h")));
    // Open a source file and get the IEditorPart
    cppSourceEditorPart = openEditor(cppSourceFile);
    // make sure editor content is correct
    assertEquals(cppSourceCode, getContent(cppSourceEditorPart));
    // make sure we have the proper editor type
    assertTrue(cppSourceEditorPart instanceof AbstractTextEditor);
    // Select the snippet we want
    int selectionStart = cppSourceCode.indexOf(OFFSET_MARKER);
    assertTrue(selectionStart >= 0);
    int selectionLength = OFFSET_MARKER.length();
    AbstractTextEditor cppEditor = (AbstractTextEditor) cppSourceEditorPart;
    cppEditor.getSelectionProvider().setSelection(new TextSelection(selectionStart, selectionLength));
    final String actualFunction = cParser.parseCurrentFunction(cppSourceEditorPart);
    assertEquals(className, actualFunction);
}
Also used : Path(org.eclipse.core.runtime.Path) IFile(org.eclipse.core.resources.IFile) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) TextSelection(org.eclipse.jface.text.TextSelection) AbstractTextEditor(org.eclipse.ui.texteditor.AbstractTextEditor) Test(org.junit.Test)

Aggregations

TextSelection (org.eclipse.jface.text.TextSelection)28 Test (org.junit.Test)18 IFile (org.eclipse.core.resources.IFile)17 ByteArrayInputStream (java.io.ByteArrayInputStream)13 InputStream (java.io.InputStream)13 Path (org.eclipse.core.runtime.Path)13 IEditorPart (org.eclipse.ui.IEditorPart)8 ISelection (org.eclipse.jface.viewers.ISelection)7 AbstractDecoratedTextEditor (org.eclipse.ui.texteditor.AbstractDecoratedTextEditor)7 AbstractTextEditor (org.eclipse.ui.texteditor.AbstractTextEditor)6 ITextEditor (org.eclipse.ui.texteditor.ITextEditor)6 ILaunchConfiguration (org.eclipse.debug.core.ILaunchConfiguration)5 XtextResource (org.eclipse.xtext.resource.XtextResource)4 XExpression (org.eclipse.xtext.xbase.XExpression)4 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)3 ITextSelection (org.eclipse.jface.text.ITextSelection)3 RefactoringStatus (org.eclipse.ltk.core.refactoring.RefactoringStatus)3 XtextEditor (org.eclipse.xtext.ui.editor.XtextEditor)3 BadLocationException (org.eclipse.jface.text.BadLocationException)2 TreePath (org.eclipse.jface.viewers.TreePath)2