Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d0b782bb1634a9f3fbdc3e4a8cf7cc3ce5df4960 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*******************************************************************************
 * Copyright (c) 2014, 2017 Red Hat, Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Red Hat - initial API and implementation
 *******************************************************************************/

package org.eclipse.linuxtools.internal.systemtap.ui.ide.handlers;

import java.io.File;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.linuxtools.internal.systemtap.ui.ide.CommentRemover;
import org.eclipse.linuxtools.internal.systemtap.ui.ide.OpenFileHandler;
import org.eclipse.linuxtools.internal.systemtap.ui.ide.editors.stp.STPEditor;
import org.eclipse.linuxtools.internal.systemtap.ui.ide.structures.nodedata.ISearchableNode;
import org.eclipse.linuxtools.systemtap.structures.TreeDefinitionNode;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.handlers.HandlerUtil;

public class DefinitionHandler extends AbstractHandler {

    @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;
    }

	private TreeDefinitionNode getSelection(ExecutionEvent event) {
		IStructuredSelection selection = HandlerUtil.getCurrentStructuredSelection(event);
		Object[] selections = selection.toArray();
		return (selections.length == 1 && selections[0] instanceof TreeDefinitionNode)
				? (TreeDefinitionNode) selections[0]
				: null;
	}

    private int findDefinitionLine(ISearchableNode data, STPEditor editor) {
        int locationIndex;
        String contents = CommentRemover.exec(editor.getDocumentProvider().getDocument(editor.getEditorInput()).get());
        if (data.isRegexSearch()) {
            Pattern pattern = Pattern.compile(data.getSearchToken());
            Matcher matcher = pattern.matcher(contents);
            locationIndex = matcher.find() ? matcher.start() : -1;
        } else {
            locationIndex = contents.indexOf(data.getSearchToken());
        }
        if (locationIndex != -1) {
            // Get the line of the match by counting newlines.
            contents = contents.substring(0, locationIndex);
            return contents.length() - contents.replaceAll("\n", "").length(); //$NON-NLS-1$ //$NON-NLS-2$
        } else {
            return 0;
        }
    }

}

Back to the top