@@ -9,10 +9,105 @@ export async function readJSON(uri: vscode.Uri): Promise<Record<string, any>> {
99 return JSON5 . parse ( fileContentString ) ;
1010}
1111
12+ export async function readTextFile ( uri : vscode . Uri ) : Promise < string > {
13+ const fileContentRaw = await vscode . workspace . fs . readFile ( uri ) ;
14+ return Buffer . from ( fileContentRaw ) . toString ( "utf8" ) ;
15+ }
16+
1217export function resolveTemplateFile (
1318 workspace : vscode . WorkspaceFolder ,
1419 filename : string
1520) : vscode . Uri {
1621 const actualFilename = filename . replace ( / ^ ~ \/ / , configRoot + "/" ) ;
1722 return vscode . Uri . joinPath ( workspace . uri , actualFilename ) ;
1823}
24+
25+ export function getImportSpecifierFromLine ( line : string ) :
26+ | {
27+ filename : string ;
28+ importSpecifier : string ;
29+ }
30+ | undefined {
31+ line = line . trim ( ) ;
32+ if ( ! line . startsWith ( `"$import":` ) ) {
33+ return undefined ;
34+ }
35+
36+ line = line . substring ( line . indexOf ( ":" ) + 1 ) . trim ( ) ;
37+ line = line . substring ( line . indexOf ( '"' ) + 1 ) ;
38+ if ( line . includes ( '"' ) ) {
39+ line = line . substring ( 0 , line . indexOf ( '"' ) ) ;
40+ }
41+
42+ if ( ! line . includes ( ".json#" ) ) {
43+ return undefined ;
44+ }
45+
46+ const [ filename , importSpecifier ] = line . split ( "#" ) ;
47+ return { filename, importSpecifier } ;
48+ }
49+
50+ export async function resolveTemplate (
51+ workspace : vscode . WorkspaceFolder ,
52+ filename : string ,
53+ importSpecifier : string
54+ ) : Promise < Record < string , any > | undefined > {
55+ const uri = resolveTemplateFile ( workspace , filename ) ;
56+ const fileContent = await readJSON ( uri ) ;
57+ return fileContent [ importSpecifier ] ;
58+ }
59+
60+ export function formatTemplateDefinition (
61+ template : Record < string , any > ,
62+ label : string | undefined ,
63+ description : string | undefined
64+ ) : string {
65+ let ret = `\`\`\`json
66+ ${ JSON . stringify ( template , null , 2 ) }
67+ \`\`\`` ;
68+ if ( description ) {
69+ ret = description + "\n\n" + ret ;
70+ }
71+ if ( label ) {
72+ ret = `**${ label } **\n\n${ ret } ` ;
73+ }
74+ return ret ;
75+ }
76+
77+ function getLineIndentation ( line : string ) : string {
78+ return line . match ( / ^ ( \s * ) / ) ?. [ 1 ] ?? "" ;
79+ }
80+
81+ export function getBlockRange (
82+ jsonDoc : string ,
83+ position : vscode . Position
84+ ) : vscode . Range {
85+ const lines = jsonDoc . split ( "\n" ) ;
86+ let start = position . line ;
87+ let end = position . line ;
88+ const initialLineIndent = getLineIndentation ( lines [ start ] ) . length ;
89+ while ( start > 0 ) {
90+ const line = lines [ start ] ;
91+ if (
92+ getLineIndentation ( line ) . length < initialLineIndent &&
93+ line . trim ( ) . endsWith ( "{" )
94+ ) {
95+ break ;
96+ }
97+ start -- ;
98+ }
99+ while ( end < lines . length ) {
100+ const line = lines [ end ] ;
101+ if (
102+ getLineIndentation ( line ) . length < initialLineIndent &&
103+ line . trim ( ) . startsWith ( "}" )
104+ ) {
105+ break ;
106+ }
107+ end ++ ;
108+ }
109+ return new vscode . Range (
110+ new vscode . Position ( start , getLineIndentation ( lines [ start ] ) . length ) ,
111+ new vscode . Position ( end , getLineIndentation ( lines [ end ] ) . length + 1 )
112+ ) ;
113+ }
0 commit comments