Skip to content

Commit 542fc6c

Browse files
committed
Add option to wrap snippet in Markdown code block
1 parent 5290123 commit 542fc6c

File tree

4 files changed

+46
-4
lines changed

4 files changed

+46
-4
lines changed

package.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@
2929
"title": "Copy Without Leading Indentation"
3030
}
3131
],
32+
"configuration": {
33+
"title": "Snippet Copy",
34+
"properties": {
35+
"snippet-copy.addLanguageIdToMarkdownBlock": {
36+
"type": "boolean",
37+
"default": false,
38+
"description": "Add a programming language identifier to the beginning of the Markdown code block. This is incompatible with some apps, for example Slack."
39+
}
40+
}
41+
},
3242
"keybindings": [
3343
{
3444
"command": "snippet-copy.copyWithoutLeadingIndentation",

src/lib/documentHelpers.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,7 @@ export function adjustedRangeWithMinimumIndentation(range: Range, minimumIndenta
3838
export function endOfLineCharacter(document: TextDocument): string {
3939
return document.eol === EndOfLine.CRLF ? '\r\n' : '\n';
4040
}
41+
42+
export function languageId(document: TextDocument): string {
43+
return document.languageId;
44+
}

src/lib/textHelpers.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
1-
import { Selection, TextDocument } from "vscode";
2-
import { contentOfLinesWithAdjustedIndentation, endOfLineCharacter, minimumIndentationForLineIndexes } from "./documentHelpers";
1+
import { Selection, TextDocument, workspace } from "vscode";
2+
import { contentOfLinesWithAdjustedIndentation, endOfLineCharacter, languageId, minimumIndentationForLineIndexes } from "./documentHelpers";
33
import { lineIndexesForSelection } from "./selectionHelpers";
44

5-
export function generateSnippet(document: TextDocument, selections: Selection[]): string {
5+
export function generateSnippet(document: TextDocument, selections: Selection[], markdownCodeBlock = false): string {
66
let texts: string[] = [];
77
selections.forEach(selection => {
88
texts.push(generateCopyableText(document, selection));
99
});
1010

1111
const snippet = texts.join(endOfLineCharacter(document));
1212

13+
if (markdownCodeBlock) {
14+
const config = workspace.getConfiguration('snippet-copy');
15+
16+
return wrapTextInMarkdownCodeBlock(document, snippet, config.addLanguageId);
17+
}
18+
1319
return snippet;
1420
}
1521

@@ -26,3 +32,13 @@ export function generateCopyableText(document: TextDocument, selection: Selectio
2632

2733
return text;
2834
}
35+
36+
export function wrapTextInMarkdownCodeBlock(document: TextDocument, text: string, addLanguageId = false): string {
37+
const codeBlockDelimiter = '```';
38+
const eolCharacter = endOfLineCharacter(document);
39+
const optionalLanguageIdentifier = addLanguageId ? languageId(document) : '';
40+
41+
return codeBlockDelimiter + optionalLanguageIdentifier + eolCharacter +
42+
text + eolCharacter +
43+
codeBlockDelimiter;
44+
}

src/test/suite/lib/textHelpers.test.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as assert from 'assert';
22
import * as path from 'path';
33
import * as vscode from 'vscode';
44
import { Position, Selection, TextDocument } from 'vscode';
5-
import { generateCopyableText, generateSnippet } from '../../../lib/textHelpers';
5+
import { generateCopyableText, generateSnippet, wrapTextInMarkdownCodeBlock } from '../../../lib/textHelpers';
66

77
const fixturesPath = '/../../../../src/test/fixtures/';
88
const uri = vscode.Uri.file(
@@ -60,5 +60,17 @@ describe('Text Helpers', () => {
6060
generateCopyableText(document, new Selection(testSelection1.selection.start, new Position(5, 0)))
6161
);
6262
});
63+
64+
context('wrapTextInMarkdownCodeBlock', () => {
65+
it('returns the text wrapped in a Markdown code block', () => {
66+
const codeSnippet = 'console.log("Yo");';
67+
assert.equal(wrapTextInMarkdownCodeBlock(document, codeSnippet), '```\n' + codeSnippet + '\n```');
68+
});
69+
70+
it('returns the wrapped text with a language identifier', () => {
71+
const codeSnippet = 'console.log("Yo");';
72+
assert.equal(wrapTextInMarkdownCodeBlock(document, codeSnippet, true), '```javascript\n' + codeSnippet + '\n```');
73+
});
74+
});
6375
});
6476
});

0 commit comments

Comments
 (0)