Skip to content

Commit

Permalink
lsp: registerLibrary returns the loaded libraries
Browse files Browse the repository at this point in the history
  • Loading branch information
ice1000 committed Dec 25, 2024
1 parent 6a98134 commit 3bd885e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
31 changes: 19 additions & 12 deletions ide-lsp/src/main/java/org/aya/lsp/server/AyaLanguageServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,33 +92,40 @@ public AyaLanguageServer(@NotNull CompilerAdvisor advisor, @NotNull AyaLanguageC
return libraries.view();
}

public void registerLibrary(@NotNull Path path) {
/// @return the libraries that are actually loaded
public SeqView<LibraryOwner> registerLibrary(@NotNull Path path) {
Log.i("Adding library path %s", path);
if (!tryAyaLibrary(path)) mockLibraries(path);
var tryLoad = tryAyaLibrary(path);
if (tryLoad != null) return tryLoad;
return SeqView.narrow(mockLibraries(path).view());
}

private boolean tryAyaLibrary(@Nullable Path path) {
if (path == null) return false;
/// @return null if the path needs to be "mocked", empty if the library fails to load (due to IO exceptions
/// or possibly malformed config files), and nonempty if successfully loaded.
private @Nullable SeqView<LibraryOwner> tryAyaLibrary(@Nullable Path path) {
if (path == null) return null;
var ayaJson = path.resolve(Constants.AYA_JSON);
if (!Files.exists(ayaJson)) return tryAyaLibrary(path.getParent());
try {
var config = LibraryConfigData.fromLibraryRoot(path);
var owner = DiskLibraryOwner.from(config);
libraries.append(owner);
return SeqView.of(owner);
} catch (IOException e) {
var s = new StringWriter();
e.printStackTrace(new PrintWriter(s));
Log.e("Cannot load library. Stack trace:\n%s", s.toString());
} catch (LibraryConfigData.BadConfig bad) {
client.showMessage(new ShowMessageParams(MessageType.Error, "Cannot load malformed library: " + bad.getMessage()));
}
// stop retrying and mocking
return true;
// Do not mock because there is meant to be a library, but it's bad
return SeqView.empty();
}

private void mockLibraries(@NotNull Path path) {
libraries.appendAll(AyaFiles.collectAyaSourceFiles(path, 1)
.map(WsLibrary::mock));
private ImmutableSeq<WsLibrary> mockLibraries(@NotNull Path path) {
var mocked = AyaFiles.collectAyaSourceFiles(path, 1).map(WsLibrary::mock);
libraries.appendAll(mocked);
return mocked;
}

@Override public void initialized() {
Expand All @@ -132,7 +139,7 @@ private void mockLibraries(@NotNull Path path) {

@Override public InitializeResult initialize(InitializeParams params) {
var cap = new ServerCapabilities();
cap.textDocumentSync = 0;
cap.textDocumentSync = DocumentSyncKind.None;
var workOps = new ServerCapabilities.WorkspaceFoldersOptions(true, true);
var workCap = new ServerCapabilities.WorkspaceServerCapabilities(workOps);
cap.completionProvider = new ServerCapabilities.CompletionOptions(
Expand Down Expand Up @@ -274,7 +281,7 @@ private void clearProblems(@NotNull ImmutableSeq<ImmutableSeq<LibrarySource>> af
Log.d("Created new file: %s, mocked a library %s for it", newSrc, mock.mockConfig().name());
libraries.append(mock);
}
default -> {}
default -> { }
}
}
case FileChangeType.Deleted -> {
Expand All @@ -284,7 +291,7 @@ private void clearProblems(@NotNull ImmutableSeq<ImmutableSeq<LibrarySource>> af
switch (src.owner()) {
case MutableLibraryOwner owner -> owner.removeLibrarySource(src);
case WsLibrary owner -> libraries.removeIf(o -> o == owner);
default -> {}
default -> { }
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions ide-lsp/src/test/java/org/aya/lsp/tester/LspTestClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
// Use of this source code is governed by the MIT license that can be found in the LICENSE.md file.
package org.aya.lsp.tester;

import kala.collection.SeqView;
import kala.collection.immutable.ImmutableSeq;
import kala.tuple.Unit;
import org.aya.cli.library.source.LibraryOwner;
import org.aya.generic.Constants;
import org.aya.ide.Resolver;
import org.aya.lsp.server.AyaLanguageClient;
Expand All @@ -30,8 +32,8 @@ public LspTestClient(@NotNull InitializeParams param) {
service.initialize(param);
}

public void registerLibrary(@NotNull Path libraryRoot) {
service.registerLibrary(libraryRoot);
public SeqView<LibraryOwner> registerLibrary(@NotNull Path libraryRoot) {
return service.registerLibrary(libraryRoot);
}

public long loadLibraries() {
Expand Down

0 comments on commit 3bd885e

Please sign in to comment.