use of org.eclipse.xtext.resource.XtextResource in project xtext-xtend by eclipse.
the class AbstractXtendTestCase method files.
protected Iterable<XtendFile> files(boolean validate, String... contents) throws Exception {
XtextResourceSet set = getResourceSet();
List<XtendFile> result = newArrayList();
for (String string : contents) {
String fileName = getFileName(string);
Resource resource = set.createResource(URI.createURI(fileName + ".xtend"));
resource.load(new StringInputStream(string), null);
assertEquals(resource.getErrors().toString(), 0, resource.getErrors().size());
}
for (Resource resource : new ArrayList<Resource>(set.getResources())) {
XtendFile file = (XtendFile) resource.getContents().get(0);
result.add(file);
}
if (validate) {
for (XtendFile file : result) {
List<Issue> issues = ((XtextResource) file.eResource()).getResourceServiceProvider().getResourceValidator().validate(file.eResource(), CheckMode.ALL, CancelIndicator.NullImpl);
assertTrue("Resource contained errors : " + issues.toString(), issues.isEmpty());
}
}
return result;
}
use of org.eclipse.xtext.resource.XtextResource in project xtext-xtend by eclipse.
the class AbstractXtendContentAssistBugTest method getResourceFor.
@Override
public XtextResource getResourceFor(InputStream stream) {
try {
XtextResourceSet set = get(XtextResourceSet.class);
XtextResource result = (XtextResource) set.createResource(URI.createURI("Test." + testHelper.getFileExtension()));
result.load(stream, null);
initializeTypeProvider(result);
return result;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
use of org.eclipse.xtext.resource.XtextResource in project xtext-xtend by eclipse.
the class ConvertJavaCode method formatXtendCode.
private String formatXtendCode(IFile xtendFile, final String xtendCode) {
try {
XtextResource resource = (XtextResource) createResource(xtendFile, xtendCode);
ITextRegionAccess regionAccess = regionAccessBuilder.get().forNodeModel(resource).create();
FormatterRequest request = new FormatterRequest();
request.setAllowIdentityEdits(false);
request.setTextRegionAccess(regionAccess);
request.setPreferences(TypedPreferenceValues.castOrWrap(cfgProvider.getPreferenceValues(resource)));
List<ITextReplacement> replacements = formatter.format(request);
String formatted = regionAccess.getRewriter().renderToString(replacements);
return formatted;
} catch (Exception e) {
LOG.error("Formatting step canceled due to an exception.", e);
return null;
}
}
use of org.eclipse.xtext.resource.XtextResource in project xtext-xtend by eclipse.
the class ExtractMethodRefactoring method initialize.
public boolean initialize(XtextEditor editor, List<XExpression> selectedExpressions, boolean doSave) {
if (selectedExpressions.isEmpty() || editor.getDocument() == null)
return false;
this.document = editor.getDocument();
this.doSave = doSave;
this.editor = editor;
this.expressions = calculateExpressions(selectedExpressions);
this.firstExpression = this.expressions.get(0);
this.originalMethod = EcoreUtil2.getContainerOfType(firstExpression, XtendFunction.class);
this.lastExpression = this.expressions.get(this.expressions.size() - 1);
this.resourceURI = EcoreUtil2.getPlatformResourceOrNormalizedURI(firstExpression).trimFragment();
this.xtendClass = EcoreUtil2.getContainerOfType(firstExpression, XtendClass.class);
if (xtendClass == null || originalMethod == null)
return false;
this.visibility = JvmVisibility.PROTECTED;
this.isStatic = originalMethod.isStatic();
this.isExplicitlyDeclareReturnType = true;
XExpression successorExpression = expressionUtil.findSuccessorExpressionForVariableDeclaration(lastExpression);
nameUtil.setFeatureScopeContext(successorExpression);
rewriter = rewriterFactory.create(document, (XtextResource) firstExpression.eResource());
return true;
}
use of org.eclipse.xtext.resource.XtextResource in project xtext-xtend by eclipse.
the class XtendQuickfixProvider method specifyTypeExplicitly.
@Fix(IssueCodes.API_TYPE_INFERENCE)
public void specifyTypeExplicitly(Issue issue, IssueResolutionAcceptor acceptor) {
acceptor.accept(issue, "Infer type", "Infer type", null, new ISemanticModification() {
@Override
public void apply(EObject element, IModificationContext context) throws Exception {
EStructuralFeature featureAfterType = null;
JvmIdentifiableElement jvmElement = null;
if (element instanceof XtendFunction) {
XtendFunction function = (XtendFunction) element;
if (function.getCreateExtensionInfo() == null) {
featureAfterType = XtendPackage.Literals.XTEND_FUNCTION__NAME;
} else {
featureAfterType = XtendPackage.Literals.XTEND_FUNCTION__CREATE_EXTENSION_INFO;
}
jvmElement = associations.getDirectlyInferredOperation((XtendFunction) element);
} else if (element instanceof XtendField) {
featureAfterType = XtendPackage.Literals.XTEND_FIELD__NAME;
jvmElement = associations.getJvmField((XtendField) element);
}
if (jvmElement != null) {
LightweightTypeReference type = batchTypeResolver.resolveTypes(element).getActualType(jvmElement);
INode node = Iterables.getFirst(NodeModelUtils.findNodesForFeature(element, featureAfterType), null);
if (node == null) {
throw new IllegalStateException("Could not determine node for " + element);
}
if (type == null) {
throw new IllegalStateException("Could not determine type for " + element);
}
ReplacingAppendable appendable = appendableFactory.create(context.getXtextDocument(), (XtextResource) element.eResource(), node.getOffset(), 0);
appendable.append(type);
appendable.append(" ");
appendable.commitChanges();
}
}
});
}
Aggregations