Browse Source

handle situation where a vanilla project adds a new TS file before the TS extension has been initialized

Shaddock Heath 9 years ago
parent
commit
7b26a2b93c

+ 34 - 16
Script/AtomicEditor/hostExtensions/languageExtensions/TypscriptLanguageExtension.ts

@@ -62,13 +62,10 @@ export default class TypescriptLanguageExtension implements Editor.HostExtension
      * @return {boolean}
      * @return {boolean}
      */
      */
     private isValidFiletype(path: string): boolean {
     private isValidFiletype(path: string): boolean {
-        if (this.isTypescriptProject) {
-            const ext = Atomic.getExtension(path);
-            if (ext == ".ts") {
-                return true;
-            }
+        const ext = Atomic.getExtension(path);
+        if (ext == ".ts") {
+            return true;
         }
         }
-        return false;
     }
     }
 
 
     /**
     /**
@@ -150,6 +147,23 @@ export default class TypescriptLanguageExtension implements Editor.HostExtension
         }
         }
     }
     }
 
 
+    /**
+     * Configures the project to be a Typescript Project
+     * @return {[type]}
+     */
+    private configureTypescriptProject() {
+        if (this.isTypescriptProject) {
+            this.setTsConfigOnWebView(this.buildTsConfig());
+            const isCompileOnSave = this.serviceRegistry.projectServices.getUserPreference(this.name, "CompileOnSave", false);
+
+            // Build the menu - First build up an empty menu then manually add the items so we can have reference to them
+            const menu = this.serviceRegistry.uiServices.createPluginMenuItemSource("TypeScript", {});
+            this.compileOnSaveMenuItem = new Atomic.UIMenuItem(`Compile on Save: ${isCompileOnSave ? "On" : "Off"}`, `${this.name}.compileonsave`);
+            menu.addItem(this.compileOnSaveMenuItem);
+            menu.addItem(new Atomic.UIMenuItem("Compile Project", `${this.name}.compileproject`));
+        }
+    }
+
     /**
     /**
      * Inject this language service into the registry
      * Inject this language service into the registry
      * @return {[type]}             True if successful
      * @return {[type]}             True if successful
@@ -162,6 +176,18 @@ export default class TypescriptLanguageExtension implements Editor.HostExtension
         this.serviceRegistry = serviceLocator;
         this.serviceRegistry = serviceLocator;
     }
     }
 
 
+    /**
+     * Handle when a new file is loaded and we have not yet configured the editor for TS.
+     * This could be when someone adds a TS file to a vanilla project
+     * @param  {Editor.EditorEvents.EditResourceEvent} ev
+     */
+    edit(ev: Editor.EditorEvents.EditResourceEvent) {
+        if (this.isValidFiletype(ev.path) && !this.isTypescriptProject) {
+            this.isTypescriptProject = true;
+            this.configureTypescriptProject();
+        }
+    }
+
     /**
     /**
      * Handle the delete.  This should delete the corresponding javascript file
      * Handle the delete.  This should delete the corresponding javascript file
      * @param  {Editor.EditorEvents.DeleteResourceEvent} ev
      * @param  {Editor.EditorEvents.DeleteResourceEvent} ev
@@ -247,18 +273,10 @@ export default class TypescriptLanguageExtension implements Editor.HostExtension
             this.isTypescriptProject = true;
             this.isTypescriptProject = true;
         });
         });
 
 
-        if (this.isTypescriptProject) {
-            this.setTsConfigOnWebView(this.buildTsConfig());
-            const isCompileOnSave = this.serviceRegistry.projectServices.getUserPreference(this.name, "CompileOnSave", false);
-
-            // Build the menu - First build up an empty menu then manually add the items so we can have reference to them
-            const menu = this.serviceRegistry.uiServices.createPluginMenuItemSource("TypeScript", {});
-            this.compileOnSaveMenuItem = new Atomic.UIMenuItem(`Compile on Save: ${isCompileOnSave ? "On" : "Off"}`, `${this.name}.compileonsave`);
-            menu.addItem(this.compileOnSaveMenuItem);
-            menu.addItem(new Atomic.UIMenuItem("Compile Project", `${this.name}.compileproject`));
-        }
+        this.configureTypescriptProject();
     }
     }
 
 
+
     /**
     /**
      * Called when the project is unloaded
      * Called when the project is unloaded
      */
      */