Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Examining the tree structure of XtextEditor's document
Examining the tree structure of XtextEditor's document [message #721493] Thu, 01 September 2011 22:37 Go to next message
David Struck is currently offline David StruckFriend
Messages: 29
Registered: July 2010
Junior Member
Is it possible to examine the tree structure of the XtextEditor's document? For example, let's say I am viewing the following code in an Xtext editor:

state idle
  actions {unlockDoor lockPanel}
  doorClosed => active
end

state active
  drawOpened => waitingForLight
  MyTest    => waitingForDraw
end


(This is based on the Finite State Machine example from the Xtext documentation.)

Now let's say that I move the cursor to the "doorClosed => active line" and put it in the word "active". From XtextEditor (or my subclass of it), is it possible to determine these things?

1. The cursor is on the State object named "active"?

2. The ancestor/enclosing "node" of "active" is the State object named "idle"?

Specifically, I am wondering about the notion that the editor document can be structured as a tree, where each keyword or phrase in the code represents a tree node that can have parents and children.

Thanks,

David
Re: Examining the tree structure of XtextEditor's document [message #721563 is a reply to message #721493] Fri, 02 September 2011 06:31 Go to previous messageGo to next message
Alexander Nittka is currently offline Alexander NittkaFriend
Messages: 1193
Registered: July 2009
Senior Member
Hi,

there already are two tree structures. The NodeModel (syntaxt tree containing all keywords, white spaces, terminals,...) and the semantic model (AST), you can view the latter one easily if you open the dsl file in the generic ecore editor. Another tree view that is already in place is the outline whose structure you can influence yourself.

Alex


Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext@itemis.de
Re: Examining the tree structure of XtextEditor's document [message #721710 is a reply to message #721563] Fri, 02 September 2011 16:21 Go to previous message
David Struck is currently offline David StruckFriend
Messages: 29
Registered: July 2010
Junior Member
Thanks, Alex!

Is there any way I can access the NodeModel from a subclass of XtextEditor so that I can examine it programmatically? Something like a getNodeModel method?

Right now I am trying to determine which state definition the cursor is positioned within when the cursor position changes. This is what I am doing right now:

	@Override
	protected void handleCursorPositionChanged() {
		super.handleCursorPositionChanged();
		try {
			State state = getState();
			System.out.println("&&&&& " + (state != null ? state.getName() : "null"));
		}
		catch(Exception e) {
			e.printStackTrace();
		}
	}

	private State getState() throws BadLocationException {
		int offset = getOffset();
		EObject context = getContext(offset);
		if(context != null) {
			ICompositeNode root = NodeModelUtils.getNode(context).getRootNode();
			if(root != null) {
				BidiIterator<INode> it = root.getChildren().iterator();
				while(it.hasNext()) {
					INode node = it.next();
					if(node.getSemanticElement() instanceof State) {
						int start = node.getOffset();
						int end = start + node.getLength();
						if(start < offset && offset < end) {
							return (State) node.getSemanticElement();
						}
					}
				}
			}
		}
		return null;
	}

	private int getOffset() throws BadLocationException {
		String cursorPosition = this.getCursorPosition();
		String[] position = cursorPosition.split(":");
		int line = Integer.parseInt(position[0].trim());
		int col = Integer.parseInt(position[1].trim());
		return this.getDocument().getLineOffset(line - 1) + (col - 1);
	}

	private EObject getContext(final int offset) {
		//see FindReferencesHandler
		return this.getDocument().readOnly(new IUnitOfWork<EObject, XtextResource>() {
			public EObject exec(XtextResource localResource) throws Exception {
				return eObjectAtOffsetHelper.resolveElementAt(localResource, offset);
			}
		});
	}

	@Inject
	protected EObjectAtOffsetHelper eObjectAtOffsetHelper;


This code is part of my subclass of XtextEditor. It works, but I'm betting there's a better way to accomplish what I'm trying to do.

Thanks,

David
Previous Topic:How do I handle xtext 1.0-indexes?
Next Topic:Xtext -> XPand Workflow - How to for new Eclipse version
Goto Forum:
  


Current Time: Thu Mar 28 21:56:46 GMT 2024

Powered by FUDForum. Page generated in 0.02888 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top