Skip to content

Commit

Permalink
Merge branch 'develop' of https://github.com/pajoma/vscode-journal in…
Browse files Browse the repository at this point in the history
…to develop
  • Loading branch information
pajoma committed Apr 28, 2022
2 parents 9169335 + 9e6106a commit 6f4d4cf
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 74 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@
"journal.locale": {
"type": "string",
"default": "",
"description": "The locale to use (required for the date format). Defaults to 'en-US'."
"description": "The locale to use (required for the date format). Defaults to the language setting from Visual Studio Code"
},
"journal.patterns": {
"type": "object",
Expand Down
25 changes: 23 additions & 2 deletions src/ext/conf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,10 @@ export class Configuration {


public getLocale(): string {

let locale: string | undefined = this.config.get<string>('locale');
return (isNullOrUndefined(locale) || (locale!.length === 0)) ? 'en-US' : locale!;

return (isNullOrUndefined(locale) || (locale!.length === 0)) ? vscode.env.language : locale!;
}


Expand Down Expand Up @@ -589,7 +591,7 @@ export class Configuration {
if (this.getLocale().startsWith("en")) {
return `Create or open entry ${dayAsString}`;
} else if (this.getLocale().startsWith("de")) {
return `Eintrag für ${dayAsString} erstellen oder öffnen`;
return `Eintrag ${dayAsString} erstellen oder öffnen`;
} else if (this.getLocale().startsWith("fr")) {
return `Créer ou ouvrir une entrée ${dayAsString}`;
} else if (this.getLocale().startsWith("es")) {
Expand Down Expand Up @@ -689,6 +691,25 @@ export class Configuration {

}


private pickTranslations: Map<string, string> = new Map();
public getPickDetailsTranslation(code: number): string | undefined {
if (this.pickTranslations.size === 0) {
this.pickTranslations.set("en"+1, "[from] ll");
this.pickTranslations.set("en"+2, "[from] dddd");
this.pickTranslations.set("de"+1, "[von] ll");
this.pickTranslations.set("de"+2, "[vom] dddd");
this.pickTranslations.set("fr"+1, "[du] ll");
this.pickTranslations.set("fr"+2, "[du] dddd");
this.pickTranslations.set("es"+1, "[desde] el");
this.pickTranslations.set("es"+2, "[del] dddd");
}
let val = this.pickTranslations.get(this.getLocale().substring(0, 2)+code);
if (isNullOrUndefined(val)) {val = this.pickTranslations.get("en" + code);}
return <string>val;

}

/**
* Helper Method, threshold (maximal age) of files shown in the quick picker
*/
Expand Down
10 changes: 6 additions & 4 deletions src/ext/dialogues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,16 +212,18 @@ export class Dialogues {
// if its with the last week, we just print the weekday.. otherwise localised date

let desc = "";
let displayDate = moment(fe.createdAt);

let displayDate = moment(fe.createdAt).locale(this.ctrl.config.getLocale());

if(displayDate.isAfter(moment().subtract(7, "d"))) {
desc += displayDate.format("[from] dddd");
desc += displayDate.format(this.ctrl.config.getPickDetailsTranslation(2));
} else {
desc += displayDate.format("[from] ll");
desc += displayDate.format(this.ctrl.config.getPickDetailsTranslation(1));
}



if (fe.scope !== SCOPE_DEFAULT) { desc += " in scope " + fe.scope; }
if (fe.scope !== SCOPE_DEFAULT) { desc += ` | #${fe.scope}`; }

let item: DecoratedQuickPickItem = {
label: fe.name,
Expand Down
131 changes: 69 additions & 62 deletions src/provider/features/match-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Logger } from "../../util/logger";
import { isNullOrUndefined, isNotNullOrUndefined, getDayOfWeekForString} from "../../util/";
import { Input } from "../../model/input";
import moment = require("moment");
import { getMonthForString } from "../../util/dates";

/**
* Feature responsible for parsing the user input and and extracting offset, flags and text.
Expand Down Expand Up @@ -98,39 +99,40 @@ export class MatchInput {



private extractText(inputGroups: string[]): string {
private extractText(inputGroups: RegExpMatchArray): string {
const text = inputGroups.groups!["text"];
/* Groups
10: text of memo
*/
return (inputGroups[10] === null) ? "" : inputGroups[10];
return isNotNullOrUndefined(text) ? text : "";
}


private extractFlags(inputGroups: string[]): string {
/* Groups (see https://regex101.com/r/sCtPOb/2)
1: flag "task"
9: flag "task"
*/

let res = (isNotNullOrUndefined(inputGroups[1])) ? inputGroups[1] : inputGroups[9];
return (isNullOrUndefined(res)) ? "" : res;
private extractFlags(inputGroups: RegExpMatchArray): string {
const flagPre = inputGroups.groups!["flag"];
const flagPost = inputGroups.groups!["flagPost"];

if(isNotNullOrUndefined(flagPre)) { return flagPre; }
if(isNotNullOrUndefined(flagPost)) { return flagPost; }
return "";
}

/**
* Tries to extract the mentioned week
*
* Groups
* "w13": 7
* "next week": 5: "next"
*
*/
extractWeek(inputGroups: RegExpMatchArray): number {
if (isNotNullOrUndefined(inputGroups[8])) {
return this.resolveNumberedWeek(inputGroups[8]);
let week = inputGroups.groups!["week"];
let weekNum = inputGroups.groups!["weekNum"];
let modifier = inputGroups.groups!["modifier"];

if (isNotNullOrUndefined(weekNum)) {
return this.resolveNumberedWeek(weekNum);
}

if (isNotNullOrUndefined(inputGroups[7])) {
return this.resolveRelatedWeek(inputGroups[5]);
if (isNotNullOrUndefined(week)) {
return this.resolveRelatedWeek(modifier);
}

return -1;
Expand All @@ -152,37 +154,38 @@ export class MatchInput {

/**
*
* @param arg0 numbered week, e.g. "w13"
* @param weekAsNumber numbered week, e.g. "w13"
*/
resolveNumberedWeek(arg0: string): number {
return parseInt(arg0.substring(1, arg0.length));
resolveNumberedWeek(weekAsNumber: string): number {
return parseInt(weekAsNumber);
}


private extractOffset(inputGroups: string[]): number {
private extractOffset(inputGroups: RegExpMatchArray): number {
let shortcut = inputGroups.groups!["shortcut"];
let offset = inputGroups.groups!["offset"];
let iso = inputGroups.groups!["iso"];
let weekday = inputGroups.groups!["weekday"];
let modifier = inputGroups.groups!["modifier"];
let dayOfMonth = inputGroups.groups!["dayOfMonth"];
let month = inputGroups.groups!["month"];

/* Groups (see https://regex101.com/r/sCtPOb/2)
2:today
3:+22
4:11-24
5:"next"
6:"monday"
*/

if (isNotNullOrUndefined(inputGroups[2])) {
return this.resolveShortcutString(inputGroups[2]);
if (isNotNullOrUndefined(shortcut)) {
return this.resolveShortcutString(shortcut);
}
if (isNotNullOrUndefined(inputGroups[3])) {
return this.resolveOffsetString(inputGroups[3]);
if (isNotNullOrUndefined(offset)) {
return this.resolveOffsetString(offset);
}
if (isNotNullOrUndefined(inputGroups[4])) {
return this.resolveISOString(inputGroups[4]);
if (isNotNullOrUndefined(iso)) {
return this.resolveISOString(iso);
}
if ((isNullOrUndefined(inputGroups[5])) && (isNotNullOrUndefined(inputGroups[6]))) {
return this.resolveWeekday(inputGroups[6]);
if (isNotNullOrUndefined(weekday)) {
return this.resolveWeekday(weekday, modifier);
}
if ((isNotNullOrUndefined(inputGroups[5])) && (isNotNullOrUndefined(inputGroups[6]))) {
return this.resolveWeekday(inputGroups[6], inputGroups[5]);

if (isNotNullOrUndefined(month) && isNotNullOrUndefined(dayOfMonth)) {

return this.resolveDayOfMonth(month, dayOfMonth);
}


Expand All @@ -192,6 +195,7 @@ export class MatchInput {




private resolveOffsetString(inputString: string): number {
if (inputString.startsWith("+", 0)) {
return parseInt(inputString.substring(1, inputString.length));
Expand All @@ -218,6 +222,8 @@ export class MatchInput {
private resolveISOString(inputString: string): number {

let todayInMS: number = Date.UTC(this.today.getFullYear(), this.today.getMonth(), this.today.getDate());

inputString = inputString.replace("/", "-"); // american formatting, e.g. 11\12
let dt: string[] = inputString.split("-");

let year: number | undefined, month: number | undefined, day: number | undefined;
Expand Down Expand Up @@ -303,8 +309,22 @@ export class MatchInput {
return NaN;
}


/**
* Parses strings like "Jun 1" and returns the offset from today
*
* @param month
* @param dayOfMonth
* @returns
*/
private resolveDayOfMonth(month: string, dayOfMonth: string): number {
let current = moment();
let date = moment().month(getMonthForString(month)).date(parseInt(dayOfMonth));
let diff = date.diff(current, "days");
return diff;
}


/**
* Takes any given string as input and tries to compute the offset from today's date.
* It translates something like "next wednesday" into "4" (if next wednesday is in four days).
*
Expand All @@ -314,17 +334,16 @@ export class MatchInput {
*/
private getExpression(): RegExp {
/*
v6 with week modifier https://regex101.com/r/sCtPOb/6
(?:(task|todo)\s)?(?:(?:(today|tod|yesterday|yes|tomorrow|tom|0)(?:\s|$))|(?:((?:\+|\-)\d+)(?:\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|wednesday|thursday|friday|saturday|sunday|mon|tue|wed|thu|fri|sat|sun|montag|dienstag|mittwoch|donnerstag|freitag|samstag|sonntag)\s?))?(?:(task|todo)\s)?(.*)
*/
/*
^(?:(task|todo)\s)?
(?:(?:(today|tod|yesterday|yes|tomorrow|tom|0)
(?:\s|$))|(?:((?:\+|\-)\d+)(?:\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|wednesday|thursday|friday|saturday|sunday|mon|tue|wed|thu|fri|sat|sun|montag|dienstag|mittwoch|donnerstag|freitag|samstag|sonntag)?|(week|w$|w\s))?\s?)|(w\d{1,2}))?(?:(task|todo)\s)?(.*)$
*/
v8 (with Month + Day) https://regex101.com/r/sCtPOb/7
^(?:(?<flag>task|todo)\s)?(?:(?:(?:(?<shortcut>today|tod|yesterday|yes|tomorrow|tom|0)(?:\s|$)))|(?:(?<offset>(?:\+|\-)\d+)(?:\s|$))|(?:(?<iso>(?:\d{4}(?:\-|\\)\d{1,2}(?:\-|\\)\d{1,2})|(?:\d{1,2}(?:\-|\\)\d{1,2})|(?:\d{1,2}))(?:\s|$))|(?:(?<modifier>next|last|n|l)?\s?(?:(?<weekday>monday|tuesday|wednesday|thursday|friday|saturday|sunday|mon|tue|wed|thu|fri|sat|sun|montag|dienstag|mittwoch|donnerstag|freitag|samstag|sonntag)?|(?<week>w(?:eek)?(?:\s\D|$)))?\s?)|(?:w(?:eek)?\s?(?<weekNum>[1-5]?[0-9])(?:\s|$))|(?:(?<month>Jan|Feb|Mar|Apr|Apr(?:il)?|May|June?|July?|Aug(?:gust)?|Sep(?:tember)?|Oct(?:ober)?|Nov|Dec)+)+\s?(?<dayOfMonth>(?:[1-9]|1[0-9]|2[0-9]|3[0-1])(?:\s|$))+)?(?:(?<flagPost>task|todo)\s)?(?<text>.*)$
/* Groups (see https://regex101.com/r/sCtPOb/2) (! // -> /)
Groups (see https://regex101.com/r/sCtPOb) (! // -> /)
1: flag "task"
2: shortcut "today"
3: offset "+1"
Expand All @@ -348,20 +367,8 @@ export class MatchInput {
8:"hello world"
*/
if (isNullOrUndefined(this.expr)) {
// check current version at https://regex101.com/r/sCtPOb/6
let regExp = '^(?:(task|todo)\\s)?(?:(?:(today|tod|yesterday|yes|tomorrow|tom|0)(?:\\s|$))|(?:((?:\\+|\\-)\\d+)(?:\\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|wednesday|thursday|friday|saturday|sunday|mon|tue|wed|thu|fri|sat|sun|montag|dienstag|mittwoch|donnerstag|freitag|samstag|sonntag)?|(week|w$|w\\s))?\\s?)|(w\\d{1,2}))?(?:(task|todo)\\s)?(.*)$';

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)?|(week|w$|w\\s))?\\s?)";
let weekOfYearRX = "(w\\d{1,2})?";
let completeExpression: string = "^" + flagsRX + "?(?:" + shortcutRX + "|" + offsetRX + "|" + isoDateRX + "|" + weekdayRX + "|" + weekOfYearRX + ")?" + flagsRX + "?(.*)" + "$";


// let completeExpression: string = "^" + flagsRX + "?(?:" + shortcutRX + "|" + offsetRX + "|" + isoDateRX + "|" + weekdayRX + "|" + weekOfYearRX + "|" + offsetWeekRX + "|" + weekRX+")?" + flagsRX + "?(.*)" + "$";
// see links above for current version in regexp.com
let regExp = /^(?:(?<flag>task|todo)\s)?(?:(?:(?:(?<shortcut>today|tod|yesterday|yes|tomorrow|tom|0)(?:\s|$)))|(?:(?<offset>(?:\+|\-)\d+)(?:\s|$))|(?:(?<iso>(?:\d{4}(?:\-|\/)\d{1,2}(?:\-|\/)\d{1,2})|(?:\d{1,2}(?:\-|\/)\d{1,2})|(?:\d{1,2}))(?:\s|$))|(?:(?<modifier>next|last|n|l)?\s?(?:(?<weekday>monday|tuesday|wednesday|thursday|friday|saturday|sunday|mon|tue|wed|thu|fri|sat|sun|montag|dienstag|mittwoch|donnerstag|freitag|samstag|sonntag)?|(?<week>w(?:eek)?(?:\s\D|$)))?\s?)|(?:w(?:eek)?\s?(?<weekNum>[1-5]?[0-9])(?:\s|$))|(?:(?<month>Jan|Feb|Mar|Apr|Apr(?:il)?|May|June?|July?|Aug(?:gust)?|Sep(?:tember)?|Oct(?:ober)?|Nov|Dec)+)+\s?(?<dayOfMonth>(?:[1-9]|1[0-9]|2[0-9]|3[0-1])(?:\s|$))+)?(?:(?<flagPost>task|todo)\s)?(?<text>.*)$/;

this.expr = new RegExp(regExp);
}
Expand Down
33 changes: 29 additions & 4 deletions src/util/dates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,19 +77,44 @@ export function normalizeDayAsString(input: string, locale?: string): moment.Mom
day = day.toLowerCase();
moment.locale(locale);

return moment().day(day).weekday();
/*
// return moment().day(day).weekday(); this doesn't work as expected

let dayAsString = "";

if (day.match(/monday|mon|montag/)) { day }
if (day.match(/monday|mon|montag/)) { return 1; }
if (day.match(/tuesday|tue|dienstag/)) { return 2; }
if (day.match(/wednesday|wed|mittwoch/)) { return 3; }
if (day.match(/thursday|thu|donnerstag/)) { return 4; }
if (day.match(/friday|fri|freitag/)) { return 5; }
if (day.match(/saturday|sat|samstag/)) { return 6; }
if (day.match(/sunday|sun|sonntag/)) { return 7; }
return -1;
*/

}

/**
* Return day of week for given string.
*
* Update: Using momentjs to support locales (since first day of week differs internationally)
*/
export function getMonthForString(month: string): number {
month = month.toLowerCase();


if (month.match(/jan|january/)) { return 0; }
if (month.match(/feb|february/)) { return 1; }
if (month.match(/mar|march/)) { return 2; }
if (month.match(/apr|april/)) { return 3; }
if (month.match(/may/)) { return 4; }
if (month.match(/jun|june/)) { return 5; }
if (month.match(/jul|july/)) { return 6; }
if (month.match(/aug|august/)) { return 7; }
if (month.match(/sep|sept|september/)) { return 8; }
if (month.match(/oct|october/)) { return 9; }
if (month.match(/nov|november/)) { return 10; }
if (month.match(/dec|december/)) { return 11; }
return -1;

}


Expand Down
3 changes: 2 additions & 1 deletion src/util/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ export {
getDayOfWeekForString,
normalizeDayAsString,
replaceDateFormats,
replaceDateTemplatesWithMomentsFormats
replaceDateTemplatesWithMomentsFormats,
getMonthForString
}
from './dates';

Expand Down

0 comments on commit 6f4d4cf

Please sign in to comment.