Search in sources :

Example 1 with IDelegateCommandHandler

use of org.eclipse.jdt.ls.core.internal.IDelegateCommandHandler 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)

Aggregations

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 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)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 SafeRunner (org.eclipse.core.runtime.SafeRunner)1 Status (org.eclipse.core.runtime.Status)1 IDelegateCommandHandler (org.eclipse.jdt.ls.core.internal.IDelegateCommandHandler)1 JSONUtility (org.eclipse.jdt.ls.core.internal.JSONUtility)1 JavaLanguageServerPlugin (org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin)1 ExecuteCommandParams (org.eclipse.lsp4j.ExecuteCommandParams)1