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

Make background LSP requests wait for pending update #24

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 42 additions & 6 deletions src/main/groovy/nextflow/lsp/services/LanguageService.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import nextflow.lsp.ast.ASTNodeCache;
import nextflow.lsp.compiler.Compiler;
Expand Down Expand Up @@ -223,6 +227,7 @@ public List<DocumentLink> documentLink(DocumentLinkParams params) {
if( provider == null )
return Collections.emptyList();

awaitUpdate();
return provider.documentLink(params.getTextDocument());
}

Expand All @@ -231,6 +236,7 @@ public List<Either<SymbolInformation, DocumentSymbol>> documentSymbol(DocumentSy
if( provider == null )
return Collections.emptyList();

awaitUpdate();
return provider.documentSymbol(params.getTextDocument());
}

Expand Down Expand Up @@ -269,27 +275,57 @@ public List<? extends SymbolInformation> symbol(WorkspaceSymbolParams params) {

// --- INTERNAL

private Lock updateLock = new ReentrantLock();

private Condition updateCondition = updateLock.newCondition();

private volatile boolean awaitingUpdate;

protected void updateLater() {
awaitingUpdate = true;
updateExecutor.submit(DEBOUNCE_KEY);
}

protected void updateNow() {
updateExecutor.executeNow(DEBOUNCE_KEY);
}

protected void awaitUpdate() {
if( !awaitingUpdate )
return;

updateLock.lock();
try {
updateCondition.await(DEBOUNCE_MILLIS * 2, TimeUnit.MILLISECONDS);
}
catch( InterruptedException e ) {
}
finally {
updateLock.unlock();
}
}

/**
* Re-compile any changed files.
*/
protected void update() {
synchronized (this) {
if( !initialized )
return;
if( initialized ) {
var uris = fileCache.removeChangedFiles();

var uris = fileCache.removeChangedFiles();
log.debug("update " + DefaultGroovyMethods.join(uris, " , "));
var errors = getAstCache().update(uris, fileCache);
publishDiagnostics(errors);
}
}

log.debug("update " + DefaultGroovyMethods.join(uris, " , "));
var errors = getAstCache().update(uris, fileCache);
publishDiagnostics(errors);
updateLock.lock();
try {
updateCondition.signalAll();
awaitingUpdate = false;
}
finally {
updateLock.unlock();
}
}

Expand Down