Search in sources :

Example 36 with ITextEditor

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

the class ProfileUIUtils method openEditorAndSelect.

/**
 * Opens an editor on the given file and selects the line.
 * @param file The file to open.
 * @param line The line to select.
 * @throws PartInitException If opening editor failed.
 * @throws BadLocationException If line number is invalid.
 *
 * @since 2.0
 */
public static void openEditorAndSelect(IFile file, int line) throws PartInitException, BadLocationException {
    if (file.exists()) {
        IWorkbenchPage activePage = ProfileUIPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getActivePage();
        IEditorPart editor = IDE.openEditor(activePage, file);
        if (editor instanceof ITextEditor) {
            ITextEditor textEditor = (ITextEditor) editor;
            if (line > 0) {
                IDocumentProvider provider = textEditor.getDocumentProvider();
                IDocument document = provider.getDocument(textEditor.getEditorInput());
                // zero-indexed
                int start = document.getLineOffset(line - 1);
                textEditor.selectAndReveal(start, 0);
            }
        }
    }
}
Also used : ITextEditor(org.eclipse.ui.texteditor.ITextEditor) IDocumentProvider(org.eclipse.ui.texteditor.IDocumentProvider) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) IEditorPart(org.eclipse.ui.IEditorPart) IDocument(org.eclipse.jface.text.IDocument)

Example 37 with ITextEditor

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

the class AbstractMassifTest method checkLine.

protected void checkLine(MassifHeapTreeNode node) {
    IEditorPart editor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
    assertTrue(editor instanceof ITextEditor);
    ITextEditor textEditor = (ITextEditor) editor;
    ISelection selection = textEditor.getSelectionProvider().getSelection();
    assertTrue(selection instanceof TextSelection);
    TextSelection textSelection = (TextSelection) selection;
    // zero-indexed
    int line = textSelection.getStartLine() + 1;
    assertEquals(node.getLine(), line);
}
Also used : ITextEditor(org.eclipse.ui.texteditor.ITextEditor) TextSelection(org.eclipse.jface.text.TextSelection) ISelection(org.eclipse.jface.viewers.ISelection) IEditorPart(org.eclipse.ui.IEditorPart)

Example 38 with ITextEditor

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

the class DoubleClickTest method testDoubleClickLaunchRemoved.

@Test
public void testDoubleClickLaunchRemoved() throws Exception {
    ILaunchConfiguration config = createConfiguration(proj.getProject());
    // $NON-NLS-1$
    ILaunch launch = doLaunch(config, "testDoubleClickLine");
    // Remove launch - tests #284919
    DebugPlugin.getDefault().getLaunchManager().removeLaunch(launch);
    doDoubleClick();
    IEditorPart editor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
    assertTrue("editor should be ITextEditor", editor instanceof ITextEditor);
    ITextEditor textEditor = (ITextEditor) editor;
    ISelection selection = textEditor.getSelectionProvider().getSelection();
    assertTrue("selection must be TextSelection", selection instanceof TextSelection);
    TextSelection textSelection = (TextSelection) selection;
    // zero-indexed
    int line = textSelection.getStartLine() + 1;
    assertEquals(frame.getLine(), line);
}
Also used : ILaunchConfiguration(org.eclipse.debug.core.ILaunchConfiguration) ITextEditor(org.eclipse.ui.texteditor.ITextEditor) TextSelection(org.eclipse.jface.text.TextSelection) ILaunch(org.eclipse.debug.core.ILaunch) ISelection(org.eclipse.jface.viewers.ISelection) IEditorPart(org.eclipse.ui.IEditorPart) Test(org.junit.Test)

Example 39 with ITextEditor

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

the class LinkedResourceDoubleClickTest method testLinkedDoubleClickLine.

@Test
public void testLinkedDoubleClickLine() throws Exception {
    ILaunchConfiguration config = createConfiguration(proj.getProject());
    // $NON-NLS-1$
    doLaunch(config, "testLinkedDoubleClickLine");
    doDoubleClick();
    IEditorPart editor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
    assertTrue("editor must be text editor", editor instanceof ITextEditor);
    ITextEditor textEditor = (ITextEditor) editor;
    ISelection selection = textEditor.getSelectionProvider().getSelection();
    assertTrue("selection must be text one", selection instanceof TextSelection);
    TextSelection textSelection = (TextSelection) selection;
    // zero-indexed
    int line = textSelection.getStartLine() + 1;
    assertEquals(frame.getLine(), line);
}
Also used : ILaunchConfiguration(org.eclipse.debug.core.ILaunchConfiguration) ITextEditor(org.eclipse.ui.texteditor.ITextEditor) TextSelection(org.eclipse.jface.text.TextSelection) ISelection(org.eclipse.jface.viewers.ISelection) IEditorPart(org.eclipse.ui.IEditorPart) Test(org.junit.Test)

Example 40 with ITextEditor

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

