diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9209ef5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +node_modules +out diff --git a/README.md b/README.md index 369daf7..f3acbdb 100644 --- a/README.md +++ b/README.md @@ -5,12 +5,24 @@ It adds a CodeLens action to run shell snippets in any markdown file. screenshot -### How does it work? +## How does it work? `markdown-command-runner` detects `` ``` ``, `` ```sh `` and `` ```bash `` snippets in markdown files an adds a `Run command in terminal` action at the top. When clicked the associated snippet is run the current active terminal or a new one if it is busy. +### Point of execution + +You can config wether it should change the directory to the one where the README +is by adding this in your .vscode/settings.json: + +```json +{ + "markdown-command-runner": { + "change-directory": "true" + } +} +``` *This extension is not actively maintained but contributions are welcome!* diff --git a/src/commandCodeLensProvider.ts b/src/commandCodeLensProvider.ts index ff44792..25743d5 100644 --- a/src/commandCodeLensProvider.ts +++ b/src/commandCodeLensProvider.ts @@ -1,4 +1,5 @@ import vscode = require('vscode'); +import path = require('path'); export class CommandCodeLensProvider implements vscode.CodeLensProvider { onDidChangeCodeLenses?: vscode.Event; @@ -6,6 +7,9 @@ export class CommandCodeLensProvider implements vscode.CodeLensProvider { provideCodeLenses(document: vscode.TextDocument, token: vscode.CancellationToken): vscode.ProviderResult { var codeLenses = []; const lines = document.getText().split('\n'); + const directory = path.dirname(document.fileName); + + const changeDirectoryConfig = vscode.workspace.getConfiguration().get('markdown-command-runner.change-directory'); var inCommand = false; var currentCommand = ''; @@ -14,10 +18,11 @@ export class CommandCodeLensProvider implements vscode.CodeLensProvider { const line = lines[i].trim(); if (inCommand) { if (line === '```') { + const cdCmd = changeDirectoryConfig == "true" ? `cd ${directory};` : '' const cmd: vscode.Command = { title: 'Run command in terminal', command: 'markdown.run.command', - arguments: [{ command: currentCommand }] + arguments: [{ command: `${cdCmd} ${currentCommand}` }] }; codeLenses.push( new vscode.CodeLens(new vscode.Range(new vscode.Position(commandStartLine, 0), new vscode.Position(commandStartLine + 1, 0)), cmd)