Skip to content
This repository was archived by the owner on Jan 19, 2025. It is now read-only.

Commit 4955009

Browse files
authored
fix(gui): previous/next if selected element not appear in filtered tree view (#780)
fix(gui): previous/next if element was selected that did not appear in filtered tree view
1 parent 6e1d0b2 commit 4955009

File tree

3 files changed

+54
-34
lines changed

3 files changed

+54
-34
lines changed

api-editor/gui/src/features/actionBar/ActionBar.tsx

Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -149,64 +149,73 @@ export const ActionBar: React.FC<ActionBarProps> = function ({ declaration }) {
149149
);
150150
};
151151

152-
const getNextElementPath = function (
152+
const getPreviousElementPath = function (
153153
declarations: PythonDeclaration[],
154154
start: PythonDeclaration,
155155
filter: AbstractPythonFilter,
156156
annotations: AnnotationStore,
157157
usages: UsageCountStore,
158158
): string {
159-
let current = getNextElementInTree(declarations, start);
160-
while (current !== start) {
159+
let currentIndex = getPreviousIndex(declarations, getIndex(declarations, start));
160+
let current = getElementAtIndex(declarations, currentIndex);
161+
while (current !== null && current !== start) {
161162
if (filter.shouldKeepDeclaration(current, annotations, usages)) {
162163
return current.id;
163164
}
164-
current = getNextElementInTree(declarations, current);
165+
currentIndex = getPreviousIndex(declarations, currentIndex);
166+
current = getElementAtIndex(declarations, currentIndex);
165167
}
166168
return start.id;
167169
};
168170

169-
const getNextElementInTree = function (
170-
declarations: PythonDeclaration[],
171-
current: PythonDeclaration,
172-
): PythonDeclaration {
173-
if (declarations.length === 0) {
174-
return current;
175-
}
176-
177-
const index = declarations.findIndex((it) => it.id === current.id);
178-
const nextIndex = (index + 1) % declarations.length;
179-
return declarations[nextIndex];
180-
};
181-
182-
const getPreviousElementPath = function (
171+
const getNextElementPath = function (
183172
declarations: PythonDeclaration[],
184173
start: PythonDeclaration,
185174
filter: AbstractPythonFilter,
186175
annotations: AnnotationStore,
187176
usages: UsageCountStore,
188-
): string | null {
189-
let current = getPreviousElementInTree(declarations, start);
190-
while (current !== start && current !== null) {
177+
): string {
178+
let currentIndex = getNextIndex(declarations, getIndex(declarations, start));
179+
let current = getElementAtIndex(declarations, currentIndex);
180+
while (current !== null && current !== start) {
191181
if (filter.shouldKeepDeclaration(current, annotations, usages)) {
192182
return current.id;
193183
}
194-
current = getPreviousElementInTree(declarations, current);
184+
currentIndex = getNextIndex(declarations, currentIndex);
185+
current = getElementAtIndex(declarations, currentIndex);
195186
}
196-
return null;
187+
return start.id;
197188
};
198189

199-
const getPreviousElementInTree = function (
200-
declarations: PythonDeclaration[],
201-
current: PythonDeclaration,
202-
): PythonDeclaration {
203-
if (declarations.length === 0) {
204-
return current;
190+
const getPreviousIndex = function (declarations: PythonDeclaration[], currentIndex: number | null): number | null {
191+
if (currentIndex === null || currentIndex < 0 || currentIndex >= declarations.length) {
192+
return null;
193+
}
194+
195+
return (currentIndex - 1 + declarations.length) % declarations.length;
196+
};
197+
198+
const getNextIndex = function (declarations: PythonDeclaration[], currentIndex: number | null): number | null {
199+
if (currentIndex === null || currentIndex < 0 || currentIndex >= declarations.length) {
200+
return null;
205201
}
206202

203+
return (currentIndex + 1) % declarations.length;
204+
};
205+
206+
const getIndex = function (declarations: PythonDeclaration[], current: PythonDeclaration): number | null {
207207
const index = declarations.findIndex((it) => it.id === current.id);
208-
const previousIndex = (index - 1 + declarations.length) % declarations.length;
209-
return declarations[previousIndex];
208+
if (index === -1) {
209+
return null;
210+
}
211+
return index;
212+
};
213+
214+
const getElementAtIndex = function (declarations: PythonDeclaration[], index: number | null): PythonDeclaration | null {
215+
if (index === null) {
216+
return null;
217+
}
218+
return declarations[index];
210219
};
211220

212221
const getAncestors = function (navStr: string, filteredPythonPackage: PythonPackage): string[] {

api-editor/gui/src/features/packageData/apiSlice.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,17 @@ export const apiReducer = reducer;
6464

6565
const selectAPI = (state: RootState) => state.api;
6666
export const selectRawPythonPackage = (state: RootState) => selectAPI(state).pythonPackage;
67+
export const selectFlatSortedDeclarationList = createSelector(
68+
[selectRawPythonPackage, selectSortingMode, selectUsages],
69+
(pythonPackage, sortingMode, usages) => {
70+
switch (sortingMode) {
71+
case SortingMode.Alphabetical:
72+
return walkChildrenInPreorder(pythonPackage, sortAlphabetically);
73+
case SortingMode.Usages: // Descending
74+
return walkChildrenInPreorder(pythonPackage, sortByUsages(usages));
75+
}
76+
},
77+
);
6778
export const selectFilteredPythonPackage = createSelector(
6879
[selectRawPythonPackage, selectAnnotationStore, selectUsages, selectFilter],
6980
(pythonPackage, annotations, usages, filter) => {
@@ -88,7 +99,7 @@ export const selectMatchedNodes = createSelector(
8899
export const selectNumberOfMatchedNodes = createSelector([selectMatchedNodes], (matchedNodes) => {
89100
return matchedNodes.length;
90101
});
91-
export const selectFlatSortedDeclarationList = createSelector(
102+
export const selectFlatFilteredAndSortedDeclarationList = createSelector(
92103
[selectFilteredPythonPackage, selectSortingMode, selectUsages],
93104
(pythonPackage, sortingMode, usages) => {
94105
switch (sortingMode) {

api-editor/gui/src/features/packageData/treeView/TreeView.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { FunctionNode } from './FunctionNode';
1919
import { ModuleNode } from './ModuleNode';
2020
import { ParameterNode } from './ParameterNode';
2121
import { AutoSizer } from '../../../common/AutoSizer';
22-
import { selectFlatSortedDeclarationList } from '../apiSlice';
22+
import { selectFlatFilteredAndSortedDeclarationList } from '../apiSlice';
2323
import { selectUsages } from '../../usages/usageSlice';
2424

2525
interface ScrollOffset {
@@ -36,7 +36,7 @@ export const TreeView: React.FC = memo(() => {
3636
const filter = useAppSelector(selectFilter);
3737
const usages = useAppSelector(selectUsages);
3838
const allExpanded = useAppSelector(selectAllExpandedInTreeView);
39-
const flatSortedList = useAppSelector(selectFlatSortedDeclarationList);
39+
const flatSortedList = useAppSelector(selectFlatFilteredAndSortedDeclarationList);
4040
const children = getNodes(allExpanded, flatSortedList);
4141
const previousScrollOffset = useAppSelector(selectTreeViewScrollOffset);
4242

0 commit comments

Comments
 (0)