Skip to content

Commit

Permalink
See #7
Browse files Browse the repository at this point in the history
  • Loading branch information
pajoma committed Dec 16, 2016
1 parent 6462c1e commit c0fb167
Show file tree
Hide file tree
Showing 12 changed files with 347 additions and 84 deletions.
5 changes: 3 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{
"version": "0.1.0",
"configurations": [

{
"name": "Launch Extension",
"type": "extensionHost",
Expand All @@ -10,7 +11,7 @@
"args": ["--extensionDevelopmentPath=${workspaceRoot}" ],
"stopOnEntry": false,
"sourceMaps": true,
"outDir": "${workspaceRoot}/out/src",
"outFiles": ["${workspaceRoot}/out/src"],
"preLaunchTask": "npm"
},
{
Expand All @@ -21,7 +22,7 @@
"args": ["--extensionDevelopmentPath=${workspaceRoot}", "--extensionTestsPath=${workspaceRoot}/out/test" ],
"stopOnEntry": false,
"sourceMaps": true,
"outDir": "${workspaceRoot}/out/test",
"outFiles": ["${workspaceRoot}/out/test"],
"preLaunchTask": "npm"
}
]
Expand Down
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@
"main": "./out/src/extension",
"contributes": {
"commands": [
{
"command": "journal.test",
"title": "Tests",
"category": "Journal"
},

{
"command": "journal.today",
"title": "Today",
Expand Down
31 changes: 22 additions & 9 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
'use strict';

import * as vscode from 'vscode';
import Journal from './journal';
import Journal from './journal';

// this method is called when your extension is activated
// your extension is activated the very first time the command is executed
Expand All @@ -29,23 +29,23 @@ export function activate(context: vscode.ExtensionContext) {
// This line of code will only be executed once when your extension is activated
console.log('vscode-journal is now active!');

let config:vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration("journal");
let journal = new Journal(config);
let config: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration("journal");
let journal = new Journal(config);


context.subscriptions.push(
vscode.commands.registerCommand('journal.today', () => {
journal.openDay(0);
journal.openDay(0);
}),
vscode.commands.registerCommand('journal.yesterday', () => {
journal.openDay(-1).catch(error => vscode.window.showErrorMessage(error));
journal.openDay(-1).catch(error => vscode.window.showErrorMessage(error));
}),
vscode.commands.registerCommand('journal.tomorrow', () => {
journal.openDay(1).catch(error => vscode.window.showErrorMessage(error));
journal.openDay(1).catch(error => vscode.window.showErrorMessage(error));
}),
vscode.commands.registerCommand('journal.day', () => {
journal.openDayByInput().catch(error => vscode.window.showErrorMessage(error));
}),
journal.openDayByInputOrSelection().catch(error => vscode.window.showErrorMessage(error));
}),
vscode.commands.registerCommand('journal.memo', () => {
journal.openDayByInput().catch(error => vscode.window.showErrorMessage(error));
}),
Expand All @@ -54,6 +54,19 @@ export function activate(context: vscode.ExtensionContext) {
}),
vscode.commands.registerCommand('journal.open', () => {
journal.openJournal().catch(error => vscode.window.showErrorMessage(error));
}),

vscode.commands.registerCommand('journal.test', function () {
// The code you place here will be executed every time your command is executed

function delayedQuickPickItems() {
return new Promise((resolve, reject) => {
setTimeout(() => resolve(['aaaa', 'bbbb', 'cccc', 'abc', 'bcd']), 2000)
})
}

// Display a message box to the user
vscode.window.showQuickPick(delayedQuickPickItems()).then(x => vscode.window.showInformationMessage(x))
})
);
}
Expand Down
69 changes: 64 additions & 5 deletions src/journal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,32 +32,87 @@ export default class Journal {
private parser: journal.Parser;
private writer: journal.Writer;
private vsExt: journal.VSCode;
private reader: journal.Reader;

constructor(private vscodeConfig: vscode.WorkspaceConfiguration) {
this.config = new journal.Configuration(vscodeConfig);
this.util = new journal.Util(this.config);
this.parser = new journal.Parser(this.util);
this.writer = new journal.Writer(this.config);
this.reader = new journal.Reader(this.config, this.util);
this.vsExt = new journal.VSCode(this.writer);

}



public openDayByInputOrSelection(): Q.Promise<vscode.TextDocument> {
let deferred: Q.Deferred<vscode.TextDocument> = Q.defer<vscode.TextDocument>();


this.gatherSelection()
.then(items => {
console.log(JSON.stringify(items));

return this.vsExt.getUserInputComboSync("Enter day or memo (with flags)", items)
}
)
.then( (value: string) => this.parser.tokenize(value) )
.then( (input: journal.Input) => this.open(input) )
.then( (doc: vscode.TextDocument) => deferred.resolve(doc) )
.catch((err) => {
if(err != 'cancel') {
let msg = 'Failed to translate input into action';
vscode.window.showErrorMessage(msg);
deferred.reject(msg)
}


});

return deferred.promise;
}

private gatherSelection(): Q.Promise<[journal.PickDayItem]> {
let deferred: Q.Deferred<[journal.PickDayItem]> = Q.defer<[journal.PickDayItem]>();

let res:[journal.PickDayItem] = <[journal.PickDayItem]> new Array();
this.reader.getPreviousJournalFiles()
.then(files => {
files.forEach(file => {
res.push(new journal.PickDayItem(file, "This is a generic desc"));
});
deferred.resolve(res);

});

this.reader.getPreviousJournalFiles();

return deferred.promise;
}

/**
* Opens the editor for a specific day. Supported values are explicit dates (in ISO format),
* offsets (+ or - as prefix and 0) and weekdays (next wednesday)
*/
public openDayByInput(): Q.Promise<vscode.TextDocument> {
let deferred: Q.Deferred<vscode.TextDocument> = Q.defer<vscode.TextDocument>();


this.reader.getPreviousJournalFiles();

this.vsExt.getUserInput("Enter day or memo (with flags) ")
.then( (value: string) => this.parser.tokenize(value) )
.then( (input: journal.Input) => this.open(input) )
.then( (doc: vscode.TextDocument) => deferred.resolve(doc) )
.catch((err) => {
let msg = 'Failed to translate input into action';
vscode.window.showErrorMessage(msg);
deferred.reject(msg)
if(err != 'cancel') {
let msg = 'Failed to translate input into action';
vscode.window.showErrorMessage(msg);
deferred.reject(msg)
}


});

return deferred.promise;
Expand All @@ -78,7 +133,7 @@ export default class Journal {
let tpl: string = this.config.getPageTemplate();
let content: string = tpl.replace('{content}', this.util.formatDate(date));

this.util.getDateFile(date)
this.util.getFileForDate(date)
.then((path: string) => this.vsExt.loadTextDocument(path))
.catch((path: string) => this.vsExt.createSaveLoadTextDocument(path, content))
.then((doc: vscode.TextDocument) => {
Expand Down Expand Up @@ -113,7 +168,11 @@ export default class Journal {
this.vsExt.showDocument(doc);
deferred.resolve(doc);
})
.catch((err) => deferred.reject("Failed to create a new note"));
.catch((err) => {
if(err != 'cancel') {
deferred.reject("Failed to create a new note. Reason is ["+err+"]");
}
});

return deferred.promise;
}
Expand Down
2 changes: 2 additions & 0 deletions src/util/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
export { Util } from './util';
export { Configuration } from './model/conf';
export { Input } from './model/input';
export { PickDayItem } from './model/pickDayItem';
export { Parser } from './parser';
export { Writer } from './writer';
export { Reader } from './reader';
export { VSCode } from './vscode';
7 changes: 5 additions & 2 deletions src/util/model/conf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,15 @@ export class Configuration {
// TODO: default should be user documents dir
}

// defaults to .md
// defaults to md
public getFileExtension(): string {
let ext: string = this.config.get<string>('ext');
return (ext.length > 0) ? ext : '.md';
if(ext.startsWith(".")) ext = ext.substring(1,ext.length);
return (ext.length > 0) ? ext : 'md';
}




public getPageTemplate(): string {
return this.config.get<string>('tpl-page');
Expand Down
32 changes: 32 additions & 0 deletions src/util/model/pickDayItem.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (C) 2016 Patrick Maué
//
// This file is part of vscode-journal.
//
// vscode-journal is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// vscode-journal is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with vscode-journal. If not, see <http://www.gnu.org/licenses/>.
//

'use strict';

import * as vscode from 'vscode';

export class PickDayItem implements vscode.QuickPickItem {
constructor(label: string, description: string ) {
this.label = label;
this.description = description;
}

public label: string;
public description: string;
public detail: string;
}
99 changes: 53 additions & 46 deletions src/util/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,17 @@ import * as journal from '.';
*/
export class Parser {
public today: Date;
private expr: RegExp = null;

constructor(public util: journal.Util) {

}






/**
* Takes a string and separates the flag, date and text
*/
Expand Down Expand Up @@ -109,53 +115,8 @@ export class Parser {


public split(value: string): RegExpMatchArray {
/*
(?:(task|todo)\s)?(?:(?:(today|tod)\s?)|((?:(?:(?:\+|\-)\d+)|(0))\s?)|((?:\d{4}\-\d{1,2}\-\d{1,2})|(?:\d{1,2}\-\d{1,2})|(?:\d{1,2})\s?)|(?:(next|last|n|l)\s(monday|tuesday)\s?))?(?:(task|todo)\s)?(.*)
*/

/* Groups (see https://regex101.com/r/sCtPOb/2)
1: flag "task"
2: shortcut "today"
3: offset "+1"
4: iso date "2012-12-23"
5: month and day "12-23"
6: day of month "23"
7: weekday flag "next"
8: weekday name "monday"
9: flag "task"
10: text of memo
0:"..."
1:task
2:today
3:+22
4:11-24
5:"next"
6:"monday"
7:"task"
8:"hello world"
*/

// open problem "today", da fehlt der space am Ende


let flagsRX = "(?:(task|todo)\\s)";
let shortcutRX = "(?:(today|tod|yesterday|yes|tomorrow|tom|0)(?:\\s|$))";
let offsetRX = "(?:((?:\\+|\\-)\\d+)(?:\\s|$))"
// let isoDateRX = "(?:(\\d{4})\\-?(\\d{1,2})?\\-?(\\d{1,2})?\\s)";
let isoDateRX = "(?:((?:\\d{4}\\-\\d{1,2}\\-\\d{1,2})|(?:\\d{1,2}\\-\\d{1,2})|(?:\\d{1,2}))(?:\\s|$))"
let weekdayRX = "(?:(next|last|n|l)\\s(monday|tuesday|wednesday|thursday|friday|saturday|sunday|mon|tue|wed|thu|fri|sat|sun|montag|dienstag|mittwoch|donnerstag|freitag|samstag|sonntag)\\s?)";
let remainder = "(.+)"

let completeExpression: string = "^" + flagsRX + "?(?:" + shortcutRX + "|" + offsetRX + "|" + isoDateRX + "|" + weekdayRX + ")?" + flagsRX + "?(.*)" + "$";
console.log(completeExpression);

let regularExpression: RegExp = new RegExp(completeExpression);

return value.match(regularExpression);
return value.match(this.getExpression());
}


Expand Down Expand Up @@ -460,5 +421,51 @@ export class Parser {
}


private getExpression() {
/*
(?:(task|todo)\s)?(?:(?:(today|tod)\s?)|((?:(?:(?:\+|\-)\d+)|(0))\s?)|((?:\d{4}\-\d{1,2}\-\d{1,2})|(?:\d{1,2}\-\d{1,2})|(?:\d{1,2})\s?)|(?:(next|last|n|l)\s(monday|tuesday)\s?))?(?:(task|todo)\s)?(.*)
*/

/* Groups (see https://regex101.com/r/sCtPOb/2)
1: flag "task"
2: shortcut "today"
3: offset "+1"
4: iso date "2012-12-23"
5: month and day "12-23"
6: day of month "23"
7: weekday flag "next"
8: weekday name "monday"
9: flag "task"
10: text of memo
0:"..."
1:task
2:today
3:+22
4:11-24
5:"next"
6:"monday"
7:"task"
8:"hello world"
*/
if(this.expr == null) {
let flagsRX = "(?:(task|todo)\\s)";
let shortcutRX = "(?:(today|tod|yesterday|yes|tomorrow|tom|0)(?:\\s|$))";
let offsetRX = "(?:((?:\\+|\\-)\\d+)(?:\\s|$))"
// let isoDateRX = "(?:(\\d{4})\\-?(\\d{1,2})?\\-?(\\d{1,2})?\\s)";
let isoDateRX = "(?:((?:\\d{4}\\-\\d{1,2}\\-\\d{1,2})|(?:\\d{1,2}\\-\\d{1,2})|(?:\\d{1,2}))(?:\\s|$))"
let weekdayRX = "(?:(next|last|n|l)\\s(monday|tuesday|wednesday|thursday|friday|saturday|sunday|mon|tue|wed|thu|fri|sat|sun|montag|dienstag|mittwoch|donnerstag|freitag|samstag|sonntag)\\s?)";
let remainder = "(.+)"

let completeExpression: string = "^" + flagsRX + "?(?:" + shortcutRX + "|" + offsetRX + "|" + isoDateRX + "|" + weekdayRX + ")?" + flagsRX + "?(.*)" + "$";
console.log(completeExpression);

this.expr = new RegExp(completeExpression);
}
return this.expr;
}
}

Loading

0 comments on commit c0fb167

Please sign in to comment.