33 * Licensed under the MIT License. See LICENSE file in the project root for license information.
44 *-----------------------------------------------------------------------------------------------*/
55
6+ import * as _ from 'lodash' ;
7+ import * as querystring from 'querystring' ;
68import * as vscode from 'vscode' ;
79import { DocumentLinkProvider , Uri } from 'vscode' ;
8-
9- import * as querystring from 'querystring' ;
10- import * as _ from 'lodash' ;
11- import { MappingItem , Node , NodeProvider } from './locator-util' ;
10+ import * as jl from './json-locator' ;
1211import * as kuberesources from './kuberesources' ;
12+ import { K8S_RESOURCE_SCHEME , K8S_RESOURCE_SCHEME_READONLY , getOutputFormat , helmfsUri , kubefsUri } from './kuberesources.utils' ;
13+ import { MappingItem , Node , NodeProvider } from './locator-util' ;
1314import * as yl from './yaml-locator' ;
14- import * as jl from './json-locator' ;
15- import { K8S_RESOURCE_SCHEME , K8S_RESOURCE_SCHEME_READONLY , getOutputFormat , helmfsUri , kubefsUri } from './kuberesources.virtualfs' ;
1615
1716// >>> URI Cache >>>
1817
@@ -38,10 +37,7 @@ function cacheEditingUris() {
3837export class KubernetesResourceLinkProvider implements DocumentLinkProvider {
3938 provideDocumentLinks ( document : vscode . TextDocument , _token : vscode . CancellationToken ) : vscode . ProviderResult < vscode . DocumentLink [ ] > {
4039 const sourceKind = k8sKind ( document ) ;
41- const df = getDocumentFormat ( document ) ;
42- const docs = ( df === 'yaml' || df === 'yml' ) ?
43- yl . yamlLocator . getYamlDocuments ( document )
44- : df === 'json' ? jl . jsonLocator . getJsonDocuments ( document ) : [ ] ;
40+ const docs = getParsedDocuments ( document ) ;
4541 const leaves = getLeafNodes ( docs ) ;
4642
4743 try {
@@ -54,13 +50,21 @@ export class KubernetesResourceLinkProvider implements DocumentLinkProvider {
5450 }
5551}
5652
53+ export function getParsedDocuments ( document : vscode . TextDocument ) : NodeProvider [ ] {
54+ const df = getDocumentFormat ( document ) ;
55+ const docs = ( df === 'yaml' || df === 'yml' ) ?
56+ yl . yamlLocator . getYamlDocuments ( document )
57+ : df === 'json' ? jl . jsonLocator . getJsonDocuments ( document ) : [ ] ;
58+ return docs ;
59+ }
60+
5761function getDocumentFormat ( document : vscode . TextDocument ) : string | undefined {
5862 const path = document . uri . path ;
5963 const sepIndex = path . indexOf ( '.' ) ;
6064 return sepIndex >= 0 ? path . substring ( sepIndex + 1 ) . toLocaleLowerCase ( ) : undefined ;
6165}
6266
63- function getLeafNodes ( docs : NodeProvider [ ] ) : Node [ ] {
67+ export function getLeafNodes ( docs : NodeProvider [ ] ) : Node [ ] {
6468 const rootNodes = _ . flatMap ( docs , ( d ) => d . nodes ) ;
6569 const nonRootNodes = _ . flatMap ( rootNodes , ( n ) => descendants ( n ) ) ;
6670 const allNodes = rootNodes . concat ( nonRootNodes ) ;
@@ -80,13 +84,20 @@ function getLink(document: vscode.TextDocument, sourceKind: string, node: Node):
8084 return undefined ;
8185}
8286
83- function range ( document : vscode . TextDocument , node : MappingItem ) {
87+ export function range ( document : vscode . TextDocument , node : MappingItem ) : vscode . Range {
8488 return new vscode . Range (
8589 document . positionAt ( node . value . startPosition ) ,
8690 document . positionAt ( node . value . endPosition )
8791 ) ;
8892}
8993
94+ export function keyRange ( document : vscode . TextDocument , node : MappingItem ) : vscode . Range {
95+ return new vscode . Range (
96+ document . positionAt ( node . key . startPosition ) ,
97+ document . positionAt ( node . key . endPosition )
98+ ) ;
99+ }
100+
90101function descendants ( node : Node ) : Node [ ] {
91102 const direct = children ( node ) ;
92103 const indirect = direct . map ( ( n ) => descendants ( n ) ) ;
0 commit comments