Search in sources :

Example 1 with ResourceTaskContext

use of org.eclipse.n4js.xtext.ide.server.ResourceTaskContext 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;
}
Also used : RenameParams(org.eclipse.lsp4j.RenameParams) EObject(org.eclipse.emf.ecore.EObject) ResourceTaskContext(org.eclipse.n4js.xtext.ide.server.ResourceTaskContext) XtextResource(org.eclipse.xtext.resource.XtextResource) WorkspaceEdit(org.eclipse.lsp4j.WorkspaceEdit) CancelIndicator(org.eclipse.xtext.util.CancelIndicator) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) XDocument(org.eclipse.n4js.xtext.ide.server.XDocument)

Example 2 with ResourceTaskContext

use of org.eclipse.n4js.xtext.ide.server.ResourceTaskContext in project n4js by eclipse.

the class XWorkspaceResourceAccess method readOnly.

@Override
public <R> R readOnly(URI targetURI, IUnitOfWork<R, ResourceSet> work) {
    // note: targetURI may point to an EObject inside an EMF resource!
    URI uri = targetURI.trimFragment();
    ResourceTaskContext currRTC = resourceTaskManager.currentContext();
    if (currRTC != null) {
        return doWork(currRTC.getResourceSet(), work, CancelIndicator.NullImpl);
    }
    // TODO consider making a current context mandatory by removing the following (see GH-1774):
    CompletableFuture<R> future = resourceTaskManager.runInTemporaryContext(uri, "XWorkspaceResourceAccess", resolveAndValidate, (ofc, ci) -> doWork(ofc.getResourceSet(), work, ci));
    return FutureUtil.getCancellableResult(future);
}
Also used : ResourceTaskContext(org.eclipse.n4js.xtext.ide.server.ResourceTaskContext) URI(org.eclipse.emf.common.util.URI)

Aggregations

ResourceTaskContext (org.eclipse.n4js.xtext.ide.server.ResourceTaskContext)2 URI (org.eclipse.emf.common.util.URI)1 EObject (org.eclipse.emf.ecore.EObject)1 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)1 RenameParams (org.eclipse.lsp4j.RenameParams)1 WorkspaceEdit (org.eclipse.lsp4j.WorkspaceEdit)1 XDocument (org.eclipse.n4js.xtext.ide.server.XDocument)1 XtextResource (org.eclipse.xtext.resource.XtextResource)1 CancelIndicator (org.eclipse.xtext.util.CancelIndicator)1