use of org.eclipse.xtext.util.ITextRegion in project xtext-eclipse by eclipse.
the class TypeChooser method revealInEditor.
protected void revealInEditor(XtextEditor activeXtextEditor, Iterable<TypeUsage> usages, final XtextResource resource) {
boolean isRevealUsages = activeXtextEditor.getDocument().priorityReadOnly(new IUnitOfWork<Boolean, XtextResource>() {
@Override
public Boolean exec(XtextResource state) throws Exception {
return state.getURI().equals(resource.getURI());
}
});
if (isRevealUsages) {
originalSelection = activeXtextEditor.getSelectionProvider().getSelection();
ITextRegion firstOccurrence = usages.iterator().next().getTextRegion();
activeXtextEditor.selectAndReveal(firstOccurrence.getOffset(), firstOccurrence.getLength());
}
}
use of org.eclipse.xtext.util.ITextRegion in project xtext-eclipse by eclipse.
the class XbaseHyperLinkHelper method createHyperlinksTo.
@Override
protected void createHyperlinksTo(XtextResource resource, INode node, EObject target, IHyperlinkAcceptor acceptor) {
EObject semanticObj = NodeModelUtils.findActualSemanticObjectFor(node);
if (semanticObj instanceof XImportDeclaration) {
if (((XImportDeclaration) semanticObj).isStatic()) {
final ITextRegion textRegion = this.locationInFileProvider.getSignificantTextRegion(semanticObj, XtypePackage.Literals.XIMPORT_DECLARATION__IMPORTED_TYPE, 0);
int _offset = textRegion.getOffset();
int _length = textRegion.getLength();
final Region region = new Region(_offset, _length);
this.createHyperlinksTo(resource, region, target, acceptor);
}
} else if (semanticObj instanceof XAbstractFeatureCall) {
if (target instanceof JvmType) {
XAbstractFeatureCall casted = (XAbstractFeatureCall) semanticObj;
while (casted.isPackageFragment()) {
casted = (XAbstractFeatureCall) casted.eContainer();
}
if (casted.isTypeLiteral()) {
ITextRegion textRegion = locationInFileProvider.getSignificantTextRegion(casted);
Region jfaceRegion = new Region(textRegion.getOffset(), textRegion.getLength());
createHyperlinksTo(resource, jfaceRegion, target, acceptor);
return;
}
}
}
super.createHyperlinksTo(resource, node, target, acceptor);
}
use of org.eclipse.xtext.util.ITextRegion in project xtext-eclipse by eclipse.
the class ExpressionUtil method isBeginOfExpression.
/**
* @return whether the given selection is zero length
*/
protected boolean isBeginOfExpression(INode node) {
ITextRegion textRegion = node.getTextRegion();
if (textRegion.getLength() == 0)
return false;
char firstChar = node.getText().charAt(0);
return Character.isLetterOrDigit(firstChar) || firstChar == '\'' || firstChar == '"' || firstChar == '[' || firstChar == '(' || firstChar == '{' || firstChar == '#' || firstChar == '@';
}
use of org.eclipse.xtext.util.ITextRegion in project xtext-eclipse by eclipse.
the class ExtractVariableHandler method execute.
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
try {
syncUtil.totalSync(false);
final XtextEditor editor = EditorUtils.getActiveXtextEditor(event);
if (editor != null) {
final ITextSelection selection = (ITextSelection) editor.getSelectionProvider().getSelection();
final IXtextDocument document = editor.getDocument();
XtextResource resource = document.priorityReadOnly(new IUnitOfWork<XtextResource, XtextResource>() {
@Override
public XtextResource exec(XtextResource state) throws Exception {
return resourceCopier.loadIntoNewResourceSet(state);
}
});
XExpression expression = expressionUtil.findSelectedExpression(resource, selection);
if (expression != null) {
ExtractVariableRefactoring introduceVariableRefactoring = refactoringProvider.get();
if (introduceVariableRefactoring.initialize(editor, expression)) {
ITextRegion region = locationInFileProvider.getFullTextRegion(expression);
editor.selectAndReveal(region.getOffset(), region.getLength());
ExtractVariableWizard wizard = new ExtractVariableWizard(introduceVariableRefactoring);
RefactoringWizardOpenOperation_NonForking openOperation = new RefactoringWizardOpenOperation_NonForking(wizard);
openOperation.run(editor.getSite().getShell(), "Extract Local Variable");
}
}
}
} catch (InterruptedException e) {
return null;
} catch (Exception exc) {
LOG.error("Error during refactoring", exc);
MessageDialog.openError(Display.getCurrent().getActiveShell(), "Error during refactoring", exc.getMessage() + "\nSee log for details");
}
return null;
}
use of org.eclipse.xtext.util.ITextRegion in project xtext-eclipse by eclipse.
the class SemanticHighlighter method highlightNode.
/**
* Highlights the non-hidden parts of {@code node} with the style that is associated with {@code id}.
*/
protected void highlightNode(INode node, String id, IHighlightedPositionAcceptor acceptor) {
if (node == null)
return;
if (node instanceof ILeafNode) {
ITextRegion textRegion = node.getTextRegion();
acceptor.addPosition(textRegion.getOffset(), textRegion.getLength(), id);
} else {
for (ILeafNode leaf : node.getLeafNodes()) {
if (!leaf.isHidden()) {
ITextRegion leafRegion = leaf.getTextRegion();
acceptor.addPosition(leafRegion.getOffset(), leafRegion.getLength(), id);
}
}
}
}
Aggregations