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);
}
}
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);
}
}
Aggregations