Skip to content

Commit

Permalink
launch CheckExecutor at the beginning
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangjiangqige committed Nov 21, 2019
1 parent f436719 commit 71663e7
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 25 deletions.
9 changes: 5 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ plugins {
// Apply the application plugin to add support for building a CLI application
id 'application'

id "com.github.johnrengelman.shadow" version "5.1.0"
id 'com.github.johnrengelman.shadow' version '5.1.0'
}

repositories {
Expand All @@ -22,10 +22,11 @@ repositories {
}

dependencies {
compile group: 'org.checkerframework', name: 'checker', version: '2.11.1'
implementation "org.checkerframework:javacutil:2.11.1"
implementation "org.eclipse.lsp4j:org.eclipse.lsp4j:0.7.2"
implementation 'org.checkerframework:checker:2.11.1'
implementation 'org.checkerframework:javacutil:2.11.1'
implementation 'org.eclipse.lsp4j:org.eclipse.lsp4j:0.7.2'
implementation 'com.google.code.gson:gson:2.8.5'
implementation 'commons-cli:commons-cli:1.4'
testImplementation 'junit:junit:4.12'
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,33 @@ public class CFLanguageServer implements LanguageServer, LanguageClientAware {
public static final String SERVER_NAME = "checker-framework";

private LanguageClient client;
private Settings settings;
private CheckExecutor executor;

private final CFTextDocumentService textDocumentService;
private final CFWorkspaceService workspaceService;

public CFLanguageServer() {
this.client = null;
CFLanguageServer(Settings settings) throws IOException {
this.settings = settings;
this.textDocumentService = new CFTextDocumentService(this);
this.executor = buildExecutor(settings);
this.textDocumentService.setExecutor(this.executor);
this.client = null;
this.workspaceService = new CFWorkspaceService(this);
}

private CheckExecutor buildExecutor(Settings settings) throws IOException {
String checker = settings.getCheckerPath();
logger.info("Launching CheckExecutor using " + checker);
return new CheckExecutor(
this.textDocumentService,
settings.getJdkPath(),
settings.getCheckerPath(),
settings.getCheckers(),
settings.getCommandLineOptions()
);
}

@Override
public void connect(LanguageClient client) {
this.client = client;
Expand Down Expand Up @@ -101,7 +118,7 @@ public WorkspaceService getWorkspaceService() {
*/
void didChangeConfiguration(Settings settings) {
try {
textDocumentService.didChangeConfiguration(settings);
textDocumentService.setExecutor(buildExecutor(settings));
} catch (IOException e) {
logger.severe("Failed to change configuration: " + e.toString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import org.eclipse.lsp4j.services.TextDocumentService;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.util.Collections;
import java.util.List;
Expand All @@ -26,19 +25,8 @@ public class CFTextDocumentService implements TextDocumentService, Publisher {
this.server = server;
}

/**
* Accepts a new configuration from {@link CFLanguageServer}.
*
* @param settings the new configuration, containing parameters for the underlying checker.
*/
void didChangeConfiguration(Settings settings) throws IOException {
executor = new CheckExecutor(
this,
settings.getJdkPath(),
settings.getCheckerPath(),
settings.getCheckers(),
settings.getCommandLineOptions()
);
void setExecutor(CheckExecutor executor) {
this.executor = executor;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.checkerframework.languageserver;

import javax.tools.*;
import java.io.*;
import java.util.*;
import java.util.logging.Logger;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package org.checkerframework.languageserver;

import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.commons.cli.*;
import org.eclipse.lsp4j.jsonrpc.Launcher;
import org.eclipse.lsp4j.launch.LSPLauncher;
import org.eclipse.lsp4j.services.LanguageClient;
Expand All @@ -13,6 +15,10 @@
public class ServerMain {

private static final Logger logger = Logger.getLogger(ServerMain.class.getName());
private static final String OPT_FRAMEWORKPATH = "frameworkPath";
private static final String OPT_CHECKERS = "checkers";
private static final String OPT_COMMANDLINEOPTIONS = "commandLineOptions";


/**
* The entry point of application. Sets up and launches {@link CFLanguageServer}.
Expand All @@ -22,8 +28,9 @@ public class ServerMain {
*/
public static void main(String[] args) {
try {
Settings settings = getSettings(args);
logger.info("Launching checker framework languageserver");
CFLanguageServer server = new CFLanguageServer();
CFLanguageServer server = new CFLanguageServer(settings);
Launcher<LanguageClient> launcher = LSPLauncher.createServerLauncher(server, System.in, System.out);
LanguageClient client = launcher.getRemoteProxy();
server.connect(client);
Expand All @@ -33,4 +40,33 @@ public static void main(String[] args) {
System.exit(1);
}
}

private static Options getOptions() {
Options options = new Options();
options.addRequiredOption(OPT_FRAMEWORKPATH, OPT_FRAMEWORKPATH, true, "Absolute path of the Checker Framework");
options.addRequiredOption(OPT_CHECKERS, OPT_CHECKERS, true, "List of checkers enabled for compilation");
options.addOption(OPT_COMMANDLINEOPTIONS, OPT_COMMANDLINEOPTIONS, true, "List of command line options that gets passed in to javac");
return options;
}

private static Settings getSettings(String[] args) throws ParseException {
CommandLineParser parser = new DefaultParser();
CommandLine cmd = parser.parse(getOptions(), args);
String fp = "";
String[] checkers = new String[0];
String[] cmo = new String[0];
if (cmd.hasOption(OPT_FRAMEWORKPATH)) {
fp = cmd.getOptionValue(OPT_FRAMEWORKPATH);
logger.info("got frameworkPath " + fp);
}
if (cmd.hasOption(OPT_CHECKERS)) {
checkers = cmd.getOptionValues(OPT_CHECKERS);
logger.info("got checkers " + Arrays.toString(checkers));
}
if (cmd.hasOption(OPT_COMMANDLINEOPTIONS)) {
cmo = cmd.getOptionValues(OPT_COMMANDLINEOPTIONS);
logger.info("got cliOptions " + Arrays.toString(cmo));
}
return new Settings(new Settings.Config(fp, Arrays.asList(checkers), Arrays.asList(cmo)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@
import java.nio.file.Paths;
import java.util.List;

public class Settings {
class Settings {
@SerializedName(CFLanguageServer.SERVER_NAME)
Config config;

Settings(Config config) {
this.config = config;
}

/**
* This class is used for converting from/to JSON as all settings will be under {@link CFLanguageServer.SERVER_NAME}.
*/
private static class Config {
static class Config {
final String frameworkPath;
final List<String> checkers;
final List<String> commandLineOptions;
Expand Down

0 comments on commit 71663e7

Please sign in to comment.