Search in sources :

Example 86 with ITextRegion

use of org.eclipse.xtext.util.ITextRegion in project xtext-xtend by eclipse.

the class XtendExpressionUtilTest method assertExpressionSelected.

protected void assertExpressionSelected(final String modelWithSelectionMarkup, final String expectedSelection) {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("class Foo {");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("def foo() ");
    _builder.append(modelWithSelectionMarkup, "\t");
    _builder.newLineIfNotEmpty();
    _builder.append("}");
    _builder.newLine();
    final String model = _builder.toString();
    final String cleanedModel = model.replaceAll("\\$", "");
    final XtendFile expression = this.parse(cleanedModel);
    final int selectionOffset = model.indexOf("$");
    int _lastIndexOf = model.lastIndexOf("$");
    int _minus = (_lastIndexOf - selectionOffset);
    final int selectionLength = (_minus - 1);
    Resource _eResource = expression.eResource();
    TextSelection _textSelection = new TextSelection(selectionOffset, selectionLength);
    final XExpression selectedExpression = this.util.findSelectedExpression(((XtextResource) _eResource), _textSelection);
    final ITextRegion selectedRegion = this.locationInFileProvider.getFullTextRegion(selectedExpression);
    int _offset = selectedRegion.getOffset();
    int _offset_1 = selectedRegion.getOffset();
    int _length = selectedRegion.getLength();
    int _plus = (_offset_1 + _length);
    Assert.assertEquals(expectedSelection, cleanedModel.substring(_offset, _plus));
}
Also used : XtendFile(org.eclipse.xtend.core.xtend.XtendFile) TextSelection(org.eclipse.jface.text.TextSelection) ITextRegion(org.eclipse.xtext.util.ITextRegion) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) XtextResource(org.eclipse.xtext.resource.XtextResource) Resource(org.eclipse.emf.ecore.resource.Resource) XExpression(org.eclipse.xtext.xbase.XExpression) XtextResource(org.eclipse.xtext.resource.XtextResource)

Example 87 with ITextRegion

use of org.eclipse.xtext.util.ITextRegion in project xtext-xtend by eclipse.

the class AbstractXtendFormatterTest method assertFormatted.

public void assertFormatted(final Procedure1<? super MapBasedPreferenceValues> cfg, final CharSequence expectation, final CharSequence toBeFormatted, final String prefix, final String postfix, final boolean allowErrors) {
    final Procedure1<FormatterTestRequest> _function = (FormatterTestRequest it) -> {
        final Procedure1<MapBasedPreferenceValues> _function_1 = (MapBasedPreferenceValues it_1) -> {
            it_1.<Integer>put(FormatterPreferenceKeys.maxLineWidth, Integer.valueOf(80));
            it_1.<Boolean>put(XtendFormatterPreferenceKeys.keepOneLineMethods, Boolean.valueOf(false));
            if (cfg != null) {
                cfg.apply(it_1);
            }
        };
        it.preferences(_function_1);
        it.setExpectation(((prefix + expectation) + postfix));
        it.setToBeFormatted(((prefix + toBeFormatted) + postfix));
        Collection<ITextRegion> _regions = it.getRequest().getRegions();
        int _length = prefix.length();
        int _length_1 = toBeFormatted.length();
        TextRegion _textRegion = new TextRegion(_length, _length_1);
        _regions.add(_textRegion);
        it.setAllowSyntaxErrors(allowErrors);
    };
    this.tester.assertFormatted(_function);
}
Also used : TextRegion(org.eclipse.xtext.util.TextRegion) ITextRegion(org.eclipse.xtext.util.ITextRegion) Procedure1(org.eclipse.xtext.xbase.lib.Procedures.Procedure1) FormatterTestRequest(org.eclipse.xtext.testing.formatter.FormatterTestRequest) MapBasedPreferenceValues(org.eclipse.xtext.preferences.MapBasedPreferenceValues) Collection(java.util.Collection)

Example 88 with ITextRegion

use of org.eclipse.xtext.util.ITextRegion in project xtext-xtend by eclipse.

the class XtendQuickfixProvider method replaceKeyword.

protected void replaceKeyword(Keyword keyword, String replacement, EObject container, IXtextDocument document) throws BadLocationException {
    ICompositeNode node = NodeModelUtils.findActualNodeFor(container);
    if (node != null) {
        for (ILeafNode leafNode : node.getLeafNodes()) {
            if (leafNode.getGrammarElement() == keyword) {
                ITextRegion leafRegion = leafNode.getTextRegion();
                String actualReplacement = replacement;
                if (!Character.isWhitespace(document.getChar(leafRegion.getOffset() - 1))) {
                    actualReplacement = " " + replacement;
                }
                document.replace(leafRegion.getOffset(), leafRegion.getLength(), actualReplacement);
            }
        }
    }
}
Also used : ILeafNode(org.eclipse.xtext.nodemodel.ILeafNode) ITextRegion(org.eclipse.xtext.util.ITextRegion) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode)

