-
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathdialogues.ts
More file actions
531 lines (417 loc) · 20.2 KB
/
Copy pathdialogues.ts
File metadata and controls
531 lines (417 loc) · 20.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
// 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';
import * as Path from 'path';
import { isNotNullOrUndefined, isString, isError, stringIsNotEmpty, denormalizeFilename } from '../../shared/index';
import { SCOPE_DEFAULT } from '../../shared/config/configuration';
import moment = require('moment');
import { IConfiguration, IFileSystem, ILogger, IParser, JournalPageType, Input, ScopeDirectory, NoteInput, SelectedInput, FileEntry } from '../../shared/model/index';
import { sortPickEntries, ScanEntries, TimedQuickPick, DecoratedQuickPickItem } from '../entries/scan-entries';
/**
* Anything which extends Visual Studio Code goes here
*
*/
export class Dialogues {
private scanner: ScanEntries;
constructor(private config: IConfiguration, private logger: ILogger, private parser: IParser, fs: IFileSystem) {
this.scanner = new ScanEntries(config, logger, fs);
}
public getScanner(): ScanEntries {
return this.scanner;
}
/**
*
*/
public async getUserInputWithValidation(): Promise<Input> {
return new Promise((resolve, reject) => {
const disposables: vscode.Disposable[] = [];
try {
// see https://github.com/Microsoft/vscode-extension-samples/blob/master/quickinput-sample/src/quickOpen.ts
const input: TimedQuickPick = vscode.window.createQuickPick<DecoratedQuickPickItem>();
input.start = new Date().getTime();
// FIXME: localize
input.show();
let today: DecoratedQuickPickItem = { label: vscode.l10n.t("Today"), description: vscode.l10n.t("Jump to today's entry."), pickItem: JournalPageType.entry, parsedInput: new Input(0), alwaysShow: true, path: "" };
let tomorrow: DecoratedQuickPickItem = { label: vscode.l10n.t("Tomorrow"), description: vscode.l10n.t("Jump to tomorrow's entry."), pickItem: JournalPageType.entry, parsedInput: new Input(1), alwaysShow: true, path: "" };
let pickEntry: DecoratedQuickPickItem = { label: vscode.l10n.t("Select entry"), description: vscode.l10n.t("Select from the last journal entries."), pickItem: JournalPageType.entry, alwaysShow: true, path: "" };
let pickNote: DecoratedQuickPickItem = { label: vscode.l10n.t("Select/Create a note"), description: vscode.l10n.t("Create a new note or select from recently created or updated notes."), pickItem: JournalPageType.note, alwaysShow: true, path: "" };
input.items = [today, tomorrow, pickEntry, pickNote];
let selected: DecoratedQuickPickItem | undefined;
input.onDidChangeValue(val => {
// remove placeholder if val is empty
if (val.length === 0) {
if (input.items[0].replace && input.items[0].replace === true) {
input.items = input.items.slice(1);
}
return;
} else {
this.parser.parseInput(val).then((parsed: Input) => {
// this is the placeholder, which gets continuously updated when the user types in anything
let item: DecoratedQuickPickItem = {
label: val,
path: "",
alwaysShow: true,
parsedInput: parsed,
replace: true,
description: this.generateDescription(parsed),
detail: this.generateDetail(parsed)
};
if (input.items[0].replace && input.items[0].replace === true) {
input.items = [item].concat(input.items.slice(1));
} else {
input.items = [item].concat(input.items);
}
}).catch(error => {
this.logger.trace("Warning: " + error);
// do nothin
});
}
}, disposables);
input.onDidChangeSelection(sel => {
selected = sel[0];
}, disposables);
input.onDidHide(() => {
input.dispose();
}, disposables);
input.onDidAccept(val => {
if (!selected) { return; }
if (isNotNullOrUndefined(selected.parsedInput)) {
resolve(selected.parsedInput as Input);
} else if (isNotNullOrUndefined(selected.pickItem) && selected.pickItem === JournalPageType.entry) {
this.pickItem(JournalPageType.entry).then(selected => {
resolve(selected);
});
} else if (isNotNullOrUndefined(selected.pickItem) && selected.pickItem === JournalPageType.note) {
this.pickItem(JournalPageType.note).then(selected => {
resolve(selected);
});
} else if (isNotNullOrUndefined(selected.pickItem) && selected.pickItem === JournalPageType.attachment) {
this.pickItem(JournalPageType.attachment).then(selected => {
resolve(selected);
});
} else {
this.parser.parseInput(selected.label).then(resolve);
}
}, disposables);
} catch (error) {
this.logger.error("Failed to get user input", error);
reject(error);
} finally {
disposables.forEach(d => d.dispose());
}
});
};
private generateDescription(parsed: Input): string {
moment.locale(this.config.getLocale());
let date = new Date();
date.setDate(date.getDate() + parsed.offset);
const dateStr = moment(date).format("ddd, LL");
if (parsed.confidence === 'resolved') { return `$(check) ${dateStr}`; }
if (parsed.confidence === 'ambiguous') { return `$(warning) ${dateStr}`; }
return dateStr;
}
private generateDetail(parsed: Input): string {
moment.locale(this.config.getLocale());
let date = new Date();
date.setDate(date.getDate() + parsed.offset);
let t: moment.Moment = moment(date);
let time: string = t.calendar(moment(), this.config.getInputDetailsTimeFormat());
if (parsed.hasWeek() && !parsed.hasTask()) {
return vscode.l10n.t("Open notes for week {week}", { week: parsed.week });
}
if (parsed.hasWeek() && parsed.hasTask()) {
return vscode.l10n.t("Add task to entry for week {week}", { week: parsed.week });
}
if (parsed.hasTask()) {
return vscode.l10n.t("Add task to entry {day}", { day: time });
}
if (parsed.hasMemo()) {
return vscode.l10n.t("Add memo to entry {day}", { day: time });
}
return vscode.l10n.t("Create or open entry {day}", { day: time });
}
private collectScanDirectories(type: JournalPageType): Set<ScopeDirectory> {
let baseDirectories: ScopeDirectory[] = [];
this.config.getScopes().forEach(scope => {
// let dir = this.config.getBasePath(scope);
let pattern = "";
if (type === JournalPageType.entry) {
pattern = this.config.getEntryPathPattern(scope);
}
else if (type === JournalPageType.note) {
pattern = this.config.getNotesPathPattern(scope);
}
// replace base and resolve
pattern = pattern.replace("${base}", this.config.getBasePath(scope));
pattern = Path.normalize(pattern);
// stop when date variables appear in path
let pathSegments: string[] = pattern.split(Path.sep);
let filteredSegments: string[] = [];
for (let segment of pathSegments) {
if (segment.startsWith("${")) { break; }
else { filteredSegments.push(segment); }
}
const directory = Path.join(...filteredSegments);
let scopedBaseDirectory: ScopeDirectory = {
path: directory,
scope: scope
};
if (stringIsNotEmpty(scopedBaseDirectory.path)) {
if (baseDirectories.findIndex(item => item.path === scopedBaseDirectory.path) === -1) {
baseDirectories.push(scopedBaseDirectory);
}
}
});
return new Set(baseDirectories);
}
/**
*
* @param type
*/
public async pickItem(type: JournalPageType): Promise<Input> {
return new Promise((resolve, reject) => {
const disposables: vscode.Disposable[] = [];
try {
// Fixme, identify scopes while typing and switch base path if needed
const input: TimedQuickPick = vscode.window.createQuickPick<DecoratedQuickPickItem>();
input.start = new Date().getTime();
input.matchOnDescription = true;
let selected: DecoratedQuickPickItem | undefined;
input.busy = true;
// collect directories to scan (including in scopes)
const directories = this.collectScanDirectories(type);
this.scanner.getPreviouslyAccessedFiles(this.config.getInputTimeThreshold(), addItemToPickList, input, type, directories);
input.show();
input.onDidChangeSelection(sel => {
selected = sel[0];
}, disposables);
// placeholder only for notes
if (type === JournalPageType.note) {
input.onDidChangeValue(val => {
if (val.length === 0) {
if (input.items[0].replace && input.items[0].replace === true) {
input.items = input.items.slice(1);
}
return;
} else {
let inputText = new NoteInput();
inputText.text = val;
this.parser.resolveNotePathForInput(inputText).then(path => {
inputText.path = path;
this.logger.debug("Tags in input string: [" + ((inputText.tags.length === 0) ? "" : inputText.tags) + "] and scope " + inputText.scope);
// infer description
let description: string = "";
if (inputText.scope === SCOPE_DEFAULT) {
description = "Create new note in default path";
} else {
description = "Create new note in scope \"" + inputText.scope + "\"";
}
if (inputText.tags.length > 0) {
description += " and tags " + inputText.tags;
}
let item: DecoratedQuickPickItem = {
label: inputText.text,
path: path,
alwaysShow: true,
replace: true,
parsedInput: inputText,
description: description
};
if (input.items.length > 0 && input.items[0].replace && input.items[0].replace === true) {
input.items = [item].concat(input.items.slice(1));
} else {
input.items = [item].concat(input.items);
}
});
}
}, disposables);
}
input.onDidAccept(() => {
if (isNotNullOrUndefined(selected)) {
if (isNotNullOrUndefined(selected!.parsedInput)) {
resolve(selected!.parsedInput!);
} else {
resolve(new SelectedInput(selected!.path));
}
} else { reject("cancel"); }
}, disposables);
} catch (error) {
this.logger.error("Failed to pick item", error);
reject(error);
} finally {
disposables.forEach(d => d.dispose());
}
});
}
/**
* Simple method to have Q Promise for vscode API call to get user input
*/
public async getUserInput(tip: string): Promise<string> {
this.logger.trace("Entering getUserInput() in ext/vscode.ts");
const value = await vscode.window.showInputBox({ prompt: tip });
if (isNotNullOrUndefined(value) && value!.length > 0) {
return value!;
}
this.logger.debug("User canceled");
throw new Error("cancel");
}
public async saveDocument(textDocument: vscode.TextDocument): Promise<vscode.TextDocument> {
if (!textDocument.isDirty) { return textDocument; }
const isSaved = await textDocument.save();
if (isSaved) { return textDocument; }
throw new Error("Failed to save file with path: " + textDocument.fileName);
}
public async openDocument(path: string | vscode.Uri): Promise<vscode.TextDocument> {
if (!(path instanceof vscode.Uri)) { path = vscode.Uri.file(path); }
return vscode.workspace.openTextDocument(path);
}
/**
* Shows the given document in Visual Studio Code
*
* @param {vscode.TextDocument} textDocument the document to show
* @returns {vscode.TextEditor} the associated text editor
* @memberOf VsCode
*/
public async showDocument(textDocument: vscode.TextDocument): Promise<vscode.TextEditor> {
this.logger.trace("Entering showDocument() in ext/vscode.ts for document: ", textDocument.fileName);
if (textDocument.isDirty) { textDocument.save(); }
const existingEditor = vscode.window.visibleTextEditors.find(
editor => textDocument.fileName.startsWith(editor.document.fileName)
);
if (existingEditor) {
this.logger.debug("Document ", textDocument.fileName, " is already opened.");
return existingEditor;
}
const col = this.config.isOpenInNewEditorGroup() ? 2 : 1;
const view = await vscode.window.showTextDocument(textDocument, col, false);
vscode.commands.executeCommand("cursorMove", {
to: "down",
by: "line",
value: textDocument.lineCount
});
this.logger.debug("Showed document ", textDocument.fileName);
return view;
}
public async showError(error: string | Error): Promise<void> {
if (isString(error)) {
this.showErrorInternal(error as string);
}
if (isError(error)) {
this.showErrorInternal((error as Error).message);
}
}
private showErrorInternal(errorMessage: string): void {
let hint = "Open the logs to see details.";
vscode.window.showErrorMessage(errorMessage, hint)
.then(clickedHint => {
this.logger.showChannel();
});
}
}
/**
* Callback function for filewalker to add an item to our quickpick list.
*
* Note: This is a trigger as callback, has no access to current class members
*
* @param fe
*/
function addItemToPickList(entries: FileEntry[], input: TimedQuickPick, type: JournalPageType) {
const items: DecoratedQuickPickItem[] = [];
entries.forEach(fe => {
Object.freeze(fe); // immutable
if (fe.type !== type) { return; }
// check if already present
if (input.items.findIndex(item => fe.path === item.path) >= 0) { return; }
let displayName = fe.name;
// if it's a journal page, we prefix the month for visualizing
if (type === JournalPageType.entry) {
let pathItems = fe.path.split(Path.sep);
displayName = pathItems[pathItems.length - 2] + Path.sep + pathItems[pathItems.length - 1];
}
// if it's a note, we denormalize the displayed name
if (type === JournalPageType.note) {
displayName = denormalizeFilename(fe.name);
}
/* and we prefix the scope (#122)
if (fe.scope && fe.scope.length > 0 && fe.scope !== J.VSCode.SCOPE_DEFAULT) {
fe.name = `#${fe.scope} ${fe.name}`;
}*/
// add icons
switch (fe.type) {
case JournalPageType.note: {
if (fe.scope === SCOPE_DEFAULT) { displayName = `$(circle-large-outline) ${displayName}`; break; }
else { displayName = `$(circle-large-filled) ${displayName}`; break; }
}
case JournalPageType.entry: displayName = `$(clock) ${displayName}`; break;
case JournalPageType.attachment: displayName = `$(package) ${displayName}`; break;
}
// format description
// if its with the last week, we just print the weekday..
let displayDescription = "";
try {
let displayDate = moment(fe.createdAt);
if (displayDate.isAfter(moment().subtract(7, "d"))) {
displayDescription += displayDate.format(vscode.l10n.t("[from] dddd"));
} else {
displayDescription += displayDate.format(vscode.l10n.t("[from] ll"));
}
} catch (error) {
console.error("Failed to extract date from entry with name: ", displayName, error);
}
if (fe.scope !== SCOPE_DEFAULT) {
displayDescription += ` | #${fe.scope}`;
}
let item: DecoratedQuickPickItem = {
label: displayName,
path: fe.path,
fileEntry: fe,
description: displayDescription
};
items.push(item);
});
/* Sorted insertion into input.items via binary search (#187): avoids the O(n² log n)
cost of re-sorting the growing items array on every directory-level callback. */
if (items.length > 0) {
const merged = Array.from(input.items);
for (const item of items) {
if (!item.fileEntry) {
merged.push(item);
continue;
}
let lo = 0;
let hi = merged.length;
while (lo < hi) {
const mid = (lo + hi) >>> 1;
const midEntry = merged[mid].fileEntry;
// items without fileEntry (the placeholder rows) stay at the front
if (!midEntry || sortPickEntries(midEntry, item.fileEntry) <= 0) {
lo = mid + 1;
} else {
hi = mid;
}
}
merged.splice(lo, 0, item);
}
input.items = merged;
}
/* Some voodoo to stop the spinner. Since it's a mess to find out when the recursive directory walker is finished, we simply finish after 3 seconds. */
if ((input.items.length > 20) || (((new Date().getTime()) - input.start!) > 3000)) {
input.busy = false;
}
}