Skip to content

Commit

Permalink
Merge pull request #2 from lorchrob/call-kind2-filename
Browse files Browse the repository at this point in the history
Call kind2 with filename
  • Loading branch information
daniel-larraz authored Apr 19, 2023
2 parents 91002eb + cdfa822 commit b4364b5
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions src/main/java/edu/uiowa/kind2/lsp/Kind2LanguageServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ public class Kind2LanguageServer
private Map<String, String> openDocuments;
private Map<String, Result> parseResults;
private Map<String, Map<String, NodeResult>> analysisResults;
private String workingDirectory;

public Kind2LanguageServer() {
client = null;
Expand All @@ -93,6 +94,7 @@ public Kind2LanguageServer() {
analysisResults = new HashMap<>();
Result.setOpeningSymbols("");
Result.setClosingSymbols("");
workingDirectory = null;
}

public String getText(String uri) throws IOException, URISyntaxException {
Expand Down Expand Up @@ -153,6 +155,19 @@ Diagnostic logToDiagnostic(Log log) {
log.getValue(), ds, "Kind 2: " + log.getSource());
}

/**
* Compute a relative filepath from the working directory and file URI,
* both as absolute filepaths.
*
* @param workingDirectory the current working directory
* @param uri the uri of the lustre file
*/
private String computeRelativeFilepath(String workingDirectory, String uri) {
return Paths.get(URI.create(workingDirectory)).relativize(
Paths.get(URI.create(uri)))
.toString();
}

/**
* Call Kind 2 to parse a lustre file and check for syntax errors.
*
Expand All @@ -163,13 +178,18 @@ void parse(String uri) {

// ignore exceptions from syntax errors
try {
if (workingDirectory == null) {
workingDirectory = client.workspaceFolders().get().get(0).getUri();
}
Kind2Api api = getPresetKind2Api();
api.setOldFrontend(false);
api.setOnlyParse(true);
api.setLsp(true);
String filepath = computeRelativeFilepath(workingDirectory, uri);
api.setFakeFilepath(filepath);
api.includeDir(Paths.get(new URI(uri)).getParent().toString());
parseResults.put(uri, api.execute(getText(uri)));
} catch (Kind2Exception | IOException | URISyntaxException
} catch (Kind2Exception | URISyntaxException | IOException
| InterruptedException | ExecutionException e) {
throw new ResponseErrorException(
new ResponseError(ResponseErrorCode.ParseError, e.getMessage(), e));
Expand Down Expand Up @@ -273,9 +293,16 @@ public void done() {
};

try {
if (workingDirectory == null) {
workingDirectory = client.workspaceFolders().get().get(0).getUri();
}
Kind2Api api = getCheckKind2Api(name);
api.includeDir(Paths.get(new URI(uri)).getParent().toString());
api.execute(getText(uri), result, monitor);
String filepath = computeRelativeFilepath(workingDirectory, uri);
api.setFakeFilepath(filepath);
api.execute(getText(uri),
result,
monitor);
} catch (Kind2Exception | IOException | URISyntaxException
| InterruptedException | ExecutionException e) {
throw new ResponseErrorException(new ResponseError(
Expand Down

0 comments on commit b4364b5

Please sign in to comment.