Skip to content

Commit

Permalink
LSP Client: Improve handling of DiagnosticFixList (don't call diagnos…
Browse files Browse the repository at this point in the history
…tic beyond document, reduce log level)

 - Log errors in DiagnosticFixList#getFixes by using LOG#log with level
   INFO, instead of Exceptions#printStackTrace which logs on level
   SEVERE. This reduces the level so much, that is not raised as a user
   visible (exception error marker in status line) message anymore.

 - it could happen, that the fixlist for a position outside the
   document range is queried, when characters at the end of a document
   were deleted. This is no prevented.
  • Loading branch information
matthiasblaesing committed Sep 16, 2024
1 parent f20b1a9 commit 110c7db
Showing 1 changed file with 16 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ public void publishDiagnostics(PublishDiagnosticsParams pdp) {
}
assert file != null;
List<ErrorDescription> diags = pdp.getDiagnostics().stream().map(d -> {
LazyFixList fixList = allowCodeActions ? new DiagnosticFixList(pdp.getUri(), d) : ErrorDescriptionFactory.lazyListForFixes(Collections.emptyList());
LazyFixList fixList = allowCodeActions ? new DiagnosticFixList(doc, pdp.getUri(), d) : ErrorDescriptionFactory.lazyListForFixes(Collections.emptyList());
return ErrorDescriptionFactory.createErrorDescription(severityMap.get(d.getSeverity()), d.getMessage(), fixList, file, Utils.getOffset(doc, d.getRange().getStart()), Utils.getOffset(doc, d.getRange().getEnd()));
}).collect(Collectors.toList());
HintsController.setErrors(doc, LanguageClientImpl.class.getName(), diags);
Expand Down Expand Up @@ -302,12 +302,14 @@ private final class DiagnosticFixList implements LazyFixList {

private final PropertyChangeSupport pcs = new PropertyChangeSupport(this);
private final String fileUri;
private final Document doc;
private final Diagnostic diagnostic;
private List<Fix> fixes;
private boolean computing;
private boolean computed;

public DiagnosticFixList(String fileUri, Diagnostic diagnostic) {
public DiagnosticFixList(Document doc, String fileUri, Diagnostic diagnostic) {
this.doc = doc;
this.fileUri = fileUri;
this.diagnostic = diagnostic;
}
Expand All @@ -334,17 +336,19 @@ public synchronized List<Fix> getFixes() {
computing = true;
bindings.runOnBackground(() -> {
try {
List<Either<Command, CodeAction>> commands =
bindings.getTextDocumentService().codeAction(new CodeActionParams(new TextDocumentIdentifier(fileUri),
diagnostic.getRange(),
new CodeActionContext(Collections.singletonList(diagnostic)))).get();

List<Fix> newFixes = Collections.emptyList();

if (commands != null) {
newFixes = commands.stream()
.map(cmd -> new CommandBasedFix(cmd))
.collect(Collectors.toList());
if (Utils.getOffset(doc, diagnostic.getRange().getEnd()) < doc.getEndPosition().getOffset()) {
List<Either<Command, CodeAction>> commands
= bindings.getTextDocumentService().codeAction(new CodeActionParams(new TextDocumentIdentifier(fileUri),
diagnostic.getRange(),
new CodeActionContext(Collections.singletonList(diagnostic)))).get();

if (commands != null) {
newFixes = commands.stream()
.map(cmd -> new CommandBasedFix(cmd))
.collect(Collectors.toList());
}
}

synchronized (this) {
Expand All @@ -355,7 +359,7 @@ public synchronized List<Fix> getFixes() {
pcs.firePropertyChange(PROP_COMPUTED, null, null);
pcs.firePropertyChange(PROP_FIXES, null, null);
} catch (InterruptedException | ExecutionException ex) {
Exceptions.printStackTrace(ex);
LOG.log(Level.INFO, "Failure fetching DiagnosticFixList (at least typescript server has known problems)", ex);
}
});
}
Expand Down

0 comments on commit 110c7db

Please sign in to comment.