eclipse rcp - find matching 'begin' - 'end' and highlight them -
i newbie in developing dsl in xtext. add feature of highlighting matching start , end of code block. in language every function must start 'begin' , end 'end' , same if conditions , loops. need highlight matching/corresponding 'end' when cursor on 'begin'. example below, need highlight corresponding first 'end' when cursor on second 'begin':
function x begin if a>b begin a=b; end end
any please?
thanks
i dont think support good. if can live minor solution try following
the grammar
model: greetings+=greeting*; greeting: 'if' name=id 'endif';
in uimodule
@override public icharacterpairmatcher bindicharacterpairmatcher() { return new mydslcharacterpairmatcher(new char[] { '(', ')', '{', '}', '[', ']' }); }
with
public class mydslcharacterpairmatcher extends defaultcharacterpairmatcher { public mydslcharacterpairmatcher(char[] chars) { super(chars); } @override public iregion match(idocument document, int offset, int length) { if (document instanceof ixtextdocument) { ixtextdocument xtextdocument = (ixtextdocument)document; iregion result = xtextdocument.readonly(new iunitofwork<iregion, xtextresource>() { @override public iregion exec(xtextresource state) throws exception { mydslgrammaraccess ga = state.getresourceserviceprovider().get(mydslgrammaraccess.class); icompositenode root = state.getparseresult().getrootnode(); ileafnode matchnode = nodemodelutils.findleafnodeatoffset(root, offset); eobject object = nodemodelutils.findactualsemanticobjectfor(matchnode); if (object instanceof greeting) { greeting g = (greeting)object; if (matchnode.getgrammarelement() == ga.getgreetingaccess().getifkeyword_0()) { icompositenode objectnode = nodemodelutils.findactualnodefor(object); (inode n : objectnode.getastreeiterable()) { if (n.getgrammarelement() != null && n.getgrammarelement() == ga.getgreetingaccess().getendifkeyword_2()) { return new region(n.getoffset(), n.getlength()); } } } if (matchnode.getgrammarelement() == ga.getgreetingaccess().getendifkeyword_2()) { icompositenode objectnode = nodemodelutils.findactualnodefor(object); (inode n : objectnode.getastreeiterable()) { if (n.getgrammarelement() != null && n.getgrammarelement() == ga.getgreetingaccess().getifkeyword_0()) { return new region(n.getoffset(), n.getlength()); } } } } return null; } }); if (result != null) { return result; } } return super.match(document, offset, length); } }
unfortunately jface highlight single character. if want more think have hook ioccurrencecomputer (you have have mark occurences on then)
package org.xtext.example.mydsl1.ui; import java.util.hashmap; import java.util.map; import org.eclipse.core.runtime.submonitor; import org.eclipse.emf.ecore.eobject; import org.eclipse.jface.text.itextselection; import org.eclipse.jface.text.position; import org.eclipse.jface.text.source.annotation; import org.eclipse.xtext.nodemodel.icompositenode; import org.eclipse.xtext.nodemodel.ileafnode; import org.eclipse.xtext.nodemodel.inode; import org.eclipse.xtext.nodemodel.util.nodemodelutils; import org.eclipse.xtext.resource.xtextresource; import org.eclipse.xtext.ui.editor.xtexteditor; import org.eclipse.xtext.ui.editor.model.ixtextdocument; import org.eclipse.xtext.ui.editor.occurrences.defaultoccurrencecomputer; import org.eclipse.xtext.util.cancelindicator; import org.eclipse.xtext.util.textregion; import org.eclipse.xtext.util.concurrent.cancelableunitofwork; import org.xtext.example.mydsl1.mydsl.greeting; import org.xtext.example.mydsl1.services.mydslgrammaraccess; public class mydsloccurrencecomputer extends defaultoccurrencecomputer { @override public map<annotation, position> createannotationmap(xtexteditor editor, itextselection selection, submonitor monitor) { final ixtextdocument document = editor.getdocument(); if (document != null) { map<annotation, position> result = document.readonly(new cancelableunitofwork<map<annotation, position>, xtextresource>() { @override public map<annotation, position> exec(xtextresource state, final cancelindicator cancelindicator) throws exception { mydslgrammaraccess ga = state.getresourceserviceprovider() .get(mydslgrammaraccess.class); icompositenode root = state.getparseresult().getrootnode(); ileafnode matchnode = nodemodelutils.findleafnodeatoffset( root, selection.getoffset()); eobject object = nodemodelutils .findactualsemanticobjectfor(matchnode); if (object instanceof greeting) { greeting g = (greeting) object; if (matchnode.getgrammarelement() == ga .getgreetingaccess().getifkeyword_0()) { icompositenode objectnode = nodemodelutils .findactualnodefor(object); (inode n : objectnode.getastreeiterable()) { if (n.getgrammarelement() != null && n.getgrammarelement() == ga .getgreetingaccess() .getendifkeyword_3()) { map<annotation, position> result = new hashmap<>(); addoccurrenceannotation( declaration_annotation_type, document, new textregion(matchnode .getoffset(), matchnode .getlength()), result); addoccurrenceannotation( occurrence_annotation_type, document, new textregion(n.getoffset(), n .getlength()), result); return result; } } } if (matchnode.getgrammarelement() == ga .getgreetingaccess().getendifkeyword_3()) { icompositenode objectnode = nodemodelutils .findactualnodefor(object); (inode n : objectnode.getastreeiterable()) { if (n.getgrammarelement() != null && n.getgrammarelement() == ga .getgreetingaccess() .getifkeyword_0()) { map<annotation, position> result = new hashmap<>(); addoccurrenceannotation( declaration_annotation_type, document, new textregion(matchnode .getoffset(), matchnode .getlength()), result); addoccurrenceannotation( occurrence_annotation_type, document, new textregion(n.getoffset(), n .getlength()), result); return result; } } } } return null; } }); if (result != null) { return result; } } return super.createannotationmap(editor, selection, monitor); } }
Comments
Post a Comment