Search in sources :

Example 1 with STPEditor

use of org.eclipse.linuxtools.internal.systemtap.ui.ide.editors.stp.STPEditor 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)

Example 2 with STPEditor

use of org.eclipse.linuxtools.internal.systemtap.ui.ide.editors.stp.STPEditor in project linuxtools by eclipse.

the class DefinitionHandler method execute.

@Override
public Object execute(ExecutionEvent event) {
    TreeDefinitionNode t = getSelection(event);
    if (t == null) {
        return null;
    }
    String filename = t.getDefinition();
    if (filename == null) {
        return null;
    }
    File file = new File(filename);
    OpenFileHandler open = new OpenFileHandler();
    open.executeOnFile(file);
    if (open.isSuccessful() && t.getData() instanceof ISearchableNode) {
        IEditorPart editorPart = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
        STPEditor editor = (STPEditor) editorPart;
        editor.jumpToLocation(findDefinitionLine((ISearchableNode) t.getData(), editor) + 1, 0);
    }
    return null;
}
Also used : OpenFileHandler(org.eclipse.linuxtools.internal.systemtap.ui.ide.OpenFileHandler) TreeDefinitionNode(org.eclipse.linuxtools.systemtap.structures.TreeDefinitionNode) ISearchableNode(org.eclipse.linuxtools.internal.systemtap.ui.ide.structures.nodedata.ISearchableNode) STPEditor(org.eclipse.linuxtools.internal.systemtap.ui.ide.editors.stp.STPEditor) IEditorPart(org.eclipse.ui.IEditorPart) File(java.io.File)

Example 3 with STPEditor

use of org.eclipse.linuxtools.internal.systemtap.ui.ide.editors.stp.STPEditor in project linuxtools by eclipse.

the class IDESessionSettings method getOrAskForActiveSTPEditor.

/**
 * Restore and return an STPEditor based on the user's choice.
 * @param checkCurrent Set to <code>true</code> if the currently active editor may be returned
 * if it is an {@link STPEditor}.
 * @return
 */
public static STPEditor getOrAskForActiveSTPEditor(boolean checkCurrent) {
    STPEditor stpEditor;
    IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
    IWorkbenchPage page = window.getActivePage();
    if (checkCurrent) {
        IEditorPart editor = page.getActiveEditor();
        stpEditor = editor instanceof STPEditor ? (STPEditor) editor : askForSTPEditor(window);
    } else {
        stpEditor = askForSTPEditor(window);
    }
    if (stpEditor != null) {
        page.activate(stpEditor);
    }
    return stpEditor;
}
Also used : IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) STPEditor(org.eclipse.linuxtools.internal.systemtap.ui.ide.editors.stp.STPEditor) IEditorPart(org.eclipse.ui.IEditorPart)

Example 4 with STPEditor

use of org.eclipse.linuxtools.internal.systemtap.ui.ide.editors.stp.STPEditor in project linuxtools by eclipse.

the class IndentHandler method selectAndReveal.

/**
 * Selects the given range on the editor.
 *
 * @param newOffset
 *            the selection offset
 * @param newLength
 *            the selection range
 */
private void selectAndReveal(ITextEditor editor, int newOffset, int newLength) {
    Assert.isTrue(newOffset >= 0);
    Assert.isTrue(newLength >= 0);
    if (editor instanceof STPEditor) {
        ISourceViewer viewer = ((STPEditor) editor).getMySourceViewer();
        if (viewer != null) {
            viewer.setSelectedRange(newOffset, newLength);
        }
    } else {
        // this is too intrusive, but will never get called anyway
        editor.selectAndReveal(newOffset, newLength);
    }
}
Also used : STPEditor(org.eclipse.linuxtools.internal.systemtap.ui.ide.editors.stp.STPEditor) ISourceViewer(org.eclipse.jface.text.source.ISourceViewer)

Aggregations

STPEditor (org.eclipse.linuxtools.internal.systemtap.ui.ide.editors.stp.STPEditor)4 IEditorPart (org.eclipse.ui.IEditorPart)2 File (java.io.File)1 ArrayList (java.util.ArrayList)1 ExecutionException (org.eclipse.core.commands.ExecutionException)1 IPreferenceStore (org.eclipse.jface.preference.IPreferenceStore)1 BadLocationException (org.eclipse.jface.text.BadLocationException)1 IDocument (org.eclipse.jface.text.IDocument)1 ISourceViewer (org.eclipse.jface.text.source.ISourceViewer)1 IVerticalRulerInfo (org.eclipse.jface.text.source.IVerticalRulerInfo)1 OpenFileHandler (org.eclipse.linuxtools.internal.systemtap.ui.ide.OpenFileHandler)1 ISearchableNode (org.eclipse.linuxtools.internal.systemtap.ui.ide.structures.nodedata.ISearchableNode)1 TreeDefinitionNode (org.eclipse.linuxtools.systemtap.structures.TreeDefinitionNode)1 Shell (org.eclipse.swt.widgets.Shell)1 IEditorInput (org.eclipse.ui.IEditorInput)1 IWorkbenchPage (org.eclipse.ui.IWorkbenchPage)1 IWorkbenchWindow (org.eclipse.ui.IWorkbenchWindow)1 FileStoreEditorInput (org.eclipse.ui.ide.FileStoreEditorInput)1 ITextEditor (org.eclipse.ui.texteditor.ITextEditor)1