@@ -28,6 +28,11 @@ import { selectExpandDocumentationByDefault } from '../../ui/uiSlice';
28
28
import { Link as RouterLink } from 'react-router-dom' ;
29
29
import { PythonDeclaration } from '../model/PythonDeclaration' ;
30
30
import { PythonPackage } from '../model/PythonPackage' ;
31
+ import { Optional } from '../../../common/util/types' ;
32
+ import { PythonFunction } from '../model/PythonFunction' ;
33
+ import { PythonClass } from '../model/PythonClass' ;
34
+ import { PythonParameter } from '../model/PythonParameter' ;
35
+ import { PythonModule } from '../model/PythonModule' ;
31
36
32
37
interface DocumentationTextProps {
33
38
declaration : PythonDeclaration ;
@@ -87,9 +92,16 @@ export const DocumentationText: React.FC<DocumentationTextProps> = function ({ d
87
92
// replace double colons with single colon
88
93
. replaceAll ( / : : / gu, ':' )
89
94
// replace relative links to classes
90
- . replaceAll ( / : c l a s s : ` ( \w * ) ` / gu, ( _match , name ) => resolveRelativeLink ( declaration , name ) )
95
+ . replaceAll ( / : c l a s s : ` ( \w * ) ` / gu, ( _match , name ) =>
96
+ resolveRelativeLink ( declaration , name , DeclarationLevel . CLASS ) ,
97
+ )
91
98
// replace relative links to functions
92
- . replaceAll ( / : f u n c : ` ( \w * ) ` / gu, ( _match , name ) => resolveRelativeLink ( declaration , name ) )
99
+ . replaceAll ( / : f u n c : ` ( \w * ) ` / gu, ( _match , name ) =>
100
+ resolveRelativeLink ( declaration , name , DeclarationLevel . FUNCTION ) ,
101
+ )
102
+ . replaceAll ( / : m e t h : ` ( \w * ) ` / gu, ( _match , name ) =>
103
+ resolveRelativeLink ( declaration , name , DeclarationLevel . FUNCTION ) ,
104
+ )
93
105
// replace absolute links to modules
94
106
. replaceAll ( / : m o d : ` ( [ \w . ] * ) ` / gu, ( _match , qualifiedName ) => resolveAbsoluteLink ( declaration , qualifiedName , 1 ) )
95
107
// replace absolute links to classes
@@ -147,12 +159,33 @@ export const DocumentationText: React.FC<DocumentationTextProps> = function ({ d
147
159
) ;
148
160
} ;
149
161
150
- const resolveRelativeLink = function ( currentDeclaration : PythonDeclaration , linkedDeclarationName : string ) : string {
151
- const parent = currentDeclaration . parent ( ) ;
162
+ const resolveRelativeLink = function (
163
+ currentDeclaration : PythonDeclaration ,
164
+ linkedDeclarationName : string ,
165
+ targetLevel : DeclarationLevel ,
166
+ ) : string {
167
+ let parent : Optional < PythonDeclaration > = currentDeclaration ;
168
+ do {
169
+ parent = parent . parent ( ) ;
170
+ } while (
171
+ parent &&
172
+ Object . keys ( DeclarationLevel ) [ getDeclarationLevel ( parent ) ] >= Object . keys ( DeclarationLevel ) [ targetLevel ]
173
+ ) ;
174
+
152
175
if ( ! parent ) {
153
176
return linkedDeclarationName ;
154
177
}
155
178
179
+ if ( targetLevel === DeclarationLevel . FUNCTION ) {
180
+ const grandparent = parent . parent ( ) ;
181
+ if ( grandparent ) {
182
+ const sibling = grandparent . children ( ) . find ( ( it ) => it . name === linkedDeclarationName ) ;
183
+ if ( sibling ) {
184
+ return `[${ sibling . preferredQualifiedName ( ) } ](${ sibling . id } )` ;
185
+ }
186
+ }
187
+ }
188
+
156
189
const sibling = parent . children ( ) . find ( ( it ) => it . name === linkedDeclarationName ) ;
157
190
if ( ! sibling ) {
158
191
return linkedDeclarationName ;
@@ -192,3 +225,28 @@ const resolveAbsoluteLink = function (
192
225
193
226
return `[${ current . preferredQualifiedName ( ) } ](${ current . id } )` ;
194
227
} ;
228
+
229
+ const getDeclarationLevel = function ( element : PythonDeclaration ) : DeclarationLevel {
230
+ if ( element instanceof PythonPackage ) {
231
+ return DeclarationLevel . PACKAGE ;
232
+ } else if ( element instanceof PythonModule ) {
233
+ return DeclarationLevel . MODULE ;
234
+ } else if ( element instanceof PythonClass ) {
235
+ return DeclarationLevel . CLASS ;
236
+ } else if ( element instanceof PythonFunction ) {
237
+ return DeclarationLevel . FUNCTION ;
238
+ } else if ( element instanceof PythonParameter ) {
239
+ return DeclarationLevel . PARAMETER ;
240
+ } else {
241
+ return DeclarationLevel . DEFAULT ;
242
+ }
243
+ } ;
244
+
245
+ enum DeclarationLevel {
246
+ DEFAULT ,
247
+ PACKAGE ,
248
+ MODULE ,
249
+ CLASS ,
250
+ FUNCTION ,
251
+ PARAMETER ,
252
+ }
0 commit comments