Skip to content

Commit 669e7f8

Browse files
committed
Add clear command; make command tests more useful
1 parent 0acd3a9 commit 669e7f8

File tree

4 files changed

+26
-28
lines changed

4 files changed

+26
-28
lines changed

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@
4141
"command": "syntaxTree.restart",
4242
"title": "Syntax Tree: Restart"
4343
},
44+
{
45+
"command": "syntaxTree.clearOutputChannel",
46+
"title": "Syntax Tree: Clear Output Channel"
47+
},
4448
{
4549
"command": "syntaxTree.showOutputChannel",
4650
"title": "Syntax Tree: Show Output Channel"

src/extension.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ export async function activate(context: ExtensionContext) {
3939
commands.registerCommand("syntaxTree.stop", stopLanguageServer),
4040
commands.registerCommand("syntaxTree.restart", restartLanguageServer),
4141
commands.registerCommand("syntaxTree.visualize", () => visualizer?.visualize()),
42+
commands.registerCommand("syntaxTree.clearOutputChannel", () => outputChannel.clear()),
4243
commands.registerCommand("syntaxTree.showOutputChannel", () => outputChannel.show()),
4344
workspace.onDidChangeConfiguration(event => {
4445
if (event.affectsConfiguration("syntaxTree")) {
@@ -54,6 +55,10 @@ export async function activate(context: ExtensionContext) {
5455
// This function is called when the extension is activated or when the
5556
// language server is restarted.
5657
async function startLanguageServer() {
58+
if (languageClient) {
59+
return; // preserve idempotency
60+
}
61+
5762
// The top-level configuration group is syntaxTree. All of the configuration
5863
// for the extension is under that group.
5964
const config = workspace.getConfiguration("syntaxTree");
@@ -131,6 +136,7 @@ export async function activate(context: ExtensionContext) {
131136
if (languageClient) {
132137
outputChannel.appendLine("Stopping language server...");
133138
await languageClient.stop();
139+
languageClient = null;
134140
}
135141
}
136142

src/test/suite/automation.ts

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,15 @@ import { Range, Uri, commands, window, workspace } from 'vscode';
66

77
import { WORKSPACE_DIR } from './setup';
88

9-
export async function closeAll() {
9+
export async function reset() {
1010
await commands.executeCommand('workbench.action.closeAllEditors');
11+
await clearOutputChannel();
1112

13+
// Ensure output panel is closed
1214
await commands.executeCommand('workbench.panel.output.focus');
1315
await commands.executeCommand('workbench.action.output.toggleOutput');
14-
await showOutputChannel();
1516

16-
// @see https://stackoverflow.com/questions/44733028/how-to-close-textdocument-in-vs-code
17-
const { visibleTextEditors: editors } = window;
18-
for (let i = 0; i < editors.length; i++) {
19-
const { document } = editors[i];
20-
await window.showTextDocument(document, { preview: true, preserveFocus: true });
21-
await editors[i].edit(cb => {
22-
cb.delete(new Range(0, 0, document.lineCount, 0));
23-
});
24-
await commands.executeCommand('workbench.action.closeActiveEditor');
25-
}
26-
await commands.executeCommand('workbench.action.output.toggleOutput');
27-
28-
// Set the focus back to something neutral
17+
// Ensure something neutral is focused
2918
await commands.executeCommand('workbench.explorer.fileView.focus');
3019
}
3120

@@ -46,6 +35,10 @@ export function restart() {
4635
return commands.executeCommand('syntaxTree.restart');
4736
}
4837

38+
export function clearOutputChannel() {
39+
return commands.executeCommand('syntaxTree.showOutputChannel');
40+
}
41+
4942
export function showOutputChannel(timeout = 15000) {
5043
// TODO: make use of await commands.executeCommand('workbench.panel.output.focus'); ??
5144
return new Promise<string>((c, e) => {

src/test/suite/index.test.ts

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,35 +18,30 @@ end
1818

1919
suite('Syntax Tree', () => {
2020
suite('commands', () => {
21-
beforeEach(async () => {
22-
await auto.stop();
23-
await auto.closeAll();
24-
});
25-
26-
test('showOutputChannel', async () => {
27-
const text = await auto.showOutputChannel();
28-
assert.match(text, /^Starting language server/m);
29-
})
21+
beforeEach(auto.reset);
3022

3123
test('start', async () => {
3224
await auto.start();
3325
const text = await auto.showOutputChannel();
34-
// HACK: with no way to clear the channel, this is a flakey assertion
35-
assert.match(text, /^Starting language server/m);
26+
assert.match(text, /^Starting language server/);
3627
});
3728

3829
test('stop', async () => {
3930
await auto.start();
4031
await auto.stop();
4132
const text = await auto.showOutputChannel();
42-
// HACK: with no way to clear the channel, this is a flakey assertion
4333
assert.match(text, /^Stopping language server/m);
4434
})
35+
36+
test('restart', async () => {
37+
await auto.restart();
38+
const text = await auto.showOutputChannel();
39+
assert.match(text, /^Restarting language server/m);
40+
})
4541
});
4642

4743
// TODO: figure out re-entrancy issue in CI (prolly two LanguageClient instances at once?)
4844
test.skip('Format Document', async () => {
49-
await auto.closeAll();
5045
const editor = await auto.createEditor(UNFORMATTED);
5146
await auto.formatDocument();
5247
assert.equal(editor.document.getText(), FORMATTED);

0 commit comments

Comments
 (0)