@@ -149,64 +149,73 @@ export const ActionBar: React.FC<ActionBarProps> = function ({ declaration }) {
149
149
) ;
150
150
} ;
151
151
152
- const getNextElementPath = function (
152
+ const getPreviousElementPath = function (
153
153
declarations : PythonDeclaration [ ] ,
154
154
start : PythonDeclaration ,
155
155
filter : AbstractPythonFilter ,
156
156
annotations : AnnotationStore ,
157
157
usages : UsageCountStore ,
158
158
) : 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 ) {
161
162
if ( filter . shouldKeepDeclaration ( current , annotations , usages ) ) {
162
163
return current . id ;
163
164
}
164
- current = getNextElementInTree ( declarations , current ) ;
165
+ currentIndex = getPreviousIndex ( declarations , currentIndex ) ;
166
+ current = getElementAtIndex ( declarations , currentIndex ) ;
165
167
}
166
168
return start . id ;
167
169
} ;
168
170
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 (
183
172
declarations : PythonDeclaration [ ] ,
184
173
start : PythonDeclaration ,
185
174
filter : AbstractPythonFilter ,
186
175
annotations : AnnotationStore ,
187
176
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 ) {
191
181
if ( filter . shouldKeepDeclaration ( current , annotations , usages ) ) {
192
182
return current . id ;
193
183
}
194
- current = getPreviousElementInTree ( declarations , current ) ;
184
+ currentIndex = getNextIndex ( declarations , currentIndex ) ;
185
+ current = getElementAtIndex ( declarations , currentIndex ) ;
195
186
}
196
- return null ;
187
+ return start . id ;
197
188
} ;
198
189
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 ;
205
201
}
206
202
203
+ return ( currentIndex + 1 ) % declarations . length ;
204
+ } ;
205
+
206
+ const getIndex = function ( declarations : PythonDeclaration [ ] , current : PythonDeclaration ) : number | null {
207
207
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 ] ;
210
219
} ;
211
220
212
221
const getAncestors = function ( navStr : string , filteredPythonPackage : PythonPackage ) : string [ ] {
0 commit comments