Search in sources :

Example 21 with AbstractTextEditor

use of org.eclipse.ui.texteditor.AbstractTextEditor in project dbeaver by dbeaver.

the class TextEditorUtils method enableHostEditorKeyBindingsSupport.

public static void enableHostEditorKeyBindingsSupport(final IWorkbenchPartSite partSite, Control control) {
    if (!(partSite.getPart() instanceof AbstractTextEditor)) {
        return;
    }
    final boolean[] activated = new boolean[] { false };
    control.addFocusListener(new FocusListener() {

        @Override
        public void focusGained(FocusEvent e) {
            if (!activated[0]) {
                enableHostEditorKeyBindings(partSite, false);
                activated[0] = true;
            }
        }

        @Override
        public void focusLost(FocusEvent e) {
            if (activated[0]) {
                enableHostEditorKeyBindings(partSite, true);
                activated[0] = false;
            }
        }
    });
    control.addDisposeListener(e -> {
        if (activated[0]) {
            if (!DBWorkbench.getPlatform().isShuttingDown()) {
                enableHostEditorKeyBindings(partSite, true);
            }
            activated[0] = false;
        }
    });
}
Also used : AbstractTextEditor(org.eclipse.ui.texteditor.AbstractTextEditor) FocusListener(org.eclipse.swt.events.FocusListener) FocusEvent(org.eclipse.swt.events.FocusEvent)

Example 22 with AbstractTextEditor

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

the class CParserTest method canParseCurrentFunctionFromCFile.

/**
 * Given an IEditorPart we should be able to retrieve the currently
 * function active C function inside a C source file.
 *
 * @throws Exception
 */
@Test
public void canParseCurrentFunctionFromCFile() throws Exception {
    // make testproject a C project
    project.addCNature();
    final String expectedFunctionName = "doSomething";
    final String cSourceCode = "static int " + expectedFunctionName + "(char *test)\n" + "{\n" + "int index = 0;\n" + "// " + OFFSET_MARKER + "\n" + "return 0;\n" + "}\n";
    assertNull(project.getTestProject().findMember(new Path("/src/some_c_file.c")));
    // Add some_c_file.c to project
    InputStream newFileInputStream = new ByteArrayInputStream(cSourceCode.getBytes());
    IFile cSourceFile = project.addFileToProject("/src", "some_c_file.c", newFileInputStream);
    assertNotNull(project.getTestProject().findMember(new Path("/src/some_c_file.c")));
    // Open a source file and get the IEditorPart
    cppSourceEditorPart = openEditor(cSourceFile);
    assertEquals(cSourceCode, getContent(cppSourceEditorPart));
    // make sure we have the proper editor type
    assertTrue(cppSourceEditorPart instanceof AbstractTextEditor);
    // Select the snippet we want
    int selectionStart = cSourceCode.indexOf(OFFSET_MARKER);
    assertTrue(selectionStart >= 0);
    int selectionLength = OFFSET_MARKER.length();
    AbstractTextEditor cEditor = (AbstractTextEditor) cppSourceEditorPart;
    cEditor.getSelectionProvider().setSelection(new TextSelection(selectionStart, selectionLength));
    final String actualFunctionName = cParser.parseCurrentFunction(cppSourceEditorPart);
    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) AbstractTextEditor(org.eclipse.ui.texteditor.AbstractTextEditor) Test(org.junit.Test)

Example 23 with AbstractTextEditor

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

the class CParserTest method canParseClassNameIfNoVariableIdentifierSelectedInCppFile.

/**
 * Given an IEditorPart and not selected any variable identifier in a class, we should
 * get the class name as selected function name only.
 *
 * @throws Exception
 */
@Test
public void canParseClassNameIfNoVariableIdentifierSelectedInCppFile() throws Exception {
    // make test project a C++ project
    project.addCCNature();
    final String className = "shape";
    final String cppSourceCode = "class " + className + " {\n" + "int x;\n" + "int y;\n" + "// " + OFFSET_MARKER + "\n" + "private:\n" + "int color;\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(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(className, 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 24 with AbstractTextEditor

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

the class CParserTest method canDetermineThatInNoFunctionInCFile.

/**
 * Given an IEditorPart and not being inside any function within a C
 * source file, no function should be determined.
 *
 * @throws Exception
 */
@Test
public void canDetermineThatInNoFunctionInCFile() throws Exception {
    // make test project a C project
    project.addCNature();
    final String cSourceCode = "// Prototype " + OFFSET_MARKER + "\n" + "static int doSomething(char *test);\n\n" + "static int doSomething(char *test)\n" + "{\n" + "int index = 0;\n" + "return 0;\n" + "}\n";
    assertNull(project.getTestProject().findMember(new Path("/src/some_c_file.c")));
    // Add some_c_file.c to project
    InputStream newFileInputStream = new ByteArrayInputStream(cSourceCode.getBytes());
    IFile cSourceFile = project.addFileToProject("/src", "some_c_file.c", newFileInputStream);
    assertNotNull(project.getTestProject().findMember(new Path("/src/some_c_file.c")));
    // Open a source file and get the IEditorPart
    cppSourceEditorPart = openEditor(cSourceFile);
    assertEquals(cSourceCode, getContent(cppSourceEditorPart));
    // make sure we have the proper editor type
    assertTrue(cppSourceEditorPart instanceof AbstractTextEditor);
    // Select the snippet we want
    int selectionStart = cSourceCode.indexOf(OFFSET_MARKER);
    assertTrue(selectionStart >= 0);
    int selectionLength = OFFSET_MARKER.length();
    AbstractTextEditor cEditor = (AbstractTextEditor) cppSourceEditorPart;
    cEditor.getSelectionProvider().setSelection(new TextSelection(selectionStart, selectionLength));
    final String actualFunctionName = cParser.parseCurrentFunction(cppSourceEditorPart);
    assertEquals("", /* expect empty function name */
    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) AbstractTextEditor(org.eclipse.ui.texteditor.AbstractTextEditor) Test(org.junit.Test)

Example 25 with AbstractTextEditor

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

the class JavaParserTest method tearDown.

@After
public void tearDown() throws Exception {
    // content to the empty string).
    if (javaSourceEditorPart != null) {
        AbstractTextEditor castEditor = (AbstractTextEditor) javaSourceEditorPart;
        IDocumentProvider iDocProvider = castEditor.getDocumentProvider();
        IDocument changelogContentDoc = iDocProvider.getDocument(castEditor.getEditorInput());
        changelogContentDoc.set("");
        javaSourceEditorPart.doSave(null);
        // Also close open editor in order for default content to work.
        // I.e. avoid spill over from previous test runs
        closeEditor(javaSourceEditorPart);
    }
    // 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)

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