Package: GotoDefinitionHandler

GotoDefinitionHandler

nameinstructionbranchcomplexitylinemethod
GotoDefinitionHandler()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
execute(ExecutionEvent)
M: 222 C: 0
0%
M: 38 C: 0
0%
M: 20 C: 0
0%
M: 55 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * #%~
3: * org.overture.ide.ui
4: * %%
5: * Copyright (C) 2008 - 2014 Overture
6: * %%
7: * This program is free software: you can redistribute it and/or modify
8: * it under the terms of the GNU General Public License as
9: * published by the Free Software Foundation, either version 3 of the
10: * License, or (at your option) any later version.
11: *
12: * This program is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: * GNU General Public License for more details.
16: *
17: * You should have received a copy of the GNU General Public
18: * License along with this program. If not, see
19: * <http://www.gnu.org/licenses/gpl-3.0.html>.
20: * #~%
21: */
22: package org.overture.ide.ui.handlers;
23:
24: import org.eclipse.core.commands.AbstractHandler;
25: import org.eclipse.core.commands.ExecutionEvent;
26: import org.eclipse.core.commands.ExecutionException;
27: import org.eclipse.core.resources.IProject;
28: import org.eclipse.core.runtime.CoreException;
29: import org.eclipse.jface.text.TextSelection;
30: import org.eclipse.jface.viewers.ISelection;
31: import org.eclipse.ui.IEditorPart;
32: import org.eclipse.ui.PartInitException;
33: import org.eclipse.ui.PlatformUI;
34: import org.eclipse.ui.handlers.HandlerUtil;
35: import org.eclipse.ui.ide.IDE;
36: import org.overture.ast.definitions.PDefinition;
37: import org.overture.ast.expressions.AVariableExp;
38: import org.overture.ast.expressions.PExp;
39: import org.overture.ast.intf.lex.ILexLocation;
40: import org.overture.ast.modules.AModuleModules;
41: import org.overture.ast.node.INode;
42: import org.overture.ast.statements.PStm;
43: import org.overture.ast.types.ANamedInvariantType;
44: import org.overture.ast.types.PType;
45: import org.overture.ide.core.IVdmElement;
46: import org.overture.ide.core.resources.IVdmProject;
47: import org.overture.ide.core.resources.IVdmSourceUnit;
48: import org.overture.ide.ui.editor.core.VdmEditor;
49: import org.overture.ide.ui.utility.VdmTypeCheckerUi;
50: import org.overture.ide.ui.utility.ast.AstLocationSearcher2;
51: import org.overture.ide.ui.utility.ast.AstLocationSearcher2.TextReference;
52:
53: public class GotoDefinitionHandler extends AbstractHandler
54: {
55:
56:         @Override
57:         public Object execute(ExecutionEvent event) throws ExecutionException
58:         {
59:                 ISelection selection = HandlerUtil.getActiveWorkbenchWindow(event).getActivePage().getSelection();
60:•                if (selection != null & selection instanceof TextSelection)
61:                 {
62:                         TextSelection tselection = (TextSelection) selection;
63:
64:                         IEditorPart activeEditor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
65:                         // IEditorInput editorInput = activeEditor.getEditorInput();
66:•                        if (activeEditor instanceof VdmEditor)
67:                         {
68:                                 VdmEditor editor = (VdmEditor) activeEditor;
69:                                 IVdmElement input = editor.getInputVdmElement();
70:•                                if (input instanceof IVdmSourceUnit)
71:                                 {
72:                                         IVdmSourceUnit source = (IVdmSourceUnit) input;
73:                                         IProject iproject = source.getFile().getProject();
74:                                         IVdmProject p = (IVdmProject) iproject.getAdapter(IVdmProject.class);
75:•                                        if (p != null)
76:                                         {
77:•                                                if (!p.getModel().isTypeChecked())
78:                                                 {
79:                                                         // We need to get rid of the untyped stuff
80:                                                         VdmTypeCheckerUi.typeCheck(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), p);
81:                                                 }
82:                                         }
83:                                         INode node = new AstLocationSearcher2().getNode(new TextReference(source.getSystemFile(), tselection.getOffset()),source.getParseList() );
84:
85:                                         PType gotoType = null;//all code related to this var is actually togo type defeinition
86:                                         ILexLocation gotoLocation = null;
87:                                         //behaviour toto definition - connecnt this block to get goto type
88:•                                        if(node instanceof AVariableExp)
89:                                         {
90:                                                 gotoLocation = ((AVariableExp) node).getVardef().getLocation();
91:                                         }
92:                                         else
93:                                                 //end goto definition
94:•                                        if (node instanceof PDefinition)
95:                                         {
96:                                                 gotoType = ((PDefinition) node).getType();
97:•                                        } else if (node instanceof PStm)
98:                                         {
99:                                                 gotoType = ((PStm) node).getType();
100:•                                        } else if (node instanceof PExp)
101:                                         {
102:                                                 gotoType = ((PExp) node).getType();
103:•                                        } else if (node instanceof PType)
104:                                         {
105:•                                                if (node instanceof ANamedInvariantType)
106:                                                 {
107:                                                         gotoLocation = ((ANamedInvariantType) node).getName().getLocation();
108:                                                 } else
109:                                                 {
110:                                                         gotoLocation = ((PType) node).getLocation();// we have no where to goto begining of the type
111:                                                 }
112:                                         } else if (node instanceof AModuleModules)
113:                                         {
114:                                                 // //do something special
115:                                         }
116:
117:•                                        if (gotoType != null)
118:                                         {
119:                                                 gotoLocation = gotoType.getLocation();
120:                                         }
121:
122:•                                        if (gotoLocation != null)
123:                                         {
124:•                                                if (!source.getFile().getName().startsWith(gotoLocation.getFile().getName()))
125:                                                 {
126:                                                         try
127:                                                         {
128:•                                                                for (IVdmSourceUnit su : p.getSpecFiles())
129:                                                                 {
130:•                                                                        if(su.getFile().getName().startsWith(gotoLocation.getFile().getName()))
131:                                                                         {
132:                                                                                 source = su;
133:                                                                                 break;
134:                                                                         }
135:                                                                 }
136:                                                                 IEditorPart newEditor = IDE.openEditor(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(), source.getFile(), true);
137:•                                                                if(newEditor instanceof VdmEditor)
138:                                                                 {
139:                                                                         editor = (VdmEditor) newEditor;
140:                                                                 }
141:                                                         } catch (PartInitException e)
142:                                                         {
143:                                                                 // TODO Auto-generated catch block
144:                                                                 e.printStackTrace();
145:                                                         } catch (CoreException e)
146:                                                         {
147:                                                                 // TODO Auto-generated catch block
148:                                                                 e.printStackTrace();
149:                                                         }
150:                                                 }
151:                                                 
152:•                                                if(editor!=null)
153:                                                 {
154:                                                         int start = gotoLocation.getStartOffset();
155:                                                         int length = gotoLocation.getEndOffset()
156:                                                                         - gotoLocation.getEndOffset();
157:                                                         editor.setFocus();
158:                                                         editor.selectAndReveal(start, length);
159:                                                 }
160:                                         }
161:                                         System.out.println("Selected:" + tselection.getText()+" Node: "+node);
162:                                 }
163:                         }
164:                 }
165:                 return null;
166:         }
167: }