提交 f500f659 编写于 作者: M Matt Bierner

Create links for files section in tsconfig

上级 91d55c96
......@@ -7,6 +7,12 @@ import * as jsonc from 'jsonc-parser';
import { dirname, join } from 'path';
import * as vscode from 'vscode';
function mapNode<R>(node: jsonc.Node | undefined, f: (x: jsonc.Node) => R): R[] {
return node && node.type === 'array' && node.children
? node.children.map(f)
: [];
}
class TsconfigLinkProvider implements vscode.DocumentLinkProvider {
public provideDocumentLinks(
......@@ -18,34 +24,43 @@ class TsconfigLinkProvider implements vscode.DocumentLinkProvider {
return null;
}
return this.getNodes(root).map(node =>
new vscode.DocumentLink(
this.getRange(document, node),
this.getTarget(document, node)));
return [
this.getExendsLink(document, root),
...this.getFilesLinks(document, root),
...this.getReferencesLinks(document, root)
].filter(x => !!x) as vscode.DocumentLink[];
}
private getNodes(root: jsonc.Node): ReadonlyArray<jsonc.Node> {
const nodes: jsonc.Node[] = [];
const extendsNode = jsonc.findNodeAtLocation(root, ['extends']);
if (this.isPathValue(extendsNode)) {
nodes.push(extendsNode);
}
private getExendsLink(document: vscode.TextDocument, root: jsonc.Node): vscode.DocumentLink | undefined {
return this.pathNodeToLink(document, jsonc.findNodeAtLocation(root, ['extends']));
}
const referencesNode = jsonc.findNodeAtLocation(root, ['references']);
if (referencesNode && referencesNode.type === 'array' && referencesNode.children) {
for (const child of referencesNode.children) {
const path = jsonc.findNodeAtLocation(child, ['path']);
if (this.isPathValue(path)) {
nodes.push(path);
}
}
}
private getFilesLinks(document: vscode.TextDocument, root: jsonc.Node) {
return mapNode(
jsonc.findNodeAtLocation(root, ['files']),
node => this.pathNodeToLink(document, node));
}
private getReferencesLinks(document: vscode.TextDocument, root: jsonc.Node) {
return mapNode(
jsonc.findNodeAtLocation(root, ['references']),
child => this.pathNodeToLink(document, jsonc.findNodeAtLocation(child, ['path'])));
}
return nodes;
private pathNodeToLink(
document: vscode.TextDocument,
node: jsonc.Node | undefined
): vscode.DocumentLink | undefined {
return this.isPathValue(node)
? new vscode.DocumentLink(this.getRange(document, node), this.getTarget(document, node))
: undefined;
}
private isPathValue(extendsNode: jsonc.Node | undefined): extendsNode is jsonc.Node {
return extendsNode && extendsNode.type === 'string' && extendsNode.value;
return extendsNode
&& extendsNode.type === 'string'
&& extendsNode.value
&& !(extendsNode.value as string).includes('*');
}
private getTarget(document: vscode.TextDocument, node: jsonc.Node): vscode.Uri {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册