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