Skip to content

Commit

Permalink
perf: Improve start/stop of language servers (#835)
Browse files Browse the repository at this point in the history
Fixes #835

Signed-off-by: azerr <[email protected]>
  • Loading branch information
angelozerr committed May 3, 2023
1 parent 794eb67 commit 12cd09d
Show file tree
Hide file tree
Showing 9 changed files with 478 additions and 256 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public IPsiUtils refine(Module module) {

@Override
public Module getModule(VirtualFile file) {
if (file != null) {
if (file != null && !project.isDisposed()) {
return ProjectFileIndex.getInstance(project).getModuleForFile(file, false);
}
return null;
Expand All @@ -93,7 +93,7 @@ public Module getModule(VirtualFile file) {
@Override
public Module getModule(String uri) throws IOException {
VirtualFile file = findFile(uri);
return file!=null?getModule(file):null;
return file != null ? getModule(file) : null;
}

@Override
Expand Down Expand Up @@ -173,15 +173,19 @@ public int toOffset(Document document, int line, int character) {
@Override
public int toOffset(PsiFile file, int line, int character) {
Document document = PsiDocumentManager.getInstance(file.getProject()).getDocument(file);
return document!=null?toOffset(document, line, character):0;
return document != null ? toOffset(document, line, character) : 0;
}

@Override
public PsiFile resolveCompilationUnit(String uri) {
try {
VirtualFile file = findFile(uri);
if (file != null) {
return PsiManager.getInstance(getModule(file).getProject()).findFile(file);
Module module = getModule(file);
if (module == null) {
return null;
}
return PsiManager.getInstance(module.getProject()).findFile(file);
}
} catch (IOException e) {
LOGGER.error(e.getLocalizedMessage(), e);
Expand All @@ -204,7 +208,7 @@ public static ClasspathKind getClasspathKind(VirtualFile file, Module module) {
}

public static String getProjectURI(Module module) {
return module != null?module.getModuleFilePath():null;
return module != null ? module.getModuleFilePath() : null;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ public void projectOpened() {
project.getMessageBus().connect(project).subscribe(FileEditorManagerListener.FILE_EDITOR_MANAGER, this);
}

@Override
public void projectClosed() {
LanguageServiceAccessor.getInstance(project).shutdownAllDispatchers();
}

@Override
public void fileOpened(@NotNull FileEditorManager source, @NotNull VirtualFile file) {
Document document = FileDocumentManager.getInstance().getDocument(file);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,7 @@ private void sendDidChangeEvents() {
DidChangeTextDocumentParams changeParamsToSend = new DidChangeTextDocumentParams(new VersionedTextDocumentIdentifier(), events);
changeParamsToSend.getTextDocument().setUri(fileUri.toString());
changeParamsToSend.getTextDocument().setVersion(++version);
languageServerWrapper.getInitializedServer()
.thenAcceptAsync(ls -> ls.getTextDocumentService().didChange(changeParamsToSend));
languageServerWrapper.sendNotification(ls -> ls.getTextDocumentService().didChange(changeParamsToSend));
}

@Override
Expand Down Expand Up @@ -171,18 +170,21 @@ public void documentSaved(long timestamp) {
return;
}
}
TextDocumentIdentifier identifier = new TextDocumentIdentifier(fileUri.toString());
final var identifier = LSPIJUtils.toTextDocumentIdentifier(fileUri);
DidSaveTextDocumentParams params = new DidSaveTextDocumentParams(identifier, document.getText());
languageServerWrapper.getInitializedServer().thenAcceptAsync(ls -> ls.getTextDocumentService().didSave(params));
languageServerWrapper.sendNotification(ls -> ls.getTextDocumentService().didSave(params));
}

public void documentClosed() {
// When LS is shut down all documents are being disconnected. No need to send "didClose" message to the LS that is being shut down or not yet started
final var identifier = LSPIJUtils.toTextDocumentIdentifier(fileUri);
// WILL_SAVE_WAIT_UNTIL_TIMEOUT_MAP.remove(identifier.getUri());
// When LS is shut down all documents are being disconnected. No need to send
// "didClose" message to the LS that is being shut down or not yet started
if (languageServerWrapper.isActive()) {
TextDocumentIdentifier identifier = new TextDocumentIdentifier(fileUri.toString());
DidCloseTextDocumentParams params = new DidCloseTextDocumentParams(identifier);
languageServerWrapper.getInitializedServer().thenAcceptAsync(ls -> ls.getTextDocumentService().didClose(params));
final var params = new DidCloseTextDocumentParams(identifier);
languageServerWrapper.sendNotification(ls -> ls.getTextDocumentService().didClose(params));
}
//return CompletableFuture.completedFuture(null);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ private static <T extends TextDocumentPositionParams> T toTextDocumentPositionPa
param.setPosition(start);
TextDocumentIdentifier id = new TextDocumentIdentifier();
if (uri != null) {
id.setUri(uri.toString());
id.setUri(uri.toASCIIString());
}
param.setTextDocument(id);
return param;
Expand Down Expand Up @@ -325,4 +325,8 @@ public static boolean hasCapability(final Either<Boolean, ? extends Object> eith
}
return eitherCapability.isRight() || (eitherCapability.isLeft() && eitherCapability.getLeft());
}

public static List<WorkspaceFolder> getWorkspaceFolders() {
return null;
}
}
Loading

0 comments on commit 12cd09d

Please sign in to comment.