Skip to content

Commit deee398

Browse files
authored
handle case where onDidChangeActiveTextEditor fires an undefined event (#13)
- also did the same for onDidChangeTextEditorSelection just to be safe - random formatting changes - updated change log
1 parent 00332fc commit deee398

2 files changed

Lines changed: 32 additions & 19 deletions

File tree

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ only tested on Ubuntu and OSX, so may not other Linuxs, and/or Windows
2727

2828
## Release Notes
2929

30+
### 0.1.2
31+
fixed issue [#12](https://github.com/nexus-uw/vscode-gtm/issues/12) and [#7](https://github.com/nexus-uw/vscode-gtm/issues/7)
32+
3033
### 0.1.1 (arbitrary minor version bump)
3134
fixed https://github.com/nexus-uw/vscode-gtm/issues/5
3235
updated README to have proper version

src/extension.ts

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ export function activate(context: vscode.ExtensionContext) {
2525
// check if gtm is installed + available
2626
run_cmd('gtm', ['verify', '>= 1.2.1'])
2727
.then((res: Result) => {
28-
if(res.output != 'true'){
29-
vscode.window.showWarningMessage('Installed gtm version is below v1.2.1. Please update your gtm installation.');
30-
}
28+
if (res.output != 'true') {
29+
vscode.window.showWarningMessage('Installed gtm version is below v1.2.1. Please update your gtm installation.');
30+
}
3131
}, (res: Result) => {
3232
if (res.code < 0) {
3333
vscode.window.showErrorMessage('gtm is not available on your $PATH. please install it first');
@@ -40,7 +40,7 @@ export function activate(context: vscode.ExtensionContext) {
4040

4141
let gtmStatusBar = new GTMStatusBar()
4242

43-
function handleUpdateEvent(fileName: string){
43+
function handleUpdateEvent(fileName: string) {
4444
const now = new Date();
4545
// if a new file is being saved OR it have been at least MIN_UPDATE_FREQUENCE_MS, record it
4646
if (fileName !== lastSavedFileName || (now.getTime() - lastUpdated.getTime()) >= MIN_UPDATE_FREQUENCE_MS) {
@@ -55,32 +55,42 @@ export function activate(context: vscode.ExtensionContext) {
5555
vscode.workspace.onDidSaveTextDocument((e: vscode.TextDocument) => handleUpdateEvent(e.fileName), this, subscriptions);
5656

5757
// report to gtm everytime the user's selection of text changes
58-
vscode.window.onDidChangeTextEditorSelection((e:vscode.TextEditorSelectionChangeEvent) => handleUpdateEvent(e.textEditor.document.fileName), this, subscriptions);
58+
vscode.window.onDidChangeTextEditorSelection((e: vscode.TextEditorSelectionChangeEvent) => {
59+
if (e && e.textEditor && e.textEditor.document) {
60+
handleUpdateEvent(e.textEditor.document.fileName);
61+
}
62+
}, this, subscriptions);
5963

6064
// report to gtm everytime the user switches textEditors
61-
vscode.window.onDidChangeActiveTextEditor((e:vscode.TextEditor) => handleUpdateEvent(e.document.fileName), this, subscriptions);
65+
vscode.window.onDidChangeActiveTextEditor((e: vscode.TextEditor) => {
66+
// Note that the event also fires when the active editor changes to undefined.
67+
if (e && e.document) {
68+
handleUpdateEvent(e.document.fileName);
69+
}
70+
71+
}, this, subscriptions);
6272
}
6373

6474
class GTMStatusBar {
6575
private statusBarItem: vscode.StatusBarItem;
6676

6777
public updateStatus(statusText: string) {
6878

69-
// Create as needed
70-
if (!this.statusBarItem) {
71-
this.statusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left);
72-
}
79+
// Create as needed
80+
if (!this.statusBarItem) {
81+
this.statusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left);
82+
}
7383

74-
// Get the current text editor
75-
let editor = vscode.window.activeTextEditor;
76-
if (!editor) {
77-
this.statusBarItem.hide();
78-
return;
79-
}
84+
// Get the current text editor
85+
let editor = vscode.window.activeTextEditor;
86+
if (!editor) {
87+
this.statusBarItem.hide();
88+
return;
89+
}
8090

81-
// Update the status bar
82-
this.statusBarItem.text = statusText;
83-
this.statusBarItem.show();
91+
// Update the status bar
92+
this.statusBarItem.text = statusText;
93+
this.statusBarItem.show();
8494
}
8595
}
8696

0 commit comments

Comments
 (0)