Search in sources :

Example 1 with ResponseError

use of org.eclipse.lsp4j.jsonrpc.messages.ResponseError in project xtext-core by eclipse.

the class ServerRefactoringIssueAcceptor method checkSeverity.

public void checkSeverity() {
    RefactoringIssueAcceptor.Severity _maximumSeverity = this.getMaximumSeverity();
    boolean _lessThan = (_maximumSeverity.compareTo(RefactoringIssueAcceptor.Severity.WARNING) < 0);
    if (_lessThan) {
        ResponseError _responseError = this.toResponseError();
        throw new ResponseErrorException(_responseError);
    }
}
Also used : RefactoringIssueAcceptor(org.eclipse.xtext.ide.refactoring.RefactoringIssueAcceptor) ResponseError(org.eclipse.lsp4j.jsonrpc.messages.ResponseError) ResponseErrorException(org.eclipse.lsp4j.jsonrpc.ResponseErrorException)

Example 2 with ResponseError

use of org.eclipse.lsp4j.jsonrpc.messages.ResponseError in project eclipse.jdt.ls by eclipse.

the class WorkspaceExecuteCommandHandler method executeCommand.

/**
 * Execute workspace command and invoke language server delegate command
 * handler for matching command
 *
 * @param params
 *            parameter from the protocol
 * @param monitor
 * @return execution result
 */
public Object executeCommand(ExecuteCommandParams params, IProgressMonitor monitor) {
    if (params == null || params.getCommand() == null) {
        String errorMessage = "The workspace/executeCommand has empty params or command";
        JavaLanguageServerPlugin.logError(errorMessage);
        throw new ResponseErrorException(new ResponseError(ResponseErrorCode.InvalidParams, errorMessage, null));
    }
    Set<DelegateCommandHandlerDescriptor> handlers = getDelegateCommandHandlerDescriptors();
    // no cancellation here but it's super fast so it's ok.
    Collection<DelegateCommandHandlerDescriptor> candidates = handlers.stream().filter(desc -> desc.getCommands().contains(params.getCommand())).collect(Collectors.toSet());
    if (candidates.size() > 1) {
        Exception ex = new IllegalStateException(String.format("Found multiple delegateCommandHandlers (%s) matching command %s", candidates, params.getCommand()));
        throw new ResponseErrorException(new ResponseError(ResponseErrorCode.InternalError, ex.getMessage(), ex));
    }
    if (monitor.isCanceled()) {
        return "";
    }
    if (candidates.isEmpty()) {
        throw new ResponseErrorException(new ResponseError(ResponseErrorCode.MethodNotFound, String.format("No delegateCommandHandler for %s", params.getCommand()), null));
    }
    final Object[] resultValues = new Object[1];
    SafeRunner.run(new ISafeRunnable() {

        @Override
        public void run() throws Exception {
            final IDelegateCommandHandler delegateCommandHandler = candidates.iterator().next().getDelegateCommandHandler();
            if (delegateCommandHandler != null) {
                // Convert args to java objects before sending to extensions
                List<Object> args = Collections.emptyList();
                if (params.getArguments() != null) {
                    args = params.getArguments().stream().map((element) -> {
                        return JSONUtility.toModel(element, Object.class);
                    }).collect(Collectors.toList());
                }
                resultValues[0] = delegateCommandHandler.executeCommand(params.getCommand(), args, monitor);
            }
        }

        @Override
        public void handleException(Throwable ex) {
            IStatus status = new Status(IStatus.ERROR, JavaLanguageServerPlugin.PLUGIN_ID, IStatus.OK, "Error in calling delegate command handler", ex);
            JavaLanguageServerPlugin.log(status);
            if (ex instanceof ResponseErrorException) {
                throw (ResponseErrorException) ex;
            }
            throw new ResponseErrorException(new ResponseError(ResponseErrorCode.UnknownErrorCode, ex.getMessage(), ex));
        }
    });
    return resultValues[0];
}
Also used : JSONUtility(org.eclipse.jdt.ls.core.internal.JSONUtility) Arrays(java.util.Arrays) SafeRunner(org.eclipse.core.runtime.SafeRunner) ISafeRunnable(org.eclipse.core.runtime.ISafeRunnable) ResponseError(org.eclipse.lsp4j.jsonrpc.messages.ResponseError) Collection(java.util.Collection) JavaLanguageServerPlugin(org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin) Set(java.util.Set) Status(org.eclipse.core.runtime.Status) CoreException(org.eclipse.core.runtime.CoreException) ResponseErrorException(org.eclipse.lsp4j.jsonrpc.ResponseErrorException) Collectors(java.util.stream.Collectors) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) HashSet(java.util.HashSet) List(java.util.List) IStatus(org.eclipse.core.runtime.IStatus) Stream(java.util.stream.Stream) IDelegateCommandHandler(org.eclipse.jdt.ls.core.internal.IDelegateCommandHandler) ExecuteCommandParams(org.eclipse.lsp4j.ExecuteCommandParams) IConfigurationElement(org.eclipse.core.runtime.IConfigurationElement) Platform(org.eclipse.core.runtime.Platform) ResponseErrorCode(org.eclipse.lsp4j.jsonrpc.messages.ResponseErrorCode) Collections(java.util.Collections) Status(org.eclipse.core.runtime.Status) IStatus(org.eclipse.core.runtime.IStatus) IStatus(org.eclipse.core.runtime.IStatus) IDelegateCommandHandler(org.eclipse.jdt.ls.core.internal.IDelegateCommandHandler) ResponseError(org.eclipse.lsp4j.jsonrpc.messages.ResponseError) CoreException(org.eclipse.core.runtime.CoreException) ResponseErrorException(org.eclipse.lsp4j.jsonrpc.ResponseErrorException) ResponseErrorException(org.eclipse.lsp4j.jsonrpc.ResponseErrorException) ISafeRunnable(org.eclipse.core.runtime.ISafeRunnable) List(java.util.List)

