@@ -89,7 +89,8 @@ export async function activate(context: ExtensionContext): Promise<void> {
8989 command : `jsonnet.evalItem` ,
9090 arguments : [ evalFilePath ( editor ) , editor . selection . active ] ,
9191 } ;
92- evalAndDisplay ( params , false ) ;
92+ const tempFile = createTmpFile ( false ) ;
93+ evalAndDisplay ( params , false , tempFile ) ;
9394 } ) ,
9495 commands . registerCommand ( 'jsonnet.evalFile' , evalFileFunc ( false ) ) ,
9596 commands . registerCommand ( 'jsonnet.evalFileYaml' , evalFileFunc ( true ) ) ,
@@ -100,49 +101,122 @@ export async function activate(context: ExtensionContext): Promise<void> {
100101
101102function evalFileFunc ( yaml : boolean ) {
102103 return async ( ) => {
103- const editor = window . activeTextEditor ;
104+ const currentFilePath = evalFilePath ( window . activeTextEditor ) ;
104105 const params : ExecuteCommandParams = {
105106 command : `jsonnet.evalFile` ,
106- arguments : [ evalFilePath ( editor ) ] ,
107+ arguments : [ currentFilePath ] ,
107108 } ;
108- evalAndDisplay ( params , yaml ) ;
109+ const tempFile = createTmpFile ( yaml ) ;
110+ const uri = Uri . file ( tempFile ) ;
111+
112+ fs . writeFileSync ( tempFile , '"Evaluating..."' ) ;
113+
114+ if ( workspace . getConfiguration ( 'jsonnet' ) . get ( 'languageServer.continuousEval' ) === false ) {
115+ evalAndDisplay ( params , yaml , tempFile ) ;
116+ }
117+ else {
118+
119+ // Initial eval
120+ evalOnDisplay ( params , yaml , tempFile ) ;
121+
122+ const watcher = workspace . createFileSystemWatcher ( currentFilePath ) ;
123+
124+ window . showTextDocument ( uri , {
125+ preview : true ,
126+ viewColumn : ViewColumn . Beside ,
127+ preserveFocus : true ,
128+ } ) ;
129+ watcher . onDidChange ( ( e ) => {
130+ evalOnDisplay ( params , yaml , tempFile ) ;
131+ }
132+ ) ;
133+ }
109134 } ;
110135}
111136
137+ function createTmpFile ( yaml ) : string {
138+ const tempDir = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , 'jsonnet-eval' ) ) ;
139+ const fileEnding = yaml ? 'yaml' : 'json' ;
140+ const tempFile = path . join ( tempDir , `result.${ fileEnding } ` ) ;
141+ return tempFile ;
142+ }
143+
112144function evalExpressionFunc ( yaml : boolean ) {
113145 return async ( ) => {
114- const editor = window . activeTextEditor ;
115146 window . showInputBox ( { prompt : 'Expression to evaluate' } ) . then ( async ( expr ) => {
116147 if ( expr ) {
148+ const currentFilePath = evalFilePath ( window . activeTextEditor ) ;
117149 const params : ExecuteCommandParams = {
118150 command : `jsonnet.evalExpression` ,
119- arguments : [ evalFilePath ( editor ) , expr ] ,
151+ arguments : [ currentFilePath , expr ] ,
120152 } ;
121- evalAndDisplay ( params , yaml ) ;
153+ const tempFile = createTmpFile ( yaml ) ;
154+ const uri = Uri . file ( tempFile ) ;
155+
156+ fs . writeFileSync ( tempFile , '"Evaluating..."' ) ;
157+
158+ if ( workspace . getConfiguration ( 'jsonnet' ) . get ( 'languageServer.continuousEval' ) === false ) {
159+ evalAndDisplay ( params , yaml , tempFile ) ;
160+ }
161+ else {
162+ // Initial eval
163+ evalOnDisplay ( params , yaml , tempFile ) ;
164+
165+ const watcher = workspace . createFileSystemWatcher ( currentFilePath ) ;
166+
167+ window . showTextDocument ( uri , {
168+ preview : true ,
169+ viewColumn : ViewColumn . Beside ,
170+ preserveFocus : true ,
171+ } ) ;
172+ watcher . onDidChange ( ( e ) => {
173+ evalOnDisplay ( params , yaml , tempFile ) ;
174+ }
175+ ) ;
176+ }
122177 } else {
123178 window . showErrorMessage ( 'No expression provided' ) ;
124179 }
125180 } ) ;
126181 } ;
127182}
128183
129- function evalAndDisplay ( params : ExecuteCommandParams , yaml : boolean ) : void {
184+ function evalOnDisplay ( params : ExecuteCommandParams , yaml : boolean , tempFile : string ) : void {
185+ channel . appendLine ( `Sending eval request: ${ JSON . stringify ( params ) } ` ) ;
186+ client
187+ . sendRequest ( ExecuteCommandRequest . type , params )
188+ . then ( ( result ) => {
189+ let uri = Uri . file ( tempFile ) ;
190+ fs . writeFileSync ( tempFile , result ) ;
191+
192+ if ( yaml ) {
193+ const file = fs . readFileSync ( tempFile , 'utf8' ) ;
194+ const parsed = JSON . parse ( file ) ;
195+ const yamlString = stringifyYaml ( parsed ) ;
196+ uri = Uri . file ( tempFile ) ;
197+ fs . writeFileSync ( tempFile , yamlString ) ;
198+ }
199+ } )
200+ . catch ( ( err ) => {
201+ window . showErrorMessage ( err . message ) ;
202+ fs . writeFileSync ( tempFile , err . message ) ;
203+ } ) ;
204+ }
205+
206+ function evalAndDisplay ( params : ExecuteCommandParams , yaml : boolean , tempFile : string ) : void {
130207 channel . appendLine ( `Sending eval request: ${ JSON . stringify ( params ) } ` ) ;
131208 client
132209 . sendRequest ( ExecuteCommandRequest . type , params )
133210 . then ( ( result ) => {
134- const tempDir = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , 'jsonnet-eval' ) ) ;
135- const tempFile = path . join ( tempDir , 'result.json' ) ;
136211 let uri = Uri . file ( tempFile ) ;
137212 fs . writeFileSync ( tempFile , result ) ;
138213
139214 if ( yaml ) {
140215 const file = fs . readFileSync ( tempFile , 'utf8' ) ;
141216 const parsed = JSON . parse ( file ) ;
142217 const yamlString = stringifyYaml ( parsed ) ;
143- const tempYamlFile = path . join ( tempDir , 'result.yaml' ) ;
144- uri = Uri . file ( tempYamlFile ) ;
145- fs . writeFileSync ( tempYamlFile , yamlString ) ;
218+ uri = Uri . file ( tempFile ) ;
219+ fs . writeFileSync ( tempFile , yamlString ) ;
146220 }
147221 window . showTextDocument ( uri , {
148222 preview : true ,
@@ -151,6 +225,12 @@ function evalAndDisplay(params: ExecuteCommandParams, yaml: boolean): void {
151225 } )
152226 . catch ( ( err ) => {
153227 window . showErrorMessage ( err . message ) ;
228+ fs . writeFileSync ( tempFile , err . message ) ;
229+ const uri = Uri . file ( tempFile ) ;
230+ window . showTextDocument ( uri , {
231+ preview : true ,
232+ viewColumn : ViewColumn . Beside ,
233+ } ) ;
154234 } ) ;
155235}
156236
0 commit comments