-
-
Notifications
You must be signed in to change notification settings - Fork 959
/
Copy pathdocumentLinkManager.ts
65 lines (58 loc) · 2.33 KB
/
documentLinkManager.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
'use strict'
import { v4 as uuid } from 'uuid'
import { TextDocument } from 'vscode-languageserver-textdocument'
import { DocumentLink, Range } from 'vscode-languageserver-types'
import { omit } from '../util/lodash'
import { CancellationToken, Disposable } from '../util/protocol'
import { DocumentLinkProvider, DocumentSelector } from './index'
import Manager from './manager'
interface DocumentLinkWithSource extends DocumentLink {
source?: string
}
function rangeToString(range: Range): string {
return `${range.start.line},${range.start.character},${range.end.line},${range.end.character}`
}
export default class DocumentLinkManager extends Manager<DocumentLinkProvider> {
public register(selector: DocumentSelector, provider: DocumentLinkProvider): Disposable {
return this.addProvider({
id: uuid(),
selector,
provider
})
}
public async provideDocumentLinks(document: TextDocument, token: CancellationToken): Promise<DocumentLinkWithSource[] | null> {
let items = this.getProviders(document)
if (items.length == 0) return null
const links: DocumentLinkWithSource[] = []
const seenRanges: Set<string> = new Set()
const results = await Promise.allSettled(items.map(async item => {
let { id, provider } = item
const arr = await provider.provideDocumentLinks(document, token)
if (Array.isArray(arr)) {
let check = links.length > 0
arr.forEach(link => {
if (check) {
const rangeString = rangeToString(link.range)
if (!seenRanges.has(rangeString)) {
seenRanges.add(rangeString)
links.push(Object.assign({ source: id }, link))
}
} else {
if (items.length > 1) seenRanges.add(rangeToString(link.range))
links.push(Object.assign({ source: id }, link))
}
})
}
}))
this.handleResults(results, 'provideDocumentLinks')
return links
}
public async resolveDocumentLink(link: DocumentLinkWithSource, token: CancellationToken): Promise<DocumentLink> {
let provider = this.getProviderById(link.source)
if (typeof provider.resolveDocumentLink === 'function') {
let resolved = await Promise.resolve(provider.resolveDocumentLink(omit(link, ['source']), token))
if (resolved) Object.assign(link, resolved)
}
return link
}
}