Skip to content

Commit

Permalink
handle comments better, auto-create file if it doesn't exist
Browse files Browse the repository at this point in the history
  • Loading branch information
mousetraps committed Nov 23, 2015
1 parent 8659802 commit 267b7fa
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 26 deletions.
84 changes: 59 additions & 25 deletions extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ var vscode = require('vscode');
var path = require('path');
var fs = require('fs');
var json = require('comment-json');
var stringify = require('json-stable-stringify');
var prependFile = require('prepend-file');

var DEFAULT_SNIPPET_NAME = 'REPLACE-WITH-YOUR-SNIPPET-NAME';

Expand All @@ -21,25 +21,50 @@ exports.activate = function activate(context) {

var snippetFilename = getSnippetFilename(editor);

fs.readFile(snippetFilename, 'utf8', function(e, data) {
var snippetBody = getSnippetBody(editor);

console.log(data);
var jsonData = JSON.parse(data, null, true);
jsonData[DEFAULT_SNIPPET_NAME] = {
prefix: 'yourPrefixHere',
body: snippetBody,
description: 'Your snippet description here.'
};
console.log(jsonData);

updateSnippetFile(snippetFilename, jsonData);
fs.exists(snippetFilename, function(exists) {
if (!exists) {
fs.writeFileSync(snippetFilename, '{ }', {encoding: 'utf8'});
}
createSnippet(snippetFilename, editor);
});
});

context.subscriptions.push(disposable);
};

function createSnippet(snippetFilename, editor) {
fs.readFile(snippetFilename, 'utf8', function(e, data) {
console.log(data);
var jsonData = json.parse(data, null, false);
jsonData = addSelectedSnippet(jsonData, editor);
updateSnippetFile(snippetFilename, jsonData);
});
}

function addSelectedSnippet(jsonData, editor) {
var newJsonData = {};

var snippetBody = getSnippetBody(editor);

if (typeof (jsonData['//$']) !== 'undefined') {
// newJsonData['//$'] = jsonData['//$'];
}

newJsonData[DEFAULT_SNIPPET_NAME] = {
prefix: 'yourPrefixHere',
body: snippetBody,
description: 'Your snippet description here.'
};

for (var key in jsonData) {
if (key !== DEFAULT_SNIPPET_NAME && key !== '//$') {
newJsonData[key] = jsonData[key];
}
}
console.log(newJsonData);
return newJsonData;
}

function getSnippetFilename(editor) {
var userDataPath = process.env.APPDATA ||
(process.platform === 'darwin' ?
Expand Down Expand Up @@ -80,23 +105,32 @@ function getSnippetBody(editor) {
}

function updateSnippetFile(snippetFilename, jsonData) {
console.log(jsonData);
fs.writeFile(
snippetFilename,
stringify(jsonData, {space: ' ', cmp: snippetCompare}), {encoding: 'utf8'},
json.stringify(jsonData, null, ' '), {encoding: 'utf8'},
function(err) {
vscode.workspace.openTextDocument(snippetFilename).then(function(document) {
vscode.window.showTextDocument(document, vscode.ViewColumn.Two);
if (err) {
throw err;
}
var snippetComment = fs.readFileSync('snippet-comment-template.txt', 'utf8').replace('$(languageId)', path.basename(snippetFilename));
var current = fs.readFileSync(snippetFilename, 'utf8');
if (!current.startsWith(snippetComment)) {
prependFile.sync(snippetFilename, snippetComment, {encoding: 'utf8'});
}
vscode.workspace.openTextDocument(snippetFilename).then(function(doc) {
vscode.window.showTextDocument(doc, vscode.ViewColumn.Two)
.then(selectDefaultSnippet);
});
}
);
}

function snippetCompare(a, b) {
if (b.key === DEFAULT_SNIPPET_NAME) {
return 1;
} else if (a.key === DEFAULT_SNIPPET_NAME) {
return -1;
}
return a.key < b.key ? -1 : 1;
function selectDefaultSnippet() {
var editor = vscode.window.activeTextEditor;
var index = editor.document.getText().indexOf(DEFAULT_SNIPPET_NAME);
editor.selection = new vscode.Selection(
editor.document.positionAt(index),
editor.document.positionAt(index + DEFAULT_SNIPPET_NAME.length)
);
}

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@
},
"dependencies": {
"comment-json": "^1.1.3",
"json-stable-stringify": "^1.0.0"
"prepend-file": "^1.3.0"
}
}
14 changes: 14 additions & 0 deletions snippet-comment-template.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
// Place your snippets for $(languageId) here. Each snippet is defined under a snippet name and has a prefix, body and
// description. The prefix is what is used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
// $1, $2 for tab stops, ${id} and ${id:label} and ${1:label} for variables. Variables with the same id are connected.
// Example:
"Print to console": {
"prefix": "log",
"body": [
"console.log('$1');",
"$2"
],
"description": "Log output to console"
}
*/
9 changes: 9 additions & 0 deletions todo.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
x handle comments in json file
(A) TODO figure out why parsing doesn't work sometimes
x TODO create file if it doesn't already exist
x TODO Add comments to generated json
(B) TODO fix tab / spaces parsing code
(B) TODO add unit tests
(A) TODO cleanup
(B) TODO readme
(B) TODO publish extension

0 comments on commit 267b7fa

Please sign in to comment.