-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: link to assets and file references (#39)
- Loading branch information
Showing
3 changed files
with
130 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { Node } from 'jsonc-parser'; | ||
import { Range, TextDocument } from 'vscode'; | ||
|
||
import { getPluginsArrayNode, rangeForOffset } from './utils/iteratePlugins'; | ||
|
||
function rangeForMatch(document: TextDocument, match: { index: number; length: number }) { | ||
return new Range( | ||
document.positionAt(match.index), | ||
document.positionAt(match.index + match.length) | ||
); | ||
} | ||
|
||
export function iterateFileReferences( | ||
document: TextDocument, | ||
node: Node | undefined, | ||
callback: (props: { fileReference: string; range: Range; match: RegExpMatchArray }) => void | ||
) { | ||
const pluginsNode = getPluginsArrayNode(node); | ||
const pluginsRange = pluginsNode ? rangeForOffset(document, pluginsNode) : null; | ||
|
||
const matches = document.getText().matchAll(/"(\.\/.*)"/g); | ||
for (const match of matches) { | ||
if (match.index == null) { | ||
continue; | ||
} | ||
const [, fileReference] = match; | ||
const range = rangeForMatch(document, { | ||
// index includes the quotes, so move it by 1 to exclude the first quote | ||
index: match.index + 1, | ||
length: fileReference.length, | ||
}); | ||
// Avoid matching against file imports that are inside of the plugins object. | ||
// Otherwise a plugin like `./my-plugin` would overwrite the better link we already created. | ||
if (pluginsRange && pluginsRange.contains(range)) { | ||
continue; | ||
} | ||
callback({ fileReference, range, match }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters