Skip to content

Commit

Permalink
fix: Use project instead of Module
Browse files Browse the repository at this point in the history
Fixes #891

Signed-off-by: azerr <[email protected]>
  • Loading branch information
angelozerr committed Aug 28, 2023
1 parent d379c70 commit 7180363
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 68 deletions.
16 changes: 13 additions & 3 deletions src/main/java/com/redhat/devtools/intellij/lsp4ij/LSPIJUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public static Document getDocument(VirtualFile docFile) {
return FileDocumentManager.getInstance().getDocument(docFile);
}

public static @Nullable Module getProject(@Nullable VirtualFile file) {
public static @Nullable Module getModule(@Nullable VirtualFile file) {
if (file == null) {
return null;
}
Expand All @@ -163,6 +163,11 @@ public static Document getDocument(VirtualFile docFile) {
return null;
}

public static @Nullable Project getProject(@Nullable VirtualFile file) {
Module module = getModule(file);
return module != null ? module.getProject() : null;
}

public static int toOffset(Position start, Document document) throws IndexOutOfBoundsException {
int lineStartOffset = document.getLineStartOffset(start.getLine());
return lineStartOffset + start.getCharacter();
Expand All @@ -189,6 +194,11 @@ public static URI toUri(Module project) {
return file.toURI();
}

public static URI toUri(Project project) {
File file = new File(project.getProjectFilePath()).getParentFile();
return file.toURI();
}

public static Range toRange(TextRange range, Document document) {
return new Range(LSPIJUtils.toPosition(range.getStartOffset(), document), LSPIJUtils.toPosition(range.getEndOffset(), document));
}
Expand Down Expand Up @@ -396,8 +406,8 @@ public static Editor[] editorsForFile(VirtualFile file) {
}

public static Editor[] editorsForFile(VirtualFile file, Document document) {
Module module = LSPIJUtils.getProject(file);
return module != null ? EditorFactory.getInstance().getEditors(document, module.getProject()) : new Editor[0];
Project project = LSPIJUtils.getProject(file);
return project != null ? EditorFactory.getInstance().getEditors(document, project) : new Editor[0];
}

public static Editor editorForFile(VirtualFile file) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public class LSPVirtualFileWrapper implements Disposable {
this.file = file;
this.dataPerServer = new HashMap<>();
this.hover = new LSPTextHoverForFile();
Module project = LSPIJUtils.getProject(file);
Module project = LSPIJUtils.getModule(file);
if (project != null) {
Disposer.register(project, this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public void fileClosed(@NotNull FileEditorManager source, @NotNull VirtualFile f
@Nonnull
public final LanguageServersRegistry.LanguageServerDefinition serverDefinition;
@Nullable
protected final Module initialProject;
protected final Project initialProject;
@Nonnull
protected final Set<Module> allWatchedProjects;
@Nonnull
Expand Down Expand Up @@ -154,7 +154,7 @@ public void fileClosed(@NotNull FileEditorManager source, @NotNull VirtualFile f
private boolean initiallySupportsWorkspaceFolders = false;

/* Backwards compatible constructor */
public LanguageServerWrapper(@Nonnull Module project, @Nonnull LanguageServersRegistry.LanguageServerDefinition serverDefinition) {
public LanguageServerWrapper(@Nonnull Project project, @Nonnull LanguageServersRegistry.LanguageServerDefinition serverDefinition) {
this(project, serverDefinition, null);
}

Expand All @@ -165,7 +165,7 @@ public LanguageServerWrapper(@Nonnull LanguageServersRegistry.LanguageServerDefi
/**
* Unified private constructor to set sensible defaults in all cases
*/
private LanguageServerWrapper(@Nullable Module project, @Nonnull LanguageServersRegistry.LanguageServerDefinition serverDefinition,
private LanguageServerWrapper(@Nullable Project project, @Nonnull LanguageServersRegistry.LanguageServerDefinition serverDefinition,
@Nullable URI initialPath) {
this.initialProject = project;
this.initialPath = initialPath;
Expand Down Expand Up @@ -198,7 +198,7 @@ public Set<Module> getAllWatchedProjects() {
}

public Project getProject() {
return initialProject.getProject();
return initialProject;
}

void stopDispatcher() {
Expand Down Expand Up @@ -293,7 +293,7 @@ public synchronized void start() throws LanguageServerException {
final URI rootURI = getRootURI();
this.launcherFuture = new CompletableFuture<>();
this.initializeFuture = CompletableFuture.supplyAsync(() -> {
this.lspStreamProvider = serverDefinition.createConnectionProvider(initialProject.getProject());
this.lspStreamProvider = serverDefinition.createConnectionProvider(initialProject);
initParams.setInitializationOptions(this.lspStreamProvider.getInitializationOptions(rootURI));

// Starting process...
Expand All @@ -317,7 +317,7 @@ public synchronized void start() throws LanguageServerException {
lspStreamProvider.ensureIsAlive();
return null;
}).thenRun(() -> {
languageClient = serverDefinition.createLanguageClient(initialProject.getProject());
languageClient = serverDefinition.createLanguageClient(initialProject);
initParams.setProcessId(getParentProcessId());

if (rootURI != null) {
Expand Down Expand Up @@ -366,7 +366,7 @@ public synchronized void start() throws LanguageServerException {
final Map<URI, Document> toReconnect = filesToReconnect;
initializeFuture.thenRunAsync(() -> {
if (this.initialProject != null) {
watchProject(this.initialProject, true);
//watchProject(this.initialProject, true);
}
for (Map.Entry<URI, Document> fileToReconnect : toReconnect.entrySet()) {
try {
Expand Down Expand Up @@ -447,7 +447,7 @@ private ClientInfo getClientInfo() {

@Nullable
private URI getRootURI() {
final Module project = this.initialProject;
final Project project = this.initialProject;
if (project != null && !project.isDisposed()) {
return LSPIJUtils.toUri(project);
}
Expand Down Expand Up @@ -754,7 +754,7 @@ public IStatus runInWorkspace(IProgressMonitor monitor) throws CoreException {
* @return whether this language server can operate on the given project
* @since 0.5
*/
public boolean canOperate(Module project) {
public boolean canOperate(Project project) {
if (project != null && (project.equals(this.initialProject) || this.allWatchedProjects.contains(project))) {
return true;
}
Expand Down Expand Up @@ -793,7 +793,7 @@ private CompletableFuture<LanguageServer> connect(@Nonnull URI absolutePath, Doc

VirtualFile file = FileDocumentManager.getInstance().getFile(document);
if (file != null && file.exists()) {
watchProject(LSPIJUtils.getProject(file), false);
watchProject(LSPIJUtils.getModule(file), false);
}

if (this.connectedDocuments.containsKey(thePath)) {
Expand Down Expand Up @@ -860,22 +860,6 @@ private void disconnect(URI path, boolean stopping) {
}
}

public void disconnectContentType(@Nonnull Language language) {
List<URI> pathsToDisconnect = new ArrayList<>();
for (URI path : connectedDocuments.keySet()) {
VirtualFile foundFiles = LSPIJUtils.findResourceFor(path);
if (foundFiles != null) {
Language fileLanguage = LSPIJUtils.getFileLanguage(foundFiles, initialProject.getProject());
if (fileLanguage.isKindOf(language)) {
pathsToDisconnect.add(path);
}
}
}
for (URI path : pathsToDisconnect) {
disconnect(path);
}
}

/**
* checks if the wrapper is already connected to the document at the given path
*
Expand Down Expand Up @@ -1132,8 +1116,8 @@ public boolean canOperate(@Nonnull Document document) {
}

private LanguageServerLifecycleManager getLanguageServerLifecycleManager() {
Project project = initialProject.getProject();
if (project.isDisposed()) {
Project project = initialProject;
if (project == null || project.isDisposed()) {
return NullLanguageServerLifecycleManager.INSTANCE;
}
return LanguageServerLifecycleManager.getInstance(project);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ private Collection<LanguageServerWrapper> getLSWrappers(@Nonnull Document docume
// we already checked a compatible LS with this definition
continue;
}
final Module fileProject = file != null ? LSPIJUtils.getProject(file) : null;
final Project fileProject = file != null ? LSPIJUtils.getProject(file) : null;
if (fileProject != null) {
LanguageServerWrapper wrapper = new LanguageServerWrapper(fileProject, serverDefinition);
startedServers.add(wrapper);
Expand Down Expand Up @@ -234,14 +234,6 @@ private LanguageServerWrapper getLSWrapperForConnection(Document document,
return wrapper;
}

private @Nonnull
List<LanguageServerWrapper> getStartedLSWrappers(
@Nonnull Module project) {
return startedServers.stream().filter(wrapper -> wrapper.canOperate(project))
.collect(Collectors.toList());
// TODO multi-root: also return servers which support multi-root?
}

private List<LanguageServerWrapper> getStartedLSWrappers(
Document document) {
return getStartedLSWrappers(wrapper -> wrapper.canOperate(document));
Expand Down Expand Up @@ -286,7 +278,7 @@ public List<LanguageServer> getActiveLanguageServers(Predicate<ServerCapabilitie
* @return list of Language Servers
*/
@Nonnull
public List<LanguageServer> getLanguageServers(@Nullable Module project,
public List<LanguageServer> getLanguageServers(@Nullable Project project,
Predicate<ServerCapabilities> request, boolean onlyActiveLS) {
List<LanguageServer> serverInfos = new ArrayList<>();
for (LanguageServerWrapper wrapper : startedServers) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ protected <R> CompletableFuture<R> runAsBackground(String progressTitle, Functio
protected String getFilePath(String fileUri) {
VirtualFile file = LSPIJUtils.findResourceFor(fileUri);
if (file != null) {
Module module = LSPIJUtils.getProject(file);
Module module = LSPIJUtils.getModule(file);
if (module != null) {
ModuleRootManager rootManager = ModuleRootManager.getInstance(module);
VirtualFile[] contentRoots = rootManager.getContentRoots();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,8 @@ public void updateDiagnostics(PublishDiagnosticsParams params) { //Applic
if (file == null) {
return;
}
Module module = LSPIJUtils.getProject(file);
if (module == null) {
return;
}
Project project = module.getProject();
if (project.isDisposed()) {
Project project = LSPIJUtils.getProject(file);
if (project == null || project.isDisposed()) {
return;
}
final PsiFile psiFile = PsiManager.getInstance(project).findFile(file);
Expand All @@ -92,6 +88,6 @@ public void updateDiagnostics(PublishDiagnosticsParams params) { //Applic
// Trigger Intellij validation to execute
// {@link LSPDiagnosticAnnotator}.
// which translates LSP Diagnostics into Intellij Annotation
DaemonCodeAnalyzer.getInstance(module.getProject()).restart(psiFile);
DaemonCodeAnalyzer.getInstance(project).restart(psiFile);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import com.redhat.devtools.intellij.lsp4ij.LSPIJUtils;
import com.redhat.devtools.intellij.lsp4ij.LSPVirtualFileWrapper;
import com.redhat.devtools.intellij.lsp4ij.LanguageServerBundle;
import com.redhat.devtools.intellij.lsp4mp4ij.psi.internal.core.ls.PsiUtilsLSImpl;
import org.eclipse.lsp4j.DocumentLink;
import org.jetbrains.annotations.Nullable;

Expand All @@ -46,7 +45,7 @@ public class LSPDocumentLinkGotoDeclarationHandler implements GotoDeclarationHan
if (!LSPVirtualFileWrapper.hasWrapper(file)) {
return PsiElement.EMPTY_ARRAY;
}
Module module = LSPIJUtils.getProject(file);
Module module = LSPIJUtils.getModule(file);
Project project = module != null ? module.getProject() : null;
if (project == null || project.isDisposed()) {
return PsiElement.EMPTY_ARRAY;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@
package com.redhat.devtools.intellij.lsp4mp4ij.classpath;

import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleManager;
import com.intellij.openapi.project.ModuleListener;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.impl.libraries.LibraryEx;
import com.intellij.openapi.roots.libraries.Library;
import com.intellij.openapi.roots.libraries.LibraryTable;
import com.intellij.openapi.util.Pair;
Expand All @@ -36,10 +33,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CompletableFuture;

/**
* Classpath resource changed listener used to track update of:
Expand Down Expand Up @@ -163,7 +157,7 @@ private void tryToAddSourceFile(VirtualFile file, boolean checkExistingFile) {
return;
}
// The file is a Java file or microprofile-config.properties
Module module = LSPIJUtils.getProject(file);
Module module = LSPIJUtils.getModule(file);
if (module == null || module.isDisposed()) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import com.intellij.openapi.module.Module;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.ModuleRootManager;
import com.intellij.openapi.roots.ProjectFileIndex;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.JavaPsiFacade;
import com.intellij.psi.PsiClass;
Expand Down Expand Up @@ -79,7 +78,7 @@ public IPsiUtils refine(Module module) {

@Override
public Module getModule(VirtualFile file) {
return LSPIJUtils.getProject(file);
return LSPIJUtils.getModule(file);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class QuteFileIndentOptionsProvider extends FileIndentOptionsProvider {
public CommonCodeStyleSettings.@Nullable IndentOptions getIndentOptions(@NotNull CodeStyleSettings settings, @NotNull PsiFile file) {
if (file.getFileType().equals(QuteFileType.QUTE)) {
VirtualFile virtualFile = file.getVirtualFile();
Project project = LSPIJUtils.getProject(virtualFile).getProject();
Project project = LSPIJUtils.getModule(virtualFile).getProject();
FileViewProvider provider = PsiManagerEx.getInstanceEx(project).findViewProvider(virtualFile);
if (provider instanceof TemplateLanguageFileViewProvider) {
Language language = ((TemplateLanguageFileViewProvider)provider).getTemplateDataLanguage();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,17 @@

import java.util.Collections;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleUtilCore;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiMethod;
import com.intellij.psi.util.PsiTreeUtil;
import com.redhat.devtools.intellij.lsp4mp4ij.psi.core.utils.PsiTypeUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import static com.redhat.devtools.intellij.lsp4ij.LSPIJUtils.getProject;


/**
* Utilities for working with Renarde applications.
Expand Down

0 comments on commit 7180363

Please sign in to comment.