2023-05-15 10:21:43 +00:00
|
|
|
import type { TextDocument } from 'vscode-languageserver-textdocument'
|
2022-10-17 16:59:07 +00:00
|
|
|
import { State } from './util/state'
|
2023-05-15 10:21:43 +00:00
|
|
|
import type { DocumentLink, Range } from 'vscode-languageserver'
|
2022-10-17 16:59:07 +00:00
|
|
|
import { isCssDoc } from './util/css'
|
|
|
|
import { getLanguageBoundaries } from './util/getLanguageBoundaries'
|
|
|
|
import { findAll, indexToPosition } from './util/find'
|
|
|
|
import { getTextWithoutComments } from './util/doc'
|
|
|
|
import { absoluteRange } from './util/absoluteRange'
|
|
|
|
import * as semver from './util/semver'
|
|
|
|
|
|
|
|
export function getDocumentLinks(
|
|
|
|
state: State,
|
|
|
|
document: TextDocument,
|
|
|
|
resolveTarget: (linkPath: string) => string
|
|
|
|
): DocumentLink[] {
|
|
|
|
return getConfigDirectiveLinks(state, document, resolveTarget)
|
|
|
|
}
|
|
|
|
|
|
|
|
function getConfigDirectiveLinks(
|
|
|
|
state: State,
|
|
|
|
document: TextDocument,
|
|
|
|
resolveTarget: (linkPath: string) => string
|
|
|
|
): DocumentLink[] {
|
|
|
|
if (!semver.gte(state.version, '3.2.0')) {
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
|
|
|
|
let links: DocumentLink[] = []
|
|
|
|
let ranges: Range[] = []
|
|
|
|
|
|
|
|
if (isCssDoc(state, document)) {
|
|
|
|
ranges.push(undefined)
|
|
|
|
} else {
|
|
|
|
let boundaries = getLanguageBoundaries(state, document)
|
|
|
|
if (!boundaries) return []
|
|
|
|
ranges.push(...boundaries.filter((b) => b.type === 'css').map(({ range }) => range))
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let range of ranges) {
|
|
|
|
let text = getTextWithoutComments(document, 'css', range)
|
|
|
|
let matches = findAll(/@config\s*(?<path>'[^']+'|"[^"]+")/g, text)
|
|
|
|
|
|
|
|
for (let match of matches) {
|
|
|
|
links.push({
|
|
|
|
target: resolveTarget(match.groups.path.slice(1, -1)),
|
|
|
|
range: absoluteRange(
|
|
|
|
{
|
|
|
|
start: indexToPosition(text, match.index + match[0].length - match.groups.path.length),
|
|
|
|
end: indexToPosition(text, match.index + match[0].length),
|
|
|
|
},
|
|
|
|
range
|
|
|
|
),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return links
|
|
|
|
}
|