From 8e091682ab74ccdbcf476ca14e618f8d81389cf7 Mon Sep 17 00:00:00 2001 From: wagner1343 Date: Thu, 29 Sep 2022 03:48:29 -0300 Subject: [PATCH 1/2] Adds support for flutter 3.3 in the flutter plugin library manager which now updates the flutter plugin library when .dart_tool/package_config.json or .packages files are changed --- AUTHORS | 1 + flutter-idea/src/io/flutter/pub/PubRoot.java | 16 ++++ .../sdk/FlutterPluginsLibraryManager.java | 92 ++++--------------- 3 files changed, 36 insertions(+), 73 deletions(-) diff --git a/AUTHORS b/AUTHORS index 09c2e981d1..93e435980c 100644 --- a/AUTHORS +++ b/AUTHORS @@ -20,3 +20,4 @@ Hrishikesh Kadam Sander Kersten Pradumna Saraf Pedro Massango +Wagner Silvestre \ No newline at end of file diff --git a/flutter-idea/src/io/flutter/pub/PubRoot.java b/flutter-idea/src/io/flutter/pub/PubRoot.java index 2c314043fd..6e5b82a52b 100644 --- a/flutter-idea/src/io/flutter/pub/PubRoot.java +++ b/flutter-idea/src/io/flutter/pub/PubRoot.java @@ -17,11 +17,13 @@ import com.intellij.openapi.roots.ProjectRootManager; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.psi.PsiFile; +import com.jetbrains.lang.dart.util.DotPackagesFileUtil; import io.flutter.FlutterUtils; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.util.List; +import java.util.Map; /** * A snapshot of the root directory of a pub package. @@ -279,6 +281,20 @@ public VirtualFile getPackagesFile() { return null; } + public @Nullable Map getPackagesMap() { + final var packageConfigFile = getPackageConfigFile(); + if(packageConfigFile != null) { + return DotPackagesFileUtil.getPackagesMapFromPackageConfigJsonFile(packageConfigFile); + } + + final var packagesFile = getPackagesFile(); + if(packagesFile != null) { + return DotPackagesFileUtil.getPackagesMap(packagesFile); + } + + return null; + } + /** * Returns true if the packages are up to date wrt pubspec.yaml. */ diff --git a/flutter-idea/src/io/flutter/sdk/FlutterPluginsLibraryManager.java b/flutter-idea/src/io/flutter/sdk/FlutterPluginsLibraryManager.java index 5cbd41ffc1..4b3e5c7780 100644 --- a/flutter-idea/src/io/flutter/sdk/FlutterPluginsLibraryManager.java +++ b/flutter-idea/src/io/flutter/sdk/FlutterPluginsLibraryManager.java @@ -85,16 +85,23 @@ protected PersistentLibraryKind getLibraryKind() return FlutterPluginLibraryType.LIBRARY_KIND; } - private void fileChanged(@NotNull final Project project, @NotNull final VirtualFile file) { - if (!DotPackagesFileUtil.DOT_PACKAGES.equals(file.getName())) return; - if (LocalFileSystem.getInstance() != file.getFileSystem() && !ApplicationManager.getApplication().isUnitTestMode()) return; + private boolean isPackagesFile(@NotNull final VirtualFile file) { + final VirtualFile parent = file.getParent(); + return file.getName().equals(DotPackagesFileUtil.DOT_PACKAGES) && parent != null && parent.findChild(PUBSPEC_YAML) != null; + } + private boolean isPackageConfigFile(@NotNull final VirtualFile file) { final VirtualFile parent = file.getParent(); - final VirtualFile pubspec = parent == null ? null : parent.findChild(PUBSPEC_YAML); + return file.getName().equals(DotPackagesFileUtil.PACKAGE_CONFIG_JSON) + && parent != null + && parent.getName().equals(DotPackagesFileUtil.DART_TOOL_DIR); + } - if (pubspec != null) { - scheduleUpdate(); - } + private void fileChanged(@NotNull final Project project, @NotNull final VirtualFile file) { + if(!isPackageConfigFile(file) && !isPackagesFile(file)) return; + if (LocalFileSystem.getInstance() != file.getFileSystem() && !ApplicationManager.getApplication().isUnitTestMode()) return; + + scheduleUpdate(); } private void scheduleUpdate() { @@ -132,24 +139,12 @@ private static Set getFlutterPluginPaths(List roots) { final Set paths = new HashSet<>(); for (PubRoot pubRoot : roots) { - - final Map map; - if (pubRoot.getPackagesFile() == null) { - @Nullable VirtualFile configFile = pubRoot.getPackageConfigFile(); - if (configFile == null) { - continue; - } - // TODO(messick) Use the code in the Dart plugin when available. - // This is just a backup in case we need it. It does not have a proper cache, but the Dart plugin does. - map = loadPackagesMap(configFile); - } - else { - map = DotPackagesFileUtil.getPackagesMap(pubRoot.getPackagesFile()); - if (map == null) { - continue; - } + final var packagesMap = pubRoot.getPackagesMap(); + if(packagesMap == null) { + continue; } - for (String packagePath : map.values()) { + + for (String packagePath : packagesMap.values()) { final VirtualFile libFolder = LocalFileSystem.getInstance().findFileByPath(packagePath); if (libFolder == null) { continue; @@ -167,53 +162,4 @@ private static Set getFlutterPluginPaths(List roots) { return paths; } - - private static Map loadPackagesMap(@NotNull VirtualFile root) { - Map result = new HashMap<>(); - try { - JsonElement element = JsonUtils.parseString(new String(root.contentsToByteArray(), StandardCharsets.UTF_8)); - if (element != null) { - JsonElement packages = element.getAsJsonObject().get("packages"); - if (packages != null) { - JsonArray array = packages.getAsJsonArray(); - for (int i = 0; i < array.size(); i++) { - JsonObject pkg = array.get(i).getAsJsonObject(); - String name = pkg.get("name").getAsString(); - String rootUri = pkg.get("rootUri").getAsString(); - if (name != null && rootUri != null) { - // need to protect '+' chars because URLDecoder.decode replaces '+' with space - final String encodedUriWithoutPluses = StringUtil.replace(rootUri, "+", "%2B"); - final String uri = URLUtil.decode(encodedUriWithoutPluses); - final String packageUri = getAbsolutePackageRootPath(root.getParent().getParent(), uri); - result.put(name, packageUri); - } - } - } - } - } - catch (IOException | JsonSyntaxException ignored) { - } - return result; - } - - @Nullable - private static String getAbsolutePackageRootPath(@NotNull final VirtualFile baseDir, @NotNull final String uri) { - // Copied from the Dart plugin. - if (uri.startsWith("file:/")) { - final String pathAfterSlashes = StringUtil.trimEnd(StringUtil.trimLeading(StringUtil.trimStart(uri, "file:/"), '/'), "/"); - if (SystemInfo.isWindows && !ApplicationManager.getApplication().isUnitTestMode()) { - if (pathAfterSlashes.length() > 2 && Character.isLetter(pathAfterSlashes.charAt(0)) && ':' == pathAfterSlashes.charAt(1)) { - return pathAfterSlashes; - } - } - else { - return "/" + pathAfterSlashes; - } - } - else { - return FileUtil.toCanonicalPath(baseDir.getPath() + "/" + uri); - } - - return null; - } } From dd95ce034d6532808001a337af36dbdb221f1d92 Mon Sep 17 00:00:00 2001 From: wagner1343 Date: Thu, 29 Sep 2022 15:56:07 -0300 Subject: [PATCH 2/2] Fix formatting errors on FlutterPluginsLibraryManager and PubRoot --- flutter-idea/src/io/flutter/pub/PubRoot.java | 4 ++-- .../src/io/flutter/sdk/FlutterPluginsLibraryManager.java | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/flutter-idea/src/io/flutter/pub/PubRoot.java b/flutter-idea/src/io/flutter/pub/PubRoot.java index 6e5b82a52b..e31aa9ec12 100644 --- a/flutter-idea/src/io/flutter/pub/PubRoot.java +++ b/flutter-idea/src/io/flutter/pub/PubRoot.java @@ -283,12 +283,12 @@ public VirtualFile getPackagesFile() { public @Nullable Map getPackagesMap() { final var packageConfigFile = getPackageConfigFile(); - if(packageConfigFile != null) { + if (packageConfigFile != null) { return DotPackagesFileUtil.getPackagesMapFromPackageConfigJsonFile(packageConfigFile); } final var packagesFile = getPackagesFile(); - if(packagesFile != null) { + if (packagesFile != null) { return DotPackagesFileUtil.getPackagesMap(packagesFile); } diff --git a/flutter-idea/src/io/flutter/sdk/FlutterPluginsLibraryManager.java b/flutter-idea/src/io/flutter/sdk/FlutterPluginsLibraryManager.java index 4b3e5c7780..7f0302049e 100644 --- a/flutter-idea/src/io/flutter/sdk/FlutterPluginsLibraryManager.java +++ b/flutter-idea/src/io/flutter/sdk/FlutterPluginsLibraryManager.java @@ -98,7 +98,7 @@ private boolean isPackageConfigFile(@NotNull final VirtualFile file) { } private void fileChanged(@NotNull final Project project, @NotNull final VirtualFile file) { - if(!isPackageConfigFile(file) && !isPackagesFile(file)) return; + if (!isPackageConfigFile(file) && !isPackagesFile(file)) return; if (LocalFileSystem.getInstance() != file.getFileSystem() && !ApplicationManager.getApplication().isUnitTestMode()) return; scheduleUpdate(); @@ -140,7 +140,7 @@ private static Set getFlutterPluginPaths(List roots) { for (PubRoot pubRoot : roots) { final var packagesMap = pubRoot.getPackagesMap(); - if(packagesMap == null) { + if (packagesMap == null) { continue; }