Skip to content

Commit

Permalink
feat: Path patching API
Browse files Browse the repository at this point in the history
Fixes #702

Signed-off-by: azerr <[email protected]>
  • Loading branch information
angelozerr committed Dec 17, 2024
1 parent 926caf2 commit fb6cdb9
Show file tree
Hide file tree
Showing 65 changed files with 716 additions and 372 deletions.
11 changes: 7 additions & 4 deletions docs/LSPApi.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,18 @@ public class MyLanguageServerFactory implements LanguageServerFactory {

| API | Description | Default Behaviour |
|--------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------|-------------------|
| boolean isEnabled(VirtualFile) | Returns `true` if the language server is enabled for the given file and `false` otherwise. | `true` |
| boolean isEnabled(VirtualFile) | Returns `true` if the language server is enabled for the given file and `false` otherwise. | `true` |
| URI getFileUri(VirtualFile file) | Returns the file Uri from the given virtual file and null otherwise (to use default `FileUriSupport.DEFAULT.getFileUri`). | `null` |
| VirtualFile findFileByUri(String fileUri) | Returns the virtual file found by the given file Uri and null otherwise (to use default `FileUriSupport.DEFAULT.findFileByUri`). | `null` |
| boolean isCaseSensitive(PsiFile file) | Returns `true` if the language grammar for the given file is case-sensitive and `false` otherwise. | `false` |
| boolean keepServerAlive() | Returns `true` if the server is kept alive even if all files associated with the language server are closed and `false` otherwise. | `false` |
| boolean canStopServerByUser() | Returns `true` if the user can stop the language server in LSP console from the context menu and `false` otherwise. | `true` |
| Project getProject() | Returns the project. | |
| LanguageServerDefinition getServerDefinition() | Returns the language server definition. | |
| boolean isServerDefinition(@NotNull String languageServerId) | Returns `true` if the given language server id matches the server definition and `false` otherwise. | |
| ServerStatus getServerStatus() | Returns the server status. | |
| LanguageServer getLanguageServer() | Returns the LSP4J language server. | |
| boolean keepServerAlive() | Returns `true` if the server is kept alive even if all files associated with the language server are closed and `false` otherwise. | `false` |
| boolean canStopServerByUser() | Returns `true` if the user can stop the language server in LSP console from the context menu and `false` otherwise. | `true` |
| boolean isEnabled(VirtualFile) | Returns `true` if the language server is enabled for the given file and `false` otherwise. | `true` |
| boolean isCaseSensitive(PsiFile file) | Returns `true` if the language grammar for the given file is case-sensitive and `false` otherwise. | `false` |

```java
package my.language.server;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
Expand All @@ -46,12 +45,12 @@ public class DocumentContentSynchronizer implements DocumentListener {
@NotNull final CompletableFuture<Void> didOpenFuture;

public DocumentContentSynchronizer(@NotNull LanguageServerWrapper languageServerWrapper,
@NotNull URI fileUri,
@NotNull String fileUri,
@NotNull VirtualFile file,
@NotNull Document document,
@Nullable TextDocumentSyncKind syncKind) {
this.languageServerWrapper = languageServerWrapper;
this.fileUri = fileUri.toASCIIString();
this.fileUri = fileUri;
this.syncKind = syncKind != null ? syncKind : TextDocumentSyncKind.Full;

this.document = document;
Expand Down
37 changes: 17 additions & 20 deletions src/main/java/com/redhat/devtools/lsp4ij/LSPFileListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -55,14 +54,11 @@ public void fileClosed(@NotNull FileEditorManager source, @NotNull VirtualFile f
return;
}
// Manage textDocument/didClose
URI uri = LSPIJUtils.toUri(file);
if (uri != null) {
try {
// Disconnect the given file from the current language servers
languageServerWrapper.disconnect(uri, !languageServerWrapper.isDisposed());
} catch (Exception e) {
LOGGER.warn("Error while disconnecting the file '" + uri + "' from all language servers", e);
}
try {
// Disconnect the given file from the current language servers
languageServerWrapper.disconnect(file, !languageServerWrapper.isDisposed());
} catch (Exception e) {
LOGGER.warn("Error while disconnecting the file '" + file.getUrl() + "' from language server '" + languageServerWrapper.getServerDefinition().getDisplayName() + "'.", e);
}
}

Expand Down Expand Up @@ -96,7 +92,7 @@ private void moveFile(URI oldFileUri, VirtualFile newFile) {
if (isMatchFilePatterns(oldFileUri, WatchKind.Delete)) {
changes.add(fe(oldFileUri, FileChangeType.Deleted));
}
URI newFileUri = LSPIJUtils.toUri(newFile);
URI newFileUri = languageServerWrapper.toUri(newFile);
if (isMatchFilePatterns(newFileUri, WatchKind.Create)) {
changes.add(fe(newFileUri, FileChangeType.Created));
}
Expand All @@ -109,7 +105,7 @@ private void moveFile(URI oldFileUri, VirtualFile newFile) {
@Override
public void contentsChanged(@NotNull VirtualFileEvent event) {
VirtualFile file = event.getFile();
URI uri = LSPIJUtils.toUri(file);
URI uri = languageServerWrapper.toUri(file);
if (uri != null) {
LSPVirtualFileData documentListener = languageServerWrapper.connectedDocuments.get(uri);
if (documentListener != null) {
Expand All @@ -126,7 +122,7 @@ public void contentsChanged(@NotNull VirtualFileEvent event) {
@Override
public void fileCreated(@NotNull VirtualFileEvent event) {
VirtualFile file = event.getFile();
URI uri = LSPIJUtils.toUri(file);
URI uri = languageServerWrapper.toUri(file);
if (isMatchFilePatterns(uri, WatchKind.Create)) {
// 2. Send a workspace/didChangeWatchedFiles with 'Created' file change type.
didChangeWatchedFiles(fe(uri, FileChangeType.Created));
Expand All @@ -136,7 +132,7 @@ public void fileCreated(@NotNull VirtualFileEvent event) {
@Override
public void fileDeleted(@NotNull VirtualFileEvent event) {
VirtualFile file = event.getFile();
URI uri = LSPIJUtils.toUri(file);
URI uri = languageServerWrapper.toUri(file);
if (isMatchFilePatterns(uri, WatchKind.Delete)) {
// Send a workspace/didChangeWatchedFiles with 'Deleted' file change type.
didChangeWatchedFiles(fe(uri, FileChangeType.Deleted));
Expand Down Expand Up @@ -166,20 +162,21 @@ private FileEvent fe(URI uri, FileChangeType type) {
* to a different directory.
*
* @param virtualParentFile directory of the file after renaming
* @param oldFileName file name before renaming
* @param virtualNewFile virtual file after renaming
* @param oldFileName file name before renaming
* @param virtualNewFile virtual file after renaming
* @return URI of the file before renaming
*/
private @NotNull URI didRename(VirtualFile virtualParentFile, String oldFileName, VirtualFile virtualNewFile) {
File parentFile = VfsUtilCore.virtualToIoFile(virtualParentFile);
URI oldUri = LSPIJUtils.toUri(new File(parentFile, oldFileName));
private @NotNull URI didRename(@NotNull VirtualFile virtualParentFile,
@NotNull String oldFileName,
@NotNull VirtualFile virtualNewFile) {
URI parentFileUri = languageServerWrapper.toUri(virtualParentFile);
URI oldUri = parentFileUri.resolve(oldFileName);
boolean docIsConnected = languageServerWrapper.isConnectedTo(oldUri);
if (docIsConnected) {
// 1. Send a textDocument/didClose for the old file name
languageServerWrapper.disconnect(oldUri, false);
// 2. Send a textDocument/didOpen for the new file name
File newFile = VfsUtilCore.virtualToIoFile(virtualNewFile);
URI newUri = LSPIJUtils.toUri(newFile);
URI newUri = languageServerWrapper.toUri(virtualNewFile);
if (!languageServerWrapper.isConnectedTo(newUri)) {
languageServerWrapper.connect(virtualNewFile, null);
}
Expand Down
Loading

0 comments on commit fb6cdb9

Please sign in to comment.