I use the code mentioned below to implement a refactoring for my Xtext DSL.
public WorkspaceEdit executeCommand(Resource resource, Document document, ILanguageServerAccess access,
ExecuteCommandParams params) {
WorkspaceEdit edit = createWorkspaceEdit(access, resource.getURI(), document, (Resource copiedResource) -> {
StatementModel model = (StatementModel) copiedResource.getAllContents().next();
DecisionTable decisionTable = (DecisionTable) model.getStatement();
InputClause inputClause = ExpressionFactory.eINSTANCE.createInputClause();
decisionTable.getInput().add(inputClause);
PrimitiveValueExpression primitiveValueExpression = ExpressionFactory.eINSTANCE.createPrimitiveValueExpression();
IntLiteral intLiteral = ExpressionFactory.eINSTANCE.createIntLiteral();
intLiteral.setValue(1);
ExpressionWrapper expressionWrapper = ExpressionFactory.eINSTANCE.createExpressionWrapper();
expressionWrapper.setExpression(primitiveValueExpression);
inputClause.setExpressionWrapper(expressionWrapper);
Shape shape1 = MnotationFactory.eINSTANCE.createShape();
inputClause.setDiagramElement(shape1);
});
access.getLanguageClient().applyEdit(new ApplyWorkspaceEditParams(edit, "Remove Xtext! Greeting"));
return edit;
}
private WorkspaceEdit createWorkspaceEdit(ILanguageServerAccess access, URI resourceURI, Document document,
IChangeSerializer.IModification<Resource> mod) {
ResourceSet rs = access.newLiveScopeResourceSet(resourceURI);
Resource copy = rs.getResource(resourceURI, true);
IChangeSerializer serializer = serializerProvider.get();
EcoreUtil.resolveAll(copy);
serializer.addModification(copy, mod);
List<IEmfResourceChange> documentchanges = new ArrayList<>();
serializer.applyModifications(CollectionBasedAcceptor.of(documentchanges));
WorkspaceEdit workspaceEdit = new WorkspaceEdit();
for (ITextDocumentChange documentchange : Iterables.filter(documentchanges, ITextDocumentChange.class)) {
List<TextEdit> edits = ListExtensions.map(documentchange.getReplacements(),
(ITextReplacement replacement) -> {
TextEdit textEdit = new TextEdit();
textEdit.setNewText(replacement.getReplacementText());
textEdit.setRange(new Range(document.getPosition(replacement.getOffset()),
document.getPosition(replacement.getEndOffset())));
return textEdit;
});
workspaceEdit.getChanges().put(documentchange.getNewURI().toString(), edits);
}
return workspaceEdit;
}
It results the Change in my Xtext DSL but leaves the files open & unsaved in theia-editor.
How can I fix this issue to refactor files without opening them on editor?