diff --git a/elixir-ls b/elixir-ls index 3acc1fd..5b2d236 160000 --- a/elixir-ls +++ b/elixir-ls @@ -1 +1 @@ -Subproject commit 3acc1fdb2ef2c1167935f7b6e74300e039200ec5 +Subproject commit 5b2d23622614b8470293cb7cadba530170f8f70f diff --git a/package.json b/package.json index 104e8f5..ac5380b 100644 --- a/package.json +++ b/package.json @@ -144,6 +144,11 @@ "type": "boolean", "description": "Show signature help after confirming autocomplete", "default": true + }, + "elixirLS.enableTestLenses": { + "type": "boolean", + "description": "Show code lenses to run tests in terminal", + "default": false } } }, diff --git a/src/commands/runTestFromCodeLens.ts b/src/commands/runTestFromCodeLens.ts new file mode 100644 index 0000000..90e9d1d --- /dev/null +++ b/src/commands/runTestFromCodeLens.ts @@ -0,0 +1,39 @@ +import { window } from "vscode"; + +type RunArgs = { + filePath: string, + describe: string | null, + testName?: string, + module?: string +} + +export default function runFromCodeLens(args: RunArgs): void { + const elixirLsTerminal = + window.terminals.find(terminal => terminal.name == "ElixirLS") || window.createTerminal("ElixirLS"); + + elixirLsTerminal.show() + elixirLsTerminal.sendText('clear') + elixirLsTerminal.sendText(buildTestCommand(args)); +} + +function buildTestCommand(args: RunArgs): string { + const testFilter = buildTestInclude(args.describe, args.testName, args.module) + + return `mix test --exclude test --include "${testFilter}" ${args.filePath}` +} + +function buildTestInclude(describe: string | null, testName?: string, module?: string) { + if (module) { + return `module:${module}` + } + + if (!testName) { + return `describe:${describe}` + } + + if (describe) { + return `test:test ${describe} ${testName}` + } + + return `test:test ${testName}` +} diff --git a/src/constants/commands.ts b/src/constants/commands.ts new file mode 100644 index 0000000..d939008 --- /dev/null +++ b/src/constants/commands.ts @@ -0,0 +1,3 @@ +export default { + RUN_TEST_FROM_CODELENS: 'elixir.lens.test.run' +} diff --git a/src/extension.ts b/src/extension.ts index 7fcf968..35b07ea 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -17,6 +17,8 @@ import { ServerOptions, } from "vscode-languageclient"; import * as os from "os"; +import Commands from "./constants/commands"; +import runFromCodeLens from "./commands/runTestFromCodeLens"; export let defaultClient: LanguageClient; const clients: Map = new Map(); @@ -77,16 +79,16 @@ function sortedWorkspaceFolders(): string[] { if (_sortedWorkspaceFolders === void 0) { _sortedWorkspaceFolders = workspace.workspaceFolders ? workspace.workspaceFolders - .map((folder) => { - let result = folder.uri.toString(); - if (result.charAt(result.length - 1) !== "/") { - result = result + "/"; - } - return result; - }) - .sort((a, b) => { - return a.length - b.length; - }) + .map((folder) => { + let result = folder.uri.toString(); + if (result.charAt(result.length - 1) !== "/") { + result = result + "/"; + } + return result; + }) + .sort((a, b) => { + return a.length - b.length; + }) : []; } return _sortedWorkspaceFolders; @@ -200,23 +202,28 @@ function configureTerminalLinkProvider(context: ExtensionContext) { if (!selection) { return; } - + openUri(selection.uri, line); }); } }); } }); - + context.subscriptions.push(disposable); } +function configureRunTestFromCodeLens() { + vscode.commands.registerCommand(Commands.RUN_TEST_FROM_CODELENS, runFromCodeLens); +} + export function activate(context: ExtensionContext): void { testElixir(); detectConflictingExtension("mjmcloug.vscode-elixir"); // https://github.com/elixir-lsp/vscode-elixir-ls/issues/34 detectConflictingExtension("sammkj.vscode-elixir-formatter"); + configureRunTestFromCodeLens() configureCopyDebugInfo(context); configureDebugger(context); configureTerminalLinkProvider(context);