@@ -9,16 +9,12 @@ import picomatch from 'picomatch/posix'
99import { compact } from '@zardoy/utils'
1010import { getBinCommands , runBinCommand } from './commands/runBinCommand'
1111
12- const selector = { language : 'json' , pattern : '**/package.json' }
13- export const registerPackageJsonAutoComplete = ( ) => {
14- registerCompletions ( )
15- registerLinks ( )
16- }
12+ export const packageJsonSelector = { language : 'json' , pattern : '**/package.json' }
1713
18- const registerCompletions = ( ) => {
14+ export const registerPackageJsonCompletions = ( ) => {
1915 if ( ! getExtensionSetting ( 'packageJsonIntellisense' ) ) return
2016 vscode . languages . registerCompletionItemProvider (
21- selector ,
17+ packageJsonSelector ,
2218 {
2319 async provideCompletionItems ( document , position , token , context ) {
2420 if ( ! document . uri . path . endsWith ( 'package.json' ) ) return [ ]
@@ -121,67 +117,6 @@ const registerCompletions = () => {
121117 )
122118}
123119
124- const registerLinks = ( ) => {
125- vscode . languages . registerDocumentLinkProvider ( selector , {
126- provideDocumentLinks ( document , token ) {
127- const root = parseTree ( document . getText ( ) ) !
128- const scriptsRootNode = findNodeAtLocation ( root , [ 'scripts' ] )
129- const scriptsNodes = scriptsRootNode ?. children
130- const links : vscode . DocumentLink [ ] = [ ]
131- const nodeObjectMap = ( nodes : Node [ ] , type : 'prop' | 'value' ) => {
132- const indexGetter = type === 'prop' ? 0 : 1
133- return compact ( nodes . map ( value => value . type === 'property' && value . children ! [ indexGetter ] ) )
134- }
135-
136- // #region scripts links
137- if ( getExtensionSetting ( 'packageJsonLinks' ) && scriptsNodes )
138- for ( const scriptNode of nodeObjectMap ( scriptsNodes , 'value' ) ) {
139- const script = getNodeValue ( scriptNode )
140- let match : RegExpExecArray | null
141- while ( ( match = scriptLinksCommandRegex . exec ( script ) ) ) {
142- const scriptRefName = match . groups ! . NAME !
143- // if (!(scriptRefName in scriptsObject)) continue
144- const targetScriptNode = scriptsNodes . find ( node => node . children ! [ 0 ] ! . value === scriptRefName ) ?. children ?. [ 1 ]
145- if ( ! targetScriptNode ) continue
146- const getNodeStringStart = ( node : Node ) => node . offset + 1
147- const startOffset = getNodeStringStart ( scriptNode ) + match . index + match [ 1 ] ! . length
148- const positions = [ startOffset , startOffset + scriptRefName . length ] . map ( offset => document . positionAt ( offset ) ) as [
149- vscode . Position ,
150- vscode . Position ,
151- ]
152- const { line : targetScriptLine , character : targetScriptCharacter } = document . positionAt ( getNodeStringStart ( targetScriptNode ) )
153- const fragment = `L${ targetScriptLine + 1 } ,${ targetScriptCharacter + 1 } `
154- links . push ( {
155- range : new vscode . Range ( ...positions ) ,
156- tooltip : 'Reveal script' ,
157- target : document . uri . with ( { fragment } ) ,
158- } )
159- }
160-
161- scriptLinksCommandRegex . lastIndex = 0
162- }
163- // #endregion
164-
165- if ( getExtensionSetting ( 'packageJsonScriptNameLink' ) && scriptsNodes )
166- for ( const scriptNode of nodeObjectMap ( scriptsNodes , 'prop' ) ) {
167- const startOffset = scriptNode . offset + 1
168- const scriptName : string = scriptNode . value
169- const positions = [ startOffset , startOffset + scriptName . length ] . map ( offset => document . positionAt ( offset ) ) as [
170- vscode . Position ,
171- vscode . Position ,
172- ]
173- links . push ( {
174- range : new vscode . Range ( ...positions ) ,
175- tooltip : 'Run Script' ,
176- target : vscode . Uri . parse ( `command:${ getExtensionCommandId ( 'runNpmScript' ) } ?${ JSON . stringify ( scriptName ) } ` ) ,
177- } )
178- }
179-
180- return links
181- } ,
182- } )
183- }
184-
185120const listFilesCompletions = async ( baseDocument : vscode . TextDocument , stringContents : string , completionRange : vscode . Range , glob = '*' ) => {
186121 const folderPath = stringContents . split ( '/' ) . slice ( 0 , - 1 ) . join ( '/' )
187122 const filesList = await vscode . workspace . fs . readDirectory ( Utils . joinPath ( baseDocument . uri , '..' , folderPath ) )
@@ -210,4 +145,3 @@ const pathAutoComplete = {
210145}
211146
212147const scriptCompletionCommandRegex = / ^ ( p n p m | y a r n ) | ( p n p m | y a r n | n p m ) r u n $ /
213- const scriptLinksCommandRegex = / ( ( ^ | & & ) \s ? ( ( p n p m | y a r n ) | ( p n p m | y a r n | n p m ) r u n ) ) (?< NAME > [ A - z \d - ] + ) / g
0 commit comments