Commit 6e3765f4 authored by Johannes Roith's avatar Johannes Roith
Browse files

Hyperlinks are fully implemented now. :-)

parent d3b473ac
......@@ -12,6 +12,7 @@ import de.in.tum.www2.cup.analysis.RefResolutionVisitor;
import de.in.tum.www2.cup.internal.production;
public class ParserResult extends AbstractNode {
private boolean resolved;
public Package pack;
public List<Import> imports = new ArrayList<Import> ();
......@@ -46,10 +47,12 @@ public class ParserResult extends AbstractNode {
return declVisitor.getResult();
}
// TODO: cache in a safe way!
public void resolveReferences() {
if (resolved)
return;
RefResolutionVisitor resRefVisitor = new RefResolutionVisitor();
this.accept(resRefVisitor, null);
resolved = true;
}
// TODO: it would be better to use a hashtable here.
......
......@@ -21,7 +21,9 @@ import de.in.tum.www2.cup.ast.IHasDeclarationReference;
import de.in.tum.www2.cup.ast.IHasDefinitionReference;
import de.in.tum.www2.cup.ast.IWithName;
import de.in.tum.www2.cup.ast.Name;
import de.in.tum.www2.cup.ast.NonTerminal;
import de.in.tum.www2.cup.ast.ParserResult;
import de.in.tum.www2.cup.ast.Production;
public class CupHyperlinkDetector implements IHyperlinkDetector {
......@@ -33,8 +35,6 @@ public class CupHyperlinkDetector implements IHyperlinkDetector {
static public class HyperlinkingVisitor extends WildcardVisitor<Object> {
IRegion inputRegion;
CupTextEditor editor;
ITextViewer textViewer;
List<IHyperlink> links;
......@@ -44,53 +44,47 @@ public class CupHyperlinkDetector implements IHyperlinkDetector {
return links;
}
public HyperlinkingVisitor(IRegion region,
CupTextEditor editor, ITextViewer textViewer)
public HyperlinkingVisitor(IRegion region, ITextViewer textViewer)
{
this.links = new ArrayList<IHyperlink>();
this.inputRegion = inputRegion;
this.editor = editor;
this.textViewer = textViewer;
IDocument document = textViewer.getDocument();
offset = region.getOffset();
/*
IRegion lineRegion = null;
String candidate = null;
try {
lineRegion = document.getLineInformationOfOffset(offset);
candidate = document.get(lineRegion.getOffset(), lineRegion.getLength());
} catch (org.eclipse.jface.text.BadLocationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
*/
offset = region.getOffset() + region.getLength();
}
@Override
public void postVisitWildcard(AbstractNode node, Object data) {
if (node instanceof IHasDefinitionReference && node instanceof IWithName) {
AbstractNode def = ((IHasDefinitionReference) node).getDefinition();
Name name = ((IWithName) node).getName();
if (def != null && name != null && name.name != null) {
if (node == null)
return;
if (!(node instanceof IHasDefinitionReference ||
node instanceof IHasDeclarationReference))
return;
if (name.getBegin().getOffsetFromStart() < offset)
if (node instanceof IWithName) {
Name name = ((IWithName) node).getName();
if (name != null && name.name != null) {
int length = name.name.length();
int offsetFromStart = name.getBegin().getOffsetFromStart();
if (offset < offsetFromStart || offset > offsetFromStart + length)
return;
Region outputRegion = new Region(
name.getBegin().getOffsetFromStart(),
name.name.length());
links.add(new CupHyperlink(outputRegion, editor, textViewer,
def.getBegin().getOffsetFromStart(), 1));
Region outputRegion = new Region(offsetFromStart, length);
AbstractNode targetNode;
if (node instanceof IHasDefinitionReference) {
targetNode = ((IHasDefinitionReference) node).getDefinition();
if (targetNode != null)
links.add(new CupDefinitionHyperlink(outputRegion, textViewer,
targetNode.getBegin().getOffsetFromStart(), 1));
}
if (node instanceof IHasDeclarationReference) {
targetNode = ((IHasDeclarationReference) node).getDeclaration();
if (targetNode != null)
links.add(new CupDeclarationHyperlink(outputRegion, textViewer,
targetNode.getBegin().getOffsetFromStart(), 1));
}
}
}
}
......@@ -100,19 +94,42 @@ public class CupHyperlinkDetector implements IHyperlinkDetector {
}
}
static public class CupHyperlink implements IHyperlink {
static public class CupDefinitionHyperlink extends CupHyperlink
{
public CupDefinitionHyperlink(IRegion region, ITextViewer textViewer,
int targetOffset, int targetLength) {
super(region, textViewer, targetOffset, targetLength);
}
@Override
public String getHyperlinkText() {
return "Open Definition";
}
}
static public class CupDeclarationHyperlink extends CupHyperlink
{
public CupDeclarationHyperlink(IRegion region, ITextViewer textViewer,
int targetOffset, int targetLength) {
super(region, textViewer, targetOffset, targetLength);
}
@Override
public String getHyperlinkText() {
return "Open Declaration";
}
}
static public abstract class CupHyperlink implements IHyperlink {
private IRegion region;
private CupTextEditor editor;
ITextViewer textViewer;
private int targetOffset;
private int targetLength;
public CupHyperlink(IRegion region,
CupTextEditor editor, ITextViewer textViewer,
public CupHyperlink(IRegion region, ITextViewer textViewer,
int targetOffset, int targetLength) {
this.region = region;
this.editor = editor;
this.textViewer = textViewer;
this.targetOffset = targetOffset;
this.targetLength = targetLength;
......@@ -127,12 +144,7 @@ public class CupHyperlinkDetector implements IHyperlinkDetector {
public String getTypeLabel() {
return null;
}
@Override
public String getHyperlinkText() {
return null;
}
@Override
public void open() {
textViewer.revealRange(targetOffset, targetLength);
......@@ -148,12 +160,15 @@ public class CupHyperlinkDetector implements IHyperlinkDetector {
ParserResult result = editor.getModel().getAstModel();
if (result != null) {
result.resolveReferences();
HyperlinkingVisitor hv = new HyperlinkingVisitor(region, editor, textViewer);
HyperlinkingVisitor hv = new HyperlinkingVisitor(region, textViewer);
result.accept(hv, null);
return hv.getLinks().toArray(new IHyperlink[0]);
List<IHyperlink> lst = hv.getLinks();
if (lst.size() == 0)
return null;
return lst.toArray(new IHyperlink[0]);
}
return new IHyperlink[0];
return null;
}
}
......@@ -10,7 +10,10 @@ import org.eclipse.jface.text.TextAttribute;
import org.eclipse.jface.text.contentassist.ContentAssistant;
import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
import org.eclipse.jface.text.contentassist.IContentAssistant;
import org.eclipse.jface.text.hyperlink.DefaultHyperlinkPresenter;
import org.eclipse.jface.text.hyperlink.IHyperlinkDetector;
import org.eclipse.jface.text.hyperlink.IHyperlinkPresenter;
import org.eclipse.jface.text.hyperlink.MultipleHyperlinkPresenter;
import org.eclipse.jface.text.presentation.PresentationReconciler;
import org.eclipse.jface.text.rules.DefaultDamagerRepairer;
import org.eclipse.jface.text.rules.IToken;
......@@ -48,6 +51,13 @@ public class CupSourceViewerConfiguration extends SourceViewerConfiguration {
this.editor = editor;
}
@Override
public IHyperlinkPresenter getHyperlinkPresenter(ISourceViewer sourceViewer)
{
return new MultipleHyperlinkPresenter(CupTextEditor.HYPERLINK.getRGB());
}
@Override
public IHyperlinkDetector[] getHyperlinkDetectors(ISourceViewer sourceViewer) {
return new IHyperlinkDetector[] { new CupHyperlinkDetector(editor) };
}
......
......@@ -56,6 +56,8 @@ public class CupTextEditor extends TextEditor {
}
public static final Color HYPERLINK = blue;
public static final Color KEYWORD = blue;
public static final Color COMMENT = gray;
public static final Color TERMINAL = orange;
......@@ -63,6 +65,7 @@ public class CupTextEditor extends TextEditor {
public static final Color SPECIAL_ERROR_TERMINAL = purple;
public static final Color STRING = red;
public static final Color IDENTIFIER = pink;
public static final Color JAVA_KEYWORD = brown;
public static final Color JAVA_TEXTUAL = brightBlue;
public static final Color JAVA_CUP_DEFINED = blueGreen;
......
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