Skip to content

Commit e51a79c

Browse files
committed
Setting to show/hide menubar item and fix issues with toggling settings
1 parent 61bd19e commit e51a79c

File tree

7 files changed

+65
-40
lines changed

7 files changed

+65
-40
lines changed

README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@ This extension contributes the following settings:
1616

1717
## TODO
1818

19-
- [ ] Ensure we can parse Flow and Typescript
20-
- [ ] Attempt to reparse on parser config change
21-
- [ ] Make config names human readable
2219
- [ ] Figure out how to configure colors
2320
- [ ] Set default color for light mode
2421
- [ ] Ensure plugin has name
@@ -28,3 +25,4 @@ This extension contributes the following settings:
2825
- Enable/disable menu bar item in config
2926
- Allow user to configure which parens are shown
3027
- Provide automated fixes for adding parens, or even extracting expressions to variables.
28+
- Use the menu bar item to indicate if parsing has failed.

package.json

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"activationEvents": [
1313
"onLanguage:typescript",
1414
"onLanguage:javascript",
15+
"onLanguage:flow",
1516
"onLanguage:javascriptreact",
1617
"onLanguage:typescriptreact"
1718
],
@@ -34,12 +35,17 @@
3435
"configuration": {
3536
"title": "Implicit Parentheses",
3637
"properties": {
37-
"implicitparens.enabled": {
38+
"Implicit Parentheses.enable": {
3839
"type": "boolean",
3940
"default": true,
4041
"description": "Show implicit parentheses"
4142
},
42-
"implicitparens.parser": {
43+
"Implicit Parentheses.Show in Menu Bar": {
44+
"type": "boolean",
45+
"default": true,
46+
"description": "Show a button in the menu bar to show/hide implicit parentheses"
47+
},
48+
"Implicit Parentheses.parser": {
4349
"type": "string",
4450
"default": "TypeScript",
4551
"description": "Parser to use",
@@ -53,6 +59,11 @@
5359
"Babel Parser with TypeScript enabled",
5460
"Babel Parser with Flow enabled"
5561
]
62+
},
63+
"Implicit Parentheses.Debounce Timeout": {
64+
"type": "number",
65+
"default": 500,
66+
"description": "Number of milliseconds that the plugin will wait after a file changes before it parses the file."
5667
}
5768
}
5869
},

src/commands.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ export function activateCommands(subscriptions: { dispose(): any }[]) {
2424
config.update(
2525
ENABLED_CONFIG,
2626
!config.get(ENABLED_CONFIG),
27-
// TODO: Should this affect the workspace instead?
28-
true
27+
vscode.ConfigurationTarget.Workspace
2928
);
3029
})
3130
);

src/constants.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ export const TOGGLE_COMMAND = "implicitparens.toggleParens";
22
export const SHOW_COMMAND = "implicitparens.showParens";
33
export const HIDE_COMMAND = "implicitparens.hideParens";
44

5-
export const ENABLED_CONFIG = "implicitparens.enabled";
6-
export const PARSER_CONFIG = "implicitparens.parser";
7-
export const DEBOUNCE_TIME = 500;
5+
export const ENABLED_CONFIG = "Implicit Parentheses.enable";
6+
export const PARSER_CONFIG = "Implicit Parentheses.parser";
7+
export const DEBOUNCE_CONFIG = "Implicit Parentheses.Debounce Timeout";
8+
export const MENU_BAR_CONFIG = "Implicit Parentheses.Show in Menu Bar";

src/extension.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import * as vscode from "vscode";
55
import { activateParens } from "./parens";
66
import { activateMenuBarItem } from "./menuBarItem";
77
import { activateCommands } from "./commands";
8-
import { ENABLED_CONFIG } from "./constants";
8+
import { ENABLED_CONFIG, DEBOUNCE_CONFIG, PARSER_CONFIG } from "./constants";
99