Example 3 with ResponseError

use of org.eclipse.lsp4j.jsonrpc.messages.ResponseError in project xtext-core by eclipse.

the class ServerRefactoringIssueAcceptor method toResponseError.

public ResponseError toResponseError() {
    ResponseError _xblockexpression = null;
    {
        final RefactoringIssueAcceptor.Severity maxSeverity = this.getMaximumSeverity();
        ResponseError _responseError = new ResponseError();
        final Procedure1<ResponseError> _function = (ResponseError it) -> {
            String _switchResult = null;
            if (maxSeverity != null) {
                switch(maxSeverity) {
                    case OK:
                        _switchResult = "Refactoring is possible";
                        break;
                    case INFO:
                        _switchResult = "Refactoring is possible";
                        break;
                    case WARNING:
                        _switchResult = "Refactoring could cause issues";
                        break;
                    case ERROR:
                        _switchResult = "Refactoring has errors";
                        break;
                    case FATAL:
                        _switchResult = "Refactoring cannot be performed";
                        break;
                    default:
                        break;
                }
            }
            it.setMessage(_switchResult);
            final Function1<ServerRefactoringIssueAcceptor.Issue, RefactoringIssueAcceptor.Severity> _function_1 = (ServerRefactoringIssueAcceptor.Issue it_1) -> {
                return it_1.severity;
            };
            final Function1<ServerRefactoringIssueAcceptor.Issue, String> _function_2 = (ServerRefactoringIssueAcceptor.Issue it_1) -> {
                return it_1.message;
            };
            it.setData(IterableExtensions.join(ListExtensions.<ServerRefactoringIssueAcceptor.Issue, String>map(ListExtensions.<ServerRefactoringIssueAcceptor.Issue>reverse(IterableExtensions.<ServerRefactoringIssueAcceptor.Issue, RefactoringIssueAcceptor.Severity>sortBy(this.issues, _function_1)), _function_2), "\n"));
            int _switchResult_1 = (int) 0;
            if (maxSeverity != null) {
                switch(maxSeverity) {
                    case OK:
                        _switchResult_1 = 0;
                        break;
                    case INFO:
                        _switchResult_1 = 0;
                        break;
                    case WARNING:
                        _switchResult_1 = 0;
                        break;
                    case ERROR:
                        _switchResult_1 = ResponseErrorCode.UnknownErrorCode.getValue();
                        break;
                    case FATAL:
                        _switchResult_1 = ResponseErrorCode.UnknownErrorCode.getValue();
                        break;
                    default:
                        break;
                }
            }
            it.setCode(_switchResult_1);
        };
        _xblockexpression = ObjectExtensions.<ResponseError>operator_doubleArrow(_responseError, _function);
    }
    return _xblockexpression;
}
Also used : ResponseError(org.eclipse.lsp4j.jsonrpc.messages.ResponseError) Procedure1(org.eclipse.xtext.xbase.lib.Procedures.Procedure1)

