Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2011 itemis AG (http://www.itemis.eu) and others. All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse Public License v1.0 which accompanies this distribution, and is available at http://www.eclipse.org/legal/epl-v10.html /
  
  package org.eclipse.xtext.ui.editor.selection;
  
 import static java.util.Collections.*;
 import static org.eclipse.xtext.nodemodel.util.NodeModelUtils.*;
 
 import java.util.Map;
 
 import  org.eclipse.jface.action.IAction;
 

Author(s):
Michael Clay - Initial contribution and API
Sven Efftinge
 
 public class AstSelectionProvider {
 	public static final String SELECT_ENCLOSING = "org.eclipse.xtext.ui.editor.select.enclosing"//$NON-NLS-1$
 	public static final String SELECT_NEXT = "org.eclipse.xtext.ui.editor.select.next"//$NON-NLS-1$
 	public static final String SELECT_PREVIOUS = "org.eclipse.xtext.ui.editor.select.previous"//$NON-NLS-1$
 	public static final String SELECT_LAST = "org.eclipse.xtext.ui.editor.select.last"//$NON-NLS-1$
 	
 	
 	
 
 	public ITextRegion selectLast(XtextResource resourceITextRegion currentEditorSelection) {
 		}
 		if (!currentEditorSelection.equals(.pop())) {
 		}
 	}
 	
 	public ITextRegion selectEnclosing(XtextResource resourceITextRegion currentEditorSelection) {
 		Pair<EObjectEObjectcurrentlySelected = getSelectedAstElements(resourcecurrentEditorSelection);
 		if (currentlySelected == null) {
 			IParseResult parseResult = resource.getParseResult();
 			if (parseResult != null) {
 				ICompositeNode rootNode = parseResult.getRootNode();
 				int offset = getSelectionOffset(rootNodecurrentEditorSelection);
 				INode node = findLeafNodeAtOffset(rootNodeoffset);
 				ITextRegion fineGrainedRegion = computeInitialFineGrainedSelection(nodecurrentEditorSelection);
 				if (fineGrainedRegion != null) {
 					register(currentEditorSelection);
 					return register(fineGrainedRegion);
 				}
 				EObject eObject = findSemanticObjectFor(node);
 				return register(getTextRegion(eObject));
 			}
 		} else {
 			EObject first = currentlySelected.getFirst();
 			if (first.eContainer() != null) {
 				return register(getTextRegion(first.eContainer()));
 			}
 		}
 	}
 	
 	public ITextRegion selectNext(XtextResource resourceITextRegion currentEditorSelection) {
 		Pair<EObjectEObjectcurrentlySelected = getSelectedAstElements(resourcecurrentEditorSelection);
 		if (currentlySelected == null) {
 			return selectEnclosing(resourcecurrentEditorSelection);
 		}
 		EObject second = currentlySelected.getSecond();
 		EObject nextSibling = EcoreUtil2.getNextSibling(second);
 		if (nextSibling != null) {
 			return register(getRegion(Tuples.create(currentlySelected.getFirst(), nextSibling)));
 		} else {
			if (second.eContainer() == null)
			return register(getRegion(Tuples.create(second.eContainer(), second.eContainer())));
		}
	}
	public ITextRegion selectPrevious(XtextResource resourceITextRegion currentEditorSelection) {
		Pair<EObjectEObjectcurrentlySelected = getSelectedAstElements(resourcecurrentEditorSelection);
		if (currentlySelected == null) {
			return selectEnclosing(resourcecurrentEditorSelection);
		}
		EObject first = currentlySelected.getFirst();
		EObject previousSibling = EcoreUtil2.getPreviousSibling(first);
		if (previousSibling != null) {
			return register(getRegion(Tuples.create(previousSibling,currentlySelected.getSecond())));
else {
			if (first.eContainer() == null)
			return register(getRegion(Tuples.create(first.eContainer(), first.eContainer())));
		}
	}

Returns:
the starting EObject and the ending EObject of the current selection or null if the selection doesn't match the exact boundaries of two EObjects with the same container.
	protected Pair<EObjectEObjectgetSelectedAstElements(XtextResource resourceITextRegion currentSelection) {
		EObject eObject = getEObjectAtOffset(resourcecurrentSelection);
		while (eObject != null) {
			Pair<EObjectEObjectresult = internalGetSelectedAstElements(eObjectcurrentSelection);
			if (result != null
				return result;
			eObject = eObject.eContainer();
		}
		return null;
	}
	protected Pair<EObjectEObjectinternalGetSelectedAstElements(EObject eObjectITextRegion currentSelection) {
		ITextRegion textRegion = getTextRegion(eObject);
		while (textRegion.getOffset() == currentSelection.getOffset()) {
			EObject container = eObject.eContainer();
			if (container != null) {
				for (EObject obj : container.eContents()) {
					ITextRegion region = getTextRegion(obj);
					if (getEndOffset(region) == getEndOffset(currentSelection)) {
						Pair<EObjectEObjectparentMatch = internalGetSelectedAstElements(eObject.eContainer(), currentSelection);
						if (parentMatch != null)
							return parentMatch;
						return Tuples.create(eObjectobj);
					}
				}
else {
				if (textRegion.equals(currentSelection))
					return Tuples.create(eObjecteObject);
				return null;
			}
			eObject = container;
			textRegion = getTextRegion(eObject);
		}
		return null;
	}
	protected ITextRegion getRegion(Pair<EObjectEObjectselection) {
		final ITextRegion firstRegion = getTextRegion(selection.getFirst());
		if (selection.getFirst() == selection.getSecond()) {
			return firstRegion;
		}
		ITextRegion secondRegion = getTextRegion(selection.getSecond());
		return new TextRegion(firstRegion.getOffset(), getEndOffset(secondRegion) - firstRegion.getOffset());
	}
	protected ITextRegion computeInitialFineGrainedSelection(INode nodeITextRegion currentEditorSelection) {
		if (node.getText().trim().length() > 0) {
			ITextRegion textRegion = node.getTextRegion();
			if (currentEditorSelection.getOffset() >= textRegion.getOffset()
				&& getEndOffset(currentEditorSelection) < textRegion.getOffset() + textRegion.getLength()) {
				//TODO enhance to just select a single word in a comment or string literal.
				return textRegion;
			}
		}
		return null;
	}
	protected EObject findSemanticObjectFor(INode node) {
		if (node instanceof ILeafNode && ((ILeafNodenode).isHidden()) {
			if (semanticEObjects.containsKey(node))
				return semanticEObjects.get(node);
	}
	protected EObject getEObjectAtOffset(XtextResource resourceITextRegion currentEditorSelection) {
		IParseResult parseResult = resource.getParseResult();
		if (parseResult != null) {
			ICompositeNode rootNode = parseResult.getRootNode();
			INode nodeAtOffset = findLeafNodeAtOffset(rootNodecurrentEditorSelection.getOffset());
			return findSemanticObjectFor(nodeAtOffset);
		}
		return null;
	}
	public void initialize(XtextEditor xtextEditor) {
		AstSelectionAction enclosingSelectionAction = new AstSelectionAction(
			protected ITextRegion internalSelect(XtextResource xtextResourceITextRegion textRegion) {
				return selectEnclosing(xtextResourcetextRegion);
			}
		};
		setAction(xtextEditorenclosingSelectionAction);
		AstSelectionAction previousSelectionAction = new AstSelectionAction(
			protected ITextRegion internalSelect(XtextResource xtextResourceITextRegion textRegion) {
				return selectPrevious(xtextResourcetextRegion);
			}
		};
		setAction(xtextEditorpreviousSelectionAction);
				xtextEditor) {
			protected ITextRegion internalSelect(XtextResource xtextResourceITextRegion textRegion) {
				return selectNext(xtextResourcetextRegion);
			}
		};
		nextSelectionAction.setActionDefinitionId();
		setAction(xtextEditornextSelectionAction);
				xtextEditor) {
			protected ITextRegion internalSelect(XtextResource xtextResourceITextRegion textRegion) {
				return selectLast(xtextResourcetextRegion);
			}
		};
		nextSelectionAction.setActionDefinitionId();
		setAction(xtextEditorlastSelectionAction);
	}
	protected void setAction(XtextEditor xtextEditor, IAction actionString actionDefinitionId) {
		action.setActionDefinitionId(actionDefinitionId);
		xtextEditor.setAction(actionDefinitionIdaction);
	}
	protected ITextRegion register(ITextRegion textRegion) {
		if (.isEmpty() || !.peek().equals(textRegion)) {
		}
		return textRegion;
	}
	protected ITextRegion getTextRegion(EObject eObject) {
		if (eObject == null)
			return null;
		IParseResult parseResult = ((XtextResource)eObject.eResource()).getParseResult();
		if (parseResult == null)
			return null;
		ICompositeNode rootNode = parseResult.getRootNode();
		final ITextRegion result = .getFullTextRegion(eObject);
		int start = result.getOffset();
		int end = result.getOffset() + result.getLength();
		for (Entry<ILeafNodeEObjectentry : comments.entrySet()) {
			if (entry.getValue() == eObject) {
				ILeafNode node = entry.getKey();
				if (node.getTotalOffset() < start) {
					start = node.getTotalOffset();
				}
				if (node.getTotalEndOffset() > end) {
					end = node.getTotalEndOffset();
				}
			}
		}
		return new TextRegion(startend-start);
	}
	protected int getEndOffset(ITextRegion region) {
		return region.getOffset() + region.getLength();
	}
	protected int getSelectionOffset(ICompositeNode rootNodeITextRegion region) {
		boolean selectionAtEndOfText = rootNode.getTotalEndOffset() == getEndOffset(region);
		return selectionAtEndOfText && region.getOffset() > 0 ? region.getOffset() - 1 : region.getOffset();
	}
New to GrepCode? Check out our FAQ X