1010
// this method is called when vs code is activated
1111
export function activate(context: vscode.ExtensionContext) {
@@ -14,17 +14,13 @@ export function activate(context: vscode.ExtensionContext) {
1414

1515
let parens: vscode.Disposable | null = null;
1616
function updateParensEnabled() {
17+
if (parens !== null) {
18+
parens.dispose();
19+
}
1720
if (vscode.workspace.getConfiguration().get(ENABLED_CONFIG)) {
18-
if (parens === null) {
19-
parens = activateParens();
20-
} else {
21-
}
21+
parens = activateParens();
2222
} else {
23-
if (parens !== null) {
24-
parens.dispose();
25-
parens = null;
26-
} else {
27-
}
23+
parens = null;
2824
}
2925
}
3026

@@ -39,7 +35,11 @@ export function activate(context: vscode.ExtensionContext) {
3935
updateParensEnabled();
4036
vscode.workspace.onDidChangeConfiguration(
4137
(event) => {
42-
if (event.affectsConfiguration(ENABLED_CONFIG)) {
38+
if (
39+
event.affectsConfiguration(PARSER_CONFIG) ||
40+
event.affectsConfiguration(ENABLED_CONFIG) ||
41+
event.affectsConfiguration(DEBOUNCE_CONFIG)
42+
) {
4343
updateParensEnabled();
4444
}
4545
},

src/menuBarItem.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,43 @@
11
import * as vscode from "vscode";
2-
import { ENABLED_CONFIG, TOGGLE_COMMAND } from "./constants";
2+
import { ENABLED_CONFIG, TOGGLE_COMMAND, MENU_BAR_CONFIG } from "./constants";
33

44
export function activateMenuBarItem(subscriptions: { dispose(): any }[]) {
55
const statusBarItem = vscode.window.createStatusBarItem(
66
vscode.StatusBarAlignment.Left,
77
100
88
);
9+
statusBarItem.command = TOGGLE_COMMAND;
910

1011
const PLAY = "\u25BA";
1112
const STOP = "\u25A0";
1213

1314
function setText() {
1415
const config = vscode.workspace.getConfiguration();
1516
const icon = config.get(ENABLED_CONFIG) ? STOP : PLAY;
17+
const action = config.get(ENABLED_CONFIG) ? "hide" : "show";
1618
statusBarItem.text = `(${icon})`;
19+
statusBarItem.tooltip = `Click to ${action} implicit parentheses.`;
20+
}
21+
22+
function setVisibility() {
23+
if (vscode.workspace.getConfiguration().get(MENU_BAR_CONFIG)) {
24+
statusBarItem.show();
25+
} else {
26+
statusBarItem.hide();
27+
}
1728
}
1829

1930
setText();
20-
statusBarItem.show();
21-
statusBarItem.command = TOGGLE_COMMAND;
31+
setVisibility();
2232

2333
vscode.workspace.onDidChangeConfiguration(
2434
(event) => {
2535
if (event.affectsConfiguration(ENABLED_CONFIG)) {
2636
setText();
2737
}
38+
if (event.affectsConfiguration(MENU_BAR_CONFIG)) {
39+
setVisibility();
40+
}
2841
},
2942
null,
3043
subscriptions

src/parens.ts

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as vscode from "vscode";
22
import { findParens } from "./parse";
33
import { debounce, repeat } from "./utils";
4-
import { DEBOUNCE_TIME, PARSER_CONFIG } from "./constants";
4+
import { DEBOUNCE_CONFIG, PARSER_CONFIG } from "./constants";
55
import { ParserPlugin } from "@babel/parser";
66

77
export function activateParens() {
@@ -21,19 +21,24 @@ export function activateParens() {
2121

2222
subscriptions.push(decorationType);
2323

24-
updateDecorations(decorationType);
24+
for (const editor of vscode.window.visibleTextEditors) {
25+
updateDecorations(editor, decorationType);
26+
}
2527

2628
const triggerUpdateDecorations = debounce(
27-
() => updateDecorations(decorationType),
28-
DEBOUNCE_TIME
29+
(editor: vscode.TextEditor) => updateDecorations(editor, decorationType),
30+
vscode.workspace.getConfiguration().get(DEBOUNCE_CONFIG)
2931
);
3032
subscriptions.push(
3133
new vscode.Disposable(() => triggerUpdateDecorations.cancel())
3234
);
3335

3436
vscode.window.onDidChangeActiveTextEditor(
3537
() => {
36-
triggerUpdateDecorations();
38+
const activeEditor = vscode.window.activeTextEditor;
39+
if (activeEditor !== undefined) {
40+
triggerUpdateDecorations(activeEditor);
41+
}
3742
},
3843
null,
3944
subscriptions
@@ -46,7 +51,7 @@ export function activateParens() {
4651
activeEditor !== undefined &&
4752
event.document === activeEditor.document
4853
) {
49-
triggerUpdateDecorations();
54+
triggerUpdateDecorations(activeEditor);
5055
}
5156
},
5257
null,
@@ -70,13 +75,11 @@ function getPlugins(): ParserPlugin[] {
7075
}
7176
}
7277

73-
function updateDecorations(decorationType: vscode.TextEditorDecorationType) {
74-
const activeEditor = vscode.window.activeTextEditor;
75-
if (!activeEditor) {
76-
return;
77-
}
78-
79-
const parens = findParens(activeEditor.document.getText(), getPlugins());
78+
function updateDecorations(
79+
editor: vscode.TextEditor,
80+
decorationType: vscode.TextEditorDecorationType
81+
) {
82+
const parens = findParens(editor.document.getText(), getPlugins());
8083
if (parens === null) {
8184
return;
8285
}
@@ -98,13 +101,13 @@ function updateDecorations(decorationType: vscode.TextEditorDecorationType) {
98101

99102
const paren = { before: "(", after: ")" };
100103

101-
activeEditor.setDecorations(
104+
editor.setDecorations(
102105
decorationType,
103106
decorations.map(({ count, pos, beforeAfter }) => {
104107
return {
105108
range: new vscode.Range(
106-
activeEditor.document.positionAt(pos),
107-
activeEditor.document.positionAt(pos)
109+
editor.document.positionAt(pos),
110+
editor.document.positionAt(pos)
108111
),
109112
renderOptions: {
110113
[beforeAfter]: { contentText: repeat(paren[beforeAfter], count) },

0 commit comments

Comments
 (0)