@@ -2,7 +2,6 @@ import * as vscode from "vscode";
22import {
33 createDocumentSelectorsForExtensions ,
44 createEndsWithRegex ,
5- fileBeginningRange ,
65 getCleanedLineUntilPosition ,
76 getCompleteStringFromLine ,
87} from "../utils" ;
@@ -32,13 +31,18 @@ const providerConfigs: ProviderConfig[] = [
3231] ;
3332
3433type GroupedUrls = {
35- [ key : string ] : vscode . Uri [ ] ;
34+ [ key : string ] : vscode . Location [ ] ;
35+ } ;
36+
37+ type UrlDefinition = {
38+ name : string ;
39+ range : vscode . Range ;
3640} ;
3741
3842type UrlFileConfig = {
3943 uri : vscode . Uri ;
4044 appName : string | null ;
41- urlNames : string [ ] ;
45+ urlDefinitions : UrlDefinition [ ] ;
4246} ;
4347
4448let cachedUrlsConfigs : vscode . CompletionItem [ ] = [ ] ;
@@ -50,24 +54,33 @@ async function getUrlsFilesUris() {
5054}
5155
5256async function getUrlsConfigsFromFile ( uri : vscode . Uri ) : Promise < UrlFileConfig > {
53- const data = ( await vscode . workspace . fs . readFile ( uri ) ) . toString ( ) ;
57+ const document = await vscode . workspace . openTextDocument ( uri ) ;
58+ const data = document . getText ( ) ;
5459 const appNameRegex = / a p p _ n a m e \s * = \s * ( .* ) / ;
5560 let appName = null ;
5661 const appNameMatch = appNameRegex . exec ( data ) ;
5762 if ( appNameMatch && appNameMatch [ 1 ] ) {
5863 appName = appNameMatch [ 1 ] . trim ( ) . replace ( / [ ' " ] / g, "" ) ;
5964 }
60- const nameArgRegex = / \b ( n a m e ) ( * = * ) * [ ' " ] ( . * ) [ ' " ] / g;
61- const urlNames = [ ] ;
65+ const nameArgRegex = / \b n a m e \s * = \s * [ ' " ] ( [ ^ ' " ] + ) [ ' " ] / g;
66+ const urlDefinitions : UrlDefinition [ ] = [ ] ;
6267 let match ;
6368
6469 while ( ( match = nameArgRegex . exec ( data ) ) !== null ) {
65- const urlName = match [ match . length - 1 ] . trim ( ) ;
66- if ( urlName !== "" ) {
67- urlNames . push ( urlName ) ;
70+ const urlName = match [ 1 ] ?. trim ( ) ;
71+ if ( urlName ) {
72+ const valueStartOffset = ( match . index ?? 0 ) + match [ 0 ] . indexOf ( urlName ) ;
73+ const valueEndOffset = valueStartOffset + urlName . length ;
74+ urlDefinitions . push ( {
75+ name : urlName ,
76+ range : new vscode . Range (
77+ document . positionAt ( valueStartOffset ) ,
78+ document . positionAt ( valueEndOffset )
79+ ) ,
80+ } ) ;
6881 }
6982 }
70- return { appName, urlNames , uri } ;
83+ return { appName, urlDefinitions , uri } ;
7184}
7285
7386export async function updateUrlsConfigsCache ( ) {
@@ -76,10 +89,10 @@ export async function updateUrlsConfigsCache() {
7689 cachedGroupUrls = { } ;
7790 for ( const url of urls ) {
7891 const configs = await getUrlsConfigsFromFile ( url ) ;
79- for ( const urlName of configs . urlNames ) {
80- const completeUrl = `${ configs . appName } ${
81- configs . appName ? ":" : ""
82- } ${ urlName } `;
92+ for ( const urlDefinition of configs . urlDefinitions ) {
93+ const completeUrl = `${ configs . appName } ${ configs . appName ? ":" : "" } ${
94+ urlDefinition . name
95+ } `;
8396 cachedUrlsConfigs . push ( {
8497 label : completeUrl ,
8598 insertText : completeUrl ,
@@ -88,7 +101,9 @@ export async function updateUrlsConfigsCache() {
88101 if ( ! cachedGroupUrls [ completeUrl ] ) {
89102 cachedGroupUrls [ completeUrl ] = [ ] ;
90103 }
91- cachedGroupUrls [ completeUrl ] . push ( configs . uri ) ;
104+ cachedGroupUrls [ completeUrl ] . push (
105+ new vscode . Location ( configs . uri , urlDefinition . range )
106+ ) ;
92107 }
93108 }
94109 cachedLastUpdatedTime = new Date ( ) . getTime ( ) ;
@@ -150,10 +165,7 @@ async function urlProviderDefinition(
150165 }
151166 await getOrUpdateCompletionItems ( ) ;
152167 if ( urlName in cachedGroupUrls ) {
153- return cachedGroupUrls [ urlName ] . map ( ( uri ) => ( {
154- uri,
155- range : fileBeginningRange ,
156- } ) ) ;
168+ return cachedGroupUrls [ urlName ] ;
157169 }
158170
159171 return [ ] ;
@@ -171,7 +183,8 @@ function activateDefinitionProviderForUrls() {
171183export function activateUrlNamesAutocompletion (
172184 context : vscode . ExtensionContext
173185) {
174- activateDefinitionProviderForUrls ( ) ;
186+ const definitionProvider = activateDefinitionProviderForUrls ( ) ;
187+ context . subscriptions . push ( definitionProvider ) ;
175188 for ( const config of providerConfigs ) {
176189 const provider = createAutocompletionProvider ( config ) ;
177190 context . subscriptions . push ( provider ) ;
0 commit comments