Skip to content

LSP client stops working on file after renaming it #7829

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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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 @@ -45,6 +45,7 @@
import java.util.Set;
import java.util.WeakHashMap;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
Expand Down Expand Up @@ -143,7 +144,7 @@ public class LSPBindings {
}

private static final Map<FileObject, Map<BackgroundTask, RequestProcessor.Task>> backgroundTasks = new WeakHashMap<>();
private final Set<FileObject> openedFiles = new HashSet<>();
private final Map<FileObject, Boolean> openedFiles = new ConcurrentHashMap<>();

public static synchronized LSPBindings getBindings(FileObject file) {
for (Entry<FileObject, Map<String, LSPBindings>> e : workspace2Extension2Server.entrySet()) {
Expand Down Expand Up @@ -542,7 +543,7 @@ private static Map<BackgroundTask, Task> backgroundTasksMapFor(FileObject file)
return backgroundTasks.computeIfAbsent(file, f -> new IdentityHashMap<>());
}

public Set<FileObject> getOpenedFiles() {
public Map<FileObject, Boolean> getOpenedFiles() {
return openedFiles;
}

Expand Down
41 changes: 39 additions & 2 deletions ide/lsp.client/src/org/netbeans/modules/lsp/client/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import org.openide.filesystems.URLMapper;
import org.openide.loaders.DataFolder;
import org.openide.loaders.DataObject;
import org.openide.loaders.DataObjectNotFoundException;
import org.openide.text.Line;
import org.openide.text.NbDocument;
import org.openide.util.Exceptions;
Expand All @@ -71,6 +72,23 @@ public static String toURI(FileObject file) {
return file.toURI().toString().replace("file:/", "file:///");
}

public static String uriReplaceFilename(String inputUriString, String filename) {
try {
URI inputUri = URI.create(inputUriString);
URI newUri = inputUri.resolve(new URI(null, null, filename, null));
if ("file".equals(newUri.getScheme())) {
// By default the java URI routines drop empty hostnames from
// file URIs. Normalize this to the empty hostname. See also
// #toURI
return new URI("file", "", newUri.getPath(), null).toString();
} else {
return newUri.toString();
}
} catch (URISyntaxException ex) {
throw new IllegalArgumentException(ex);
}
}

public static Position createPosition(Document doc, int offset) throws BadLocationException {
return new Position(LineDocumentUtils.getLineIndex((LineDocument) doc, offset),
offset - LineDocumentUtils.getLineStart((LineDocument) doc, offset));
Expand Down Expand Up @@ -137,7 +155,7 @@ public static void applyWorkspaceEdit(WorkspaceEdit edit) {
private static void applyEdits(String uri, List<TextEdit> edits) {
try {
FileObject file = URLMapper.findFileObject(new URI(uri).toURL());
EditorCookie ec = file.getLookup().lookup(EditorCookie.class);
EditorCookie ec = lookupForFile(file, EditorCookie.class);
Document doc = ec != null ? ec.openDocument() : null;
if (doc == null) {
return ;
Expand Down Expand Up @@ -274,7 +292,7 @@ public static void open(String targetUri, Range targetRange) {
FileObject targetFile = fromURI(targetUri);

if (targetFile != null) {
LineCookie lc = targetFile.getLookup().lookup(LineCookie.class);
LineCookie lc = lookupForFile(targetFile, LineCookie.class);

//TODO: expecting lc != null!

Expand All @@ -288,6 +306,25 @@ public static void open(String targetUri, Range targetRange) {
}
}

public static <T> T lookupForFile(FileObject targetFile, Class<T> clazz) {
if(targetFile == null) {
return null;
}
T lc = null;
try {
if (lc == null) {
DataObject dataObject = DataObject.find(targetFile);
lc = dataObject.getLookup().lookup(clazz);
}
} catch (DataObjectNotFoundException ex) {
// Ignore
}
if (lc == null) {
lc = targetFile.getLookup().lookup(clazz);
}
return lc;
}

private static final Comparator<TextEdit> rangeReverseSort = (s1, s2) -> {
int l1 = s1.getRange().getEnd().getLine();
int l2 = s2.getRange().getEnd().getLine();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public void release() {

@Override
public void run(LSPBindings bindings, FileObject file) {
EditorCookie ec = file.getLookup().lookup(EditorCookie.class);
EditorCookie ec = Utils.lookupForFile(file, EditorCookie.class);
Document doc = ec != null ? ec.getDocument() : null;
if (doc == null) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public void notifyProgress(ProgressParams params) {
public void publishDiagnostics(PublishDiagnosticsParams pdp) {
try {
FileObject file = URLMapper.findFileObject(new URI(pdp.getUri()).toURL());
EditorCookie ec = file != null ? file.getLookup().lookup(EditorCookie.class) : null;
EditorCookie ec = Utils.lookupForFile(file, EditorCookie.class);
Document doc = ec != null ? ec.getDocument() : null;
if (doc == null) {
return ; //ignore...
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,24 @@
*/
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.modules.lsp.client.Utils;
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 Expand Up @@ -75,7 +67,7 @@ void removeBackgroundTask(FileObject fo) {

void refreshStructure(FileObject fo) {
LOG.log(Level.INFO, "panelActivated: {0}", fo);
EditorCookie ec = fo.getLookup().lookup(EditorCookie.class);
EditorCookie ec = Utils.lookupForFile(fo, EditorCookie.class);
if (ec != null) {
StyledDocument doc = ec.getDocument();
if (doc != null) {
Expand Down Expand Up @@ -130,12 +122,12 @@ private StructureElementNode(StructureElement e, FileObject fo, InstanceContent
setShortDescription(e.getDetail());
setIconBaseWithExtension(Icons.getSymbolIconBase(e.getKind()));
Openable open = () -> {
EditorCookie ec = fo.getLookup().lookup(EditorCookie.class);
EditorCookie ec = Utils.lookupForFile(fo, EditorCookie.class);
if (ec != null) {
StyledDocument doc = ec.getDocument();
if (doc != null) {
int lineNumber = NbDocument.findLineNumber(doc, e.getSelectionStartOffset());
LineCookie lines = fo.getLookup().lookup(LineCookie.class);
LineCookie lines = Utils.lookupForFile(fo, LineCookie.class);
if (lines != null) {
Line at = lines.getLineSet().getOriginal(lineNumber);
if (at != null) {
Expand Down
Loading
Loading