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));
}
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);
}
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);
}
}
}
}
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();
}
}
}
});
}
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;
}
Aggregations