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

com.github.cameltooling.lsp.internal.CamelLanguageServer Maven / Gradle / Ivy

There is a newer version: 1.28.0
Show newest version
/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 com.github.cameltooling.lsp.internal;

import java.util.Arrays;
import java.util.concurrent.CompletableFuture;

import org.eclipse.lsp4j.CompletionOptions;
import org.eclipse.lsp4j.InitializeParams;
import org.eclipse.lsp4j.InitializeResult;
import org.eclipse.lsp4j.MessageParams;
import org.eclipse.lsp4j.MessageType;
import org.eclipse.lsp4j.ServerCapabilities;
import org.eclipse.lsp4j.TextDocumentSyncKind;
import org.eclipse.lsp4j.services.LanguageClient;
import org.eclipse.lsp4j.services.LanguageClientAware;
import org.eclipse.lsp4j.services.LanguageServer;
import org.eclipse.lsp4j.services.WorkspaceService;

/**
 * this is the actual server implementation
 * 
 * @author lhein
 */
public class CamelLanguageServer extends AbstractLanguageServer implements LanguageServer, LanguageClientAware {
	
	public static final String LANGUAGE_ID = "LANGUAGE_ID_APACHE_CAMEL";
	
	private LanguageClient client;
	
	public CamelLanguageServer() {
		super.setTextDocumentService(new CamelTextDocumentService());
		super.setWorkspaceService(new CamelWorkspaceService());
	}
	
	@Override
	public void connect(LanguageClient client) {
		this.client = client;
		sendLogMessageNotification(MessageType.Info, "Connected to Language Server...");
	}
	
	@Override
	public void exit() {
		super.stopServer();
		System.exit(0);
	}
	
	@Override
	public CompletableFuture initialize(InitializeParams params) {
		sendLogMessageNotification(MessageType.Info, "Initializing capabilities of the server...");
		Integer processId = params.getProcessId();
		if(processId != null) {
			setParentProcessId(processId.longValue());
		} else {
			sendLogMessageNotification(MessageType.Info, "Missing Parent process ID!!");
			setParentProcessId(0);
		}
		
		ServerCapabilities capabilities = createServerCapabilities();
		InitializeResult result = new InitializeResult(capabilities);
		return CompletableFuture.completedFuture(result);
	}

	private ServerCapabilities createServerCapabilities() {
		ServerCapabilities capabilities = new ServerCapabilities();
		capabilities.setTextDocumentSync(TextDocumentSyncKind.Full);
		capabilities.setCompletionProvider(new CompletionOptions(Boolean.TRUE, Arrays.asList(".","?","&", "\"", "=")));
		capabilities.setHoverProvider(Boolean.TRUE);
		capabilities.setDocumentSymbolProvider(Boolean.TRUE);
		return capabilities;
	}

	@Override
	public CompletableFuture shutdown() {
		super.shutdownServer();
		return CompletableFuture.completedFuture(new Object());
	}
	
	@Override
	public WorkspaceService getWorkspaceService() {
		return super.getWorkspaceService();
	}
	
	/**
	 * Sends the given log message notification back to the client
	 * as a notification
	 * 
	 * @param type
	 *            the type of message
	 * @param msg
	 *            The message to send back to the client
	 */
	public void sendLogMessageNotification(final MessageType type, final String msg) {
		client.logMessage(new MessageParams(type, msg));
	}

	/**
	 * Sends the given show message notification back to the client
	 * as a notification
	 * 
	 * @param type
	 *            the type of message
	 * @param msg
	 *            The message to send back to the client
	 */
	public void sendShowMessageNotification(final MessageType type, final String msg) {
		client.showMessage(new MessageParams(type, msg));
	}
}