-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Structural rewrite for better file data loading
- Loading branch information
1 parent
ec7d255
commit 12f5b20
Showing
9 changed files
with
140 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 1 addition & 92 deletions
93
src/main/java/io/github/tandemdude/hklbsupport/LightbulbPackageManagerListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,109 +1,18 @@ | ||
package io.github.tandemdude.hklbsupport; | ||
|
||
import com.fasterxml.jackson.core.type.TypeReference; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.intellij.openapi.application.ApplicationManager; | ||
import com.intellij.openapi.fileTypes.FileTypeManager; | ||
import com.intellij.openapi.module.ModuleManager; | ||
import com.intellij.openapi.project.ProjectManager; | ||
import com.intellij.openapi.projectRoots.Sdk; | ||
import com.intellij.openapi.vfs.VirtualFile; | ||
import com.intellij.psi.search.FileTypeIndex; | ||
import com.intellij.psi.search.GlobalSearchScope; | ||
import com.jetbrains.python.packaging.PyPackageManager; | ||
import com.jetbrains.python.packaging.common.PythonPackageManagementListener; | ||
import com.jetbrains.python.sdk.PythonSdkUtil; | ||
import io.github.tandemdude.hklbsupport.utils.Notifier; | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
public class LightbulbPackageManagerListener implements PythonPackageManagementListener { | ||
private static final ObjectMapper MAPPER = new ObjectMapper(); | ||
|
||
public record ParamData(Map<String, String> required, Map<String, String> optional) {} | ||
|
||
public record LightbulbData(String version, Map<String, ParamData> paramData) {} | ||
|
||
private static final ConcurrentHashMap<Sdk, LightbulbData> sdkLightbulbData = new ConcurrentHashMap<>(); | ||
|
||
public static LightbulbData getDataFor(Sdk sdk) { | ||
return sdkLightbulbData.get(sdk); | ||
} | ||
|
||
public static void flush() { | ||
sdkLightbulbData.clear(); | ||
} | ||
|
||
LightbulbData readMetaparamsFile(String version, VirtualFile vf) { | ||
try { | ||
var parsedParamData = MAPPER.readValue(vf.getInputStream(), new TypeReference<Map<String, ParamData>>() {}); | ||
return new LightbulbData(version, parsedParamData); | ||
} catch (IOException e) { | ||
return null; | ||
} | ||
} | ||
|
||
@Override | ||
public void packagesChanged(@NotNull Sdk sdk) { | ||
ApplicationManager.getApplication().runReadAction(() -> { | ||
var packages = PyPackageManager.getInstance(sdk).getPackages(); | ||
if (packages == null) { | ||
return; | ||
} | ||
|
||
var lightbulb = packages.stream() | ||
.filter(p -> p.getName().equals("hikari-lightbulb")) | ||
.findFirst(); | ||
|
||
if (lightbulb.isEmpty()) { | ||
return; | ||
} | ||
|
||
var existingData = sdkLightbulbData.get(sdk); | ||
if (existingData != null | ||
&& existingData.version().equals(lightbulb.get().getVersion())) { | ||
// The cache is still up-to-date - do not refresh | ||
return; | ||
} | ||
|
||
var lightbulbLocation = lightbulb.get().getLocation(); | ||
if (lightbulbLocation == null) { | ||
return; | ||
} | ||
|
||
var searchScopes = new ArrayList<GlobalSearchScope>(); | ||
for (var project : ProjectManager.getInstance().getOpenProjects()) { | ||
Arrays.stream(ModuleManager.getInstance(project).getModules()).forEach(module -> { | ||
var maybeSdk = PythonSdkUtil.findPythonSdk(module); | ||
if (maybeSdk == sdk) { | ||
searchScopes.add(GlobalSearchScope.moduleWithDependenciesAndLibrariesScope(module)); | ||
} | ||
}); | ||
project.getService(ProjectDataService.class).notifyChange(sdk); | ||
} | ||
|
||
FileTypeIndex.processFiles( | ||
FileTypeManager.getInstance().getFileTypeByExtension("json"), | ||
file -> { | ||
if (file.getPath().endsWith("metaparams.json") | ||
&& file.getPath().contains("lightbulb") | ||
&& file.getPath().contains(lightbulbLocation)) { | ||
var data = readMetaparamsFile(lightbulb.get().getVersion(), file); | ||
if (data == null) { | ||
return false; | ||
} | ||
|
||
sdkLightbulbData.put(sdk, data); | ||
Notifier.notifyInformation( | ||
null, "Lightbulb configuration loaded successfully (%s)", sdk.getName()); | ||
return false; | ||
} | ||
return true; | ||
}, | ||
GlobalSearchScope.union(searchScopes)); | ||
}); | ||
} | ||
} |
103 changes: 103 additions & 0 deletions
103
src/main/java/io/github/tandemdude/hklbsupport/ProjectDataService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package io.github.tandemdude.hklbsupport; | ||
|
||
import com.fasterxml.jackson.core.type.TypeReference; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.intellij.openapi.components.Service; | ||
import com.intellij.openapi.fileTypes.FileTypeManager; | ||
import com.intellij.openapi.module.ModuleManager; | ||
import com.intellij.openapi.project.Project; | ||
import com.intellij.openapi.projectRoots.Sdk; | ||
import com.intellij.openapi.vfs.VirtualFile; | ||
import com.intellij.psi.search.FileTypeIndex; | ||
import com.intellij.psi.search.GlobalSearchScope; | ||
import com.jetbrains.python.sdk.PythonSdkUtil; | ||
import io.github.tandemdude.hklbsupport.models.LightbulbData; | ||
import io.github.tandemdude.hklbsupport.models.ParamData; | ||
import io.github.tandemdude.hklbsupport.utils.Notifier; | ||
|
||
import java.io.IOException; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.regex.Pattern; | ||
|
||
@Service(Service.Level.PROJECT) | ||
public final class ProjectDataService { | ||
private static final ObjectMapper MAPPER = new ObjectMapper(); | ||
private static final Pattern VERSION_PATTERN = Pattern.compile("__version__\\s*=\\s*\"([^\"]+)\""); | ||
|
||
private final Project project; | ||
|
||
private final ConcurrentHashMap<Sdk, LightbulbData> sdkCache = new ConcurrentHashMap<>(); | ||
|
||
public ProjectDataService(Project project) { | ||
this.project = project; | ||
} | ||
|
||
public void loadModules() { | ||
Arrays.stream(ModuleManager.getInstance(project).getModules()).forEach(module -> { | ||
var maybeSdk = PythonSdkUtil.findPythonSdk(module); | ||
if (maybeSdk != null) { | ||
sdkCache.put(maybeSdk, new LightbulbData("-1", Collections.emptyMap())); | ||
} | ||
}); | ||
} | ||
|
||
public void flush() { | ||
this.sdkCache.clear(); | ||
} | ||
|
||
public LightbulbData getLightbulbData(Sdk sdk) { | ||
return sdkCache.get(sdk); | ||
} | ||
|
||
LightbulbData readMetaparamsFile(String version, VirtualFile vf) throws IOException { | ||
var parsedParamData = MAPPER.readValue(vf.getInputStream(), new TypeReference<Map<String, ParamData>>() {}); | ||
return new LightbulbData(version, parsedParamData); | ||
} | ||
|
||
public void notifyChange(Sdk sdk) { | ||
if (!sdkCache.containsKey(sdk)) { | ||
return; | ||
} | ||
|
||
FileTypeIndex.processFiles( | ||
FileTypeManager.getInstance().getFileTypeByExtension("json"), | ||
file -> { | ||
if (!file.getName().equals("metaparams.json") | ||
|| !file.getParent().getName().equals("lightbulb")) { | ||
return true; | ||
} | ||
|
||
var initFile = file.getParent().findChild("__init__.py"); | ||
if (initFile == null) { | ||
return true; | ||
} | ||
|
||
try { | ||
var matcher = VERSION_PATTERN.matcher(new String(initFile.contentsToByteArray())); | ||
String version = null; | ||
while (matcher.find()) { | ||
version = matcher.group(1); | ||
} | ||
|
||
if (version == null) { | ||
return true; | ||
} | ||
|
||
if (version.equals(sdkCache.get(sdk).version())) { | ||
return false; | ||
} | ||
|
||
var data = readMetaparamsFile(version, file); | ||
sdkCache.put(sdk, data); | ||
Notifier.notifyInformation(project, "Lightbulb configuration loaded successfully (%s)", sdk.getName()); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
return false; | ||
}, | ||
GlobalSearchScope.allScope(project)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
src/main/java/io/github/tandemdude/hklbsupport/models/LightbulbData.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package io.github.tandemdude.hklbsupport.models; | ||
|
||
import java.util.Map; | ||
|
||
public record LightbulbData(String version, Map<String, ParamData> paramData) {} |
5 changes: 5 additions & 0 deletions
5
src/main/java/io/github/tandemdude/hklbsupport/models/ParamData.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package io.github.tandemdude.hklbsupport.models; | ||
|
||
import java.util.Map; | ||
|
||
public record ParamData(Map<String, String> required, Map<String, String> optional) {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/main/kotlin/io/github/tandemdude/hklbsupport/StartupActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package io.github.tandemdude.hklbsupport | ||
|
||
import com.intellij.openapi.project.Project | ||
import com.intellij.openapi.startup.ProjectActivity | ||
|
||
/** | ||
* Startup activity that causes module Lightbulb configurations to attempt to be loaded when the | ||
* user opens a new project. | ||
*/ | ||
class StartupActivity : ProjectActivity { | ||
override suspend fun execute(project: Project) { | ||
project.getService(ProjectDataService::class.java).loadModules() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters