Skip to content

Commit

Permalink
convert spaces to \t
Browse files Browse the repository at this point in the history
  • Loading branch information
jwdeane committed Jan 19, 2022
1 parent 804f97e commit c50b6a3
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
12 changes: 11 additions & 1 deletion .eleventy.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,20 @@ module.exports = function (eleventyConfig) {
eleventyConfig.addNunjucksFilter("formatSnippetBody", function (content) {
// escape " and create array of lines
const snippet = content.replace(/"/g, '\\"').split("\n");
// default indentation
// TODO make indent value configurable
const indent = 2;
// wrap each line in double quotes
const formattedSnippet = snippet.map((line, index) => {
// count the number of leading spaces or \t chars
const indentCount = line.search(/\S/) !== -1 ? line.search(/\S/) : 0;
const newLine = line
// remove the leading whitespace
.trimStart()
// add a \t for each tabstop according to indentCount
.replace(/^/, "\t".repeat(indentCount / indent));
// don't add a comma after the last line
return index === snippet.length - 1 ? `"${line}"` : `"${line}",`;
return index === snippet.length - 1 ? `"${newLine}"` : `"${newLine}",`;
});

return formattedSnippet.join("\n");
Expand Down
2 changes: 1 addition & 1 deletion _includes/vscode-snippet-template.njk
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ permalink: "{{ page.filePathStem }}.json"
"scope": "{{ scope }}",{% endif %}
"prefix": "{{ prefix }}",
"body": [
{{ content | trim | formatSnippetBody | indent(6) | safe }}
{{ content | trim | formatSnippetBody | safe }}
],
"description": "{% if description %}{{ description }}{% endif %}"
}
Expand Down
16 changes: 16 additions & 0 deletions src/snippets/test-snippet-tabs.njk
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
name: snippet-name-goes-here
prefix: badger
description: snippet description goes here
scope: javascript, typescript
---
const req = async fetch($1)
// this is a single line comment
const res = await req.json()
const string = "badger" // inline comment test
for (const item of res) {
if (item.name.includes(string)) {
console.log(item.name)
}
}
$0
2 changes: 1 addition & 1 deletion src/snippets/test-snippet.njk
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ for (const item of res) {
console.log(item.name)
}
}
$0
$0

0 comments on commit c50b6a3

Please sign in to comment.