All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.contextmapper.dsl.ide.commands.CMLCommandService Maven / Gradle / Ivy

Go to download

Use the ContextMapper language server (LSP) to integrate the ContextMapper DSL within your IDE.

The newest version!
/**
 * Copyright 2020 The Context Mapper Project Team
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *    http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.contextmapper.dsl.ide.commands;

import com.google.common.collect.Lists;
import com.google.gson.JsonPrimitive;
import com.google.inject.Inject;
import java.util.List;
import java.util.function.Function;
import org.apache.log4j.Logger;
import org.contextmapper.dsl.cml.CMLResource;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.lsp4j.ExecuteCommandParams;
import org.eclipse.xtext.ide.server.ILanguageServerAccess;
import org.eclipse.xtext.ide.server.commands.IExecutableCommandService;
import org.eclipse.xtext.util.CancelIndicator;
import org.eclipse.xtext.xbase.lib.Exceptions;
import org.eclipse.xtext.xbase.lib.IterableExtensions;

@SuppressWarnings("all")
public class CMLCommandService implements IExecutableCommandService {
  private static final Logger LOG = Logger.getLogger(CMLCommandService.class);

  public static final String COMMAND_EXECUTED_RETURN_VALUE = "Command executed.";

  public static final String COMMAND_EXECUTION_ERROR_PREFIX = "Error occurred:";

  @Inject
  private CMLCommandRegistry commandRegistry;

  @Override
  public List initialize() {
    return Lists.newLinkedList(this.commandRegistry.getCommandIds());
  }

  @Override
  public Object execute(final ExecuteCommandParams params, final ILanguageServerAccess access, final CancelIndicator cancelIndicator) {
    try {
      final CMLResourceCommand command = this.commandRegistry.getCommand(params.getCommand());
      if ((command == null)) {
        return "Unknown command ID!";
      }
      Object _head = IterableExtensions.head(params.getArguments());
      final JsonPrimitive uri = ((JsonPrimitive) _head);
      if ((uri != null)) {
        final Function _function = (ILanguageServerAccess.Context it) -> {
          String _command = params.getCommand();
          String _plus = ("CML LSP command has been called: " + _command);
          String _plus_1 = (_plus + " (");
          Object _get = params.getArguments().get(0);
          String _plus_2 = (_plus_1 + _get);
          String _plus_3 = (_plus_2 + 
            ")");
          CMLCommandService.LOG.info(_plus_3);
          try {
            Resource _resource = it.getResource();
            CMLResource _cMLResource = new CMLResource(_resource);
            command.executeCommand(_cMLResource, it.getDocument(), access, params);
          } catch (final Throwable _t) {
            if (_t instanceof Exception) {
              final Exception e = (Exception)_t;
              CMLCommandService.LOG.error((("The command \'" + command) + "\' resulted in an error"), e);
              e.printStackTrace(System.out);
              String _message = e.getMessage();
              return ((CMLCommandService.COMMAND_EXECUTION_ERROR_PREFIX + " ") + _message);
            } else {
              throw Exceptions.sneakyThrow(_t);
            }
          }
          return CMLCommandService.COMMAND_EXECUTED_RETURN_VALUE;
        };
        return access.doRead(uri.getAsString(), _function).get();
      } else {
        return (CMLCommandService.COMMAND_EXECUTION_ERROR_PREFIX + " No resource URI found in command arguments!");
      }
    } catch (Throwable _e) {
      throw Exceptions.sneakyThrow(_e);
    }
  }
}