2017-02-20 15:33:51 +00:00
|
|
|
// The module 'vscode' contains the VS Code extensibility API
|
|
|
|
// Import the module and reference it with the alias vscode in your code below
|
2017-02-20 17:25:48 +00:00
|
|
|
var vscode = require('vscode');
|
|
|
|
var fs = require('fs');
|
|
|
|
var dateFormat = require('dateformat');
|
2017-02-20 15:33:51 +00:00
|
|
|
|
|
|
|
// this method is called when your extension is activated
|
|
|
|
// your extension is activated the very first time the command is executed
|
|
|
|
function activate(context) {
|
2017-02-20 15:39:04 +00:00
|
|
|
console.log('next-note: activated');
|
2017-02-20 15:33:51 +00:00
|
|
|
|
|
|
|
context.subscriptions.push(vscode.commands.registerTextEditorCommand('extension.nextNoteInsertDate', function () {
|
|
|
|
var editor = vscode.window.activeTextEditor;
|
|
|
|
if (!editor) {
|
2017-02-20 17:25:48 +00:00
|
|
|
console.error("next-note: no active editor");
|
2017-02-20 15:33:51 +00:00
|
|
|
return;
|
|
|
|
};
|
|
|
|
//editor.document.getWordRangeAtPosition
|
|
|
|
var cursorEnd = editor.selection.end;
|
|
|
|
if (editor.document.lineCount > cursorEnd.line+1) {
|
|
|
|
cursorEnd.line += 1;
|
|
|
|
}
|
|
|
|
editor.edit(editBuilder => {
|
2017-02-20 17:25:48 +00:00
|
|
|
editBuilder.insert(cursorEnd, dateLine());
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
|
|
context.subscriptions.push(vscode.commands.registerTextEditorCommand('extension.nextNoteOpenCurrentWeek', function () {
|
|
|
|
var basedir = process.env["NOTEDIR"];
|
|
|
|
|
|
|
|
if (!basedir) {
|
|
|
|
basedir = process.env["HOME"]+"/Notes";
|
|
|
|
console.log("next-note: no basedir. Using "+basedir);
|
|
|
|
console.log("next-note: set NOTEDIR to use a diferent basedir");
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
fs.statSync(basedir);
|
|
|
|
} catch(e) {
|
|
|
|
try {
|
|
|
|
fs.mkdirSync(basedir);
|
|
|
|
} catch(e) {
|
|
|
|
vscode.window.showErrorMessage("next-note: failed to make directory: "+basedir);
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var notepath = basedir +"/"+ currentWeekFilename();
|
|
|
|
fs.access(notepath, fs.constants.R_OK | fs.constants.W_OK, (err) => {
|
|
|
|
if (err) {
|
|
|
|
try {
|
|
|
|
fs.writeFileSync(notepath, dateLine());
|
|
|
|
} catch (e) {
|
|
|
|
vscode.window.showErrorMessage("next-note: failed to create "+ notepath);
|
|
|
|
console.error(e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
vscode.workspace.openTextDocument(notepath).then(doc => {
|
|
|
|
vscode.window.showTextDocument(doc);
|
|
|
|
vscode.window.activeTextEditor.selection.active.line += 1;
|
2017-02-20 15:33:51 +00:00
|
|
|
});
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
exports.activate = activate;
|
|
|
|
|
2017-02-20 17:25:48 +00:00
|
|
|
function dateLine() {
|
|
|
|
var d = new Date();
|
|
|
|
return "## "+ dateFormat(d,"ddd mmm dd HH:MM:ss Z yyyy") +"\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
function currentWeekFilename() {
|
|
|
|
return "Tasks-"+currentWeek()+".md";
|
|
|
|
}
|
|
|
|
|
|
|
|
function currentWeek() {
|
|
|
|
var d = new Date();
|
|
|
|
var beginDate = new Date();
|
|
|
|
beginDate.setDate(d.getDate()-d.getDay()); // This will be sunday of current current week
|
|
|
|
d.setDate(beginDate.getDate()+6); // this is a week from that sunday
|
|
|
|
|
|
|
|
return dateFormat(beginDate, "yyyymmdd")+"-"+dateFormat(d,"yyyymmdd");
|
|
|
|
//return String(beginDate.getFullYear())+String(beginDate.getMonth())+String(beginDate.getDate()) +"-"+ String(d.getFullYear())+String(d.getMonth())+String(d.getDate());
|
|
|
|
}
|
|
|
|
|
2017-02-20 15:33:51 +00:00
|
|
|
// this method is called when your extension is deactivated
|
|
|
|
function deactivate() {
|
2017-02-20 15:39:04 +00:00
|
|
|
console.log("next-note: deactivated")
|
2017-02-20 15:33:51 +00:00
|
|
|
}
|
|
|
|
exports.deactivate = deactivate;
|