11import * as vscode from "vscode" ;
22import { AtelierAPI } from "../api" ;
33import { config , FILESYSTEM_SCHEMA } from "../extension" ;
4- import { outputChannel } from "../utils" ;
4+ import { outputChannel , outputConsole } from "../utils" ;
55import { DocumentContentProvider } from "../providers/DocumentContentProvider" ;
66import { ClassNode } from "../explorer/models/classesNode" ;
77import { PackageNode } from "../explorer/models/packageNode" ;
88import { RoutineNode } from "../explorer/models/routineNode" ;
99import { NodeBase } from "../explorer/models/nodeBase" ;
10+ import { importAndCompile } from "./compile" ;
11+
12+ export let documentBeingProcessed : vscode . TextDocument = null ;
1013
1114interface StudioAction extends vscode . QuickPickItem {
1215 name : string ;
@@ -18,18 +21,20 @@ class StudioActions {
1821 private api : AtelierAPI ;
1922 private name : string ;
2023
21- public constructor ( uriOrNode : vscode . Uri | PackageNode | ClassNode | RoutineNode ) {
24+ public constructor ( uriOrNode ? : vscode . Uri | PackageNode | ClassNode | RoutineNode ) {
2225 if ( uriOrNode instanceof vscode . Uri ) {
2326 const uri : vscode . Uri = uriOrNode ;
2427 this . uri = uri ;
2528 this . name = this . uri . path . slice ( 1 ) . replace ( / \/ / g, "." ) ;
2629 this . api = new AtelierAPI ( uri . authority ) ;
27- } else {
30+ } else if ( uriOrNode ) {
2831 const node : NodeBase = uriOrNode ;
2932 this . api = new AtelierAPI ( ) ;
3033 this . name = ( node instanceof PackageNode )
3134 ? node . fullName + ".PKG"
3235 : node . fullName ;
36+ } else {
37+ this . api = new AtelierAPI ( ) ;
3338 }
3439 }
3540
@@ -40,7 +45,7 @@ class StudioActions {
4045 outputChannel . appendLine ( errorText ) ;
4146 outputChannel . show ( ) ;
4247 }
43- outputChannel . appendLine ( JSON . stringify ( userAction ) ) ;
48+ // outputChannel.appendLine(JSON.stringify(userAction));
4449 switch ( serverAction ) {
4550 case 0 :
4651 /// do nothing
@@ -179,11 +184,10 @@ class StudioActions {
179184 const query = `select * from %Atelier_v1_Utils.Extension_${ func } ` ;
180185 let selectedText = "" ;
181186 const editor = vscode . window . activeTextEditor ;
182- if ( ! editor ) {
183- selectedText = "" ;
187+ if ( editor ) {
188+ const selection = editor . selection ;
189+ selectedText = editor . document . getText ( selection ) ;
184190 }
185- const selection = editor . selection ;
186- selectedText = editor . document . getText ( selection ) ;
187191
188192 const parameters = afterUserAction
189193 ? [ type . toString ( ) , action . id , this . name , answer , msg ]
@@ -198,12 +202,13 @@ class StudioActions {
198202 ( ) =>
199203 this . api
200204 . actionQuery ( query , parameters )
201- . then ( data => {
202- const actionInfo = data . result . content . pop ( ) ;
203- actionInfo . save = action . save ;
204- return actionInfo ;
205+ . then ( async data => {
206+ if ( action . save ) {
207+ await this . processSaveFlag ( action . save ) ;
208+ }
209+ outputConsole ( data . console ) ;
210+ return data . result . content . pop ( ) ;
205211 } )
206- . then ( this . processSaveFlag )
207212 . then ( this . processUserAction )
208213 . then ( answer => {
209214 if ( answer ) {
@@ -286,19 +291,38 @@ class StudioActions {
286291 } ) ;
287292 }
288293
289- private async processSaveFlag ( userAction ) {
290- if ( userAction . save ) {
291- const bitString = userAction . save . toString ( ) . padStart ( 3 , "0" ) ;
292- // Save the current document
293- if ( bitString . charAt ( 0 ) === "1" ) {
294- await vscode . window . activeTextEditor . document . save ( ) ;
294+ public changedNamespace ( ) {
295+ const changedNamespaceAction = {
296+ id : "5" ,
297+ label : "Changed Namespace"
298+ } ;
299+ this . userAction ( changedNamespaceAction , false , "" , "" , 1 ) ;
300+ }
301+
302+ private async processSaveFlag ( saveFlag : number ) {
303+ const bitString = saveFlag . toString ( ) . padStart ( 3 , "0" ) ;
304+ const saveAndCompile = async ( document : vscode . TextDocument ) => {
305+ if ( document . isDirty ) {
306+ // Prevent onDidSave from compiling the file
307+ // in order to await the importAndCompile function
308+ documentBeingProcessed = document ;
309+ await document . save ( ) ;
310+ await importAndCompile ( false , document ) ;
311+ documentBeingProcessed = null ;
295312 }
296- // Save all documents
297- if ( bitString . charAt ( 2 ) === "1" ) {
298- await vscode . workspace . saveAll ( ) ;
313+ } ;
314+
315+ // Save the current document
316+ if ( bitString . charAt ( 0 ) === "1" ) {
317+ await saveAndCompile ( vscode . window . activeTextEditor . document ) ;
318+ }
319+
320+ // Save all documents
321+ if ( bitString . charAt ( 2 ) === "1" ) {
322+ for ( const document of vscode . workspace . textDocuments ) {
323+ await saveAndCompile ( document )
299324 }
300325 }
301- return userAction ;
302326 }
303327}
304328
@@ -330,4 +354,9 @@ export async function contextMenu(node: PackageNode | ClassNode | RoutineNode):
330354 }
331355 const studioActions = new StudioActions ( nodeOrUri ) ;
332356 return studioActions && studioActions . getMenu ( "" , true ) ;
357+ }
358+
359+ export async function fireChangedNamespace ( ) {
360+ const studioActions = new StudioActions ( ) ;
361+ return studioActions && studioActions . changedNamespace ( ) ;
333362}
0 commit comments