Skip to content

Commit f6dd261

Browse files
committed
fixed handling moved/renamed files.
1 parent 646a46f commit f6dd261

1 file changed

Lines changed: 47 additions & 40 deletions

File tree

src/main/java/com/ss/editor/ui/component/editor/area/EditorAreaComponent.java

Lines changed: 47 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -207,66 +207,73 @@ private boolean isIgnoreOpenedFiles() {
207207
}
208208

209209
/**
210-
* Handle renaming a file.
210+
* Handle renaming of a file.
211211
*/
212212
private void processEvent(@NotNull final RenamedFileEvent event) {
213213

214214
final Path prevFile = event.getPrevFile();
215215
final Path newFile = event.getNewFile();
216216

217-
final ConcurrentObjectDictionary<Path, Tab> openedEditors = getOpenedEditors();
218-
final Tab tab = DictionaryUtils.getInReadLock(openedEditors, prevFile, ObjectDictionary::get);
219-
if (tab == null) return;
220-
221-
final ObservableMap<Object, Object> properties = tab.getProperties();
222-
final FileEditor fileEditor = (FileEditor) properties.get(KEY_EDITOR);
223-
fileEditor.notifyRenamed(prevFile, newFile);
224-
225-
final Path editFile = fileEditor.getEditFile();
226-
if (!editFile.equals(newFile)) return;
227-
228-
if (fileEditor.isDirty()) {
229-
tab.setText("*" + fileEditor.getFileName());
230-
} else {
231-
tab.setText(fileEditor.getFileName());
232-
}
233-
234-
final Workspace workspace = WORKSPACE_MANAGER.getCurrentWorkspace();
235-
236-
if (workspace != null) {
237-
workspace.removeOpenedFile(prevFile);
238-
workspace.addOpenedFile(newFile, fileEditor);
239-
}
240-
241-
DictionaryUtils.runInWriteLock(openedEditors, prevFile, ObjectDictionary::remove);
242-
DictionaryUtils.runInWriteLock(openedEditors, newFile, tab, ObjectDictionary::put);
217+
handleMovingFiles(prevFile, newFile);
243218
}
244219

245220
/**
246-
* Handle moving a file.
221+
* Handle moving of a file.
247222
*/
248223
private void processEvent(@NotNull final MovedFileEvent event) {
249224

250225
final Path prevFile = event.getPrevFile();
251226
final Path newFile = event.getNewFile();
252227

253-
final ObservableList<Tab> tabs = getTabs();
254-
tabs.forEach(tab -> {
228+
handleMovingFiles(prevFile, newFile);
229+
}
255230

256-
final ObservableMap<Object, Object> properties = tab.getProperties();
257-
final FileEditor fileEditor = (FileEditor) properties.get(KEY_EDITOR);
258-
fileEditor.notifyMoved(prevFile, newFile);
231+
/**
232+
* Handle moving/renaming of a file.
233+
*
234+
* @param prevFile the prev version of the file.
235+
* @param newFile the new version of the file.
236+
*/
237+
private void handleMovingFiles(@NotNull final Path prevFile, @NotNull final Path newFile) {
259238

260-
final Path editFile = fileEditor.getEditFile();
261-
if (!editFile.equals(newFile)) return;
239+
final ConcurrentObjectDictionary<Path, Tab> openedEditors = getOpenedEditors();
240+
final long stamp = openedEditors.writeLock();
241+
try {
262242

263-
final Workspace workspace = WORKSPACE_MANAGER.getCurrentWorkspace();
243+
final Array<Path> files = openedEditors.keyArray(Path.class);
244+
for (final Path file : files) {
245+
246+
if (!file.startsWith(prevFile)) {
247+
continue;
248+
}
249+
250+
final Tab tab = openedEditors.get(file);
251+
final ObservableMap<Object, Object> properties = tab.getProperties();
252+
final FileEditor fileEditor = (FileEditor) properties.get(KEY_EDITOR);
253+
fileEditor.notifyRenamed(prevFile, newFile);
264254

265-
if (workspace != null) {
266-
workspace.removeOpenedFile(prevFile);
267-
workspace.addOpenedFile(newFile, fileEditor);
255+
if (fileEditor.isDirty()) {
256+
tab.setText("*" + fileEditor.getFileName());
257+
} else {
258+
tab.setText(fileEditor.getFileName());
259+
}
260+
261+
final Path editFile = fileEditor.getEditFile();
262+
263+
openedEditors.remove(file);
264+
openedEditors.put(editFile, tab);
265+
266+
final Workspace workspace = WORKSPACE_MANAGER.getCurrentWorkspace();
267+
268+
if (workspace != null) {
269+
workspace.removeOpenedFile(file);
270+
workspace.addOpenedFile(editFile, fileEditor);
271+
}
268272
}
269-
});
273+
274+
} finally {
275+
openedEditors.writeUnlock(stamp);
276+
}
270277
}
271278

272279
/**

0 commit comments

Comments
 (0)