Search in sources :

Example 1 with AbstractDecoratedTextEditor

use of org.eclipse.ui.texteditor.AbstractDecoratedTextEditor in project linuxtools by eclipse.

the class JavaParser method parseCurrentFunction.

@Override
public String parseCurrentFunction(IEditorPart editor) throws CoreException {
    // Check for correct editor type
    if (!(editor instanceof AbstractDecoratedTextEditor)) {
        return "";
    }
    // Get the editor, test selection and input.
    AbstractDecoratedTextEditor java_editor = (AbstractDecoratedTextEditor) editor;
    ITextSelection selection = (ITextSelection) (java_editor.getSelectionProvider().getSelection());
    IEditorInput input = java_editor.getEditorInput();
    // Parse it and return the function.
    return parseCurrentFunction(input, selection.getOffset());
}
Also used : ITextSelection(org.eclipse.jface.text.ITextSelection) IEditorInput(org.eclipse.ui.IEditorInput) AbstractDecoratedTextEditor(org.eclipse.ui.texteditor.AbstractDecoratedTextEditor)

Example 2 with AbstractDecoratedTextEditor

use of org.eclipse.ui.texteditor.AbstractDecoratedTextEditor 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 3 with AbstractDecoratedTextEditor

use of org.eclipse.ui.texteditor.AbstractDecoratedTextEditor 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 4 with AbstractDecoratedTextEditor

use of org.eclipse.ui.texteditor.AbstractDecoratedTextEditor 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)

Example 5 with AbstractDecoratedTextEditor

use of org.eclipse.ui.texteditor.AbstractDecoratedTextEditor in project linuxtools by eclipse.

the class JavaParserTest method canIdentifyStaticInitializerWhenInStaticClassInitializer.

/**
 * Given an IEditorPart and current selection is in a static class initializer
 * block, JavaParser should be able to figure out that we were in an static
 * initializer block.
 *
 * @throws Exception
 */
@Test
public void canIdentifyStaticInitializerWhenInStaticClassInitializer() throws Exception {
    final String javaSourceCode = "public class JavaParserExampleClass {\n" + "private String someStrVariable = null;\n" + // create static class initializer block
    "static {\n" + "// " + OFFSET_MARKER + "\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 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 actualFunctionName = javaParser.parseCurrentFunction(javaSourceEditorPart);
    assertEquals("static initializer", 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)

Aggregations

AbstractDecoratedTextEditor (org.eclipse.ui.texteditor.AbstractDecoratedTextEditor)11 IFile (org.eclipse.core.resources.IFile)10 Path (org.eclipse.core.runtime.Path)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 InputStream (java.io.InputStream)7 TextSelection (org.eclipse.jface.text.TextSelection)7 Test (org.junit.Test)7 CoreException (org.eclipse.core.runtime.CoreException)3 IEditorInput (org.eclipse.ui.IEditorInput)2 IEditorPart (org.eclipse.ui.IEditorPart)2 IDocumentProvider (org.eclipse.ui.texteditor.IDocumentProvider)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 IMarker (org.eclipse.core.resources.IMarker)1 IPath (org.eclipse.core.runtime.IPath)1 FileRevisionEditorInput (org.eclipse.egit.ui.internal.revision.FileRevisionEditorInput)1 BadLocationException (org.eclipse.jface.text.BadLocationException)1 IDocument (org.eclipse.jface.text.IDocument)1 ITextSelection (org.eclipse.jface.text.ITextSelection)1 IRevisionRulerColumn (org.eclipse.jface.text.revisions.IRevisionRulerColumn)1