Search in sources :

Example 16 with PrepareRenameParams

use of org.eclipse.lsp4j.PrepareRenameParams in project xtext-core by eclipse.

the class RenameService2 method prepareRename.

@Override
public Either<Range, PrepareRenameResult> prepareRename(IRenameService2.PrepareRenameOptions options) {
    try {
        String uri = options.getParams().getTextDocument().getUri();
        boolean shouldPrepareRename = shouldPrepareRename(options.getLanguageServerAccess());
        return options.getLanguageServerAccess().doRead(uri, (ILanguageServerAccess.Context context) -> {
            if (!shouldPrepareRename) {
                return null;
            }
            Resource resource = context.getResource();
            Document document = context.getDocument();
            PrepareRenameParams params = options.getParams();
            CancelIndicator cancelIndicator = options.getCancelIndicator();
            return doPrepareRename(resource, document, params, cancelIndicator);
        }).exceptionally((Throwable exception) -> {
            try {
                Throwable rootCause = Throwables.getRootCause(exception);
                if (rootCause instanceof FileNotFoundException) {
                    if (shouldPrepareRename) {
                        return null;
                    }
                }
                throw exception;
            } catch (Throwable e) {
                throw Exceptions.sneakyThrow(e);
            }
        }).get();
    } catch (InterruptedException | ExecutionException e) {
        throw Exceptions.sneakyThrow(e);
    }
}
Also used : RenameContext(org.eclipse.xtext.ide.refactoring.RenameContext) Resource(org.eclipse.emf.ecore.resource.Resource) XtextResource(org.eclipse.xtext.resource.XtextResource) FileNotFoundException(java.io.FileNotFoundException) CancelIndicator(org.eclipse.xtext.util.CancelIndicator) Document(org.eclipse.xtext.ide.server.Document) PrepareRenameParams(org.eclipse.lsp4j.PrepareRenameParams) ExecutionException(java.util.concurrent.ExecutionException)

Example 17 with PrepareRenameParams

use of org.eclipse.lsp4j.PrepareRenameParams in project xtext-core by eclipse.

the class RenameService2 method rename.

@Override
public WorkspaceEdit rename(IRenameService2.Options options) {
    try {
        TextDocumentIdentifier textDocument = options.getRenameParams().getTextDocument();
        String uri = textDocument.getUri();
        ServerRefactoringIssueAcceptor issueAcceptor = issueProvider.get();
        boolean shouldPrepareRename = shouldPrepareRename(options.getLanguageServerAccess());
        return options.getLanguageServerAccess().doRead(uri, (ILanguageServerAccess.Context context) -> {
            if (shouldPrepareRename) {
                TextDocumentIdentifier identifier = new TextDocumentIdentifier(textDocument.getUri());
                Position position = options.getRenameParams().getPosition();
                PrepareRenameParams positionParams = new PrepareRenameParams(identifier, position);
                Resource resource = context.getResource();
                Document document = context.getDocument();
                CancelIndicator cancelIndicator = options.getCancelIndicator();
                Either<Range, PrepareRenameResult> prepareRenameResult = doPrepareRename(resource, document, positionParams, cancelIndicator);
                if (!mayPerformRename(prepareRenameResult, options.getRenameParams())) {
                    return null;
                }
            }
            WorkspaceEdit workspaceEdit = new WorkspaceEdit();
            ResourceSet resourceSet = options.getLanguageServerAccess().newLiveScopeResourceSet(context.getResource().getURI());
            Resource xtextResource = resourceSet.getResource(context.getResource().getURI(), true);
            if (xtextResource instanceof XtextResource) {
                Position position = options.getRenameParams().getPosition();
                EObject element = null;
                try {
                    element = getElementAtOffset((XtextResource) xtextResource, context.getDocument(), position);
                } catch (IndexOutOfBoundsException e) {
                    issueAcceptor.add(RefactoringIssueAcceptor.Severity.FATAL, "Invalid document " + toPositionFragment(position, uri));
                }
                if (element == null || element.eIsProxy()) {
                    issueAcceptor.add(RefactoringIssueAcceptor.Severity.FATAL, "No element found at " + toPositionFragment(position, uri));
                } else {
                    applyModifications(element, workspaceEdit, issueAcceptor, options, context);
                }
            } else {
                issueAcceptor.add(RefactoringIssueAcceptor.Severity.FATAL, "Loaded resource is not an XtextResource", context.getResource().getURI());
            }
            issueAcceptor.checkSeverity();
            return workspaceEdit;
        }).exceptionally((Throwable exception) -> {
            try {
                Throwable rootCause = Throwables.getRootCause(exception);
                if (rootCause instanceof FileNotFoundException) {
                    if (shouldPrepareRename) {
                        return null;
                    }
                }
                throw exception;
            } catch (Throwable e) {
                throw Exceptions.sneakyThrow(e);
            }
        }).get();
    } catch (InterruptedException | ExecutionException e) {
        throw Exceptions.sneakyThrow(e);
    }
}
Also used : RenameContext(org.eclipse.xtext.ide.refactoring.RenameContext) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) Position(org.eclipse.lsp4j.Position) Resource(org.eclipse.emf.ecore.resource.Resource) XtextResource(org.eclipse.xtext.resource.XtextResource) FileNotFoundException(java.io.FileNotFoundException) WorkspaceEdit(org.eclipse.lsp4j.WorkspaceEdit) XtextResource(org.eclipse.xtext.resource.XtextResource) PrepareRenameResult(org.eclipse.lsp4j.PrepareRenameResult) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) PrepareRenameParams(org.eclipse.lsp4j.PrepareRenameParams) Document(org.eclipse.xtext.ide.server.Document) Range(org.eclipse.lsp4j.Range) EObject(org.eclipse.emf.ecore.EObject) CancelIndicator(org.eclipse.xtext.util.CancelIndicator) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

PrepareRenameParams (org.eclipse.lsp4j.PrepareRenameParams)17 TextDocumentIdentifier (org.eclipse.lsp4j.TextDocumentIdentifier)16 Position (org.eclipse.lsp4j.Position)14 Test (org.junit.Test)13 Range (org.eclipse.lsp4j.Range)12 Document (org.eclipse.xtext.ide.server.Document)12 RenameParams (org.eclipse.lsp4j.RenameParams)8 WorkspaceEdit (org.eclipse.lsp4j.WorkspaceEdit)8 AbstractLanguageServerTest (org.eclipse.xtext.testing.AbstractLanguageServerTest)6 FileNotFoundException (java.io.FileNotFoundException)3 File (java.io.File)2 ExecutionException (java.util.concurrent.ExecutionException)2 Resource (org.eclipse.emf.ecore.resource.Resource)2 PrepareRenameResult (org.eclipse.lsp4j.PrepareRenameResult)2 ResponseErrorException (org.eclipse.lsp4j.jsonrpc.ResponseErrorException)2 RenameContext (org.eclipse.xtext.ide.refactoring.RenameContext)2 XtextResource (org.eclipse.xtext.resource.XtextResource)2 CancelIndicator (org.eclipse.xtext.util.CancelIndicator)2 EObject (org.eclipse.emf.ecore.EObject)1 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)1