Search in sources :

Example 26 with TextSelection

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

the class CParserTest method canDetermineThatInNoFunctionInCFile.

/**
 * Given an IEditorPart and not being inside any function within a C
 * source file, no function should be determined.
 *
 * @throws Exception
 */
@Test
public void canDetermineThatInNoFunctionInCFile() throws Exception {
    // make test project a C project
    project.addCNature();
    final String cSourceCode = "// Prototype " + OFFSET_MARKER + "\n" + "static int doSomething(char *test);\n\n" + "static int doSomething(char *test)\n" + "{\n" + "int index = 0;\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("", /* expect empty function name */
    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 27 with TextSelection

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

the class JavaParserTest method canDetermineThatSelectionIsJustInClass.

/**
 * Given an IEditorPart and current selection is inside a class but not within a
 * method, not selecting a field and not in a nested class (somewhere else in the
 * class) it should return an empty string for the function.
 *
 * @throws Exception
 */
@Test
public void canDetermineThatSelectionIsJustInClass() throws Exception {
    // Apparently comments don't show up in the compilation units. Makes
    // sense, right? But we can't use the OFFSET_MARKER trick in this case.
    final String javaSourceCode = "public class JavaParserExampleClass {\n" + "private String someStrVariable = null;\n" + // want to select this line indexOf(';') + 2
    "\n" + "\n" + "private void someMethod(String param) {\n" + "// empty \n" + "}\n" + "}\n";
    assertNull(project.getTestProject().findMember(new Path("/src/org/eclipse/changelog/tests/JavaParserExampleClass.java")));
    // Add JavaParserExampleClass.java to project
    InputStream newFileInputStream = new ByteArrayInputStream(javaSourceCode.getBytes());
    IFile javaSourceFile = project.addFileToProject("/src/org/eclipse/changelog/tests", "JavaParserExampleClass.java", newFileInputStream);
    assertNotNull(project.getTestProject().findMember(new Path("/src/org/eclipse/changelog/tests/JavaParserExampleClass.java")));
    // Open a source file and get the IEditorPart
    javaSourceEditorPart = openEditor(javaSourceFile);
    // make sure changelog editor content is right before merging
    assertEquals(javaSourceCode, getContent(javaSourceEditorPart));
    // make sure we have the proper editor type
    assertTrue(javaSourceEditorPart instanceof AbstractDecoratedTextEditor);
    // Select the right point
    int selectionStart = javaSourceCode.indexOf(';') + 2;
    assertTrue(selectionStart >= 2);
    int selectionLength = 0;
    AbstractDecoratedTextEditor javaEditor = (AbstractDecoratedTextEditor) javaSourceEditorPart;
    javaEditor.getSelectionProvider().setSelection(new TextSelection(selectionStart, selectionLength));
    final String actualFunctionName = javaParser.parseCurrentFunction(javaSourceEditorPart);
    assertEquals("", /* expect empty string */
    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) AbstractDecoratedTextEditor(org.eclipse.ui.texteditor.AbstractDecoratedTextEditor) Test(org.junit.Test)

Example 28 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, "testDoubleClickLine");
    doDoubleClick();
    IEditorPart editor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
    assertTrue("editor should be ITextEditor", editor instanceof ITextEditor);
    ITextEditor textEditor = (ITextEditor) editor;
    ISelection selection = textEditor.getSelectionProvider().getSelection();
    assertTrue("selection must be TextSelection", selection instanceof TextSelection);
    TextSelection textSelection = (TextSelection) selection;
    // zero-indexed
    int line = textSelection.getStartLine() + 1;
    assertEquals(frame.getLine(), line);
}
Also used : ILaunchConfiguration(org.eclipse.debug.core.ILaunchConfiguration) ITextEditor(org.eclipse.ui.texteditor.ITextEditor) TextSelection(org.eclipse.jface.text.TextSelection) ISelection(org.eclipse.jface.viewers.ISelection) IEditorPart(org.eclipse.ui.IEditorPart) 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