Search in sources :

Example 11 with TextSelection

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

the class CParserTest method canParseCurrentlySelectedVariableIdentifierInCppFile.

/**
 * Given an IEditorPart we should be able to retrieve the currently selected
 * variable identifier inside a C++ file.
 *
 * @throws Exception
 */
@Test
public void canParseCurrentlySelectedVariableIdentifierInCppFile() throws Exception {
    // make test project a C++ project
    project.addCCNature();
    final String expectedIdentifier = "myIdentifier";
    final String className = "shape";
    final String cppSourceCode = "class " + className + " {\n" + "int x;\n" + "int y;\n" + "private:\n" + "int color;\n" + "float " + expectedIdentifier + ";\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(expectedIdentifier);
    assertTrue(selectionStart >= 0);
    // shouldn't need to mark whole length of identifier.
    int selectionLength = expectedIdentifier.length() - 3;
    AbstractTextEditor cppEditor = (AbstractTextEditor) cppSourceEditorPart;
    cppEditor.getSelectionProvider().setSelection(new TextSelection(selectionStart, selectionLength));
    final String actualIdentifier = cParser.parseCurrentFunction(cppSourceEditorPart);
    assertEquals(className + "." + expectedIdentifier, actualIdentifier);
}
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 12 with TextSelection

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

the class CParserTest method canParseCurrentMethodNameInCppFile.

/**
 * Given an IEditorPart and current selection is inside a method,
 * CParser should be able to figure that out.
 *
 * @throws Exception
 */
@Test
public void canParseCurrentMethodNameInCppFile() throws Exception {
    // make test project a C++ project
    project.addCCNature();
    final String expectedMethodName = "circle::area";
    final String cppSourceCode = "#include \"circle.h\"\n" + "#include <math.h>\n" + "float " + expectedMethodName + "() {\n" + "// " + OFFSET_MARKER + "\n" + "return this->radius * this-> radius * M_PI\n" + "}\n";
    assertNull(project.getTestProject().findMember(new Path("/src/circle.cpp")));
    // Add shape.h to project
    InputStream newFileInputStream = new ByteArrayInputStream(cppSourceCode.getBytes());
    IFile cppSourceFile = project.addFileToProject("/src", "circle.cpp", newFileInputStream);
    assertNotNull(project.getTestProject().findMember(new Path("/src/circle.cpp")));
    // 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 actualMethodName = cParser.parseCurrentFunction(cppSourceEditorPart);
    assertEquals(expectedMethodName, actualMethodName);
}
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 13 with TextSelection

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

the class JavaParserTest method canIdentifyMethodWithinNestedClass.

/**
 * Given an IEditorPart and current selection is inside a method within a nested
 * class, JavaParser should return a "nestedClass.methodName" construct for the
 * current function.
 *
 * @throws Exception
 */
@Test
public void canIdentifyMethodWithinNestedClass() throws Exception {
    final String nestedClassName = "Encapsulated";
    final String currentMethodName = "getString";
    final String javaSourceCode = "public class JavaParserExampleClass {\n" + "private String someStrVariable = null;\n" + "static {\n" + "// empty \n" + "}\n" + "private void someMethod(String param) {\n" + "// empty \n" + "}\n" + "private class " + nestedClassName + "{\n" + "public String " + currentMethodName + "() throws Exception {\n" + "// " + OFFSET_MARKER + "\n" + "return \"returnString\";\n" + "}\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 snippet we want
    int selectionStart = javaSourceCode.indexOf(OFFSET_MARKER);
    assertTrue(selectionStart >= 0);
    int selectionLength = OFFSET_MARKER.length();
    AbstractDecoratedTextEditor javaEditor = (AbstractDecoratedTextEditor) javaSourceEditorPart;
    javaEditor.getSelectionProvider().setSelection(new TextSelection(selectionStart, selectionLength));
    final String expectedFunctionName = nestedClassName + "." + currentMethodName;
    final String actualFunctionName = javaParser.parseCurrentFunction(javaSourceEditorPart);
    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) AbstractDecoratedTextEditor(org.eclipse.ui.texteditor.AbstractDecoratedTextEditor) Test(org.junit.Test)

Example 14 with TextSelection

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

the class JavaParserTest method canParseSelectedField.

/**
 * Given an IEditorPart we should be able to retrieve the currently selected
 * field.
 *
 * @throws Exception
 */
@Test
public void canParseSelectedField() throws Exception {
    final String expectedFieldName = "testVar";
    final String javaSourceCode = "public class JavaParserExampleClass {\n" + "private String " + expectedFieldName + " = null;\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 snippet we want
    int selectionStart = javaSourceCode.indexOf(expectedFieldName);
    assertTrue(selectionStart >= 0);
    // Shouldn't need to select the entire field
    int selectionLength = expectedFieldName.length() - 3;
    AbstractDecoratedTextEditor javaEditor = (AbstractDecoratedTextEditor) javaSourceEditorPart;
    javaEditor.getSelectionProvider().setSelection(new TextSelection(selectionStart, selectionLength));
    final String actualFieldName = javaParser.parseCurrentFunction(javaSourceEditorPart);
    assertEquals(expectedFieldName, actualFieldName);
}
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 15 with TextSelection

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

the class JavaParserTest method canParseCurrentMethod.

/**
 * Given an IEditorPart we should be able to retrieve the current function
 * we are in.
 *
 * @throws Exception
 */
@Test
public void canParseCurrentMethod() throws Exception {
    final String expectedMethodName = "doSomething";
    final String javaSourceCode = "public class JavaParserExampleClass {\n" + "private void " + expectedMethodName + "(String param) {\n" + "// " + OFFSET_MARKER + "\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 snippet we want
    int selectionStart = javaSourceCode.indexOf(OFFSET_MARKER);
    assertTrue(selectionStart >= 0);
    int selectionLength = OFFSET_MARKER.length();
    AbstractDecoratedTextEditor javaEditor = (AbstractDecoratedTextEditor) javaSourceEditorPart;
    javaEditor.getSelectionProvider().setSelection(new TextSelection(selectionStart, selectionLength));
    final String actualMethodName = javaParser.parseCurrentFunction(javaSourceEditorPart);
    assertEquals(expectedMethodName, actualMethodName);
}
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)

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