From 44d5c77383f6f7aff425edc68e3d413ef07cf9fe Mon Sep 17 00:00:00 2001 From: zaaarf Date: Sat, 14 Sep 2024 23:28:16 +0200 Subject: [PATCH] fix: various fixes on vfs --- .../java/mp/code/intellij/util/FileUtil.java | 2 +- ...CodeMPFolder.java => CodeMPDirectory.java} | 10 +-- .../java/mp/code/intellij/vfs/CodeMPFile.java | 16 ++-- .../vfs/CodeMPFileIndexContributor.java | 32 ++++++++ .../code/intellij/vfs/CodeMPFileSystem.java | 41 +++++----- .../java/mp/code/intellij/vfs/CodeMPPath.java | 2 + src/main/resources/META-INF/plugin.xml | 2 + src/main/resources/META-INF/pluginIcon.svg | 74 ++++++++++++++++--- 8 files changed, 136 insertions(+), 43 deletions(-) rename src/main/java/mp/code/intellij/vfs/{CodeMPFolder.java => CodeMPDirectory.java} (89%) create mode 100644 src/main/java/mp/code/intellij/vfs/CodeMPFileIndexContributor.java diff --git a/src/main/java/mp/code/intellij/util/FileUtil.java b/src/main/java/mp/code/intellij/util/FileUtil.java index 378a027..9792b83 100644 --- a/src/main/java/mp/code/intellij/util/FileUtil.java +++ b/src/main/java/mp/code/intellij/util/FileUtil.java @@ -45,7 +45,7 @@ public class FileUtil { return CodeMP.getClient("buffer access") .getWorkspace(path.getWorkspaceName()) .flatMap(ws -> { - String[] matches = ws.getFileTree(Optional.of(path.getRealPath())); + String[] matches = ws.getFileTree(Optional.of(path.getRealPath()), true); if(matches.length == 0) return Optional.empty(); Optional controller = ws.getBuffer(path.getRealPath()); if(controller.isPresent()) return controller; diff --git a/src/main/java/mp/code/intellij/vfs/CodeMPFolder.java b/src/main/java/mp/code/intellij/vfs/CodeMPDirectory.java similarity index 89% rename from src/main/java/mp/code/intellij/vfs/CodeMPFolder.java rename to src/main/java/mp/code/intellij/vfs/CodeMPDirectory.java index 24a79dc..aa96b83 100644 --- a/src/main/java/mp/code/intellij/vfs/CodeMPFolder.java +++ b/src/main/java/mp/code/intellij/vfs/CodeMPDirectory.java @@ -1,6 +1,5 @@ package mp.code.intellij.vfs; -import com.intellij.openapi.vfs.VirtualFile; import lombok.Getter; import mp.code.intellij.CodeMP; import org.jetbrains.annotations.NotNull; @@ -13,10 +12,10 @@ import java.util.Arrays; import java.util.Optional; @Getter -public class CodeMPFolder extends CodeMPFile { +public class CodeMPDirectory extends CodeMPFile { - public CodeMPFolder(CodeMPFileSystem fileSystem, CodeMPPath path) { - super(fileSystem, path); + public CodeMPDirectory(CodeMPFileSystem fileSystem, CodeMPPath path) { + super(fileSystem, path, true); } @Override @@ -39,7 +38,7 @@ public class CodeMPFolder extends CodeMPFile { return CodeMP.getClient("get folder children") .getWorkspace(this.path.getWorkspaceName()) .map(ws -> - Arrays.stream(ws.getFileTree(Optional.of(this.path.getRealPath()))) + Arrays.stream(ws.getFileTree(Optional.of(this.path.getRealPath()), false)) .map(p -> new CodeMPPath(this.path.getWorkspaceName(), p)) .map(CodeMPPath::join) .map(this.fileSystem::findFileByPath) @@ -80,4 +79,5 @@ public class CodeMPFolder extends CodeMPFile { public @NotNull InputStream getInputStream() throws IOException { throw new RuntimeException("WHAT FOLDER INPUT"); } + } diff --git a/src/main/java/mp/code/intellij/vfs/CodeMPFile.java b/src/main/java/mp/code/intellij/vfs/CodeMPFile.java index ae1f1ca..4694975 100644 --- a/src/main/java/mp/code/intellij/vfs/CodeMPFile.java +++ b/src/main/java/mp/code/intellij/vfs/CodeMPFile.java @@ -1,7 +1,8 @@ package mp.code.intellij.vfs; -import com.intellij.openapi.util.NlsSafe; +import com.intellij.ide.projectView.impl.ProjectTreeStructure; import com.intellij.openapi.vfs.VirtualFile; +import com.intellij.openapi.vfs.VirtualFileWithId; import lombok.Getter; import lombok.RequiredArgsConstructor; import mp.code.exceptions.ControllerException; @@ -22,9 +23,14 @@ import java.util.Optional; public class CodeMPFile extends VirtualFile { protected final CodeMPFileSystem fileSystem; protected final CodeMPPath path; + protected final boolean isDirectory; // TODO exists ONLY for ez debugging, remove afterwards + + public CodeMPFile(CodeMPFileSystem fs, CodeMPPath p) { + this(fs, p, false); + } @Override - public @NotNull @NlsSafe String getName() { + public @NotNull String getName() { return this.path.getFileName(); } @@ -47,15 +53,15 @@ public class CodeMPFile extends VirtualFile { public boolean isValid() { return CodeMP.getClient("validity check") .getWorkspace(this.path.getWorkspaceName()) - .map(ws -> ws.getFileTree(Optional.of(this.path.getRealPath()))) + .map(ws -> ws.getFileTree(Optional.of(this.path.getRealPath()), true)) .map(buf -> buf.length != 0) .orElse(false); } @Override - public @Nullable CodeMPFolder getParent() { + public @Nullable CodeMPDirectory getParent() { return this.path.getParent() - .map(parent -> new CodeMPFolder(this.fileSystem, parent)) + .map(parent -> new CodeMPDirectory(this.fileSystem, parent)) .orElse(null); } diff --git a/src/main/java/mp/code/intellij/vfs/CodeMPFileIndexContributor.java b/src/main/java/mp/code/intellij/vfs/CodeMPFileIndexContributor.java new file mode 100644 index 0000000..388e1af --- /dev/null +++ b/src/main/java/mp/code/intellij/vfs/CodeMPFileIndexContributor.java @@ -0,0 +1,32 @@ +package mp.code.intellij.vfs; + +import com.intellij.platform.workspace.jps.entities.ModuleEntity; +import com.intellij.platform.workspace.storage.EntityStorage; +import com.intellij.workspaceModel.core.fileIndex.WorkspaceFileIndexContributor; +import com.intellij.workspaceModel.core.fileIndex.WorkspaceFileKind; +import com.intellij.workspaceModel.core.fileIndex.WorkspaceFileSetRegistrar; +import org.jetbrains.annotations.NotNull; + +@SuppressWarnings("UnstableApiUsage") +public class CodeMPFileIndexContributor implements WorkspaceFileIndexContributor { + @Override + public @NotNull Class getEntityClass() { + return ModuleEntity.class; + } + + @Override + public void registerFileSets( + @NotNull ModuleEntity moduleEntity, + @NotNull WorkspaceFileSetRegistrar workspaceFileSetRegistrar, + @NotNull EntityStorage entityStorage + ) { + moduleEntity.getContentRoots().forEach(contentRootEntity -> { + workspaceFileSetRegistrar.registerFileSet( + contentRootEntity.getUrl(), + WorkspaceFileKind.CONTENT, + moduleEntity, + null + ); + }); + } +} diff --git a/src/main/java/mp/code/intellij/vfs/CodeMPFileSystem.java b/src/main/java/mp/code/intellij/vfs/CodeMPFileSystem.java index 7ccc9b9..09debf8 100644 --- a/src/main/java/mp/code/intellij/vfs/CodeMPFileSystem.java +++ b/src/main/java/mp/code/intellij/vfs/CodeMPFileSystem.java @@ -33,8 +33,8 @@ import java.util.function.BiConsumer; * - Already open remote module will crash if not for that janky try-catch in {@link #findFileByPath(String)}, maybe * try to connect quietly? */ -@Getter -public class CodeMPFileSystem extends VirtualFileSystem { +@Getter // implements NonPhysicalFileSystem? +public class CodeMPFileSystem extends VirtualFileSystem { // implements NonPhysicalFileSystem { public static String PROTOCOL = "codemp"; private final Set listeners; @@ -51,31 +51,30 @@ public class CodeMPFileSystem extends VirtualFileSystem { public @Nullable CodeMPFile findFileByPath(@NotNull @NonNls String path) { CodeMPPath cmpPath = new CodeMPPath(path); try { - return CodeMP.getClient("file seek") - .getWorkspace(cmpPath.getWorkspaceName()) - .filter(ws -> ws.getFileTree(Optional.of(cmpPath.getRealPath())).length != 0) - .map(ws -> new CodeMPFile(this, cmpPath)) - .orElseGet(() -> new CodeMPFolder(this, cmpPath)); + return this.fileOrFolderByPath(cmpPath); } catch(NotConnectedException ex) { - CodeMPSettings.State state = Objects.requireNonNull(CodeMPSettings.getInstance().getState()); - Credentials credentials = Objects.requireNonNull(state.getCredentials()); try { + CodeMPSettings.State state = Objects.requireNonNull(CodeMPSettings.getInstance().getState()); + Credentials credentials = Objects.requireNonNull(state.getCredentials()); CodeMP.connect( - Objects.requireNonNull(state.getServerUrl()), Objects.requireNonNull(credentials.getUserName()), Objects.requireNonNull(credentials.getPasswordAsString()) ); - return CodeMP.getClient("file seek") - .getWorkspace(cmpPath.getWorkspaceName()) - .filter(ws -> ws.getFileTree(Optional.of(cmpPath.getRealPath())).length != 0) - .map(ws -> new CodeMPFile(this, cmpPath)) - .orElseGet(() -> new CodeMPFolder(this, cmpPath)); - } catch(ConnectionException e) { + return this.fileOrFolderByPath(cmpPath); + } catch(ConnectionException | NullPointerException e) { return null; } // TODO this sucks } } + private CodeMPFile fileOrFolderByPath(CodeMPPath cmpPath) { + return CodeMP.getClient("file seek") + .getWorkspace(cmpPath.getWorkspaceName()) + .filter(ws -> ws.getFileTree(Optional.ofNullable(cmpPath.getRealPath()), true).length != 0).map(ws -> new CodeMPFile(this, cmpPath)) + .orElseGet(() -> new CodeMPDirectory(this, cmpPath)); + } + + @Override public void refresh(boolean asynchronous) { // TODO find out if and where ij stores filetree @@ -127,7 +126,7 @@ public class CodeMPFileSystem extends VirtualFileSystem { @Override protected @NotNull CodeMPFile createChildFile(Object requester, @NotNull VirtualFile vDir, @NotNull String fileName) throws IOException { - if(vDir instanceof CodeMPFolder parent) { + if(vDir instanceof CodeMPDirectory parent) { try { Optional ws = CodeMP.getClient("delete file").getWorkspace(parent.path.getWorkspaceName()); if(ws.isPresent()) { @@ -147,13 +146,13 @@ public class CodeMPFileSystem extends VirtualFileSystem { } @Override - protected @NotNull CodeMPFolder createChildDirectory( + protected @NotNull CodeMPDirectory createChildDirectory( Object requester, @NotNull VirtualFile vDir, @NotNull String dirName ) throws IOException { - if(vDir instanceof CodeMPFolder parent) { - return new CodeMPFolder( + if(vDir instanceof CodeMPDirectory parent) { + return new CodeMPDirectory( this, parent.path.resolve(dirName) ); @@ -207,4 +206,6 @@ public class CodeMPFileSystem extends VirtualFileSystem { public @Nullable Path getNioPath(@NotNull VirtualFile file) { return file.toNioPath(); } + + } diff --git a/src/main/java/mp/code/intellij/vfs/CodeMPPath.java b/src/main/java/mp/code/intellij/vfs/CodeMPPath.java index e939535..c42e70c 100644 --- a/src/main/java/mp/code/intellij/vfs/CodeMPPath.java +++ b/src/main/java/mp/code/intellij/vfs/CodeMPPath.java @@ -1,5 +1,6 @@ package mp.code.intellij.vfs; +import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.Setter; import org.jetbrains.annotations.NotNull; @@ -16,6 +17,7 @@ import java.util.Optional; * This helper class manages just that. */ @Getter @Setter +@EqualsAndHashCode public class CodeMPPath { /** * The name of the workspace that contains this path. diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml index 0a6af7e..1f5b91a 100644 --- a/src/main/resources/META-INF/plugin.xml +++ b/src/main/resources/META-INF/plugin.xml @@ -23,6 +23,7 @@ @@ -31,5 +32,6 @@ instance="mp.code.intellij.settings.CodeMPSettingsConfigurable" id="mp.code.intellij.settings.CodeMPSettingsConfigurable" displayName="CodeMP"/> + diff --git a/src/main/resources/META-INF/pluginIcon.svg b/src/main/resources/META-INF/pluginIcon.svg index dcf6b99..b0179ec 100644 --- a/src/main/resources/META-INF/pluginIcon.svg +++ b/src/main/resources/META-INF/pluginIcon.svg @@ -1,12 +1,62 @@ - - - - - - - - - - \ No newline at end of file + + + + + + + + + + + +