|
14 | 14 | * limitations under the License. |
15 | 15 | */ |
16 | 16 |
|
17 | | -import * as YAML from 'yaml'; |
18 | | -import { DocumentLink, DocumentLinkProvider, TextDocument, Uri } from 'vscode'; |
19 | | -import { readMapFromMap, readSeqFromMap, requireMap } from '../parsing/yaml-file-parsing'; |
20 | | -import { rangeFromYamlNode } from './yaml-range'; |
| 17 | +import { CancellationToken, DocumentLink, DocumentLinkProvider, Range, TextDocument, Uri } from 'vscode'; |
| 18 | +import { parseYamlToCTreeItem } from '../../generic/tree-item-yaml-parser'; |
| 19 | +import { CTreeItem, ETreeItemKind, ITreeItem } from '../../generic/tree-item'; |
| 20 | +import type { SolutionManager } from '../solution-manager'; |
21 | 21 |
|
22 | | -export type ReferenceLinkProviderOptions = { |
23 | | - /** |
24 | | - * Name of the root node for the YAML file, e.g., solution. |
25 | | - */ |
26 | | - parentNode: string; |
| 22 | +/** |
| 23 | + * Provide links for file references in solution and project files. |
| 24 | + */ |
| 25 | +export class ReferenceLinkProvider implements DocumentLinkProvider<DocumentLink> { |
| 26 | + constructor( |
| 27 | + private readonly solutionManager: SolutionManager, |
| 28 | + ) { |
| 29 | + } |
27 | 30 |
|
28 | | - /** |
29 | | - * Name of the node containing the list of linkable references, e.g., projects. |
30 | | - */ |
31 | | - listNode: string; |
| 31 | + public provideDocumentLinks(textDocument: TextDocument, _token?: CancellationToken): DocumentLink[] { |
| 32 | + try { |
| 33 | + const topItem = parseYamlToCTreeItem(textDocument.getText(), textDocument.fileName); |
32 | 34 |
|
33 | | - /** |
34 | | - * Name of the node on the linkable reference, e.g., project. |
35 | | - */ |
36 | | - referenceNode: string; |
37 | | -} |
| 35 | + return (topItem?.filterItems(item => this.isReferenceFileItem(item)) ?? [])?.flatMap((item): DocumentLink[] => { |
| 36 | + const documentLink = this.treeItemToDocumentLink(item, textDocument); |
| 37 | + return documentLink ? [documentLink] : []; |
| 38 | + }) ?? []; |
| 39 | + } catch { |
| 40 | + // If we can't parse the document, we can't provide links |
| 41 | + return []; |
| 42 | + } |
| 43 | + } |
38 | 44 |
|
39 | | -const projectNodeToDocumentLink = ( |
40 | | - textDocument: TextDocument, |
41 | | - options: ReferenceLinkProviderOptions, |
42 | | - projectNode: YAML.YAMLMap, |
43 | | -): DocumentLink | undefined => { |
44 | | - const projectScalar = projectNode.get(options.referenceNode, true); |
| 45 | + public resolveDocumentLink(link: DocumentLink): DocumentLink { |
| 46 | + return link; |
| 47 | + } |
45 | 48 |
|
46 | | - if (projectScalar && typeof projectScalar.value === 'string') { |
| 49 | + protected isReferenceFileItem(item: ITreeItem<CTreeItem>): item is CTreeItem { |
| 50 | + const tag = item.getTag(); |
| 51 | + return !!tag && this.getReferenceItemTags().includes(tag); |
| 52 | + } |
| 53 | + |
| 54 | + protected getReferenceItemTags(): string[] { |
| 55 | + return ['file', 'layer', 'project', 'script', 'regions']; |
| 56 | + } |
| 57 | + |
| 58 | + private treeItemToDocumentLink(item: ITreeItem<CTreeItem> | undefined, textDocument: TextDocument) : DocumentLink | undefined { |
| 59 | + const uri = this.getUriFromItem(item); |
| 60 | + if (!uri) { |
| 61 | + return undefined; |
| 62 | + } |
| 63 | + const range = this.rangeFromItem(item, textDocument); |
47 | 64 | return { |
48 | | - range: rangeFromYamlNode(textDocument, projectScalar), |
49 | | - target: Uri.joinPath(textDocument.uri, '..', projectScalar.value), |
| 65 | + range, |
| 66 | + target: uri, |
50 | 67 | }; |
51 | 68 | } |
52 | 69 |
|
53 | | - return undefined; |
54 | | -}; |
55 | 70 |
|
56 | | -/** |
57 | | - * Provide links for file references in solution and project files. |
58 | | - */ |
59 | | -export const createReferenceLinkProvider = (options: ReferenceLinkProviderOptions): DocumentLinkProvider<DocumentLink> => ({ |
60 | | - provideDocumentLinks: (textDocument: TextDocument): DocumentLink[] => { |
61 | | - try { |
62 | | - const yamlDocument = YAML.parseDocument(textDocument.getText()); |
63 | | - const root = requireMap(yamlDocument.contents); |
64 | | - const parent = readMapFromMap(options.parentNode)(root); |
65 | | - const list = readSeqFromMap(options.listNode)(parent); |
66 | | - const itemMaps = list.items.filter(YAML.isMap); |
| 71 | + private getUriFromItem(item?: ITreeItem<CTreeItem>): Uri | undefined { |
| 72 | + if (!item || item.getKind() !== ETreeItemKind.Scalar) { |
| 73 | + return undefined; |
| 74 | + } |
| 75 | + let text = item.getText(); |
| 76 | + if (!text) { |
| 77 | + return undefined; |
| 78 | + } |
| 79 | + const rpcData = this.solutionManager.getRpcData(); |
| 80 | + const context = this.getItemContext(item); |
| 81 | + if (rpcData && context) { |
| 82 | + text = rpcData.expandString(text, context); |
| 83 | + } |
67 | 84 |
|
68 | | - return itemMaps.flatMap((projectNode): DocumentLink[] => { |
69 | | - const documentLink = projectNodeToDocumentLink(textDocument, options, projectNode); |
70 | | - return documentLink ? [documentLink] : []; |
71 | | - }); |
72 | | - } catch { |
73 | | - // If we can't parse the document, we can't provide links |
74 | | - return []; |
| 85 | + const resolvedPath = item.resolvePath(text); |
| 86 | + return resolvedPath ? Uri.file(resolvedPath) : undefined; |
| 87 | + } |
| 88 | + |
| 89 | + private rangeFromItem(item: ITreeItem<CTreeItem> | undefined, textDocument: TextDocument): Range { |
| 90 | + return new Range( |
| 91 | + textDocument.positionAt(item?.range?.[0] ?? 0), |
| 92 | + textDocument.positionAt(item?.range?.[1] ?? 0), |
| 93 | + ); |
| 94 | + } |
| 95 | + |
| 96 | + private getItemContext(item: ITreeItem<CTreeItem>): string | undefined { |
| 97 | + const csolution = this.solutionManager.getCsolution(); |
| 98 | + if (!csolution) { |
| 99 | + return undefined; |
| 100 | + } |
| 101 | + const rootFileName = item.rootFileName; |
| 102 | + let context = undefined; |
| 103 | + if (rootFileName.includes('.cproject.y')) { |
| 104 | + context = csolution.getContextDescriptor(rootFileName)?.displayName; |
75 | 105 | } |
76 | | - }, |
77 | | - resolveDocumentLink: (link: DocumentLink): DocumentLink => link, |
78 | | -}); |
| 106 | + return context ?? csolution.actionContext; |
| 107 | + } |
| 108 | +} |
0 commit comments