From f5bc60208ef18cf8f1a50ee22227cc0fc5d28c29 Mon Sep 17 00:00:00 2001 From: Leonardo Silveira Date: Sat, 13 Jul 2024 20:06:50 -0300 Subject: [PATCH 1/2] feat: added change directory so it runs in the same folder as the README.md file --- src/commandCodeLensProvider.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/commandCodeLensProvider.ts b/src/commandCodeLensProvider.ts index ff44792..cdbb5d2 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,7 @@ 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); var inCommand = false; var currentCommand = ''; @@ -17,7 +19,7 @@ export class CommandCodeLensProvider implements vscode.CodeLensProvider { const cmd: vscode.Command = { title: 'Run command in terminal', command: 'markdown.run.command', - arguments: [{ command: currentCommand }] + arguments: [{ command: `cd ${directory}; ${currentCommand}` }] }; codeLenses.push( new vscode.CodeLens(new vscode.Range(new vscode.Position(commandStartLine, 0), new vscode.Position(commandStartLine + 1, 0)), cmd) From bc9741f9b27077fe8016aea02c794a9413849e5c Mon Sep 17 00:00:00 2001 From: Leonardo Silveira Date: Wed, 11 Sep 2024 17:51:46 -0300 Subject: [PATCH 2/2] add config for the directory change --- .gitignore | 2 ++ README.md | 14 +++++++++++++- src/commandCodeLensProvider.ts | 5 ++++- 3 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 .gitignore 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 cdbb5d2..25743d5 100644 --- a/src/commandCodeLensProvider.ts +++ b/src/commandCodeLensProvider.ts @@ -9,6 +9,8 @@ export class CommandCodeLensProvider implements vscode.CodeLensProvider { 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 = ''; var commandStartLine = 0; @@ -16,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: `cd ${directory}; ${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)