Skip to content

Commit 59767ca

Browse files
committed
Clean up and Stability
* Refactor to use classes in most places * Handle case where updates to different files in quick succession could cancel pending (debounced) update for the first * More granular updating of config values * Don't use lodash * Render decorations from cache when switching active files
1 parent 4a8f0e9 commit 59767ca

8 files changed

Lines changed: 298 additions & 175 deletions

File tree

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ The color of the parentheses can be configured via:
3131

3232
## TODO
3333

34-
- [ ] Hide menu bar item when no js files are visible?
35-
- [ ] Log to output when parse fails
3634
- [ ] Correctly handle case where config has been set at the language level.
3735

3836
## Possible Future Features

package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,7 @@
102102
"dependencies": {
103103
"@babel/parser": "^7.11.4",
104104
"@babel/traverse": "^7.11.0",
105-
"@types/babel__traverse": "^7.0.13",
106-
"@types/lodash": "^4.14.159",
107-
"lodash": "^4.17.20"
105+
"@types/babel__traverse": "^7.0.13"
108106
},
109107
"prettier": {}
110108
}

src/commands.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ function getConfigurationType(
2929
return vscode.ConfigurationTarget.Global;
3030
}
3131

32-
export function activateCommands(subscriptions: { dispose(): any }[]) {
32+
export function activateCommands(): vscode.Disposable {
33+
const subscriptions = [];
3334
subscriptions.push(
3435
vscode.commands.registerCommand(SHOW_COMMAND, () => {
3536
const config = vscode.workspace.getConfiguration();
@@ -58,4 +59,6 @@ export function activateCommands(subscriptions: { dispose(): any }[]) {
5859
);
5960
})
6061
);
62+
63+
return vscode.Disposable.from(...subscriptions);
6164
}

src/extension.ts

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,41 @@
11
/* eslint-disable @typescript-eslint/naming-convention */
22
// The module 'vscode' contains the VS Code extensibility API
33
// Import the module and reference it with the alias vscode in your code below
4-
import * as vscode from "vscode";
5-
import { activateParens } from "./parens";
6-
import { activateMenuBarItem } from "./menuBarItem";
4+
import { Disposable, ExtensionContext, workspace } from "vscode";
5+
import Parens from "./parens";
6+
import MenuBarItem from "./menuBarItem";
77
import { activateCommands } from "./commands";
8-
import { ENABLED_CONFIG, DEBOUNCE_CONFIG, USE_FLOW_CONFIG } from "./constants";
8+
import { ENABLED_CONFIG } from "./constants";
99

1010
// this method is called when vs code is activated
11-
export function activate(context: vscode.ExtensionContext) {
12-
activateMenuBarItem(context.subscriptions);
13-
activateCommands(context.subscriptions);
11+
export function activate(context: ExtensionContext) {
12+
context.subscriptions.push(new MenuBarItem());
13+
context.subscriptions.push(activateCommands());
1414

15-
let parens: vscode.Disposable | null = null;
15+
let parens: Disposable | null = null;
1616
function updateParensEnabled() {
1717
if (parens !== null) {
1818
parens.dispose();
1919
}
20-
if (vscode.workspace.getConfiguration().get(ENABLED_CONFIG)) {
21-
parens = activateParens();
20+
if (workspace.getConfiguration().get(ENABLED_CONFIG)) {
21+
parens = new Parens();
2222
} else {
2323
parens = null;
2424
}
2525
}
2626

2727
context.subscriptions.push(
28-
new vscode.Disposable(() => {
28+
new Disposable(() => {
2929
if (parens !== null) {
3030
parens.dispose();
3131
}
3232
})
3333
);
3434

3535
updateParensEnabled();
36-
vscode.workspace.onDidChangeConfiguration(
36+
workspace.onDidChangeConfiguration(
3737
(event) => {
38-
if (
39-
event.affectsConfiguration(USE_FLOW_CONFIG) ||
40-
event.affectsConfiguration(ENABLED_CONFIG) ||
41-
event.affectsConfiguration(DEBOUNCE_CONFIG)
42-
) {
38+
if (event.affectsConfiguration(ENABLED_CONFIG)) {
4339
updateParensEnabled();
4440
}
4541
},

src/menuBarItem.ts

Lines changed: 52 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,65 @@
1-
import * as vscode from "vscode";
1+
import {
2+
Disposable,
3+
window,
4+
StatusBarAlignment,
5+
StatusBarItem,
6+
workspace,
7+
ConfigurationChangeEvent,
8+
} from "vscode";
29
import { ENABLED_CONFIG, TOGGLE_COMMAND, MENU_BAR_CONFIG } from "./constants";
310

4-
export function activateMenuBarItem(subscriptions: { dispose(): any }[]) {
5-
const statusBarItem = vscode.window.createStatusBarItem(
6-
vscode.StatusBarAlignment.Left,
7-
100
8-
);
9-
statusBarItem.command = TOGGLE_COMMAND;
11+
const PLAY = "\u25BA";
12+
const STOP = "\u25A0";
1013

11-
const PLAY = "\u25BA";
12-
const STOP = "\u25A0";
14+
export default class MenuBarItem implements Disposable {
15+
private readonly _disposables: Disposable[] = [];
16+
private readonly _statusBarItem: StatusBarItem;
17+
constructor() {
18+
this._disposables = [];
19+
this._statusBarItem = window.createStatusBarItem(
20+
StatusBarAlignment.Left,
21+
100 // What should this number be?
22+
);
23+
this._statusBarItem.command = TOGGLE_COMMAND;
1324

14-
function setText() {
15-
const config = vscode.workspace.getConfiguration();
25+
this._disposables.push(this._statusBarItem);
26+
27+
this._setText();
28+
this._setVisibility();
29+
30+
workspace.onDidChangeConfiguration(
31+
this._handleConfigChange,
32+
this,
33+
this._disposables
34+
);
35+
}
36+
37+
_handleConfigChange(event: ConfigurationChangeEvent) {
38+
if (event.affectsConfiguration(ENABLED_CONFIG)) {
39+
this._setText();
40+
}
41+
if (event.affectsConfiguration(MENU_BAR_CONFIG)) {
42+
this._setVisibility();
43+
}
44+
}
45+
46+
_setText() {
47+
const config = workspace.getConfiguration();
1648
const icon = config.get(ENABLED_CONFIG) ? STOP : PLAY;
1749
const action = config.get(ENABLED_CONFIG) ? "hide" : "show";
18-
statusBarItem.text = `(${icon})`;
19-
statusBarItem.tooltip = `Click to ${action} implicit parentheses.`;
50+
this._statusBarItem.text = `(${icon})`;
51+
this._statusBarItem.tooltip = `Click to ${action} implicit parentheses.`;
2052
}
2153

22-
function setVisibility() {
23-
if (vscode.workspace.getConfiguration().get(MENU_BAR_CONFIG)) {
24-
statusBarItem.show();
54+
_setVisibility() {
55+
if (workspace.getConfiguration().get(MENU_BAR_CONFIG)) {
56+
this._statusBarItem.show();
2557
} else {
26-
statusBarItem.hide();
58+
this._statusBarItem.hide();
2759
}
2860
}
2961

30-
setText();
31-
setVisibility();
32-
33-
vscode.workspace.onDidChangeConfiguration(
34-
(event) => {
35-
if (event.affectsConfiguration(ENABLED_CONFIG)) {
36-
setText();
37-
}
38-
if (event.affectsConfiguration(MENU_BAR_CONFIG)) {
39-
setVisibility();
40-
}
41-
},
42-
null,
43-
subscriptions
44-
);
45-
subscriptions.push(statusBarItem);
62+
dispose() {
63+
this._disposables.forEach((disposable) => disposable.dispose());
64+
}
4665
}

0 commit comments

Comments
 (0)