Search in sources :

Example 41 with ITextRegion

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());
    }
}
Also used : ITextRegion(org.eclipse.xtext.util.ITextRegion) XtextResource(org.eclipse.xtext.resource.XtextResource)

Example 42 with ITextRegion

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);
}
Also used : ITextRegion(org.eclipse.xtext.util.ITextRegion) EObject(org.eclipse.emf.ecore.EObject) Region(org.eclipse.jface.text.Region) TextRegion(org.eclipse.xtext.util.TextRegion) ITextRegion(org.eclipse.xtext.util.ITextRegion) JvmType(org.eclipse.xtext.common.types.JvmType) XAbstractFeatureCall(org.eclipse.xtext.xbase.XAbstractFeatureCall) XImportDeclaration(org.eclipse.xtext.xtype.XImportDeclaration)

Example 43 with ITextRegion

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 == '@';
}
Also used : ITextRegion(org.eclipse.xtext.util.ITextRegion)

Example 44 with ITextRegion

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;
}
Also used : XtextEditor(org.eclipse.xtext.ui.editor.XtextEditor) XtextResource(org.eclipse.xtext.resource.XtextResource) ITextSelection(org.eclipse.jface.text.ITextSelection) ExecutionException(org.eclipse.core.commands.ExecutionException) RefactoringWizardOpenOperation_NonForking(org.eclipse.xtext.ui.refactoring.ui.RefactoringWizardOpenOperation_NonForking) ITextRegion(org.eclipse.xtext.util.ITextRegion) XExpression(org.eclipse.xtext.xbase.XExpression) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument)

Example 45 with ITextRegion

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);
            }
        }
    }
}
Also used : ILeafNode(org.eclipse.xtext.nodemodel.ILeafNode) ITextRegion(org.eclipse.xtext.util.ITextRegion)

Aggregations

ITextRegion (org.eclipse.xtext.util.ITextRegion)122 TextRegion (org.eclipse.xtext.util.TextRegion)44 EObject (org.eclipse.emf.ecore.EObject)33 Test (org.junit.Test)20 INode (org.eclipse.xtext.nodemodel.INode)19 XtextResource (org.eclipse.xtext.resource.XtextResource)17 ICompositeNode (org.eclipse.xtext.nodemodel.ICompositeNode)15 ILeafNode (org.eclipse.xtext.nodemodel.ILeafNode)15 BadLocationException (org.eclipse.jface.text.BadLocationException)11 Region (org.eclipse.jface.text.Region)9 XExpression (org.eclipse.xtext.xbase.XExpression)9 IOException (java.io.IOException)8 CoreException (org.eclipse.core.runtime.CoreException)8 IParseResult (org.eclipse.xtext.parser.IParseResult)7 IXtextDocument (org.eclipse.xtext.ui.editor.model.IXtextDocument)7 IRegion (org.eclipse.jface.text.IRegion)6 ITextSelection (org.eclipse.jface.text.ITextSelection)6 Collection (java.util.Collection)5 ArrayList (java.util.ArrayList)4 TextSelection (org.eclipse.jface.text.TextSelection)4