Commit dd0c10e1 authored by Johannes Roith's avatar Johannes Roith

Open Definition mostly works now.

parent 29371b34
......@@ -27,7 +27,7 @@ public class Range {
if (pos == null || begin == null || end == null)
return false;
if (pos.getLine() >= begin.getLine() && pos.getLine() <= end.getLine()) {
if (pos.getLine() == begin.getLine()
&& pos.getColumn() < begin.getColumn())
return false;
......
......@@ -26,14 +26,16 @@ public class FindAtPositionVisitor extends Visitor<Object>
Range range = node.getRange();
if (range == null)
return;
if (range.contains(pos))
if (range.contains(pos)) {
result = node;
}
}
@Override
public void postVisit (Name node, Object data) {
if (node == null)
return;
System.out.println(node);
updateIfMatch(node);
}
......
package de.in.tum.www2.cup.analysis;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import de.in.tum.www2.cup.ast.AbstractNode;
import de.in.tum.www2.cup.ast.Production;
......@@ -14,9 +16,10 @@ import de.in.tum.www2.cup.ast.TypedTerminalDeclaration;
public class RefResolutionVisitor extends Visitor<Object>
{
HashMap<String, AbstractNode> declarations = new HashMap<String, AbstractNode> ();
HashMap<String, AbstractNode> definitions = new HashMap<String, AbstractNode> ();
private HashMap<String, AbstractNode> declarations = new HashMap<String, AbstractNode> ();
private HashMap<String, AbstractNode> definitions = new HashMap<String, AbstractNode> ();
private HashMap<String, List<ProductionRef>> forwardRefs = new HashMap<String, List<ProductionRef>> ();
// should resolve:
// 1. declarations
// 2. matching precedences
......@@ -49,7 +52,14 @@ public class RefResolutionVisitor extends Visitor<Object>
}
@Override public Object preVisit (Production node, Object data) {
definitions.put(node.getName().name, node);
String name = node.getName().name;
definitions.put(name, node);
// update any forward references.
if (forwardRefs.containsKey(name)) {
for (ProductionRef pr : forwardRefs.get(name))
pr.setDefinition(node);
}
return data;
}
......@@ -59,6 +69,11 @@ public class RefResolutionVisitor extends Visitor<Object>
AbstractNode def = definitions.get(name);
if (def instanceof Production)
node.setDefinition((Production) def);
} else {
// the production might still be defined, below, accumulate.
if (!forwardRefs.containsKey(name))
forwardRefs.put(name, new ArrayList<ProductionRef>());
forwardRefs.get(name).add(node);
}
return data;
}
......
......@@ -10,7 +10,7 @@ public class NonTerminal extends AbstractNode {
public String getName() {
return name;
}
public NonTerminal(String name, Range range) {
super(range);
this.name = name;
......
......@@ -49,46 +49,41 @@
<command
defaultHandler="de.tum.in.www2.cupplugin.commands.OpenDefinitionHandler"
id="de.tum.in.www2.cupplugin.commands.OpenDefinition"
name="Cup Open Definition">
name="Open Definition">
</command>
<command
defaultHandler="de.tum.in.www2.cupplugin.commands.OpenDeclarationHandler"
id="de.tum.in.www2.cupplugin.commands.OpenDeclaration"
name="Cup Open Declaration">
name="Open Declaration">
</command>
<command
defaultHandler="de.tum.in.www2.cupplugin.commands.RenameSymbolHandler"
id="de.tum.in.www2.cupplugin.commands.RenameSymbol"
name="Cup Rename Symbol">
name="Rename Symbol ...">
</command>
</extension>
<extension
point="org.eclipse.ui.menus">
<menuContribution
allPopups="true"
locationURI="popup:org.eclipse.ui.popup.any">
locationURI="popup:org.eclipse.ui.popup.any?before=additions">
<command
commandId="de.tum.in.www2.cupplugin.commands.OpenDefinition"
style="push">
<visibleWhen
checkEnabled="false">
<iterate
ifEmpty="false"
operator="or">
<and>
<with
variable="selection">
<instanceof
value="org.eclipse.jface.text.TextSelection">
</instanceof>
</with>
</and>
</iterate>
</visibleWhen>
</command>
<command
commandId="de.tum.in.www2.cupplugin.commands.OpenDeclaration"
style="push">
</command>
</menuContribution>
<menuContribution
allPopups="true"
locationURI="popup:org.eclipse.ui.popup.any?before=additions">
<command
commandId="de.tum.in.www2.cupplugin.commands.RenameSymbol"
style="push">
<!--
<visibleWhen
checkEnabled="false">
<iterate
......@@ -103,28 +98,13 @@
</with>
</and>
</iterate>
</visibleWhen>
</command>
<command
commandId="de.tum.in.www2.cupplugin.commands.RenameSymbol"
style="push">
<visibleWhen
checkEnabled="false">
<iterate
ifEmpty="false"
operator="or">
<and>
<with
variable="selection">
<instanceof
value="org.eclipse.jface.text.TextSelection">
</instanceof>
</with>
</and>
</iterate>
</visibleWhen>
</visibleWhen>//-->
</command>
</menuContribution>
</extension>
</plugin>
......@@ -3,16 +3,52 @@ package de.tum.in.www2.cupplugin.commands;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.ui.handlers.HandlerUtil;
import de.in.tum.www2.cup.Position;
import de.in.tum.www2.cup.Range;
import de.in.tum.www2.cup.analysis.FindAtPositionVisitor;
import de.in.tum.www2.cup.ast.AbstractNode;
import de.in.tum.www2.cup.ast.ParserResult;
import de.in.tum.www2.cup.ast.Production;
import de.in.tum.www2.cup.ast.ProductionRef;
import de.tum.in.www2.cupplugin.controller.PluginUtility;
import de.tum.in.www2.cupplugin.editors.CupTextEditor;
import de.tum.in.www2.cupplugin.editors.MultiPageEditor;
import de.tum.in.www2.cupplugin.model.Model;
public class OpenDeclarationHandler extends AbstractHandler {
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
PluginUtility.showMessage("OpenDeclarationHandler command called!");
MultiPageEditor mpe = (MultiPageEditor) HandlerUtil.getActiveEditorChecked(event);
CupTextEditor editor = mpe.getEditor();
Range range = PluginUtility.getRangeFromSelection(editor);
if (range == null)
return null;
IDocument doc = editor.getDocumentProvider().getDocument(editor.getEditorInput());
Model model = Model.getInstanceForDocument(doc);
ParserResult result = model.getAstModel();
if (result != null) {
result.resolveReferences();
FindAtPositionVisitor pf = new FindAtPositionVisitor(range.getBegin());
result.accept(pf, null);
AbstractNode node = pf.getResult();
if (node != null) {
AbstractNode parent = node.getParent();
if (parent != null && parent instanceof ProductionRef) {
// TODO!
}
}
}
return null;
}
......
......@@ -30,13 +30,7 @@ public class OpenDefinitionHandler extends AbstractHandler {
MultiPageEditor mpe = (MultiPageEditor) HandlerUtil.getActiveEditorChecked(event);
CupTextEditor editor = mpe.getEditor();
Range range = null;
try {
range = PluginUtility.getRangeFromSelection(editor);
} catch (BadLocationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Range range = PluginUtility.getRangeFromSelection(editor);
if (range == null)
return null;
......@@ -46,12 +40,16 @@ public class OpenDefinitionHandler extends AbstractHandler {
if (result != null) {
result.resolveReferences();
FindAtPositionVisitor pf = new FindAtPositionVisitor(range.getBegin());
result.accept(pf, null);
AbstractNode node = pf.getResult();
if (node != null) {
AbstractNode parent = node.getParent();
// TODO: we should also support this operation if we click on a definition
//
if (parent != null && parent instanceof ProductionRef) {
ProductionRef prodRef = (ProductionRef) parent;
......
......@@ -23,11 +23,9 @@ public final class PluginUtility {
mb.setMessage(message);
mb.open();
}
public static Range getRangeFromSelection (CupTextEditor editor)
throws BadLocationException
{
public static Range getRangeFromSelection (CupTextEditor editor)
{
ITextSelection textSelection = (ITextSelection) editor
.getSite().getSelectionProvider().getSelection();
......@@ -40,16 +38,21 @@ public final class PluginUtility {
// TODO: there might be off-by-one errors here.
// Check again!
int startLineNumber = document.getLineOfOffset(offset);
int startColumn = offset - document.getLineOffset(startLineNumber);
Position start = new Position(startLineNumber+1, startColumn+1, offset+1);
try {
int startLineNumber = document.getLineOfOffset(offset);
int startColumn = offset - document.getLineOffset(startLineNumber);
Position start = new Position(startLineNumber+1, startColumn+1, offset+1);
int endOffset = offset + textSelection.getLength();
int endLineNumber = document.getLineOfOffset(endOffset);
int endColumn = endOffset - document.getLineOffset(endLineNumber);
Position end = new Position(endLineNumber+1, endColumn+1, endOffset);
return new Range(start, end);
int endOffset = offset + textSelection.getLength();
int endLineNumber = document.getLineOfOffset(endOffset);
int endColumn = endOffset - document.getLineOffset(endLineNumber);
Position end = new Position(endLineNumber+1, endColumn+1, endOffset);
return new Range(start, end);
} catch (BadLocationException e) {
return null;
}
}
/**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment