Skip to content

Commit

Permalink
refact: small refactor to improve code reuse
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenporras committed Aug 26, 2024
1 parent 2d89929 commit 0cc63da
Showing 1 changed file with 22 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,7 @@
import org.eclipse.lsp4j.CompletionItem;
import org.eclipse.lsp4j.CompletionItemDefaults;
import org.eclipse.lsp4j.CompletionList;
import org.eclipse.lsp4j.CompletionOptions;
import org.eclipse.lsp4j.CompletionParams;
import org.eclipse.lsp4j.SignatureHelpOptions;
import org.eclipse.lsp4j.SignatureHelpParams;
import org.eclipse.lsp4j.SignatureInformation;
import org.eclipse.lsp4j.jsonrpc.CancelChecker;
Expand Down Expand Up @@ -216,40 +214,28 @@ private String createErrorMessage(int offset, Exception ex) {

private void initiateLanguageServers(IDocument document) {
if (currentDocument != document) {
this.currentDocument = document;
if (this.completionLanguageServersFuture != null) {
try {
this.completionLanguageServersFuture.cancel(true);
} catch (CancellationException ex) {
// nothing
}
}
if (this.contextInformationLanguageServersFuture != null) {
try {
this.contextInformationLanguageServersFuture.cancel(true);
} catch (CancellationException ex) {
// nothing
}
}
this.completionTriggerChars = NO_CHARS;
this.contextTriggerChars = NO_CHARS;
currentDocument = document;
cancelFuture(completionLanguageServersFuture);
cancelFuture(contextInformationLanguageServersFuture);

this.completionLanguageServersFuture = LanguageServers.forDocument(document)
completionTriggerChars = NO_CHARS;
contextTriggerChars = NO_CHARS;

completionLanguageServersFuture = LanguageServers.forDocument(document)
.withFilter(capabilities -> capabilities.getCompletionProvider() != null) //
.collectAll((w, ls) -> {
CompletionOptions provider = castNonNull(w.getServerCapabilities()).getCompletionProvider();
List<String> triggerChars = castNonNull(w.getServerCapabilities()).getCompletionProvider().getTriggerCharacters();
synchronized (completionTriggerCharsSemaphore) {
completionTriggerChars = mergeTriggers(completionTriggerChars,
provider.getTriggerCharacters());
completionTriggerChars = mergeTriggers(completionTriggerChars,triggerChars);
}
return CompletableFuture.completedFuture(null);
});
this.contextInformationLanguageServersFuture = LanguageServers.forDocument(document)
contextInformationLanguageServersFuture = LanguageServers.forDocument(document)
.withFilter(capabilities -> capabilities.getSignatureHelpProvider() != null) //
.collectAll((w, ls) -> {
SignatureHelpOptions provider = castNonNull(w.getServerCapabilities()).getSignatureHelpProvider();
List<String> triggerChars = castNonNull(w.getServerCapabilities()).getSignatureHelpProvider().getTriggerCharacters();
synchronized (contextTriggerCharsSemaphore) {
contextTriggerChars = mergeTriggers(contextTriggerChars, provider.getTriggerCharacters());
contextTriggerChars = mergeTriggers(contextTriggerChars, triggerChars);
}
return CompletableFuture.completedFuture(null);
});
Expand Down Expand Up @@ -341,6 +327,16 @@ private static IContextInformation toContextInformation(SignatureInformation inf
return new ContextInformation(information.getLabel(), signature.toString());
}

private void cancelFuture(@Nullable CompletableFuture<List<Void>> future) {
if (future != null) {
try {
future.cancel(true);
} catch (CancellationException ex) {
// nothing
}
}
}

private void getFuture(@Nullable CompletableFuture<List<Void>> future) {
if (future == null) {
return;
Expand Down

0 comments on commit 0cc63da

Please sign in to comment.