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());
}
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);
}
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);
}
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);
}
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);
}
Aggregations