@@ -85,16 +85,23 @@ protected PersistentLibraryKind<FlutterPluginLibraryProperties> getLibraryKind()
85
85
return FlutterPluginLibraryType .LIBRARY_KIND ;
86
86
}
87
87
88
- private void fileChanged (@ NotNull final Project project , @ NotNull final VirtualFile file ) {
89
- if (!DotPackagesFileUtil .DOT_PACKAGES .equals (file .getName ())) return ;
90
- if (LocalFileSystem .getInstance () != file .getFileSystem () && !ApplicationManager .getApplication ().isUnitTestMode ()) return ;
88
+ private boolean isPackagesFile (@ NotNull final VirtualFile file ) {
89
+ final VirtualFile parent = file .getParent ();
90
+ return file .getName ().equals (DotPackagesFileUtil .DOT_PACKAGES ) && parent != null && parent .findChild (PUBSPEC_YAML ) != null ;
91
+ }
91
92
93
+ private boolean isPackageConfigFile (@ NotNull final VirtualFile file ) {
92
94
final VirtualFile parent = file .getParent ();
93
- final VirtualFile pubspec = parent == null ? null : parent .findChild (PUBSPEC_YAML );
95
+ return file .getName ().equals (DotPackagesFileUtil .PACKAGE_CONFIG_JSON )
96
+ && parent != null
97
+ && parent .getName ().equals (DotPackagesFileUtil .DART_TOOL_DIR );
98
+ }
94
99
95
- if (pubspec != null ) {
96
- scheduleUpdate ();
97
- }
100
+ private void fileChanged (@ NotNull final Project project , @ NotNull final VirtualFile file ) {
101
+ if (!isPackageConfigFile (file ) && !isPackagesFile (file )) return ;
102
+ if (LocalFileSystem .getInstance () != file .getFileSystem () && !ApplicationManager .getApplication ().isUnitTestMode ()) return ;
103
+
104
+ scheduleUpdate ();
98
105
}
99
106
100
107
private void scheduleUpdate () {
@@ -132,24 +139,12 @@ private static Set<String> getFlutterPluginPaths(List<PubRoot> roots) {
132
139
final Set <String > paths = new HashSet <>();
133
140
134
141
for (PubRoot pubRoot : roots ) {
135
-
136
- final Map <String , String > map ;
137
- if (pubRoot .getPackagesFile () == null ) {
138
- @ Nullable VirtualFile configFile = pubRoot .getPackageConfigFile ();
139
- if (configFile == null ) {
140
- continue ;
141
- }
142
- // TODO(messick) Use the code in the Dart plugin when available.
143
- // This is just a backup in case we need it. It does not have a proper cache, but the Dart plugin does.
144
- map = loadPackagesMap (configFile );
145
- }
146
- else {
147
- map = DotPackagesFileUtil .getPackagesMap (pubRoot .getPackagesFile ());
148
- if (map == null ) {
149
- continue ;
150
- }
142
+ final var packagesMap = pubRoot .getPackagesMap ();
143
+ if (packagesMap == null ) {
144
+ continue ;
151
145
}
152
- for (String packagePath : map .values ()) {
146
+
147
+ for (String packagePath : packagesMap .values ()) {
153
148
final VirtualFile libFolder = LocalFileSystem .getInstance ().findFileByPath (packagePath );
154
149
if (libFolder == null ) {
155
150
continue ;
@@ -167,53 +162,4 @@ private static Set<String> getFlutterPluginPaths(List<PubRoot> roots) {
167
162
168
163
return paths ;
169
164
}
170
-
171
- private static Map <String , String > loadPackagesMap (@ NotNull VirtualFile root ) {
172
- Map <String , String > result = new HashMap <>();
173
- try {
174
- JsonElement element = JsonUtils .parseString (new String (root .contentsToByteArray (), StandardCharsets .UTF_8 ));
175
- if (element != null ) {
176
- JsonElement packages = element .getAsJsonObject ().get ("packages" );
177
- if (packages != null ) {
178
- JsonArray array = packages .getAsJsonArray ();
179
- for (int i = 0 ; i < array .size (); i ++) {
180
- JsonObject pkg = array .get (i ).getAsJsonObject ();
181
- String name = pkg .get ("name" ).getAsString ();
182
- String rootUri = pkg .get ("rootUri" ).getAsString ();
183
- if (name != null && rootUri != null ) {
184
- // need to protect '+' chars because URLDecoder.decode replaces '+' with space
185
- final String encodedUriWithoutPluses = StringUtil .replace (rootUri , "+" , "%2B" );
186
- final String uri = URLUtil .decode (encodedUriWithoutPluses );
187
- final String packageUri = getAbsolutePackageRootPath (root .getParent ().getParent (), uri );
188
- result .put (name , packageUri );
189
- }
190
- }
191
- }
192
- }
193
- }
194
- catch (IOException | JsonSyntaxException ignored ) {
195
- }
196
- return result ;
197
- }
198
-
199
- @ Nullable
200
- private static String getAbsolutePackageRootPath (@ NotNull final VirtualFile baseDir , @ NotNull final String uri ) {
201
- // Copied from the Dart plugin.
202
- if (uri .startsWith ("file:/" )) {
203
- final String pathAfterSlashes = StringUtil .trimEnd (StringUtil .trimLeading (StringUtil .trimStart (uri , "file:/" ), '/' ), "/" );
204
- if (SystemInfo .isWindows && !ApplicationManager .getApplication ().isUnitTestMode ()) {
205
- if (pathAfterSlashes .length () > 2 && Character .isLetter (pathAfterSlashes .charAt (0 )) && ':' == pathAfterSlashes .charAt (1 )) {
206
- return pathAfterSlashes ;
207
- }
208
- }
209
- else {
210
- return "/" + pathAfterSlashes ;
211
- }
212
- }
213
- else {
214
- return FileUtil .toCanonicalPath (baseDir .getPath () + "/" + uri );
215
- }
216
-
217
- return null ;
218
- }
219
165
}
0 commit comments