Browse Source

Ensure that cursor stays in the same location and de-select the auto-selection when executing Format Code on an entire file.

Shaddock Heath 9 years ago
parent
commit
efa6fa5298

+ 13 - 1
Script/AtomicWebViewEditor/clientExtensions/languageExtensions/typescript/TypescriptLanguageExtension.ts

@@ -294,10 +294,22 @@ export default class TypescriptLanguageExtension implements Editor.ClientExtensi
         if (this.active) {
             const action = this.editor.getAction("editor.action.format");
             if (action && action.isSupported()) {
+                let wasEmpty = false;
+                let cursorPosition;
+
                 if (this.editor.getSelection().isEmpty()) {
+                    wasEmpty = true;
+                    cursorPosition = this.editor.getPosition();
+
                     this.editor.setSelection(this.editor.getModel().getFullModelRange());
                 }
-                action.run();
+
+                action.run().then(() => {
+                    // Make sure we put the cursor back to where it was
+                    if (wasEmpty && cursorPosition) {
+                        this.editor.setPosition(cursorPosition);
+                    }
+                });
             }
         }
     }