From 7d9bc2d19898e7a7ec263f651b52a9f14f69e8fe Mon Sep 17 00:00:00 2001 From: spatten Date: Mon, 23 Dec 2024 14:10:31 -0800 Subject: [PATCH 1/6] working --- package.json | 12 ++++++- src/extension.ts | 63 +++++++++++++++++++++++++++++++++++++ src/test/fixtures/simple.md | 8 ++--- 3 files changed, 78 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 46091e7..4266f5d 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,9 @@ "onCommand:markdown-worklogs.gotoNextTopLevelHeader", "onCommand:markdown-worklogs.sortDoneToBottom", "onCommand:markdown-worklogs.sortCurrentDoneToBottom", - "onCommand:markdown-worklogs.findInWorklogs" + "onCommand:markdown-worklogs.findInWorklogs", + "onCommand:markdown-worklogs.copyCurrentCodeblock", + "onCommand:markdown-worklogs.selectCurrentCodeblock" ], "main": "./out/extension.js", "contributes": { @@ -123,6 +125,14 @@ { "command": "markdown-worklogs.findInWorklogs", "title": "Markdown Work Logs: search in your worklogs" + }, + { + "command": "markdown-worklogs.selectCurrentCodeblock", + "title": "Markdown Work Logs: select the current codeblock" + }, + { + "command": "markdown-worklogs.copyCurrentCodeblock", + "title": "Markdown Work Logs: copy the text of the current codeblock" } ], "keybindings": [ diff --git a/src/extension.ts b/src/extension.ts index 21aaedc..5e295cd 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -380,6 +380,59 @@ const moveDoneToBottom = async function (editor: vscode.TextEditor, params: { mi } }; +const currentCodeblock = (editor: vscode.TextEditor): Token | undefined => { + const currentLine = editor.selection.active.line; + console.log(`looking for codeblocks at line ${currentLine}`); + const parsed = getBlocks(editor); + const codeblock = parsed.find(block => codeblockFilter(block, { currentLine })); + return codeblock; +}; + +type CodeBlockFilterParams = { + currentLine: number; +}; + +const codeblockFilter = (block: Token, params: CodeBlockFilterParams): boolean => { + const { currentLine } = params; + console.log(`block type: ${block.type}`); + if (block.type !== 'fence') { + return false; + } + if (!block.map) { + return false; + } + const [startLine, endLine] = block.map; + console.log(`current: ${currentLine}, start: ${startLine}, end: ${endLine}`); + return startLine <= currentLine && endLine >= currentLine; +}; + +const copyCurrentCodeblock = async (editor: vscode.TextEditor) => { + const codeblock = currentCodeblock(editor); + console.log(`current codeblock: ${JSON.stringify(codeblock)}`); + if (!codeblock) { + return; + } + console.log(`content: ${codeblock.content}`); + // const code = editor.document.getText(new vscode.Range(codeblock.start, codeblock.end)); + await vscode.env.clipboard.writeText(codeblock.content); +}; + +const selectCurrentCodeblock = async (editor: vscode.TextEditor) => { + const codeblock = currentCodeblock(editor); + if (!codeblock) { + return; + } + if (!codeblock.map) { + return; + } + const [startLine, endLine] = codeblock.map; + const realStart = startLine + 1; + const realEnd = endLine > 2 ? endLine - 2 : endLine; + const lastLine = editor.document.lineAt(realEnd).text; + + editor.selection = new vscode.Selection(new vscode.Position(realStart, 0), new vscode.Position(realEnd, lastLine.length)); +}; + const getConfig = (param: string): string | undefined => vscode.workspace.getConfiguration('markdown-worklogs').get(param); // this method is called when your extension is activated @@ -501,6 +554,16 @@ export function activate(context: vscode.ExtensionContext) { }); }); context.subscriptions.push(findInWorklogs); + + const copyCodeblock = vscode.commands.registerTextEditorCommand('markdown-worklogs.copyCurrentCodeblock', async (te) => { + await copyCurrentCodeblock(te); + }); + context.subscriptions.push(copyCodeblock); + + const selectCodeblock = vscode.commands.registerTextEditorCommand('markdown-worklogs.selectCurrentCodeblock', async (te) => { + await selectCurrentCodeblock(te); + }); + context.subscriptions.push(selectCodeblock); } // this method is called when your extension is deactivated diff --git a/src/test/fixtures/simple.md b/src/test/fixtures/simple.md index d4c47ba..6a559b5 100644 --- a/src/test/fixtures/simple.md +++ b/src/test/fixtures/simple.md @@ -11,10 +11,10 @@ Some text ```ruby -# this is a comment, not a header -def foo - 'foo' -end + # this is a comment, not a header + def foo + 'foo' + end ``` # 2 From db32ada4a435362d2f1e1b423daf650e172023c7 Mon Sep 17 00:00:00 2001 From: spatten Date: Mon, 23 Dec 2024 14:37:49 -0800 Subject: [PATCH 2/6] add some tests --- src/test/fixtures/codeBlock.md | 7 ++++ src/test/suite/codeblock.test.ts | 64 ++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 src/test/fixtures/codeBlock.md create mode 100644 src/test/suite/codeblock.test.ts diff --git a/src/test/fixtures/codeBlock.md b/src/test/fixtures/codeBlock.md new file mode 100644 index 0000000..c0d0a8d --- /dev/null +++ b/src/test/fixtures/codeBlock.md @@ -0,0 +1,7 @@ +# Fenced codeblocks + +``` +def foo + "foo" +end +``` diff --git a/src/test/suite/codeblock.test.ts b/src/test/suite/codeblock.test.ts new file mode 100644 index 0000000..1daa0ba --- /dev/null +++ b/src/test/suite/codeblock.test.ts @@ -0,0 +1,64 @@ + +import * as assert from 'assert'; +import * as vscode from 'vscode'; +import * as helpers from '../helpers'; + +describe('codeblock functions', function () { + let editor: vscode.TextEditor; + + beforeEach(async function () { + editor = await helpers.openExample('codeBlock.md'); + }); + + afterEach(async function () { + // we need to close the document so it gets reloaded fresh from the fixture for each test + await vscode.commands.executeCommand('workbench.action.closeActiveEditor'); + }); + + describe('selectCurrentCodeblock', function () { + it('should select the contents if you are in the codeblock', async function () { + // put your cursor in the codeblock + helpers.gotoLine(editor, 4); + await vscode.commands.executeCommand("markdown-worklogs.selectCurrentCodeblock"); + + const s = editor.selection.start; + const e = editor.selection.end; + assert.strictEqual(JSON.stringify(s), '{"line":3,"character":0}'); + assert.strictEqual(JSON.stringify(e), '{"line":5,"character":3}'); + }); + + it('should do nothing if you are not in a codeblock', async function () { + // there is no codeblock on the first line + helpers.gotoLine(editor, 0); + await vscode.commands.executeCommand("markdown-worklogs.selectCurrentCodeblock"); + + const s = editor.selection.start; + const e = editor.selection.end; + assert.strictEqual(JSON.stringify(s), '{"line":0,"character":0}'); + assert.strictEqual(JSON.stringify(e), '{"line":0,"character":0}'); + }); + }); + + describe('copyCurrentCodeblock', function () { + it('should copy the contents into the clipboard if you are in the codeblock', async function () { + // put your cursor in the codeblock + helpers.gotoLine(editor, 4); + await vscode.commands.executeCommand("markdown-worklogs.copyCurrentCodeblock"); + + const expectedText = 'def foo\n "foo"\nend\n'; + const clipboardText = await vscode.env.clipboard.readText(); + assert.strictEqual(clipboardText, expectedText); + }); + + it('should leave the existing clipboard if you are not in the codeblock', async function () { + // There is no codeblock in the first line + helpers.gotoLine(editor, 0); + const expectedText = 'foo'; + await vscode.env.clipboard.writeText(expectedText); + await vscode.commands.executeCommand("markdown-worklogs.copyCurrentCodeblock"); + + const clipboardText = await vscode.env.clipboard.readText(); + assert.strictEqual(clipboardText, expectedText); + }); + }); +}); From 0bab55ba08c9e956bc402b9835ea3b06086fb5ec Mon Sep 17 00:00:00 2001 From: spatten Date: Mon, 23 Dec 2024 14:38:11 -0800 Subject: [PATCH 3/6] cleanup console.log --- src/extension.ts | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/extension.ts b/src/extension.ts index 5e295cd..b3408e5 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -382,7 +382,6 @@ const moveDoneToBottom = async function (editor: vscode.TextEditor, params: { mi const currentCodeblock = (editor: vscode.TextEditor): Token | undefined => { const currentLine = editor.selection.active.line; - console.log(`looking for codeblocks at line ${currentLine}`); const parsed = getBlocks(editor); const codeblock = parsed.find(block => codeblockFilter(block, { currentLine })); return codeblock; @@ -394,7 +393,6 @@ type CodeBlockFilterParams = { const codeblockFilter = (block: Token, params: CodeBlockFilterParams): boolean => { const { currentLine } = params; - console.log(`block type: ${block.type}`); if (block.type !== 'fence') { return false; } @@ -402,17 +400,14 @@ const codeblockFilter = (block: Token, params: CodeBlockFilterParams): boolean = return false; } const [startLine, endLine] = block.map; - console.log(`current: ${currentLine}, start: ${startLine}, end: ${endLine}`); return startLine <= currentLine && endLine >= currentLine; }; const copyCurrentCodeblock = async (editor: vscode.TextEditor) => { const codeblock = currentCodeblock(editor); - console.log(`current codeblock: ${JSON.stringify(codeblock)}`); if (!codeblock) { return; } - console.log(`content: ${codeblock.content}`); // const code = editor.document.getText(new vscode.Range(codeblock.start, codeblock.end)); await vscode.env.clipboard.writeText(codeblock.content); }; From 800f3114ca24c1678d9408ecfbd62cbfe8070cb5 Mon Sep 17 00:00:00 2001 From: spatten Date: Mon, 23 Dec 2024 14:40:54 -0800 Subject: [PATCH 4/6] fix up the description --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 4266f5d..8bd27df 100644 --- a/package.json +++ b/package.json @@ -132,7 +132,7 @@ }, { "command": "markdown-worklogs.copyCurrentCodeblock", - "title": "Markdown Work Logs: copy the text of the current codeblock" + "title": "Markdown Work Logs: copy the current codeblock to the clipboard" } ], "keybindings": [ From de944248ae3881e77b282584b66b08433b06bfbd Mon Sep 17 00:00:00 2001 From: spatten Date: Mon, 23 Dec 2024 14:42:13 -0800 Subject: [PATCH 5/6] fix up the yarn package command --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 8bd27df..1ad91d3 100644 --- a/package.json +++ b/package.json @@ -158,7 +158,7 @@ "lint:strict": "eslint src --ext ts --rule 'mocha/no-exclusive-tests: 2' --rule 'no-undef: 2' --rule 'no-console: 2'", "lint": "eslint src --ext ts", "test": "node ./out/test/runTest.js", - "package": "vsce package" + "package": "vsce package --no-yarn" }, "devDependencies": { "@types/glob": "^7.2.0", From 39eb16a277504bf2479a00c97bc9ac8e557d67d0 Mon Sep 17 00:00:00 2001 From: spatten Date: Mon, 23 Dec 2024 14:43:56 -0800 Subject: [PATCH 6/6] bump the version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 1ad91d3..41afbe1 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "markdown-worklogs", "displayName": "markdown-worklogs", "description": "Top-level TODO and DONE on your markdown headers", - "version": "0.0.1", + "version": "0.0.2", "engines": { "vscode": "^1.64.0" },