Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand All @@ -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": {
Expand Down Expand Up @@ -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 current codeblock to the clipboard"
}
],
"keybindings": [
Expand All @@ -148,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",
Expand Down
58 changes: 58 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,54 @@ const moveDoneToBottom = async function (editor: vscode.TextEditor, params: { mi
}
};

const currentCodeblock = (editor: vscode.TextEditor): Token | undefined => {
const currentLine = editor.selection.active.line;
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;
if (block.type !== 'fence') {
return false;
}
if (!block.map) {
return false;
}
const [startLine, endLine] = block.map;
return startLine <= currentLine && endLine >= currentLine;
};

const copyCurrentCodeblock = async (editor: vscode.TextEditor) => {
const codeblock = currentCodeblock(editor);
if (!codeblock) {
return;
}
// 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
Expand Down Expand Up @@ -501,6 +549,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
Expand Down
7 changes: 7 additions & 0 deletions src/test/fixtures/codeBlock.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Fenced codeblocks

```
def foo
"foo"
end
```
8 changes: 4 additions & 4 deletions src/test/fixtures/simple.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
64 changes: 64 additions & 0 deletions src/test/suite/codeblock.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
});
Loading