extension: current week works

command registered for opening current week document
This commit is contained in:
Vincent Batts 2017-02-20 12:25:48 -05:00
parent 11161b33c5
commit 7ed5420e13
2 changed files with 69 additions and 9 deletions

View file

@ -1,6 +1,8 @@
// The module 'vscode' contains the VS Code extensibility API // The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below // Import the module and reference it with the alias vscode in your code below
var vscode = require('vscode'); var vscode = require('vscode');
var fs = require('fs');
var dateFormat = require('dateformat');
// this method is called when your extension is activated // this method is called when your extension is activated
// your extension is activated the very first time the command is executed // your extension is activated the very first time the command is executed
@ -14,7 +16,7 @@ function activate(context) {
context.subscriptions.push(vscode.commands.registerTextEditorCommand('extension.nextNoteInsertDate', function () { context.subscriptions.push(vscode.commands.registerTextEditorCommand('extension.nextNoteInsertDate', function () {
var editor = vscode.window.activeTextEditor; var editor = vscode.window.activeTextEditor;
if (!editor) { if (!editor) {
console.error("no active editor"); console.error("next-note: no active editor");
return; return;
}; };
//editor.document.getWordRangeAtPosition //editor.document.getWordRangeAtPosition
@ -22,14 +24,70 @@ function activate(context) {
if (editor.document.lineCount > cursorEnd.line+1) { if (editor.document.lineCount > cursorEnd.line+1) {
cursorEnd.line += 1; cursorEnd.line += 1;
} }
var d = new Date();
editor.edit(editBuilder => { editor.edit(editBuilder => {
editBuilder.insert(cursorEnd, "## "+ d.toString() +"\n"); 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;
}); });
})); }));
} }
exports.activate = activate; exports.activate = activate;
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());
}
// this method is called when your extension is deactivated // this method is called when your extension is deactivated
function deactivate() { function deactivate() {
console.log("next-note: deactivated") console.log("next-note: deactivated")

View file

@ -15,16 +15,15 @@
"Other" "Other"
], ],
"activationEvents": [ "activationEvents": [
"onCommand:extension.sayHello", "onCommand:extension.nextNoteInsertDate",
"onCommand:extension.nextNoteInsertDate" "onCommand:extension.nextNoteOpenCurrentWeek"
], ],
"main": "./extension", "main": "./extension",
"contributes": { "contributes": {
"commands": [ "commands": [
{ {
"command": "extension.sayHello", "command": "extension.nextNoteOpenCurrentWeek",
"title": "Hello World" "title": "NextNote: Open Current Week"
}, },
{ {
"command": "extension.nextNoteInsertDate", "command": "extension.nextNoteInsertDate",
@ -36,6 +35,9 @@
"postinstall": "node ./node_modules/vscode/bin/install", "postinstall": "node ./node_modules/vscode/bin/install",
"test": "node ./node_modules/vscode/bin/test" "test": "node ./node_modules/vscode/bin/test"
}, },
"dependencies": {
"dateformat": "^2.0.0"
},
"devDependencies": { "devDependencies": {
"typescript": "^2.0.3", "typescript": "^2.0.3",
"vscode": "^1.0.0", "vscode": "^1.0.0",