-
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathmigrate-tasks.ts
More file actions
84 lines (69 loc) · 3.06 KB
/
Copy pathmigrate-tasks.ts
File metadata and controls
84 lines (69 loc) · 3.06 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
// Copyright (C) 2021 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 { JournalController } from '../../../shared/model/index';
/**
* The migrate task codelens is only active for the tasks header (as configured in settings, default is '## Tasks') for today's journal entry.
*
* Once activated, it scans the previous entries (last 20 entries) and copies any open tasks to today's entry.
* For each identified open task, it will trigger the according shift action for the given range.
*
*
*/
export class MigrateTasksCodeLens implements vscode.CodeLensProvider {
private codeLenses: vscode.CodeLens[] = [];
private ctrl: JournalController;
private _onDidChangeCodeLenses: vscode.EventEmitter<void> = new vscode.EventEmitter<void>();
constructor(ctrl: JournalController) {
this.ctrl = ctrl;
vscode.workspace.onDidChangeConfiguration((_) => {
this._onDidChangeCodeLenses.fire();
});
}
async getRegex() : Promise<RegExp> {
let template = await this.ctrl.config.getTaskInlineTemplate();
return new RegExp(template.after);
}
public async provideCodeLenses(document: vscode.TextDocument, token: vscode.CancellationToken): Promise<vscode.CodeLens[]> {
this.codeLenses = [];
const regex = await this.getRegex();
const text = document.getText();
const matches = regex.exec(text);
if(matches !== null) {
const line = document.lineAt(document.positionAt(matches.index).line);
const indexOf = line.text.indexOf(matches[0]);
const position = new vscode.Position(line.lineNumber, indexOf);
const range = document.getWordRangeAtPosition(position, regex);
if (range) {
this.codeLenses.push(new vscode.CodeLens(range));
}
}
return this.codeLenses;
}
public resolveCodeLens?(codeLens: vscode.CodeLens, token: vscode.CancellationToken):
vscode.CodeLens | Thenable<vscode.CodeLens> {
codeLens.command = {
title: "Collect all tasks",
tooltip: "Collect and modify tasks from this and previous journal entries",
command: "codelens-sample.codelensAction",
arguments: ["Argument 1", false]
};
return codeLens;
}
}