@@ -30,13 +30,7 @@ export interface ReferenceLinkTarget {
30
30
readonly ref : string ;
31
31
}
32
32
33
- export interface DefinitionLinkTarget {
34
- readonly kind : 'definition' ;
35
- readonly ref : string ;
36
- readonly target : ExternalLinkTarget | InternalLinkTarget ;
37
- }
38
-
39
- export type LinkTarget = ExternalLinkTarget | InternalLinkTarget | ReferenceLinkTarget | DefinitionLinkTarget ;
33
+ export type LinkTarget = ExternalLinkTarget | InternalLinkTarget | ReferenceLinkTarget ;
40
34
41
35
42
36
function parseLink (
@@ -93,20 +87,35 @@ function getWorkspaceFolder(document: SkinnyTextDocument) {
93
87
|| vscode . workspace . workspaceFolders ?. [ 0 ] ?. uri ;
94
88
}
95
89
96
- export interface LinkData {
90
+ interface MdInlineLink {
91
+ readonly kind : 'link' ;
92
+
97
93
readonly target : LinkTarget ;
98
94
99
95
readonly sourceText : string ;
100
96
readonly sourceResource : vscode . Uri ;
101
97
readonly sourceRange : vscode . Range ;
102
98
}
103
99
100
+ export interface MdLinkDefinition {
101
+ readonly kind : 'definition' ;
102
+
103
+ readonly sourceText : string ;
104
+ readonly sourceResource : vscode . Uri ;
105
+ readonly sourceRange : vscode . Range ;
106
+
107
+ readonly ref : string ;
108
+ readonly target : ExternalLinkTarget | InternalLinkTarget ;
109
+ }
110
+
111
+ export type MdLink = MdInlineLink | MdLinkDefinition ;
112
+
104
113
function extractDocumentLink (
105
114
document : SkinnyTextDocument ,
106
115
pre : number ,
107
116
link : string ,
108
117
matchIndex : number | undefined
109
- ) : LinkData | undefined {
118
+ ) : MdLink | undefined {
110
119
const offset = ( matchIndex || 0 ) + pre ;
111
120
const linkStart = document . positionAt ( offset ) ;
112
121
const linkEnd = document . positionAt ( offset + link . length ) ;
@@ -116,6 +125,7 @@ function extractDocumentLink(
116
125
return undefined ;
117
126
}
118
127
return {
128
+ kind : 'link' ,
119
129
target : linkTarget ,
120
130
sourceText : link ,
121
131
sourceResource : document . uri ,
@@ -179,7 +189,7 @@ async function findCode(document: SkinnyTextDocument, engine: MarkdownEngine): P
179
189
return { multiline, inline } ;
180
190
}
181
191
182
- function isLinkInsideCode ( code : CodeInDocument , link : LinkData ) {
192
+ function isLinkInsideCode ( code : CodeInDocument , link : MdLink ) {
183
193
return code . multiline . some ( interval => link . sourceRange . start . line >= interval [ 0 ] && link . sourceRange . start . line < interval [ 1 ] ) ||
184
194
code . inline . some ( position => position . intersection ( link . sourceRange ) ) ;
185
195
}
@@ -204,7 +214,17 @@ export class MdLinkProvider implements vscode.DocumentLinkProvider {
204
214
. map ( data => this . toValidDocumentLink ( data , definitionSet ) ) ) ;
205
215
}
206
216
207
- private toValidDocumentLink ( link : LinkData , definitionSet : LinkDefinitionSet ) : vscode . DocumentLink | undefined {
217
+ private toValidDocumentLink ( link : MdLink , definitionSet : LinkDefinitionSet ) : vscode . DocumentLink | undefined {
218
+ if ( link . kind === 'definition' ) {
219
+ return this . toValidDocumentLink ( {
220
+ kind : 'link' ,
221
+ sourceText : link . sourceText ,
222
+ sourceRange : link . sourceRange ,
223
+ sourceResource : link . sourceResource ,
224
+ target : link . target
225
+ } , definitionSet ) ;
226
+ }
227
+
208
228
switch ( link . target . kind ) {
209
229
case 'external' : {
210
230
return new vscode . DocumentLink ( link . sourceRange , link . target . uri ) ;
@@ -225,28 +245,21 @@ export class MdLinkProvider implements vscode.DocumentLinkProvider {
225
245
return undefined ;
226
246
}
227
247
}
228
- case 'definition' :
229
- return this . toValidDocumentLink ( {
230
- sourceText : link . sourceText ,
231
- sourceRange : link . sourceRange ,
232
- sourceResource : link . sourceResource ,
233
- target : link . target . target
234
- } , definitionSet ) ;
235
248
}
236
249
}
237
250
238
- public async getAllLinks ( document : SkinnyTextDocument ) : Promise < LinkData [ ] > {
251
+ public async getAllLinks ( document : SkinnyTextDocument ) : Promise < MdLink [ ] > {
239
252
return Array . from ( [
240
253
...( await this . getInlineLinks ( document ) ) ,
241
254
...this . getReferenceLinks ( document ) ,
242
- ...this . getDefinitionLinks ( document ) ,
255
+ ...this . getLinkDefinitions ( document ) ,
243
256
] ) ;
244
257
}
245
258
246
- private async getInlineLinks ( document : SkinnyTextDocument ) : Promise < LinkData [ ] > {
259
+ private async getInlineLinks ( document : SkinnyTextDocument ) : Promise < MdLink [ ] > {
247
260
const text = document . getText ( ) ;
248
261
249
- const results : LinkData [ ] = [ ] ;
262
+ const results : MdLink [ ] = [ ] ;
250
263
const codeInDocument = await findCode ( document , this . engine ) ;
251
264
for ( const match of text . matchAll ( linkPattern ) ) {
252
265
const matchImageData = match [ 4 ] && extractDocumentLink ( document , match [ 3 ] . length + 1 , match [ 4 ] , match . index ) ;
@@ -261,7 +274,7 @@ export class MdLinkProvider implements vscode.DocumentLinkProvider {
261
274
return results ;
262
275
}
263
276
264
- private * getReferenceLinks ( document : SkinnyTextDocument ) : Iterable < LinkData > {
277
+ private * getReferenceLinks ( document : SkinnyTextDocument ) : Iterable < MdLink > {
265
278
const text = document . getText ( ) ;
266
279
for ( const match of text . matchAll ( referenceLinkPattern ) ) {
267
280
let linkStart : vscode . Position ;
@@ -282,6 +295,7 @@ export class MdLinkProvider implements vscode.DocumentLinkProvider {
282
295
}
283
296
284
297
yield {
298
+ kind : 'link' ,
285
299
sourceText : reference ,
286
300
sourceRange : new vscode . Range ( linkStart , linkEnd ) ,
287
301
sourceResource : document . uri ,
@@ -293,7 +307,7 @@ export class MdLinkProvider implements vscode.DocumentLinkProvider {
293
307
}
294
308
}
295
309
296
- public * getDefinitionLinks ( document : SkinnyTextDocument ) : Iterable < LinkData > {
310
+ public * getLinkDefinitions ( document : SkinnyTextDocument ) : Iterable < MdLinkDefinition > {
297
311
const text = document . getText ( ) ;
298
312
for ( const match of text . matchAll ( definitionPattern ) ) {
299
313
const pre = match [ 1 ] ;
@@ -308,14 +322,13 @@ export class MdLinkProvider implements vscode.DocumentLinkProvider {
308
322
const target = parseLink ( document , text ) ;
309
323
if ( target ) {
310
324
yield {
325
+ kind : 'definition' ,
311
326
sourceText : link ,
312
327
sourceResource : document . uri ,
313
328
sourceRange : new vscode . Range ( linkStart , linkEnd ) ,
314
- target : {
315
- kind : 'definition' ,
316
- ref : reference ,
317
- target
318
- }
329
+
330
+ ref : reference ,
331
+ target,
319
332
} ;
320
333
}
321
334
} else {
@@ -324,14 +337,12 @@ export class MdLinkProvider implements vscode.DocumentLinkProvider {
324
337
const target = parseLink ( document , link ) ;
325
338
if ( target ) {
326
339
yield {
340
+ kind : 'definition' ,
327
341
sourceText : link ,
328
342
sourceResource : document . uri ,
329
343
sourceRange : new vscode . Range ( linkStart , linkEnd ) ,
330
- target : {
331
- kind : 'definition' ,
332
- ref : reference ,
333
- target,
334
- }
344
+ ref : reference ,
345
+ target,
335
346
} ;
336
347
}
337
348
}
@@ -340,17 +351,17 @@ export class MdLinkProvider implements vscode.DocumentLinkProvider {
340
351
}
341
352
342
353
export class LinkDefinitionSet {
343
- private readonly _map = new Map < string , LinkData > ( ) ;
354
+ private readonly _map = new Map < string , MdLink > ( ) ;
344
355
345
- constructor ( links : Iterable < LinkData > ) {
356
+ constructor ( links : Iterable < MdLink > ) {
346
357
for ( const link of links ) {
347
- if ( link . target . kind === 'definition' ) {
348
- this . _map . set ( link . target . ref , link ) ;
358
+ if ( link . kind === 'definition' ) {
359
+ this . _map . set ( link . ref , link ) ;
349
360
}
350
361
}
351
362
}
352
363
353
- public lookup ( ref : string ) : LinkData | undefined {
364
+ public lookup ( ref : string ) : MdLink | undefined {
354
365
return this . _map . get ( ref ) ;
355
366
}
356
367
}
0 commit comments