Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use javacs language server #540

Merged
merged 11 commits into from
Sep 25, 2022
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ subprojects {
doLast {
val tree = fileTree(destinationDirectory)
tree.include("**/*.class")
// tree.exclude("module-info.class")
tree.include("module-info.class")
val root = project.buildDir.toPath().resolve("classes/java/main")
tree.forEach { BuildUtil.stripPreview(root, it.toPath()) }
}
Expand Down
3 changes: 2 additions & 1 deletion buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ plugins {

repositories {
mavenCentral()
mavenLocal()
gradlePluginPortal()
}

Expand Down Expand Up @@ -46,7 +47,7 @@ dependencies {
val deps = Properties()
deps.load(rootDir.resolve("gradle/deps.properties").reader())
antlr("org.antlr", "antlr4", deps.getProperty("version.antlr"))
api("org.aya-prover.upstream", "build-util", deps.getProperty("version.build-util"))
api("org.aya-prover.upstream", "build-util", deps.getProperty("version.aya-upstream"))

// The following is required for
// - extracting common parts inside `graalvmNative` block
Expand Down
2 changes: 1 addition & 1 deletion gradle/deps.properties
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ version.annotations=23.0.0
version.antlr=4.10.1
version.kala=0.50.0
version.guest0x0=0.17.1
version.build-util=0.0.3
version.aya-upstream=0.0.4
version.hamcrest=2.2
version.junit=5.8.2
version.jacoco=0.8.8
Expand Down
4 changes: 1 addition & 3 deletions lsp/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ CommonTasks.fatJar(project, mainClassQName)
dependencies {
val deps: java.util.Properties by rootProject.ext
implementation(project(":cli"))
val lsp4jVersion = deps.getProperty("version.lsp4j")
api("org.eclipse.lsp4j", "org.eclipse.lsp4j", version = lsp4jVersion)
api("org.eclipse.lsp4j", "org.eclipse.lsp4j.jsonrpc", version = lsp4jVersion)
implementation("org.aya-prover.upstream", "javacs-protocol", version = deps.getProperty("version.aya-upstream"))
annotationProcessor("info.picocli", "picocli-codegen", version = deps.getProperty("version.picocli"))
testImplementation("org.junit.jupiter", "junit-jupiter", version = deps.getProperty("version.junit"))
testImplementation("org.hamcrest", "hamcrest", version = deps.getProperty("version.hamcrest"))
Expand Down
3 changes: 1 addition & 2 deletions lsp/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

requires static org.jetbrains.annotations;
requires com.google.gson;
requires org.eclipse.lsp4j;
requires org.eclipse.lsp4j.jsonrpc;
requires aya.javacs.protocol;
requires info.picocli;

exports org.aya.lsp.models;
Expand Down
26 changes: 8 additions & 18 deletions lsp/src/main/java/org/aya/lsp/LspMain.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// Copyright (c) 2020-2021 Yinsen (Tesla) Zhang.
// Copyright (c) 2020-2022 Tesla (Yinsen) Zhang.
// Use of this source code is governed by the MIT license that can be found in the LICENSE.md file.
package org.aya.lsp;

import org.aya.lsp.server.AyaLanguageClient;
import org.aya.lsp.server.AyaServer;
import org.aya.cli.library.incremental.CompilerAdvisor;
import org.aya.lsp.server.AyaLanguageServer;
import org.aya.lsp.utils.Log;
import org.aya.lsp.utils.LspArgs;
import org.eclipse.lsp4j.jsonrpc.Launcher;
import org.javacs.lsp.LSP;
import org.jetbrains.annotations.NotNull;
import picocli.CommandLine;

Expand All @@ -17,35 +17,25 @@
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.Callable;
import java.util.concurrent.Executors;
import java.util.function.Function;

public class LspMain extends LspArgs implements Callable<Integer> {
public static void main(String[] args) {
new CommandLine(new LspMain()).execute(args);
}

@Override
public Integer call() throws Exception {
@Override public Integer call() throws Exception {
Log.i("Hello, this is Aya language server");
var startup = switch (mode) {
case server -> runServer();
case client -> runClient();
case debug -> runDebug();
};

var executor = Executors.newSingleThreadExecutor(f -> new Thread(f, "client"));
var server = new AyaServer();
var launcher = Launcher.createLauncher(
server,
AyaLanguageClient.class,
LSP.connect(
client -> new AyaLanguageServer(CompilerAdvisor.inMemory(), client),
startup.in,
startup.out,
executor,
Function.identity()
startup.out
);
server.connect(launcher.getRemoteProxy());
launcher.startListening();
return 0;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import org.aya.ref.DefVar;
import org.aya.ref.LocalVar;
import org.aya.util.distill.DistillerOptions;
import org.eclipse.lsp4j.Position;
import org.javacs.lsp.Position;
import org.jetbrains.annotations.NotNull;

public interface ComputeSignature {
Expand Down
6 changes: 3 additions & 3 deletions lsp/src/main/java/org/aya/lsp/actions/FindReferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
import org.aya.ref.AnyVar;
import org.aya.util.error.SourcePos;
import org.aya.util.error.WithPos;
import org.eclipse.lsp4j.Location;
import org.eclipse.lsp4j.Position;
import org.javacs.lsp.Location;
import org.javacs.lsp.Position;
import org.jetbrains.annotations.NotNull;

import java.util.List;
Expand All @@ -20,7 +20,7 @@
public interface FindReferences {
static @NotNull List<Location> invoke(
@NotNull LibrarySource source,
@NotNull Position position,
Position position,
@NotNull SeqView<LibraryOwner> libraries
) {
return findRefs(source, position, libraries)
Expand Down
11 changes: 4 additions & 7 deletions lsp/src/main/java/org/aya/lsp/actions/Folding.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
import org.aya.lsp.utils.LspRange;
import org.aya.lsp.utils.Resolver;
import org.aya.util.error.SourcePos;
import org.eclipse.lsp4j.FoldingRange;
import org.eclipse.lsp4j.FoldingRangeKind;
import org.javacs.lsp.FoldingRange;
import org.javacs.lsp.FoldingRangeKind;
import org.jetbrains.annotations.NotNull;

import java.util.List;
Expand Down Expand Up @@ -40,10 +40,7 @@ public void visitCommand(@NotNull Command cmd, @NotNull MutableList<FoldingRange

private @NotNull FoldingRange toFoldingRange(@NotNull SourcePos sourcePos) {
var range = LspRange.toRange(sourcePos);
var fr = new FoldingRange(range.getStart().getLine(), range.getEnd().getLine());
fr.setStartCharacter(range.getStart().getCharacter());
fr.setEndCharacter(range.getEnd().getCharacter());
fr.setKind(FoldingRangeKind.Region);
return fr;
return new FoldingRange(range.start.line, range.start.character,
range.end.line, range.end.character, FoldingRangeKind.Region);
}
}
10 changes: 5 additions & 5 deletions lsp/src/main/java/org/aya/lsp/actions/GotoDefinition.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2020-2022 Yinsen (Tesla) Zhang.
// Copyright (c) 2020-2022 Tesla (Yinsen) Zhang.
// Use of this source code is governed by the MIT license that can be found in the LICENSE.md file.
package org.aya.lsp.actions;

Expand All @@ -13,8 +13,8 @@
import org.aya.ref.LocalVar;
import org.aya.util.error.SourcePos;
import org.aya.util.error.WithPos;
import org.eclipse.lsp4j.LocationLink;
import org.eclipse.lsp4j.Position;
import org.javacs.lsp.GenericLocation;
import org.javacs.lsp.Position;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand All @@ -25,7 +25,7 @@
* @author ice1000, kiva
*/
public interface GotoDefinition {
static @NotNull List<LocationLink> invoke(
static @NotNull List<GenericLocation> invoke(
@NotNull LibrarySource source,
@NotNull Position position,
@NotNull SeqView<LibraryOwner> libraries
Expand All @@ -34,7 +34,7 @@ public interface GotoDefinition {
var from = pos.sourcePos();
var to = pos.data();
var res = LspRange.toLoc(from, to);
if (res != null) Log.d("Resolved: %s in %s", to, res.getTargetUri());
if (res != null) Log.d("Resolved: %s in %s", to, res.targetUri);
return res;
}).collect(Collectors.toList());
}
Expand Down
13 changes: 6 additions & 7 deletions lsp/src/main/java/org/aya/lsp/actions/InlayHintMaker.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@
import org.aya.lsp.utils.LspRange;
import org.aya.lsp.utils.XYXY;
import org.aya.util.distill.DistillerOptions;
import org.eclipse.lsp4j.InlayHint;
import org.eclipse.lsp4j.InlayHintKind;
import org.eclipse.lsp4j.Range;
import org.eclipse.lsp4j.jsonrpc.messages.Either;
import org.javacs.lsp.InlayHint;
import org.javacs.lsp.InlayHintKind;
import org.javacs.lsp.Range;
import org.jetbrains.annotations.NotNull;

import java.util.Collections;
Expand All @@ -31,9 +30,9 @@ public record InlayHintMaker(@NotNull MutableList<InlayHint> hints) implements S
if (pattern instanceof Pattern.Bind bind && bind.type().get() != null) {
var type = bind.type().get().toDoc(DistillerOptions.pretty()).commonRender();
var range = LspRange.toRange(bind.sourcePos());
var hint = new InlayHint(range.getEnd(), Either.forLeft(": " + type));
hint.setKind(InlayHintKind.Type);
hint.setPaddingLeft(true);
var hint = new InlayHint(range.end, ": " + type);
hint.kind = InlayHintKind.Type;
hint.paddingLeft = true;
hints.append(hint);
}
return Ranged.super.visitPattern(pattern, pp);
Expand Down
15 changes: 8 additions & 7 deletions lsp/src/main/java/org/aya/lsp/actions/LensMaker.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2020-2022 Yinsen (Tesla) Zhang.
// Copyright (c) 2020-2022 Tesla (Yinsen) Zhang.
// Use of this source code is governed by the MIT license that can be found in the LICENSE.md file.
package org.aya.lsp.actions;

Expand All @@ -11,10 +11,11 @@
import org.aya.concrete.stmt.Decl;
import org.aya.lsp.utils.LspRange;
import org.aya.lsp.utils.Resolver;
import org.eclipse.lsp4j.CodeLens;
import org.eclipse.lsp4j.Command;
import org.javacs.lsp.CodeLens;
import org.javacs.lsp.Command;
import org.jetbrains.annotations.NotNull;

import java.util.Collections;
import java.util.List;

public record LensMaker(@NotNull SeqView<LibraryOwner> libraries) implements SyntaxDeclAction<@NotNull MutableList<CodeLens>> {
Expand All @@ -27,8 +28,8 @@ public record LensMaker(@NotNull SeqView<LibraryOwner> libraries) implements Syn
}

public static @NotNull CodeLens resolve(@NotNull CodeLens codeLens) {
var cmd = new Gson().fromJson((JsonElement) codeLens.getData(), Command.class);
return new CodeLens(codeLens.getRange(), cmd, codeLens.getData());
var cmd = new Gson().fromJson((JsonElement) codeLens.data, Command.class);
return new CodeLens(codeLens.range, cmd, codeLens.data);
}

@Override public void visitDecl(@NotNull Decl maybe, @NotNull MutableList<CodeLens> pp) {
Expand All @@ -47,8 +48,8 @@ public record LensMaker(@NotNull SeqView<LibraryOwner> libraries) implements Syn
var title = refs.sizeEquals(1) ? "1 usage" : "%d usages".formatted(refs.size());
var locations = refs.mapNotNull(LspRange::toLoc).asJava();
var cmd = uri.isDefined()
? new Command(title, "editor.action.showReferences", List.of(uri.get(), range.getEnd(), locations))
: new Command(title, "");
? new Command(title, "editor.action.showReferences", List.of(uri.get(), range.end, locations))
: new Command(title, "", Collections.emptyList());
// the type of variable `cmd` is Command, but it cannot be used as
// the command of the CodeLens created below, because VSCode cannot parse
// the argument of the command directly due to some Uri serialization problems.
Expand Down
22 changes: 11 additions & 11 deletions lsp/src/main/java/org/aya/lsp/actions/ProjectSymbol.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2020-2022 Yinsen (Tesla) Zhang.
// Copyright (c) 2020-2022 Tesla (Yinsen) Zhang.
// Use of this source code is governed by the MIT license that can be found in the LICENSE.md file.
package org.aya.lsp.actions;

Expand All @@ -11,11 +11,11 @@
import org.aya.lsp.utils.LspRange;
import org.aya.lsp.utils.Resolver;
import org.aya.ref.DefVar;
import org.eclipse.lsp4j.DocumentSymbol;
import org.eclipse.lsp4j.Location;
import org.eclipse.lsp4j.SymbolKind;
import org.eclipse.lsp4j.WorkspaceSymbol;
import org.eclipse.lsp4j.jsonrpc.messages.Either;
import org.intellij.lang.annotations.MagicConstant;
import org.javacs.lsp.DocumentSymbol;
import org.javacs.lsp.Location;
import org.javacs.lsp.SymbolKind;
import org.javacs.lsp.WorkspaceSymbol;
import org.jetbrains.annotations.NotNull;

public final class ProjectSymbol implements SyntaxDeclAction<@NotNull MutableList<ProjectSymbol.Symbol>> {
Expand Down Expand Up @@ -64,22 +64,22 @@ private void collect(@NotNull MutableList<Symbol> pp, @NotNull DefVar<?, ?> dv,
pp.append(symbol);
}

/** Our superclass of {@link org.eclipse.lsp4j.WorkspaceSymbol} and {@link org.eclipse.lsp4j.DocumentSymbol} */
/** Our superclass of {@link WorkspaceSymbol} and {@link DocumentSymbol} */
public record Symbol(
@NotNull String name,
@NotNull String description,
@NotNull SymbolKind kind,
@MagicConstant(valuesFromClass = SymbolKind.class) int kind,
@NotNull Location nameLocation,
@NotNull Location entireLocation,
@NotNull ImmutableSeq<Symbol> children
) {
public @NotNull DocumentSymbol document() {
return new DocumentSymbol(name, kind, entireLocation.getRange(), nameLocation.getRange(),
description, children.map(Symbol::document).asJava());
return new DocumentSymbol(name, description, kind, false, entireLocation.range,
nameLocation.range, children.map(Symbol::document).asJava());
}

public @NotNull WorkspaceSymbol workspace() {
return new WorkspaceSymbol(name, kind, Either.forLeft(nameLocation));
return new WorkspaceSymbol(name, kind, nameLocation);
}
}
}
9 changes: 5 additions & 4 deletions lsp/src/main/java/org/aya/lsp/actions/Rename.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
import org.aya.lsp.utils.Resolver;
import org.aya.ref.AnyVar;
import org.aya.util.error.WithPos;
import org.eclipse.lsp4j.Position;
import org.eclipse.lsp4j.TextEdit;
import org.javacs.lsp.Position;
import org.javacs.lsp.TextEdit;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.net.URI;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
Expand All @@ -26,7 +27,7 @@ public interface Rename {
return vars.first().map(AnyVar::name);
}

static Map<String, List<TextEdit>> rename(
static Map<URI, List<TextEdit>> rename(
@NotNull LibrarySource source,
@NotNull Position position,
@NotNull String newName,
Expand All @@ -36,7 +37,7 @@ static Map<String, List<TextEdit>> rename(
.flatMap(to -> {
var edit = new TextEdit(LspRange.toRange(to), newName);
return to.file().underlying().map(uri -> Tuple.of(uri, edit));
}).collect(Collectors.groupingBy(tup -> tup._1.toUri().toString(), Collectors.mapping(
}).collect(Collectors.groupingBy(tup -> tup._1.toUri(), Collectors.mapping(
tup -> tup._2,
Collectors.toList()
)));
Expand Down
7 changes: 3 additions & 4 deletions lsp/src/main/java/org/aya/lsp/actions/SyntaxHighlight.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2020-2022 Yinsen (Tesla) Zhang.
// Copyright (c) 2020-2022 Tesla (Yinsen) Zhang.
// Use of this source code is governed by the MIT license that can be found in the LICENSE.md file.
package org.aya.lsp.actions;

Expand Down Expand Up @@ -36,7 +36,7 @@ private static void highlight(@NotNull LibraryOwner owner, @NotNull MutableList<
var program = source.program().get();
if (program != null) program.forEach(d -> SyntaxHighlight.INSTANCE.visit(d, symbols));
return new HighlightResult(
source.file().toUri().toString(),
source.file().toUri(),
symbols.view().filter(t -> t.range() != LspRange.NONE));
}

Expand Down Expand Up @@ -115,8 +115,7 @@ private void visitCall(@NotNull DefVar<?, ?> ref, @NotNull SourcePos headPos, @N
pp.append(new HighlightResult.Symbol(imp.path().sourcePos(), HighlightResult.Kind.ModuleDef));
case Command.Open open ->
pp.append(new HighlightResult.Symbol(open.path().sourcePos(), HighlightResult.Kind.ModuleDef));
case Command.Module mod ->
pp.append(new HighlightResult.Symbol(mod.sourcePos(), HighlightResult.Kind.ModuleDef));
case Command.Module mod -> pp.append(new HighlightResult.Symbol(mod.sourcePos(), HighlightResult.Kind.ModuleDef));
}
StmtOps.super.visitCommand(cmd, pp);
}
Expand Down
Loading