1 /** |
|
2 * Copyright (c) 2010 Symbian Foundation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of the License "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Symbian Foundation - initial contribution. |
|
11 * Contributors: |
|
12 * Description: |
|
13 * Overview: |
|
14 * Details: |
|
15 * Platforms/Drives/Compatibility: |
|
16 * Assumptions/Requirement/Pre-requisites: |
|
17 * Failures and causes: |
|
18 */ |
|
19 package org.symbian.tools.wrttools.debug.ui.actions; |
|
20 |
|
21 import org.eclipse.debug.core.DebugPlugin; |
|
22 import org.eclipse.debug.core.IExpressionManager; |
|
23 import org.eclipse.debug.core.model.IWatchExpression; |
|
24 import org.eclipse.debug.ui.IDebugUIConstants; |
|
25 import org.eclipse.jface.action.IAction; |
|
26 import org.eclipse.jface.text.ITextSelection; |
|
27 import org.eclipse.jface.viewers.ISelection; |
|
28 import org.eclipse.ui.IEditorActionDelegate; |
|
29 import org.eclipse.ui.IEditorInput; |
|
30 import org.eclipse.ui.IEditorPart; |
|
31 import org.eclipse.ui.PartInitException; |
|
32 import org.eclipse.ui.actions.ActionDelegate; |
|
33 import org.eclipse.wst.jsdt.core.IJavaScriptUnit; |
|
34 import org.eclipse.wst.jsdt.core.dom.AST; |
|
35 import org.eclipse.wst.jsdt.core.dom.ASTNode; |
|
36 import org.eclipse.wst.jsdt.core.dom.ASTParser; |
|
37 import org.eclipse.wst.jsdt.core.dom.ASTVisitor; |
|
38 import org.eclipse.wst.jsdt.core.dom.SimpleName; |
|
39 import org.eclipse.wst.jsdt.internal.ui.JavaScriptPlugin; |
|
40 import org.eclipse.wst.jsdt.internal.ui.javaeditor.WorkingCopyManager; |
|
41 import org.symbian.tools.wrttools.debug.internal.Activator; |
|
42 |
|
43 @SuppressWarnings("restriction") |
|
44 public class WatchExpression extends ActionDelegate implements IEditorActionDelegate { |
|
45 private final class FindNode extends ASTVisitor { |
|
46 private final int offset; |
|
47 private final int length; |
|
48 |
|
49 private ASTNode node; |
|
50 |
|
51 private FindNode(int offset, int length) { |
|
52 super(false); |
|
53 this.offset = offset; |
|
54 this.length = length; |
|
55 } |
|
56 |
|
57 @Override |
|
58 public boolean visit(SimpleName node) { |
|
59 checkNode(node); |
|
60 return true; |
|
61 } |
|
62 |
|
63 public void checkNode(ASTNode node) { |
|
64 final int end = node.getStartPosition() + node.getLength(); |
|
65 if (node.getStartPosition() <= offset && end >= offset + length) { |
|
66 this.node = node; |
|
67 } |
|
68 } |
|
69 |
|
70 @Override |
|
71 public void postVisit(ASTNode node) { |
|
72 if (this.node == null) { |
|
73 checkNode(node); |
|
74 } |
|
75 } |
|
76 |
|
77 public ASTNode getNode() { |
|
78 return node; |
|
79 } |
|
80 } |
|
81 |
|
82 private IEditorPart targetEditor; |
|
83 |
|
84 public void setActiveEditor(IAction action, IEditorPart targetEditor) { |
|
85 this.targetEditor = targetEditor; |
|
86 } |
|
87 |
|
88 @Override |
|
89 public void run(IAction action) { |
|
90 ISelection selection = targetEditor.getEditorSite().getSelectionProvider().getSelection(); |
|
91 if (selection instanceof ITextSelection) { |
|
92 final ITextSelection textSelection = (ITextSelection) selection; |
|
93 final WorkingCopyManager manager = JavaScriptPlugin.getDefault().getWorkingCopyManager(); |
|
94 final IEditorInput editorInput = targetEditor.getEditorInput(); |
|
95 final IJavaScriptUnit workingCopy = manager.getWorkingCopy(editorInput); |
|
96 final ASTParser parser = ASTParser.newParser(AST.JLS3); |
|
97 |
|
98 parser.setSource(workingCopy); |
|
99 final ASTNode ast = parser.createAST(null); |
|
100 final FindNode visitor = new FindNode(textSelection.getOffset(), textSelection.getLength()); |
|
101 ast.accept(visitor); |
|
102 |
|
103 final ASTNode node = visitor.getNode(); |
|
104 if (node != null) { |
|
105 final String jsString; |
|
106 if (node.getNodeType() == ASTNode.SIMPLE_NAME) { |
|
107 switch (node.getParent().getNodeType()) { |
|
108 case ASTNode.FIELD_ACCESS: |
|
109 case ASTNode.FUNCTION_INVOCATION: |
|
110 jsString = node.getParent().toString(); |
|
111 break; |
|
112 default: |
|
113 jsString = node.toString(); |
|
114 break; |
|
115 } |
|
116 } else { |
|
117 jsString = node.toString(); |
|
118 } |
|
119 IExpressionManager expressionManager = DebugPlugin.getDefault().getExpressionManager(); |
|
120 IWatchExpression expression = expressionManager.newWatchExpression(jsString); |
|
121 expressionManager.addExpression(expression); |
|
122 |
|
123 try { |
|
124 targetEditor.getSite().getPage().showView(IDebugUIConstants.ID_EXPRESSION_VIEW); |
|
125 } catch (PartInitException e) { |
|
126 Activator.log(e); |
|
127 } |
|
128 } |
|
129 } |
|
130 } |
|
131 |
|
132 } |
|