forked from smooth80/flutter-intellij
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFileWatch.java
277 lines (242 loc) · 8.02 KB
/
FileWatch.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
* Copyright 2017 The Chromium Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
package io.flutter.utils;
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.LinkedListMultimap;
import com.google.common.collect.Multimap;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.application.Application;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.util.Disposer;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.VirtualFileManager;
import com.intellij.openapi.vfs.newvfs.BulkFileListener;
import com.intellij.openapi.vfs.newvfs.events.VFileEvent;
import com.intellij.util.messages.MessageBusConnection;
import io.flutter.FlutterUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
/**
* Watches a set of VirtualFiles for changes.
*
* <p>Each FileWatch instance represents one subscription.
*
* <p>The callback will be called when the IntelliJ Platform notices the change,
* which may be different from when it's changed on disk, due to caching.
*/
public class FileWatch {
private final @NotNull ImmutableSet<Location> watched;
private final @NotNull Runnable callback;
/**
* When true, no more events should be delivered.
*/
private final AtomicBoolean unsubscribed = new AtomicBoolean();
/**
* The representation of this FileWatch in IntelliJ's dispose tree.
*
* <p>(Private so that nobody can add children.)
*/
private final Disposable disposeLeaf;
private FileWatch(@NotNull ImmutableSet<Location> watched, @NotNull Runnable callback) {
this.watched = watched;
this.callback = callback;
this.disposeLeaf = this::unsubscribe;
}
/**
* Starts watching a single file or directory.
*/
public static @NotNull
FileWatch subscribe(@NotNull VirtualFile file, @NotNull Runnable callback) {
final FileWatch watcher = new FileWatch(ImmutableSet.of(new Location(file, null)), callback);
subscriptions.subscribe(watcher);
return watcher;
}
/**
* Starts watching some paths beneath a VirtualFile.
*
* <p>Each path is relative to the VirtualFile and need not exist.
*
* @param callback will be run asynchronously sometime after the file changed.
*/
public static @NotNull
FileWatch subscribe(@NotNull VirtualFile base, @NotNull Iterable<String> paths, @NotNull Runnable callback) {
final ImmutableSet.Builder<Location> builder = ImmutableSet.builder();
for (String path : paths) {
builder.add(new Location(base, path));
}
final FileWatch watcher = new FileWatch(builder.build(), callback);
subscriptions.subscribe(watcher);
return watcher;
}
/**
* Returns true if the given file matches this watch.
*/
public boolean matches(VirtualFile file) {
for (Location loc : watched) {
if (loc.matches(file)) {
return true;
}
}
return false;
}
/**
* Unsubscribes this FileWatch from events.
*/
public void unsubscribe() {
if (!unsubscribed.compareAndSet(false, true)) {
return; // already unsubscribed
}
subscriptions.unsubscribe(this);
// Remove from dispose tree. Calls unsubscribe() again, harmlessly.
Disposer.dispose(disposeLeaf);
}
/**
* Automatically unsubscribes when a parent object is disposed.
*
* <p>Only one parent can be registered at a time. Auto-unsubscribe
* will stop working for any object previously passed to this
* method.
*/
public void setDisposeParent(@NotNull Disposable parent) {
if (unsubscribed.get()) return;
Disposer.register(parent, disposeLeaf);
}
private void fireEvent() {
if (unsubscribed.get()) return;
try {
callback.run();
}
catch (Exception e) {
FlutterUtils.warn(LOG, "Uncaught exception in FileWatch callback", e);
unsubscribe(); // avoid further errors
}
}
/**
* The location of a file or directory being watched.
* <p>
* Since it might not exist yet, this consists of a base VirtualFile and a path to where it might appear.
*/
private static class Location {
private final @NotNull VirtualFile base;
private final @Nullable String path;
/**
* The segments in the watched path, in reverse order (from leaf to base).
*/
private final @NotNull List<String> reversedNames;
Location(@NotNull VirtualFile base, @Nullable String path) {
if (path != null && path.isEmpty()) {
throw new IllegalArgumentException("can't watch an empty path");
}
this.base = base;
this.path = path;
this.reversedNames = path == null ? ImmutableList.of() : ImmutableList.copyOf(splitter.splitToList(path)).reverse();
}
/**
* Returns the name at the end of the path being watched.
*/
String getName() {
if (reversedNames.isEmpty()) {
return base.getName();
}
else {
return reversedNames.get(0);
}
}
/**
* Returns true if the given VirtualFile is at this location.
*/
boolean matches(VirtualFile file) {
for (String name : reversedNames) {
if (file == null || !file.getName().equals(name)) {
return false;
}
file = file.getParent();
}
return base.equals(file);
}
private static final Splitter splitter = Splitter.on('/');
}
private static final Subscriptions subscriptions = new Subscriptions();
private static class Subscriptions {
/**
* For each VirtualFile name, the FileWatches that are subscribed (across all Projects).
*
* <p>For thread safety, all access should be synchronized.
*/
private final Multimap<String, FileWatch> byFile = LinkedListMultimap.create();
private final Delivery delivery = new Delivery();
synchronized void subscribe(FileWatch w) {
for (Location loc : w.watched) {
byFile.put(loc.getName(), w);
}
delivery.enable(!byFile.isEmpty());
}
synchronized void unsubscribe(FileWatch w) {
for (Location loc : w.watched) {
byFile.remove(loc.getName(), w);
}
delivery.enable(!byFile.isEmpty());
}
synchronized void addWatchesForFile(@NotNull Set<FileWatch> out, @Nullable VirtualFile f) {
if (f == null) return;
for (FileWatch w : byFile.get(f.getName())) {
if (w.matches(f)) {
out.add(w);
}
}
}
}
private static class Delivery implements BulkFileListener {
/**
* The shared connection to IDEA's event system.
*
* <p>This will be non-null when there are one or more subscriptions.
*
* <p>For thread safety, all access should be synchronized.
*/
private @Nullable MessageBusConnection bus;
void enable(boolean enabled) {
if (enabled) {
if (bus == null) {
final Application app = ApplicationManager.getApplication();
bus = app.getMessageBus().connect();
bus.subscribe(VirtualFileManager.VFS_CHANGES, this);
}
}
else {
if (bus != null) {
bus.disconnect();
bus = null;
}
}
}
@Override
public void before(@NotNull List<? extends VFileEvent> events) {
}
@Override
public void after(@NotNull List<? extends VFileEvent> events) {
final Set<FileWatch> todo = new LinkedHashSet<>();
synchronized (subscriptions) {
for (VFileEvent event : events) {
subscriptions.addWatchesForFile(todo, event.getFile());
}
}
// Deliver changes synchronously, but after releasing the lock in case
// the callback subscribes/unsubscribes.
for (FileWatch w : todo) {
w.fireEvent();
}
}
}
private static final Logger LOG = Logger.getInstance(FileWatch.class);
}