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

LSP Client: Reduce concurrency in LSP communication and register document sychronously on startup #7759

Merged
merged 1 commit into from
Oct 12, 2024
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ public static synchronized LSPBindings getBindingsImpl(Project prj, FileObject f
for(String mt: description.mimeTypes) {
mimeType2Server.put(mt, description);
}
TextDocumentSyncServerCapabilityHandler.refreshOpenedFilesInServers();
WORKER.post(() -> cs.fireChange());
}
}
Expand Down Expand Up @@ -357,7 +358,6 @@ public void close() throws IOException {
new LSPReference(b, Utilities.activeReferenceQueue());
lci.setBindings(b);
LanguageServerProviderAccessor.getINSTANCE().setBindings(desc, b);
TextDocumentSyncServerCapabilityHandler.refreshOpenedFilesInServers();
return b;
} catch (InterruptedException | ExecutionException ex) {
LOG.log(Level.WARNING, null, ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,23 @@
*/
package org.netbeans.modules.lsp.client.bindings;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.util.Collection;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.Action;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.text.StyledDocument;
import org.netbeans.api.actions.Openable;
import org.netbeans.api.editor.mimelookup.MimeLookup;
import org.netbeans.api.lsp.StructureElement;
import org.netbeans.spi.lsp.StructureProvider;
import org.netbeans.spi.navigator.NavigatorPanel;
import org.openide.awt.Actions;
import org.openide.cookies.EditorCookie;
import org.openide.cookies.LineCookie;
import org.openide.explorer.ExplorerManager;
import org.openide.explorer.view.BeanTreeView;
import org.openide.filesystems.FileObject;
import org.openide.nodes.AbstractNode;
import org.openide.nodes.Children;
import org.openide.nodes.Node;
import org.openide.text.Line;
import org.openide.text.NbDocument;
import org.openide.util.Lookup;
import org.openide.util.NbBundle;
import org.openide.util.RequestProcessor;
import org.openide.util.lookup.AbstractLookup;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,13 @@
import org.openide.filesystems.FileObject;
import org.openide.loaders.DataObject;
import org.openide.util.Exceptions;
import org.openide.util.RequestProcessor;

/**
*
* @author lahvac
*/
public class MarkOccurrences implements BackgroundTask, CaretListener, PropertyChangeListener {

private static final RequestProcessor WORKER = new RequestProcessor(MarkOccurrences.class.getName(), 1, false, false);
private final JTextComponent component;
private Document doc;
private int caretPos;
Expand Down Expand Up @@ -139,13 +137,11 @@ public synchronized void caretUpdate(CaretEvent e) {
} else {
caretPos = -1;
}
WORKER.post(() -> {
FileObject file = NbEditorUtilities.getFileObject(doc);
FileObject file = NbEditorUtilities.getFileObject(doc);

if (file != null) {
LSPBindings.rescheduleBackgroundTask(file, this);
}
});
if (file != null) {
LSPBindings.rescheduleBackgroundTask(file, this);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,13 @@
import org.openide.modules.OnStart;
import org.openide.text.NbDocument;
import org.openide.util.Exceptions;
import org.openide.util.RequestProcessor;

/**
*
* @author lahvac
*/
public class TextDocumentSyncServerCapabilityHandler {

private final RequestProcessor WORKER = new RequestProcessor(TextDocumentSyncServerCapabilityHandler.class.getName(), 1, false, false);
private final Set<JTextComponent> lastOpened = Collections.newSetFromMap(new IdentityHashMap<>());

private void handleChange() {
Expand Down Expand Up @@ -97,17 +95,14 @@ private void ensureOpenedInServer(JTextComponent opened) {
return; //ignore

Document doc = opened.getDocument();
ensureDidOpenSent(doc);
ensureDidOpenSent(doc, true);
registerBackgroundTasks(opened);
}

public static void refreshOpenedFilesInServers() {
SwingUtilities.invokeLater(() -> {
assert SwingUtilities.isEventDispatchThread();
for (JTextComponent c : EditorRegistry.componentList()) {
h.ensureOpenedInServer(c);
}
});
for (JTextComponent c : EditorRegistry.componentList()) {
h.ensureOpenedInServer(c);
}
}

private static final TextDocumentSyncServerCapabilityHandler h = new TextDocumentSyncServerCapabilityHandler();
Expand All @@ -132,7 +127,7 @@ private void documentOpened(Document doc) {

openDocument2PanesCount.computeIfAbsent(doc, d -> {
doc.putProperty(TextDocumentSyncServerCapabilityHandler.class, true);
ensureDidOpenSent(doc);
ensureDidOpenSent(doc, false);
doc.addDocumentListener(new DocumentListener() { //XXX: listener
int version; //XXX: proper versioning!
@Override
Expand Down Expand Up @@ -160,12 +155,13 @@ private void fireEvent(int start, String newText, String oldText) {
boolean typingModification = DocumentUtilities.isTypingModification(doc);
long documentVersion = DocumentUtilities.getDocumentVersion(doc);

WORKER.post(() -> {
LSPBindings server = LSPBindings.getBindings(file);
LSPBindings server = LSPBindings.getBindings(file);

if (server == null)
return ; //ignore
if(server == null) {
return;
}

server.runOnBackground(() -> {
TextDocumentSyncKind syncKind = TextDocumentSyncKind.None;
Either<TextDocumentSyncKind, TextDocumentSyncOptions> sync = server.getInitResult().getCapabilities().getTextDocumentSync();
if (sync != null) {
Expand Down Expand Up @@ -243,23 +239,25 @@ private synchronized void editorOpened(JTextComponent c) {

private synchronized void editorClosed(JTextComponent c) {
Document doc = c.getDocument();

Integer count = openDocument2PanesCount.getOrDefault(doc, -1);
if (count > 0) {
openDocument2PanesCount.put(doc, --count);
}
if (count == 0) {
//TODO modified!
WORKER.post(() -> {
FileObject file = NbEditorUtilities.getFileObject(doc);
FileObject file = NbEditorUtilities.getFileObject(doc);

if (file == null)
return; //ignore
if (file == null)
return; //ignore

LSPBindings server = LSPBindings.getBindings(file);
//TODO modified!
LSPBindings server = LSPBindings.getBindings(file);

if (server == null)
return ; //ignore
if (server == null) {
return;
}

server.runOnBackground(() -> {
TextDocumentIdentifier di = new TextDocumentIdentifier();
di.setUri(Utils.toURI(file));
DidCloseTextDocumentParams params = new DidCloseTextDocumentParams(di);
Expand All @@ -271,17 +269,19 @@ private synchronized void editorClosed(JTextComponent c) {
}
}

private void ensureDidOpenSent(Document doc) {
WORKER.post(() -> {
FileObject file = NbEditorUtilities.getFileObject(doc);
@SuppressWarnings("AssignmentToMethodParameter")
private void ensureDidOpenSent(Document doc, boolean sync) {
FileObject file = NbEditorUtilities.getFileObject(doc);

if (file == null)
return; //ignore
if (file == null)
return; //ignore

LSPBindings server = LSPBindings.getBindings(file);
LSPBindings server = LSPBindings.getBindings(file);

if (server == null)
return ; //ignore

if (server == null)
return ; //ignore
Runnable task = () -> {

if (!server.getOpenedFiles().add(file)) {
//already opened:
Expand Down Expand Up @@ -309,22 +309,28 @@ private void ensureDidOpenSent(Document doc) {

server.getTextDocumentService().didOpen(new DidOpenTextDocumentParams(textDocumentItem));
LSPBindings.scheduleBackgroundTasks(file);
});
};

if (sync) {
task.run();
} else {
server.runOnBackground(task);
}
}

private void registerBackgroundTasks(JTextComponent c) {
Document doc = c.getDocument();
WORKER.post(() -> {
FileObject file = NbEditorUtilities.getFileObject(doc);
FileObject file = NbEditorUtilities.getFileObject(doc);

if (file == null)
return; //ignore
if (file == null)
return; //ignore

LSPBindings server = LSPBindings.getBindings(file);
LSPBindings server = LSPBindings.getBindings(file);

if (server == null)
return ; //ignore
if (server == null)
return ; //ignore

server.runOnBackground(() -> {
SwingUtilities.invokeLater(() -> {
if (c.getClientProperty(MarkOccurrences.class) == null) {
MarkOccurrences mo = new MarkOccurrences(c);
Expand Down
Loading