use of org.eclipse.n4js.xtext.ide.server.XDocument in project n4js by eclipse.
the class N4JSRenameService method rename.
// #################################################################################################################
// rename
//
// In case of #rename() we entirely replace the implementation from the super class by our own implementation (in
// order to reuse reference finding as far as possible, for consistency):
@Override
public WorkspaceEdit rename(Options options) {
RenameParams renameParams = options.getRenameParams();
CancelIndicator cancelIndicator = options.getCancelIndicator();
ResourceTaskContext rtc = resourceTaskManager.currentContext();
if (rtc == null || !rtc.isTemporary()) {
throw new IllegalStateException("N4JSRenameService#rename expects to be invoked within a temporary resource task context");
}
ResourceSet resourceSet = rtc.getResourceSet();
XtextResource resource = rtc.getResource();
XDocument document = rtc.getDocument();
int offset = document.getOffSet(renameParams.getPosition());
// in case the temporary resource task context we are using was created without 'resolveAndValidate' we need to
// trigger resolution (not validation) here:
EcoreUtil2.resolveLazyCrossReferences(resource, cancelIndicator);
EObject element = getElementToBeRenamed(resource, offset);
if (element == null) {
// empty edit
return new WorkspaceEdit();
}
WorkspaceEdit result = computeRenameEdits(resourceSet, element, renameParams.getNewName(), resourceTaskManager.createLiveScopeIndex(), cancelIndicator);
return result;
}
use of org.eclipse.n4js.xtext.ide.server.XDocument in project n4js by eclipse.
the class AbstractIdeTest method applyTextEdits.
/**
* Applies the given text edits to the given character sequence and returns the resulting string.
*/
protected static String applyTextEdits(CharSequence oldContent, Iterable<? extends TextEdit> textEdits) {
XDocument oldDocument = new XDocument(0, oldContent.toString(), true, true);
XDocument newDocument = oldDocument.applyChanges(textEdits);
return newDocument.getContents();
}
use of org.eclipse.n4js.xtext.ide.server.XDocument in project n4js by eclipse.
the class N4JSCodeActionService method doApplyToFile.
/**
* Applies given quick fix to file with given URI and waits for and returns the resulting edits.
*/
protected Map<String, List<TextEdit>> doApplyToFile(URI uri, String issueCode, QuickFixImplementation quickfix, CancelIndicator cancelIndicator) {
TextEditCollector collector = new TextEditCollector();
TextDocumentIdentifier textDocId = new TextDocumentIdentifier(uriExtensions.toUriString(uri));
ResourceTaskManager resourceTaskManager = languageServer.getResourceTaskManager();
resourceTaskManager.<Void>runInTemporaryContext(uri, "doApplyToFile", false, cancelIndicator, (ofc, ci) -> {
XtextResource res = ofc.getResource();
List<? extends Issue> issues = ofc.resolveAndValidateResource(ci);
XDocument doc = ofc.getDocument();
for (Issue issue : issues) {
if (issueCode.equals(issue.getCode())) {
Options newOptions = createOptions(res, doc, textDocId, issue, ci);
quickfix.compute(issueCode, newOptions, collector);
}
}
return null;
}).join();
return collector.allEdits;
}
use of org.eclipse.n4js.xtext.ide.server.XDocument in project n4js by eclipse.
the class AbstractCompletionTest method applyCompletionItem.
private String applyCompletionItem(String oldContent, Position pos, CompletionItem item) {
String content = oldContent;
TextEdit mainEdit = item.getTextEdit().getLeft();
if (mainEdit != null) {
content = applyTextEdits(content, Collections.singleton(mainEdit));
} else {
String toInsert = item.getInsertText() != null ? item.getInsertText() : item.getLabel();
if (toInsert != null) {
XDocument doc = new XDocument(0, content.toString(), true, true);
doc.applyChanges(Collections.singleton(new TextEdit(new Range(pos, pos), toInsert)));
content = doc.getContents();
}
}
List<TextEdit> additionalEdits = item.getAdditionalTextEdits();
if (additionalEdits != null && !additionalEdits.isEmpty()) {
content = applyTextEdits(content, additionalEdits);
}
return content;
}
Aggregations