use of org.eclipse.ui.texteditor.AbstractTextEditor in project linuxtools by eclipse.
the class CParserTest method canParseCurrentFunctionInCppFile.
/**
* Given an IEditorPart and current selection is inside a method,
* CParser should be able to figure that out.
*
* @throws Exception
*/
@Test
public void canParseCurrentFunctionInCppFile() throws Exception {
// make test project a C++ project
project.addCCNature();
final String expectedFunction = "main";
final String cppSourceCode = "#include \"circle.h\"\n" + "#include <math.h>\n" + "float circle::area() {\n" + "return this->radius * this-> radius * M_PI\n" + "}\n" + "int " + expectedFunction + "() {\n" + "int some_var = 0;\n" + "// " + OFFSET_MARKER + "\n" + "return 0;\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 actualFunction = cParser.parseCurrentFunction(cppSourceEditorPart);
assertEquals(expectedFunction, actualFunction);
}
use of org.eclipse.ui.texteditor.AbstractTextEditor 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.ui.texteditor.AbstractTextEditor 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.ui.texteditor.AbstractTextEditor in project linuxtools by eclipse.
the class CParserTest method tearDown.
@After
public void tearDown() throws Exception {
// content to the empty string).
if (cppSourceEditorPart != null) {
AbstractTextEditor castEditor = (AbstractTextEditor) cppSourceEditorPart;
IDocumentProvider iDocProvider = castEditor.getDocumentProvider();
IDocument changelogContentDoc = iDocProvider.getDocument(castEditor.getEditorInput());
changelogContentDoc.set("");
cppSourceEditorPart.doSave(null);
// Also close open editor in order for default content to work.
// I.e. avoid spill over from previous test runs
closeEditor(cppSourceEditorPart);
}
// dispose
project.getTestProject().delete(true, true, null);
}
use of org.eclipse.ui.texteditor.AbstractTextEditor in project linuxtools by eclipse.
the class EditorHelper method getContent.
/**
* Return the content of the given IEditorPart as String.
* @param editorPart
* @return Content of editorPart.
*/
public static String getContent(IEditorPart editorPart) {
AbstractTextEditor castEditor = (AbstractTextEditor) editorPart;
IDocumentProvider provider = castEditor.getDocumentProvider();
IDocument document = provider.getDocument(castEditor.getEditorInput());
return document.get();
}
Aggregations