From 10afd2fead3d6491f3ba3cd47bcf15cfad9ff492 Mon Sep 17 00:00:00 2001 From: Alexey Kudravtsev Date: Tue, 30 Jul 2024 13:32:00 +0200 Subject: [PATCH] cleanup: get rid of LinkedList; hide implementation details GitOrigin-RevId: 0fce09c1eb5e86c5adda97fb23eec77d78f77da4 --- .../platform-impl/api-dump-unreviewed.txt | 4 +- .../platform-impl/exposed-private-api.txt | 1 - .../impl/IdeDocumentHistoryImpl.java | 80 ++++++++----------- 3 files changed, 36 insertions(+), 49 deletions(-) diff --git a/platform/platform-impl/api-dump-unreviewed.txt b/platform/platform-impl/api-dump-unreviewed.txt index 4a7ae2a6b3af..0ef461aaa88b 100644 --- a/platform/platform-impl/api-dump-unreviewed.txt +++ b/platform/platform-impl/api-dump-unreviewed.txt @@ -15086,7 +15086,7 @@ c:com.intellij.openapi.fileEditor.impl.IdeDocumentHistoryImpl - getChangedFiles():java.util.List - p:getFileEditorManager():com.intellij.openapi.fileEditor.ex.FileEditorManagerEx - p:getSelectedEditor():com.intellij.openapi.fileEditor.ex.FileEditorWithProvider -- getState():com.intellij.openapi.fileEditor.impl.IdeDocumentHistoryImpl$RecentlyChangedFilesState +- getState():java.lang.Object - gotoPlaceInfo(com.intellij.openapi.fileEditor.impl.IdeDocumentHistoryImpl$PlaceInfo):V - gotoPlaceInfo(com.intellij.openapi.fileEditor.impl.IdeDocumentHistoryImpl$PlaceInfo,Z):V - f:includeCurrentCommandAsNavigation():V @@ -15096,7 +15096,7 @@ c:com.intellij.openapi.fileEditor.impl.IdeDocumentHistoryImpl - isNavigateNextChangeAvailable():Z - f:isNavigatePreviousChangeAvailable():Z - isSame(com.intellij.openapi.fileEditor.impl.IdeDocumentHistoryImpl$PlaceInfo,com.intellij.openapi.fileEditor.impl.IdeDocumentHistoryImpl$PlaceInfo):Z -- loadState(com.intellij.openapi.fileEditor.impl.IdeDocumentHistoryImpl$RecentlyChangedFilesState):V +- loadState(java.lang.Object):V - navigateNextChange():V - f:navigatePreviousChange():V - f:onSelectionChanged():V diff --git a/platform/platform-impl/exposed-private-api.txt b/platform/platform-impl/exposed-private-api.txt index ece247ae8619..2866d6cdc6bf 100644 --- a/platform/platform-impl/exposed-private-api.txt +++ b/platform/platform-impl/exposed-private-api.txt @@ -52,7 +52,6 @@ com/intellij/openapi/editor/markup/AnalyzerStatus com/intellij/openapi/extensions/impl/ExtensionPointImpl com/intellij/openapi/fileEditor/impl/EditorCompositeListener com/intellij/openapi/fileEditor/impl/FileEditorOpenOptions -com/intellij/openapi/fileEditor/impl/IdeDocumentHistoryImpl$RecentlyChangedFilesState com/intellij/openapi/fileEditor/impl/text/TextEditorComponent com/intellij/openapi/keymap/impl/ActionProcessor com/intellij/openapi/keymap/impl/ui/KeymapScheme diff --git a/platform/platform-impl/src/com/intellij/openapi/fileEditor/impl/IdeDocumentHistoryImpl.java b/platform/platform-impl/src/com/intellij/openapi/fileEditor/impl/IdeDocumentHistoryImpl.java index c8104422255d..a9bb7c59f811 100644 --- a/platform/platform-impl/src/com/intellij/openapi/fileEditor/impl/IdeDocumentHistoryImpl.java +++ b/platform/platform-impl/src/com/intellij/openapi/fileEditor/impl/IdeDocumentHistoryImpl.java @@ -65,7 +65,7 @@ import java.util.function.Predicate; @State(name = "IdeDocumentHistory", storages = @Storage(StoragePathMacros.PRODUCT_WORKSPACE_FILE), reportStatistic = false) public class IdeDocumentHistoryImpl extends IdeDocumentHistory - implements Disposable, PersistentStateComponent { + implements Disposable, PersistentStateComponent { private static final Logger LOG = Logger.getInstance(IdeDocumentHistoryImpl.class); private static final int BACK_QUEUE_LIMIT = Registry.intValue("editor.navigation.history.stack.size"); @@ -75,8 +75,8 @@ public class IdeDocumentHistoryImpl extends IdeDocumentHistory private FileDocumentManager myFileDocumentManager; - private final LinkedList backPlaces = new LinkedList<>(); // LinkedList of PlaceInfo's - private final LinkedList forwardPlaces = new LinkedList<>(); // LinkedList of PlaceInfo's + private final Deque backPlaces = new ArrayDeque<>(); + private final Deque forwardPlaces = new ArrayDeque<>(); private boolean myBackInProgress; private boolean forwardInProgress; private Object myCurrentCommandGroupId; @@ -84,7 +84,7 @@ public class IdeDocumentHistoryImpl extends IdeDocumentHistory private boolean registeredBackPlaceInLastGroup; // change's navigation - private final LinkedList changePlaces = new LinkedList<>(); // LinkedList of PlaceInfo's + private final Deque changePlaces = new ArrayDeque<>(); private int currentIndex; private PlaceInfo commandStartPlace; @@ -231,29 +231,16 @@ public class IdeDocumentHistoryImpl extends IdeDocumentHistory } } - static final class RecentlyChangedFilesState { + private record RecentlyChangedFilesState( @XCollection(style = XCollection.Style.v2) - public final List changedPaths = new ArrayList<>(); - - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } - return changedPaths.equals(((RecentlyChangedFilesState)o).changedPaths); - } - - @Override - public int hashCode() { - return changedPaths.hashCode(); + List changedPaths) { + RecentlyChangedFilesState() { + this(new ArrayList<>()); } } @Override - public RecentlyChangedFilesState getState() { + public Object getState() { synchronized (state) { RecentlyChangedFilesState stateSnapshot = new RecentlyChangedFilesState(); stateSnapshot.changedPaths.addAll(state.changedPaths); @@ -262,10 +249,10 @@ public class IdeDocumentHistoryImpl extends IdeDocumentHistory } @Override - public void loadState(@NotNull RecentlyChangedFilesState state) { + public void loadState(@NotNull Object state) { synchronized (this.state) { this.state.changedPaths.clear(); - this.state.changedPaths.addAll(state.changedPaths); + this.state.changedPaths.addAll(((RecentlyChangedFilesState)state).changedPaths); } } @@ -301,7 +288,7 @@ public class IdeDocumentHistoryImpl extends IdeDocumentHistory return createPlaceInfo(selectedEditorWithProvider.getFileEditor(), selectedEditorWithProvider.getProvider()); } - private static @Nullable PlaceInfo getPlaceInfoFromFocus(Project project) { + private @Nullable PlaceInfo getPlaceInfoFromFocus(Project project) { FileEditor fileEditor = new FocusBasedCurrentEditorProvider().getCurrentEditor(project); if (fileEditor instanceof TextEditor && fileEditor.isValid()) { VirtualFile file = fileEditor.getFile(); @@ -500,6 +487,7 @@ public class IdeDocumentHistoryImpl extends IdeDocumentHistory removeInvalidFilesFromStacks(); if (currentIndex == 0) return; PlaceInfo currentPlace = getCurrentPlaceInfo(); + List changePlaces = getChangePlaces(); for (int i = currentIndex - 1; i >= 0; i--) { PlaceInfo info = changePlaces.get(i); if (currentPlace == null || !isSame(currentPlace, info)) { @@ -510,14 +498,30 @@ public class IdeDocumentHistoryImpl extends IdeDocumentHistory } } + @Override + public void navigateNextChange() { + removeInvalidFilesFromStacks(); + if (currentIndex >= changePlaces.size()) return; + PlaceInfo currentPlace = getCurrentPlaceInfo(); + List changePlaces = getChangePlaces(); + for (int i = currentIndex; i < changePlaces.size(); i++) { + PlaceInfo info = changePlaces.get(i); + if (currentPlace == null || !isSame(currentPlace, info)) { + executeCommand(() -> gotoPlaceInfo(info), "", null); + currentIndex = i + 1; + break; + } + } + } + @Override public @NotNull List getBackPlaces() { - return Collections.unmodifiableList(backPlaces); + return List.copyOf(backPlaces); } @Override public List getChangePlaces() { - return Collections.unmodifiableList(changePlaces); + return List.copyOf(changePlaces); } @Override @@ -551,27 +555,12 @@ public class IdeDocumentHistoryImpl extends IdeDocumentHistory } } - @Override - public void navigateNextChange() { - removeInvalidFilesFromStacks(); - if (currentIndex >= changePlaces.size()) return; - PlaceInfo currentPlace = getCurrentPlaceInfo(); - for (int i = currentIndex; i < changePlaces.size(); i++) { - PlaceInfo info = changePlaces.get(i); - if (currentPlace == null || !isSame(currentPlace, info)) { - executeCommand(() -> gotoPlaceInfo(info), "", null); - currentIndex = i + 1; - break; - } - } - } - @Override public boolean isNavigateNextChangeAvailable() { return currentIndex < changePlaces.size(); } - private boolean removeInvalidFilesFrom(@NotNull List backPlaces) { + private boolean removeInvalidFilesFrom(@NotNull Deque backPlaces) { return backPlaces .removeIf(info -> (info.myFile instanceof OptionallyIncluded && !((OptionallyIncluded)info.myFile).isIncludedInDocumentHistory(project)) || @@ -647,19 +636,18 @@ public class IdeDocumentHistoryImpl extends IdeDocumentHistory getCaretPosition(fileEditor), System.currentTimeMillis()); } - private static @Nullable RangeMarker getCaretPosition(@NotNull FileEditor fileEditor) { + private @Nullable RangeMarker getCaretPosition(@NotNull FileEditor fileEditor) { if (!(fileEditor instanceof TextEditor)) { return null; } Editor editor = ((TextEditor)fileEditor).getEditor(); int offset = editor.getCaretModel().getOffset(); - return editor.getDocument().createRangeMarker(offset, offset); } private void putLastOrMerge(@NotNull PlaceInfo next, int limit, boolean isChanged, Object groupId) { - LinkedList list = isChanged ? changePlaces : backPlaces; + Deque list = isChanged ? changePlaces : backPlaces; MessageBus messageBus = project.getMessageBus(); RecentPlacesListener listener = messageBus.syncPublisher(RecentPlacesListener.TOPIC); if (!list.isEmpty()) {