Skip to content

Commit

Permalink
Merge pull request #6856 from matthiasblaesing/lsp_improvement
Browse files Browse the repository at this point in the history
LSP Improvement
  • Loading branch information
matthiasblaesing authored Dec 24, 2023
2 parents 2141d37 + c36a8ad commit babcc3d
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 23 deletions.
68 changes: 49 additions & 19 deletions ide/lsp.client/src/org/netbeans/modules/lsp/client/LSPBindings.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Writer;
import java.lang.ref.Reference;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;
Expand Down Expand Up @@ -54,6 +56,7 @@
import org.eclipse.lsp4j.DocumentSymbolCapabilities;
import org.eclipse.lsp4j.InitializeParams;
import org.eclipse.lsp4j.InitializeResult;
import org.eclipse.lsp4j.InitializedParams;
import org.eclipse.lsp4j.ResourceOperationKind;
import org.eclipse.lsp4j.SemanticTokens;
import org.eclipse.lsp4j.SemanticTokensCapabilities;
Expand Down Expand Up @@ -104,6 +107,7 @@
*/
public class LSPBindings {

private static final Logger LOG = Logger.getLogger(LSPBindings.class.getName());
private static final int DELAY = 500;
private static final int LSP_KEEP_ALIVE_MINUTES = 10;
private static final int INVALID_START_TIME = 1 * 60 * 1000;
Expand Down Expand Up @@ -301,26 +305,53 @@ private static LSPBindings buildBindings(ServerDescription inDescription, Projec
InputStream in = LanguageServerProviderAccessor.getINSTANCE().getInputStream(desc);
OutputStream out = LanguageServerProviderAccessor.getINSTANCE().getOutputStream(desc);
Process p = LanguageServerProviderAccessor.getINSTANCE().getProcess(desc);
Launcher<LanguageServer> launcher = new LSPLauncher.Builder<LanguageServer>()
.setLocalService(lci)
.setRemoteInterface(LanguageServer.class)
.setInput(in)
.setOutput(out)
.configureGson(gson -> {
gson.registerTypeAdapter(SemanticTokensLegend.class, new InstanceCreator<SemanticTokensLegend>() {
@Override public SemanticTokensLegend createInstance(Type type) {
return new SemanticTokensLegend(Collections.emptyList(), Collections.emptyList());
}
});
gson.registerTypeAdapter(SemanticTokens.class, new InstanceCreator<SemanticTokens>() {
@Override public SemanticTokens createInstance(Type type) {
return new SemanticTokens(Collections.emptyList());
}
});
}).create();
Launcher.Builder<LanguageServer> launcherBuilder = new LSPLauncher.Builder<LanguageServer>()
.setLocalService(lci)
.setRemoteInterface(LanguageServer.class)
.setInput(in)
.setOutput(out)
.configureGson(gson -> {
gson.registerTypeAdapter(SemanticTokensLegend.class, new InstanceCreator<SemanticTokensLegend>() {
@Override
public SemanticTokensLegend createInstance(Type type) {
return new SemanticTokensLegend(Collections.emptyList(), Collections.emptyList());
}
});
gson.registerTypeAdapter(SemanticTokens.class, new InstanceCreator<SemanticTokens>() {
@Override
public SemanticTokens createInstance(Type type) {
return new SemanticTokens(Collections.emptyList());
}
});
});

if (LOG.isLoggable(Level.FINER)) {
PrintWriter pw = new PrintWriter(new Writer() {
StringBuffer sb = new StringBuffer();

@Override
public void write(char[] cbuf, int off, int len) throws IOException {
sb.append(cbuf, off, len);
}

@Override
public void flush() throws IOException {
LOG.finer(sb.toString());
}

@Override
public void close() throws IOException {
sb.setLength(0);
sb.trimToSize();
}
});
launcherBuilder.traceMessages(pw);
}
Launcher<LanguageServer> launcher = launcherBuilder.create();
launcher.startListening();
LanguageServer server = launcher.getRemoteProxy();
InitializeResult result = initServer(p, server, dir); //XXX: what if a different root is expected????
server.initialized(new InitializedParams());
b = new LSPBindings(server, result, LanguageServerProviderAccessor.getINSTANCE().getProcess(desc));
// Register cleanup via LSPReference#run
new LSPReference(b, Utilities.activeReferenceQueue());
Expand All @@ -339,8 +370,6 @@ private static LSPBindings buildBindings(ServerDescription inDescription, Projec
return null;
}

private static final Logger LOG = Logger.getLogger(LSPBindings.class.getName());

@Messages("LBL_Connecting=Connecting to language server")
public static void addBindings(FileObject root, int port, String... extensions) {
BaseProgressUtils.showProgressDialogAndRun(() -> {
Expand All @@ -360,6 +389,7 @@ public void write(int w) throws IOException {
launcher.startListening();
LanguageServer server = launcher.getRemoteProxy();
InitializeResult result = initServer(null, server, root);
server.initialized(new InitializedParams());
LSPBindings bindings = new LSPBindings(server, result, null);

lc.setBindings(bindings);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ public CompletableFuture<Void> createProgress(WorkDoneProgressCreateParams param

private final Map<Object, ProgressHandle> key2Progress = new HashMap<>();

@Override
public void notifyProgress(ProgressParams params) {
Either<WorkDoneProgressNotification, Object> value = params.getValue();
if (value.isRight()) {
Expand Down Expand Up @@ -163,6 +164,7 @@ public void publishDiagnostics(PublishDiagnosticsParams pdp) {
if (doc == null) {
return ; //ignore...
}
assert file != null;
List<ErrorDescription> diags = pdp.getDiagnostics().stream().map(d -> {
LazyFixList fixList = allowCodeActions ? new DiagnosticFixList(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()));
Expand Down Expand Up @@ -285,11 +287,13 @@ public CompletableFuture<List<Object>> configuration(ConfigurationParams configu
return result;
}

@Override
public CompletableFuture<?> request(String method, Object parameter) {
LOG.log(Level.WARNING, "Received unhandled request: {0}: {1}", new Object[] {method, parameter});
return CompletableFuture.completedFuture(null);
}

@Override
public void notify(String method, Object parameter) {
LOG.log(Level.WARNING, "Received unhandled notification: {0}: {1}", new Object[] {method, parameter});
}
Expand Down Expand Up @@ -324,6 +328,7 @@ public boolean probablyContainsFixes() {
}

@Override
@SuppressWarnings("ReturnOfCollectionOrArrayField")
public synchronized List<Fix> getFixes() {
if (!computing && !computed) {
computing = true;
Expand All @@ -333,11 +338,17 @@ public synchronized List<Fix> getFixes() {
bindings.getTextDocumentService().codeAction(new CodeActionParams(new TextDocumentIdentifier(fileUri),
diagnostic.getRange(),
new CodeActionContext(Collections.singletonList(diagnostic)))).get();
List<Fix> fixes = commands.stream()
.map(cmd -> new CommandBasedFix(cmd))
.collect(Collectors.toList());

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

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

synchronized (this) {
this.fixes = Collections.unmodifiableList(fixes);
this.fixes = Collections.unmodifiableList(newFixes);
this.computed = true;
this.computing = false;
}
Expand Down

0 comments on commit babcc3d

Please sign in to comment.