Skip to content

Commit adeaaa5

Browse files
author
Étienne Lévesque
authored
Add code lens for running test in terminal (#155)
* Add command for running test in terminal * Remove dead code, update mix command, update elixir-ls * Remove dead code * Remove command from package.json * Formatting * Update elixir-ls with master * Update elixir-ls * Add setting for enabling test code lenses * Update elixir-ls * Update elixir-ls to latest master * Remove trace option from mix task
1 parent 8ec1c6e commit adeaaa5

File tree

5 files changed

+67
-13
lines changed

5 files changed

+67
-13
lines changed

elixir-ls

Submodule elixir-ls updated 58 files

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,11 @@
144144
"type": "boolean",
145145
"description": "Show signature help after confirming autocomplete",
146146
"default": true
147+
},
148+
"elixirLS.enableTestLenses": {
149+
"type": "boolean",
150+
"description": "Show code lenses to run tests in terminal",
151+
"default": false
147152
}
148153
}
149154
},

src/commands/runTestFromCodeLens.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { window } from "vscode";
2+
3+
type RunArgs = {
4+
filePath: string,
5+
describe: string | null,
6+
testName?: string,
7+
module?: string
8+
}
9+
10+
export default function runFromCodeLens(args: RunArgs): void {
11+
const elixirLsTerminal =
12+
window.terminals.find(terminal => terminal.name == "ElixirLS") || window.createTerminal("ElixirLS");
13+
14+
elixirLsTerminal.show()
15+
elixirLsTerminal.sendText('clear')
16+
elixirLsTerminal.sendText(buildTestCommand(args));
17+
}
18+
19+
function buildTestCommand(args: RunArgs): string {
20+
const testFilter = buildTestInclude(args.describe, args.testName, args.module)
21+
22+
return `mix test --exclude test --include "${testFilter}" ${args.filePath}`
23+
}
24+
25+
function buildTestInclude(describe: string | null, testName?: string, module?: string) {
26+
if (module) {
27+
return `module:${module}`
28+
}
29+
30+
if (!testName) {
31+
return `describe:${describe}`
32+
}
33+
34+
if (describe) {
35+
return `test:test ${describe} ${testName}`
36+
}
37+
38+
return `test:test ${testName}`
39+
}

src/constants/commands.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default {
2+
RUN_TEST_FROM_CODELENS: 'elixir.lens.test.run'
3+
}

src/extension.ts

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import {
1717
ServerOptions,
1818
} from "vscode-languageclient";
1919
import * as os from "os";
20+
import Commands from "./constants/commands";
21+
import runFromCodeLens from "./commands/runTestFromCodeLens";
2022

2123
export let defaultClient: LanguageClient;
2224
const clients: Map<string, LanguageClient> = new Map();
@@ -77,16 +79,16 @@ function sortedWorkspaceFolders(): string[] {
7779
if (_sortedWorkspaceFolders === void 0) {
7880
_sortedWorkspaceFolders = workspace.workspaceFolders
7981
? workspace.workspaceFolders
80-
.map((folder) => {
81-
let result = folder.uri.toString();
82-
if (result.charAt(result.length - 1) !== "/") {
83-
result = result + "/";
84-
}
85-
return result;
86-
})
87-
.sort((a, b) => {
88-
return a.length - b.length;
89-
})
82+
.map((folder) => {
83+
let result = folder.uri.toString();
84+
if (result.charAt(result.length - 1) !== "/") {
85+
result = result + "/";
86+
}
87+
return result;
88+
})
89+
.sort((a, b) => {
90+
return a.length - b.length;
91+
})
9092
: [];
9193
}
9294
return _sortedWorkspaceFolders;
@@ -200,23 +202,28 @@ function configureTerminalLinkProvider(context: ExtensionContext) {
200202
if (!selection) {
201203
return;
202204
}
203-
205+
204206
openUri(selection.uri, line);
205207
});
206208
}
207209
});
208210
}
209211
});
210-
212+
211213
context.subscriptions.push(disposable);
212214
}
213215

216+
function configureRunTestFromCodeLens() {
217+
vscode.commands.registerCommand(Commands.RUN_TEST_FROM_CODELENS, runFromCodeLens);
218+
}
219+
214220
export function activate(context: ExtensionContext): void {
215221
testElixir();
216222
detectConflictingExtension("mjmcloug.vscode-elixir");
217223
// https://github.com/elixir-lsp/vscode-elixir-ls/issues/34
218224
detectConflictingExtension("sammkj.vscode-elixir-formatter");
219225

226+
configureRunTestFromCodeLens()
220227
configureCopyDebugInfo(context);
221228
configureDebugger(context);
222229
configureTerminalLinkProvider(context);

0 commit comments

Comments
 (0)