Example 4 with ResponseError

use of org.eclipse.lsp4j.jsonrpc.messages.ResponseError in project ballerina by ballerina-lang.

the class CommonUtil method getLanguageServerResponseMessageAsString.

/**
 * Get the definition response message as a string.
 *
 * @param position    hovering position to get the definition.
 * @param file        bal file path
 * @param fileContent bal file content
 * @param method      string name of the language feature method
 * @return json string value of the response
 */
public static String getLanguageServerResponseMessageAsString(Position position, String file, String fileContent, String method) throws InterruptedException {
    Gson gson = new Gson();
    BallerinaLanguageServer ballerinaLanguageServer = new BallerinaLanguageServer();
    Endpoint serviceEndpoint = ServiceEndpoints.toEndpoint(ballerinaLanguageServer);
    TextDocumentPositionParams positionParams = new TextDocumentPositionParams();
    TextDocumentIdentifier identifier = new TextDocumentIdentifier();
    identifier.setUri(Paths.get(file).toUri().toString());
    positionParams.setTextDocument(identifier);
    positionParams.setPosition(position);
    DidOpenTextDocumentParams documentParams = new DidOpenTextDocumentParams();
    TextDocumentItem textDocumentItem = new TextDocumentItem();
    textDocumentItem.setUri(identifier.getUri());
    textDocumentItem.setText(fileContent);
    documentParams.setTextDocument(textDocumentItem);
    serviceEndpoint.notify("textDocument/didOpen", documentParams);
    CompletableFuture result = serviceEndpoint.request(method, positionParams);
    ResponseMessage jsonrpcResponse = new ResponseMessage();
    try {
        jsonrpcResponse.setId("324");
        jsonrpcResponse.setResult(result.get());
    } catch (InterruptedException e) {
        ResponseError responseError = new ResponseError();
        responseError.setCode(-32002);
        responseError.setMessage("Attempted to retrieve the result of a task/s" + "that was aborted by throwing an exception");
        jsonrpcResponse.setError(responseError);
    } catch (ExecutionException e) {
        ResponseError responseError = new ResponseError();
        responseError.setCode(-32001);
        responseError.setMessage("Current thread was interrupted");
        jsonrpcResponse.setError(responseError);
    }
    return gson.toJson(jsonrpcResponse);
}
Also used : TextDocumentItem(org.eclipse.lsp4j.TextDocumentItem) BallerinaLanguageServer(org.ballerinalang.langserver.BallerinaLanguageServer) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) CompletableFuture(java.util.concurrent.CompletableFuture) Endpoint(org.eclipse.lsp4j.jsonrpc.Endpoint) DidOpenTextDocumentParams(org.eclipse.lsp4j.DidOpenTextDocumentParams) ResponseError(org.eclipse.lsp4j.jsonrpc.messages.ResponseError) Gson(com.google.gson.Gson) TextDocumentPositionParams(org.eclipse.lsp4j.TextDocumentPositionParams) ResponseMessage(org.eclipse.lsp4j.jsonrpc.messages.ResponseMessage) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

ResponseError (org.eclipse.lsp4j.jsonrpc.messages.ResponseError)4 ResponseErrorException (org.eclipse.lsp4j.jsonrpc.ResponseErrorException)2 Gson (com.google.gson.Gson)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Set (java.util.Set)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 ExecutionException (java.util.concurrent.ExecutionException)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 BallerinaLanguageServer (org.ballerinalang.langserver.BallerinaLanguageServer)1 CoreException (org.eclipse.core.runtime.CoreException)1 IConfigurationElement (org.eclipse.core.runtime.IConfigurationElement)1 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)1 ISafeRunnable (org.eclipse.core.runtime.ISafeRunnable)1 IStatus (org.eclipse.core.runtime.IStatus)1 Platform (org.eclipse.core.runtime.Platform)1