Skip to content

Commit

Permalink
Replaced Psi file with Virtual file.
Browse files Browse the repository at this point in the history
Replaced Psi file with Virtual file. Because intellij deprecated FilenameIndex.getFilesByName(...).
  • Loading branch information
aparnamichael committed Jul 9, 2024
1 parent a4857ac commit 4a8bf55
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 36 deletions.
10 changes: 4 additions & 6 deletions src/main/java/io/openliberty/tools/intellij/LibertyExplorer.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,10 @@ public static Tree buildTree(Project project, Color backgroundColor) {

for (BuildFile buildFile : mavenBuildFiles) {
// create a new Liberty project
PsiFile psiFile = buildFile.getBuildFile();
VirtualFile virtualFile = buildFile.getBuildFile();
String projectName = null;
VirtualFile virtualFile = psiFile.getVirtualFile();
if (virtualFile == null) {
LOGGER.error(String.format("Could not resolve current Maven project %s", psiFile));
LOGGER.error(String.format("Could not resolve current Maven project %s", virtualFile));
break;
}
LibertyModuleNode node;
Expand All @@ -152,7 +151,7 @@ public static Tree buildTree(Project project, Color backgroundColor) {
}

boolean validContainerVersion = buildFile.isValidContainerVersion();
LibertyModule module = libertyModules.addLibertyModule(new LibertyModule(project, psiFile.getVirtualFile(), projectName, Constants.LIBERTY_MAVEN_PROJECT, validContainerVersion));
LibertyModule module = libertyModules.addLibertyModule(new LibertyModule(project, virtualFile, projectName, Constants.LIBERTY_MAVEN_PROJECT, validContainerVersion));
node = new LibertyModuleNode(module);

top.add(node);
Expand All @@ -176,9 +175,8 @@ public static Tree buildTree(Project project, Color backgroundColor) {
}

for (BuildFile buildFile : gradleBuildFiles) {
PsiFile psiFile = buildFile.getBuildFile();
VirtualFile virtualFile = buildFile.getBuildFile();
String projectName = null;
VirtualFile virtualFile = psiFile.getVirtualFile();
if (virtualFile == null) {
LOGGER.error(String.format("Could not resolve current Gradle project %s", buildFile));
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public LibertyModule(Project project, VirtualFile buildFile, String name, String

public LibertyModule(Project project, BuildFile buildFile) {
this(project);
this.buildFile = buildFile.getBuildFile().getVirtualFile();
this.buildFile = buildFile.getBuildFile();
this.name = buildFile.getProjectName();
this.projectType = buildFile.getProjectType();
this.validContainerVersion = buildFile.isValidContainerVersion();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ protected final List<BuildFile> getBuildFileList(Project project) {
// Resolve project names and add to list
mavenBuildFiles.forEach(mavenBuildFile -> {
// resolve project name
VirtualFile virtualFile = mavenBuildFile.getBuildFile().getVirtualFile();
VirtualFile virtualFile = mavenBuildFile.getBuildFile();
if (virtualFile == null) {
LOGGER.error(String.format("Could not resolve Maven project for build file: %s", mavenBuildFile.getBuildFile()));
} else {
Expand All @@ -103,7 +103,7 @@ protected final List<BuildFile> getBuildFileList(Project project) {

});
gradleBuildFiles.forEach(gradleBuildFile -> {
VirtualFile virtualFile = gradleBuildFile.getBuildFile().getVirtualFile();
VirtualFile virtualFile = gradleBuildFile.getBuildFile();
if (virtualFile == null) {
LOGGER.error(String.format("Could not resolve Gradle project for build file: %s", gradleBuildFile.getBuildFile()));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@
*******************************************************************************/
package io.openliberty.tools.intellij.util;

import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiFile;

/**
* Defines a BuildFile object
*/
public class BuildFile {
public PsiFile buildFile;
public VirtualFile buildFile;
public boolean validBuildFile;
public boolean validContainerVersion;

Expand All @@ -37,9 +38,9 @@ public BuildFile(boolean validBuildFile, boolean validContainerVersion) {
this.buildFile = null;
}

public PsiFile getBuildFile() { return this.buildFile; }
public VirtualFile getBuildFile() { return this.buildFile; }

public void setBuildFile(PsiFile buildFile) {
public void setBuildFile(VirtualFile buildFile) {
this.buildFile = buildFile;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ public static String fileToString(String path) throws IOException {
* validContainerVersion true if plugin version is valid for dev mode in containers
* @throws IOException
*/
public static BuildFile validBuildGradle(PsiFile file) throws IOException {
String buildFile = fileToString(file.getVirtualFile().getPath());
public static BuildFile validBuildGradle(VirtualFile file) throws IOException {
String buildFile = fileToString(file.getPath());
if (buildFile.isEmpty()) { return (new BuildFile(false, false)); }

// instead of iterating over capture groups in a plugin{}, search directly
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,12 @@ public static String getProjectNameFromPom(VirtualFile file) throws ParserConfig
* @throws IOException
* @throws SAXException
*/
public static BuildFile validPom(PsiFile file) throws ParserConfigurationException, IOException, SAXException {
public static BuildFile validPom(VirtualFile file) throws ParserConfigurationException, IOException, SAXException {
BuildFile buildFile = new BuildFile(false, false);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();

File inputFile = new File(file.getVirtualFile().getPath());
File inputFile = new File(file.getPath());
Document doc = builder.parse(inputFile);

doc.getDocumentElement().normalize();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.wm.ToolWindow;
import com.intellij.psi.PsiFile;
import com.intellij.psi.search.FilenameIndex;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.terminal.JBTerminalWidget;
Expand All @@ -32,31 +32,28 @@
import javax.xml.parsers.ParserConfigurationException;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Set;
import java.util.Collections;
import java.util.HashSet;
import java.util.*;

public class LibertyProjectUtil {
private static Logger LOGGER = Logger.getInstance(LibertyProjectUtil.class);

enum BuildFileFilter {
ADDABLE {
public boolean matches(Project project, BuildFile buildFile, PsiFile psiFile) {
return !LIST.matches(project, buildFile, psiFile);
public boolean matches(Project project, BuildFile buildFile, VirtualFile virtualFile) {
return !LIST.matches(project, buildFile, virtualFile);
}
},
REMOVABLE {
public boolean matches(Project project, BuildFile buildFile, PsiFile psiFile) {
return isCustomLibertyProject(project, psiFile) && !(buildFile.isValidBuildFile() || isLibertyProject(psiFile));
public boolean matches(Project project, BuildFile buildFile, VirtualFile virtualFile) {
return isCustomLibertyProject(project, virtualFile) && !(buildFile.isValidBuildFile() || isLibertyProject(virtualFile));
}
},
LIST {
public boolean matches(Project project, BuildFile buildFile, PsiFile psiFile) {
return buildFile.isValidBuildFile() || isLibertyProject(psiFile) || isCustomLibertyProject(project, psiFile);
public boolean matches(Project project, BuildFile buildFile, VirtualFile virtualFile) {
return buildFile.isValidBuildFile() || isLibertyProject(virtualFile) || isCustomLibertyProject(project, virtualFile);
}
};
public abstract boolean matches(Project project, BuildFile buildFile, PsiFile psiFile);
public abstract boolean matches(Project project, BuildFile buildFile, VirtualFile virtualFile);
}

/** REVISIT: In memory collection of Liberty projects but need to persist. **/
Expand All @@ -82,9 +79,9 @@ public static void removeCustomLibertyProject(LibertyModule libertyModule) {
LibertyModules.getInstance().removeLibertyModule(libertyModule);
}

public static boolean isCustomLibertyProject(Project project, PsiFile buildFile) {
public static boolean isCustomLibertyProject(Project project, VirtualFile buildFile) {
final LibertyProjectSettings state = LibertyProjectSettings.getInstance(project);
return state.getCustomLibertyProjects().contains(buildFile.getVirtualFile().getPath());
return state.getCustomLibertyProjects().contains(buildFile.getPath());
}

/**
Expand Down Expand Up @@ -187,8 +184,8 @@ private static ArrayList<BuildFile> getBuildFiles(Project project, String buildF
ArrayList<BuildFile> buildFiles = new ArrayList<BuildFile>();

if (buildFileType.equals(Constants.LIBERTY_MAVEN_PROJECT)) {
PsiFile[] mavenFiles = FilenameIndex.getFilesByName(project, "pom.xml", GlobalSearchScope.projectScope(project));
for (PsiFile mavenFile : mavenFiles) {
Collection<VirtualFile> mavenFiles = FilenameIndex.getVirtualFilesByName("pom.xml", GlobalSearchScope.projectScope(project));
for (VirtualFile mavenFile : mavenFiles) {
BuildFile buildFile = LibertyMavenUtil.validPom(mavenFile);
// check if valid pom.xml, or if part of Liberty project
if (filter.matches(project, buildFile, mavenFile)) {
Expand All @@ -197,8 +194,8 @@ private static ArrayList<BuildFile> getBuildFiles(Project project, String buildF
}
}
} else if (buildFileType.equals(Constants.LIBERTY_GRADLE_PROJECT)) {
PsiFile[] gradleFiles = FilenameIndex.getFilesByName(project, "build.gradle", GlobalSearchScope.projectScope(project));
for (PsiFile gradleFile : gradleFiles) {
Collection<VirtualFile> gradleFiles = FilenameIndex.getVirtualFilesByName("build.gradle", GlobalSearchScope.projectScope(project));
for (VirtualFile gradleFile : gradleFiles) {
try {
BuildFile buildFile = LibertyGradleUtil.validBuildGradle(gradleFile);
// check if valid build.gradle, or if part of Liberty project
Expand All @@ -219,8 +216,8 @@ private static ArrayList<BuildFile> getBuildFiles(Project project, String buildF
* @param buildFile maven or gradle build file in the form of PsiFile
* @return <code>true</code> if the project contains src/main/liberty/config/server.xml relative to the build file; <code>false</code> otherwise
*/
private static boolean isLibertyProject(PsiFile buildFile) {
String rootDir = buildFile.getVirtualFile().getParent().getPath();
private static boolean isLibertyProject(VirtualFile buildFile) {
String rootDir = buildFile.getParent().getPath();
return new File(rootDir, "src/main/liberty/config/server.xml").exists();
}

Expand Down

0 comments on commit 4a8bf55

Please sign in to comment.