the class AddStapProbeHandler method execute.

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    ITextEditor editor;
    try {
        editor = (ITextEditor) HandlerUtil.getActiveEditor(event);
    } catch (ClassCastException e) {
        ExceptionErrorDialog.openError(Messages.AddStapProbe_unableToInsertProbe, Messages.AddStapProbe_editorError, e);
        throw new ExecutionException(Messages.AddStapProbe_editorError, e);
    }
    IVerticalRulerInfo rulerInfo = editor.getAdapter(IVerticalRulerInfo.class);
    Shell shell = editor.getSite().getShell();
    shell.setCursor(shell.getDisplay().getSystemCursor(SWT.CURSOR_WAIT));
    int lineno = rulerInfo.getLineOfLastMouseButtonActivity();
    IDocument document = editor.getDocumentProvider().getDocument(editor.getEditorInput());
    String s = document.get();
    // $NON-NLS-1$
    String[] lines = s.split("\n");
    String line = lines[lineno].trim();
    boolean die = false;
    if (line.isEmpty()) {
        // eat blank lines
        die = true;
    }
    if (line.startsWith("#")) {
        // eat preprocessor directives //$NON-NLS-1$
        die = true;
    }
    if (line.startsWith("//")) {
        // eat C99 comments //$NON-NLS-1$
        die = true;
    }
    if (line.startsWith("/*") && !line.contains("*/") && !line.endsWith("*/")) {
        // try to eat single-line C comments //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
        die = true;
    }
    // gogo find comment segments
    try {
        ArrayList<Integer> commentChunks = new ArrayList<>();
        char[] chars = s.toCharArray();
        int needle = 1;
        int offset = document.getLineOffset(lineno);
        while (needle < chars.length) {
            if (chars[needle - 1] == '/' && chars[needle] == '*') {
                commentChunks.add(needle);
                while (needle < chars.length) {
                    if (chars[needle - 1] == '*' && chars[needle] == '/') {
                        commentChunks.add(needle);
                        needle++;
                        break;
                    }
                    needle++;
                }
            }
            needle++;
        }
        for (int i = 0, pair, start, end; i < commentChunks.size(); i++) {
            if (!(commentChunks.get(i).intValue() < offset)) {
                pair = i - i % 2;
                start = commentChunks.get(pair).intValue();
                end = commentChunks.get(pair + 1).intValue();
                if (offset >= start && offset <= end) {
                    die = true;
                }
            }
        }
    } catch (BadLocationException excp) {
        ExceptionErrorDialog.openError(Messages.AddStapProbe_unableToInsertProbe, excp);
        return null;
    }
    if (die) {
        MessageDialog.openError(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), Messages.CEditor_probeInsertFailed, Messages.CEditor_canNotProbeLine);
    } else {
        IEditorInput in = editor.getEditorInput();
        if (in instanceof FileStoreEditorInput) {
            FileStoreEditorInput input = (FileStoreEditorInput) in;
            IPreferenceStore p = IDEPlugin.getDefault().getPreferenceStore();
            String kernroot = p.getString(IDEPreferenceConstants.P_KERNEL_SOURCE);
            String filepath = input.getURI().getPath();
            String kernrelative = filepath.substring(kernroot.length() + 1, filepath.length());
            StringBuffer sb = new StringBuffer();
            // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
            sb.append("probe kernel.statement(\"*@" + kernrelative + ":" + (lineno + 1) + "\")");
            // $NON-NLS-1$
            sb.append("\n{\n\t\n}\n");
            STPEditor activeSTPEditor = IDESessionSettings.getOrAskForActiveSTPEditor(false);
            if (activeSTPEditor != null) {
                activeSTPEditor.insertText(sb.toString());
            }
        }
    }
    // Return the cursor to normal
    shell.setCursor(null);
    return null;
}
Also used : ITextEditor(org.eclipse.ui.texteditor.ITextEditor) ArrayList(java.util.ArrayList) STPEditor(org.eclipse.linuxtools.internal.systemtap.ui.ide.editors.stp.STPEditor) Shell(org.eclipse.swt.widgets.Shell) ExecutionException(org.eclipse.core.commands.ExecutionException) IPreferenceStore(org.eclipse.jface.preference.IPreferenceStore) IVerticalRulerInfo(org.eclipse.jface.text.source.IVerticalRulerInfo) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException) IEditorInput(org.eclipse.ui.IEditorInput) FileStoreEditorInput(org.eclipse.ui.ide.FileStoreEditorInput)

Aggregations

ITextEditor (org.eclipse.ui.texteditor.ITextEditor)233 IEditorPart (org.eclipse.ui.IEditorPart)91 IDocument (org.eclipse.jface.text.IDocument)73 IWorkbenchPage (org.eclipse.ui.IWorkbenchPage)39 ITextSelection (org.eclipse.jface.text.ITextSelection)34 IFile (org.eclipse.core.resources.IFile)33 Test (org.junit.Test)31 BadLocationException (org.eclipse.jface.text.BadLocationException)27 IDocumentProvider (org.eclipse.ui.texteditor.IDocumentProvider)25 PartInitException (org.eclipse.ui.PartInitException)23 ISelection (org.eclipse.jface.viewers.ISelection)19 IWorkbenchWindow (org.eclipse.ui.IWorkbenchWindow)17 IRegion (org.eclipse.jface.text.IRegion)16 Annotation (org.eclipse.jface.text.source.Annotation)16 ArrayList (java.util.ArrayList)15 IAnnotationModel (org.eclipse.jface.text.source.IAnnotationModel)15 IResource (org.eclipse.core.resources.IResource)14 IEditorInput (org.eclipse.ui.IEditorInput)14 CoreException (org.eclipse.core.runtime.CoreException)13 IStructuredModel (org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)12