Search in sources :

Example 6 with AbstractTextEditor

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);
}
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 7 with AbstractTextEditor

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);
}
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 8 with AbstractTextEditor

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);
}
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 9 with AbstractTextEditor

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);
}
Also used : IDocumentProvider(org.eclipse.ui.texteditor.IDocumentProvider) AbstractTextEditor(org.eclipse.ui.texteditor.AbstractTextEditor) IDocument(org.eclipse.jface.text.IDocument) After(org.junit.After)

Example 10 with AbstractTextEditor

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();
}
Also used : IDocumentProvider(org.eclipse.ui.texteditor.IDocumentProvider) AbstractTextEditor(org.eclipse.ui.texteditor.AbstractTextEditor) IDocument(org.eclipse.jface.text.IDocument)

Aggregations

AbstractTextEditor (org.eclipse.ui.texteditor.AbstractTextEditor)33 IFile (org.eclipse.core.resources.IFile)14 IEditorPart (org.eclipse.ui.IEditorPart)9 IWorkbenchPage (org.eclipse.ui.IWorkbenchPage)9 PartInitException (org.eclipse.ui.PartInitException)8 FileEditorInput (org.eclipse.ui.part.FileEditorInput)8 IDocument (org.eclipse.jface.text.IDocument)7 IEditorDescriptor (org.eclipse.ui.IEditorDescriptor)7 ByteArrayInputStream (java.io.ByteArrayInputStream)6 InputStream (java.io.InputStream)6 Path (org.eclipse.core.runtime.Path)6 TextSelection (org.eclipse.jface.text.TextSelection)6 IDocumentProvider (org.eclipse.ui.texteditor.IDocumentProvider)6 Test (org.junit.Test)6 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 Method (java.lang.reflect.Method)4 After (org.junit.After)3 FocusEvent (org.eclipse.swt.events.FocusEvent)2 FocusListener (org.eclipse.swt.events.FocusListener)2 Control (org.eclipse.swt.widgets.Control)2