Example 89 with ITextRegion

use of org.eclipse.xtext.util.ITextRegion in project xtext-xtend by eclipse.

the class XtendQuickfixProvider method surroundWithTryCatch.

@Fix(org.eclipse.xtext.xbase.validation.IssueCodes.UNHANDLED_EXCEPTION)
public void surroundWithTryCatch(final Issue issue, IssueResolutionAcceptor acceptor) {
    if (issue.getData() == null || issue.getData().length <= 1) {
        return;
    }
    IModificationContext modificationContext = getModificationContextFactory().createModificationContext(issue);
    IXtextDocument xtextDocument = modificationContext.getXtextDocument();
    if (xtextDocument == null) {
        return;
    }
    if (isJvmConstructorCall(xtextDocument, issue)) {
        return;
    }
    acceptor.accept(issue, "Surround with try/catch block", "Surround with try/catch block", "fix_indent.gif", new ISemanticModification() {

        @Override
        public void apply(EObject element, IModificationContext context) throws Exception {
            String[] issueData = issue.getData();
            URI childURI = URI.createURI(issueData[issueData.length - 1]);
            XtextResource xtextResource = (XtextResource) element.eResource();
            List<JvmType> exceptions = getExceptions(issueData, xtextResource);
            if (exceptions.size() > 0) {
                EObject childThrowingException = xtextResource.getResourceSet().getEObject(childURI, true);
                XExpression toBeSurrounded = findContainerExpressionInBlockExpression(childThrowingException);
                IXtextDocument xtextDocument = context.getXtextDocument();
                if (toBeSurrounded != null) {
                    ICompositeNode toBeSurroundedNode = NodeModelUtils.findActualNodeFor(toBeSurrounded);
                    if (toBeSurroundedNode == null)
                        throw new IllegalStateException("toBeSurroundedNode may not be null");
                    ITextRegion toBeSurroundedRegion = toBeSurroundedNode.getTextRegion();
                    ReplacingAppendable appendable = appendableFactory.create(context.getXtextDocument(), (XtextResource) childThrowingException.eResource(), toBeSurroundedRegion.getOffset(), toBeSurroundedRegion.getLength());
                    appendable.append("try {").increaseIndentation().newLine().append(xtextDocument.get(toBeSurroundedRegion.getOffset(), toBeSurroundedRegion.getLength())).decreaseIndentation().newLine();
                    for (JvmType exceptionType : exceptions) {
                        appendable.append("} catch (").append(exceptionType).append(" exc) {").increaseIndentation().newLine().append("throw new RuntimeException(\"auto-generated try/catch\", exc)").decreaseIndentation().newLine();
                    }
                    appendable.append("}");
                    appendable.commitChanges();
                }
            }
        }
    });
}
Also used : ISemanticModification(org.eclipse.xtext.ui.editor.model.edit.ISemanticModification) XtextResource(org.eclipse.xtext.resource.XtextResource) ReplacingAppendable(org.eclipse.xtext.xbase.ui.contentassist.ReplacingAppendable) JvmType(org.eclipse.xtext.common.types.JvmType) URI(org.eclipse.emf.common.util.URI) CoreException(org.eclipse.core.runtime.CoreException) BadLocationException(org.eclipse.jface.text.BadLocationException) ITextRegion(org.eclipse.xtext.util.ITextRegion) EObject(org.eclipse.emf.ecore.EObject) IModificationContext(org.eclipse.xtext.ui.editor.model.edit.IModificationContext) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) ArrayList(java.util.ArrayList) List(java.util.List) EList(org.eclipse.emf.common.util.EList) XExpression(org.eclipse.xtext.xbase.XExpression) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument) Fix(org.eclipse.xtext.ui.editor.quickfix.Fix)

Example 90 with ITextRegion

use of org.eclipse.xtext.util.ITextRegion in project xtext-xtend by eclipse.

the class ExtractMethodRefactoring method getExpressionsRegion.

protected ITextRegion getExpressionsRegion() {
    ITextRegion firstRegion = expressionUtil.getTextRegion(firstExpression);
    ITextRegion lastRegion = expressionUtil.getTextRegion(lastExpression);
    ITextRegion expressionRegion = new TextRegion(firstRegion.getOffset(), lastRegion.getOffset() + lastRegion.getLength() - firstRegion.getOffset());
    return expressionRegion;
}
Also used : TextRegion(org.eclipse.xtext.util.TextRegion) ITextRegion(org.eclipse.xtext.util.ITextRegion) 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