public static void main(final String[] args) { try { ServerModule _serverModule = new ServerModule(); final Injector injector = Guice.createInjector(_serverModule); final LanguageServer languageServer = injector.<LanguageServer>getInstance(LanguageServer.class); final ServerSocketChannel serverSocket = ServerSocketChannel.open(); InetSocketAddress _inetSocketAddress = new InetSocketAddress("localhost", 5007); serverSocket.bind(_inetSocketAddress); final SocketChannel socketChannel = serverSocket.accept(); InputStream _newInputStream = Channels.newInputStream(socketChannel); OutputStream _newOutputStream = Channels.newOutputStream(socketChannel); PrintWriter _printWriter = new PrintWriter(System.out); final Launcher<LanguageClient> launcher = LSPLauncher.createServerLauncher(languageServer, _newInputStream, _newOutputStream, true, _printWriter); launcher.startListening().get(); } catch (Throwable _e) { throw Exceptions.sneakyThrow(_e); } }
@Before public void setup() throws IOException { PipedInputStream inClient = new PipedInputStream(); PipedOutputStream outClient = new PipedOutputStream(); PipedInputStream inServer = new PipedInputStream(); PipedOutputStream outServer = new PipedOutputStream(); inClient.connect(outServer); outClient.connect(inServer); server = new AssertingEndpoint(); serverLauncher = LSPLauncher.createServerLauncher(ServiceEndpoints.toServiceObject(server, LanguageServer.class), inServer, outServer); serverListening = serverLauncher.startListening(); client = new AssertingEndpoint(); clientLauncher = LSPLauncher.createClientLauncher(ServiceEndpoints.toServiceObject(client, LanguageClient.class), inClient, outClient); clientListening = clientLauncher.startListening(); }
@Test public void testNullResponse() throws InterruptedException, ExecutionException { Endpoint endpoint = ServiceEndpoints.toEndpoint(this); Map<String, JsonRpcMethod> methods = ServiceEndpoints.getSupportedMethods(LanguageServer.class); MessageJsonHandler handler = new MessageJsonHandler(methods); List<Message> msgs = new ArrayList<>(); MessageConsumer consumer = (message) -> { msgs.add(message); }; RemoteEndpoint re = new RemoteEndpoint(consumer, endpoint); RequestMessage request = new RequestMessage(); request.setId("1"); request.setMethod("shutdown"); re.consume(request); Assert.assertEquals("{\"jsonrpc\":\"2.0\",\"id\":\"1\",\"result\":null}", handler.serialize(msgs.get(0))); msgs.clear(); shutdownReturn = new Object(); re.consume(request); Assert.assertEquals("{\"jsonrpc\":\"2.0\",\"id\":\"1\",\"result\":{}}", handler.serialize(msgs.get(0))); }
@Test public void testDocumentResolve() throws Exception, ExecutionException { LanguageServer languageServer = wrap(LanguageServer.class, new MockLanguageServer() { @Override public CompletableFuture<DocumentLink> documentLinkResolve(DocumentLink params) { return CompletableFutures.computeAsync(canceler -> { params.setTarget("resolved"); return params; }); } }); CompletableFuture<DocumentLink> future = languageServer.getTextDocumentService().documentLinkResolve( new DocumentLink(new Range(new Position(0, 0), new Position(0, 0)), "unresolved") ); DocumentLink resolved = future.get(TIMEOUT, TimeUnit.MILLISECONDS); Assert.assertEquals("resolved", resolved.getTarget()); }
@Override public LanguageServer launch(String projectPath, LanguageClient client) throws LanguageServerException { try { Socket socket = new Socket(host, port); socket.setKeepAlive(true); InputStream inputStream = socket.getInputStream(); OutputStream outputStream = socket.getOutputStream(); Launcher<LanguageServer> launcher = Launcher.createLauncher(client, LanguageServer.class, inputStream, outputStream); launcher.startListening(); return launcher.getRemoteProxy(); } catch (IOException e) { throw new LanguageServerException( "Can't launch language server for project: " + projectPath, e); } }
private void onServerInitialized( LanguageServerLauncher launcher, LanguageServer server, ServerCapabilities capabilities, String projectPath) { LanguageServerDescription description = launcher.getDescription(); FileSystem fileSystem = FileSystems.getDefault(); for (String pattern : description.getFileWatchPatterns()) { PathMatcher matcher = fileSystem.getPathMatcher(pattern); int watcherId = watcherManager.registerByMatcher( matcher, s -> send(server, s, FileChangeType.Created), s -> send(server, s, FileChangeType.Changed), s -> send(server, s, FileChangeType.Deleted)); watcherIds.add(watcherId); } }
@PreDestroy protected void shutdown() { List<LanguageServer> allServers; synchronized (initializedServers) { allServers = initializedServers .values() .stream() .flatMap(l -> l.stream()) .map(s -> s.getServer()) .collect(Collectors.toList()); } for (LanguageServer server : allServers) { server.shutdown(); server.exit(); } }
@Test public void testName() throws Exception { CompletableFuture<MessageActionItem> future = new CompletableFuture<>(); when(transmitter.sendShowMessageRequest(any())).thenReturn(future); ServerInitializerImpl initializer = new ServerInitializerImpl(); CheLanguageClient client = new CheLanguageClient(eventService, transmitter, "id"); CompletableFuture<Pair<LanguageServer, InitializeResult>> initialize = initializer.initialize(launcher, client, "/tmp"); Pair<LanguageServer, InitializeResult> resultPair = initialize.get(); server = resultPair.first; ArgumentCaptor<ShowMessageRequestParams> captor = ArgumentCaptor.forClass(ShowMessageRequestParams.class); verify(transmitter, timeout(1500)).sendShowMessageRequest(captor.capture()); ShowMessageRequestParams value = captor.getValue(); assertNotNull(value); assertEquals(value.getType(), MessageType.Error); assertEquals(value.getMessage(), "Error Message!!!!"); }
@Test public void initializerShouldNotifyObservers() throws Exception { when(languageDescription.getLanguageId()).thenReturn("languageId"); when(server.initialize(any(InitializeParams.class))).thenReturn(completableFuture); when(launcher.launch(anyString(), any())).thenReturn(server); when(launcher.getDescription()).thenReturn(serverDescription); when(serverDescription.getId()).thenReturn("launcherId"); doNothing().when(initializer).registerCallbacks(any(), any()); initializer.addObserver(observer); Pair<LanguageServer, InitializeResult> initResult = initializer.initialize(launcher, null, "/path").get(); assertEquals(server, initResult.first); verify(observer, timeout(2000)) .onServerInitialized(eq(launcher), eq(server), any(ServerCapabilities.class), eq("/path")); }
@Override public void onServerInitialized( LanguageServerLauncher launcher, LanguageServer server, ServerCapabilities capabilities, String projectPath) { try { Map<String, String> preferences = requestFactory.fromUrl(apiUrl + "/preferences").useGetMethod().request().asProperties(); Endpoint endpoint = ServiceEndpoints.toEndpoint(server); YamlSchemaAssociations serviceObject = ServiceEndpoints.toServiceObject(endpoint, YamlSchemaAssociations.class); Map<String, String[]> associations = jsonToSchemaAssociations(preferences.get("yaml.preferences")); serviceObject.yamlSchemaAssociation(associations); } catch (ApiException | IOException e) { LOG.error(e.getLocalizedMessage(), e); } }
/** * Route for getting getting schemas from client side and injecting them into yaml language server * * @param yamlDto A yamlDTO containing the list of schemas you would like to add */ @POST @Path("schemas") @Consumes(MediaType.APPLICATION_JSON) public void putSchemas(YamlDTO yamlDto) throws ApiException { LanguageServer yamlLS = YamlLanguageServerLauncher.getYamlLanguageServer(); if (yamlDto != null && yamlLS != null) { Endpoint endpoint = ServiceEndpoints.toEndpoint(yamlLS); YamlSchemaAssociations serviceObject = ServiceEndpoints.toServiceObject(endpoint, YamlSchemaAssociations.class); Map<String, String[]> schemaAssociations = new HashMap<>(); Map<String, String> yamlDtoSchemas = yamlDto.getSchemas(); for (Map.Entry<String, String> schema : yamlDtoSchemas.entrySet()) { schemaAssociations.put( schema.getKey(), new Gson().fromJson(schema.getValue(), String[].class)); } serviceObject.yamlSchemaAssociation(schemaAssociations); } }
@Override public void onServerInitialized( LanguageServerLauncher launcher, LanguageServer server, ServerCapabilities capabilities, String projectPath) { Endpoint endpoint = ServiceEndpoints.toEndpoint(server); JsonExtension serviceObject = ServiceEndpoints.toServiceObject(endpoint, JsonExtension.class); Map<String, String[]> associations = new HashMap<>(); associations.put("/*.schema.json", new String[] {"http://json-schema.org/draft-04/schema#"}); associations.put("/bower.json", new String[] {"http://json.schemastore.org/bower"}); associations.put("/.bower.json", new String[] {"http://json.schemastore.org/bower"}); associations.put("/.bowerrc", new String[] {"http://json.schemastore.org/bowerrc"}); associations.put("/composer.json", new String[] {"https://getcomposer.org/schema.json"}); associations.put("/package.json", new String[] {"http://json.schemastore.org/package"}); associations.put("/jsconfig.json", new String[] {"http://json.schemastore.org/jsconfig"}); associations.put("/tsconfig.json", new String[] {"http://json.schemastore.org/tsconfig"}); serviceObject.jsonSchemaAssociation(associations); }
@Override protected void configure() { this.binder().<ExecutorService>bind(ExecutorService.class).toProvider(ExecutorServiceProvider.class); this.<LanguageServer>bind(LanguageServer.class).to(LanguageServerImpl.class); this.<IResourceServiceProvider.Registry>bind(IResourceServiceProvider.Registry.class).toProvider(ResourceServiceProviderServiceLoader.class); this.<IWorkspaceConfigFactory>bind(IWorkspaceConfigFactory.class).to(ProjectWorkspaceConfigFactory.class); this.<IProjectDescriptionFactory>bind(IProjectDescriptionFactory.class).to(DefaultProjectDescriptionFactory.class); this.<IContainer.Manager>bind(IContainer.Manager.class).to(ProjectDescriptionBasedContainerManager.class); }
@Test public void testDocumentLink_01() throws Exception, ExecutionException { LanguageServer languageServer = wrap(LanguageServer.class, new MockLanguageServer() { @Override public CompletableFuture<List<DocumentLink>> documentLink(DocumentLinkParams params) { return CompletableFutures.computeAsync(canceler -> { return new ArrayList<>(); }); } }); CompletableFuture<List<DocumentLink>> future = languageServer.getTextDocumentService().documentLink(new DocumentLinkParams(new TextDocumentIdentifier("test"))); List<DocumentLink> list = future.get(TIMEOUT, TimeUnit.MILLISECONDS); Assert.assertTrue(list.isEmpty()); }
@Test public void testDocumentLink_02() throws Exception, ExecutionException { LanguageServer languageServer = wrap(LanguageServer.class, new MockLanguageServer() { @Override public CompletableFuture<List<DocumentLink>> documentLink(DocumentLinkParams params) { return CompletableFutures.computeAsync(canceler -> { return null; }); } }); CompletableFuture<List<DocumentLink>> future = languageServer.getTextDocumentService().documentLink(new DocumentLinkParams(new TextDocumentIdentifier("test"))); List<DocumentLink> list = future.get(TIMEOUT, TimeUnit.MILLISECONDS); Assert.assertNull(list); }
@Before public void setup() { final Map<String, JsonRpcMethod> methods = ServiceEndpoints.getSupportedMethods(LanguageServer.class); this.jsonHandler = new MessageJsonHandler(methods) { @Override public GsonBuilder getDefaultGsonBuilder() { return super.getDefaultGsonBuilder().setPrettyPrinting(); } }; }
@Before public void setup() { final Map<String, JsonRpcMethod> methods = ServiceEndpoints.getSupportedMethods(LanguageServer.class); final Map<String, JsonRpcMethod> clientMethods = ServiceEndpoints.getSupportedMethods(LanguageClient.class); final HashMap<String, JsonRpcMethod> all = new HashMap<String, JsonRpcMethod>(); all.putAll(methods); all.putAll(clientMethods); MessageJsonHandler _messageJsonHandler = new MessageJsonHandler(all); this.jsonHandler = _messageJsonHandler; }
@Override public final LanguageServer launch(String projectPath, LanguageClient client) throws LanguageServerException { Process languageServerProcess = startLanguageServerProcess(projectPath); waitCheckProcess(languageServerProcess); return connectToLanguageServer(languageServerProcess, client); }
protected void registerCallbacks(LanguageServer server, LanguageServerLauncher launcher) { if (server instanceof ServerInitializerObserver) { addObserver((ServerInitializerObserver) server); } if (launcher instanceof ServerInitializerObserver) { addObserver((ServerInitializerObserver) launcher); } }
private void onServerInitialized( LanguageServerLauncher launcher, LanguageServer server, ServerCapabilities capabilities, String projectPath) { observers.forEach( observer -> observer.onServerInitialized(launcher, server, capabilities, projectPath)); }
public InitializedLanguageServer( String id, LanguageServer server, InitializeResult initializeResult, LanguageServerLauncher launcher) { this.id = id; this.server = server; this.initializeResult = initializeResult; this.launcher = launcher; }
@Override protected LanguageServer connectToLanguageServer( Process languageServerProcess, LanguageClient client) throws LanguageServerException { Launcher<LanguageServer> launcher = Launcher.createLauncher( client, LanguageServer.class, languageServerProcess.getInputStream(), languageServerProcess.getOutputStream()); launcher.startListening(); return launcher.getRemoteProxy(); }
@Override protected LanguageServer connectToLanguageServer( final Process languageServerProcess, LanguageClient client) { Launcher<LanguageServer> launcher = Launcher.createLauncher( client, LanguageServer.class, languageServerProcess.getInputStream(), languageServerProcess.getOutputStream()); launcher.startListening(); return launcher.getRemoteProxy(); }
protected LanguageServer connectToLanguageServer( final Process languageServerProcess, LanguageClient client) { Launcher<LanguageServer> launcher = Launcher.createLauncher( client, LanguageServer.class, languageServerProcess.getInputStream(), languageServerProcess.getOutputStream()); launcher.startListening(); return launcher.getRemoteProxy(); }
protected LanguageServer connectToLanguageServer( final Process languageServerProcess, LanguageClient client) { Launcher<LanguageServer> launcher = Launcher.createLauncher( client, LanguageServer.class, languageServerProcess.getInputStream(), languageServerProcess.getOutputStream()); launcher.startListening(); setYamlLanguageServer(launcher.getRemoteProxy()); return launcher.getRemoteProxy(); }
public static Launcher<LanguageClient> createServerLauncher(LanguageServer server, InputStream in, OutputStream out) { return Launcher.createLauncher(server, LanguageClient.class, in, out); }
public static Launcher<LanguageClient> createServerLauncher(LanguageServer server, InputStream in, OutputStream out, boolean validate, PrintWriter trace) { return Launcher.createLauncher(server, LanguageClient.class, in, out, validate, trace); }
public static Launcher<LanguageClient> createServerLauncher(LanguageServer server, InputStream in, OutputStream out, ExecutorService executorService, Function<MessageConsumer, MessageConsumer> wrapper) { return Launcher.createLauncher(server, LanguageClient.class, in, out, executorService, wrapper); }
public static Launcher<LanguageServer> createClientLauncher(LanguageClient client, InputStream in, OutputStream out) { return Launcher.createLauncher(client, LanguageServer.class, in, out); }
public static Launcher<LanguageServer> createClientLauncher(LanguageClient client, InputStream in, OutputStream out, boolean validate, PrintWriter trace) { return Launcher.createLauncher(client, LanguageServer.class, in, out, validate, trace); }
public static Launcher<LanguageServer> createClientLauncher(LanguageClient client, InputStream in, OutputStream out, ExecutorService executorService, Function<MessageConsumer, MessageConsumer> wrapper) { return Launcher.createLauncher(client, LanguageServer.class, in, out, executorService, wrapper); }