-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathscheduling.ts
106 lines (88 loc) · 2.79 KB
/
scheduling.ts
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
import * as vscode from "vscode";
import { WindowMessage } from "./showMessage";
module.exports = function () {
const { activeTextEditor } = vscode.window;
if (!activeTextEditor || activeTextEditor.document.languageId !== "vso") {
return;
}
const { document } = activeTextEditor;
const position: number = activeTextEditor.selection.active.line;
const currentLine: vscode.TextLine = document.lineAt(position);
let workspaceEdit = new vscode.WorkspaceEdit();
const config = vscode.workspace.getConfiguration("vsorg");
const dateFormat = config.get<string[]>("dateFormat");
// TODO: make a calendar widget
// Messages
const fullDateMessage = new WindowMessage(
"warning",
"Full date must be entered",
false,
false
);
const notADateMessage = new WindowMessage(
"warning",
"That's not a valid date.",
false,
false
);
if (currentLine.text.includes("SCHEDULED:")) {
const removeScheduled = currentLine.text
.replace(/\b(SCHEDULED)\b(.*)/, "")
.trimRight();
workspaceEdit.delete(document.uri, currentLine.range);
workspaceEdit.insert(
document.uri,
currentLine.range.start,
removeScheduled
);
return vscode.workspace.applyEdit(workspaceEdit);
}
async function getInput(
prompt: string,
placeHolder: string
): Promise<string | undefined> {
return await vscode.window.showInputBox({ prompt, placeHolder });
}
(async function () {
const year = await getInput(
"Enter in number format, the year you would like this to be scheduled",
"Year ex. 2018"
);
if (!year || year.length !== 4) {
return fullDateMessage.showMessage();
}
const month = await getInput(
"Enter in number format, the month you would like this to be scheduled",
"Month ex. 08 => August"
);
if (!month || month.length > 2) {
return fullDateMessage.showMessage();
}
const day = await getInput(
"Enter in number format, the day you would like this to be scheduled.",
"Day ex. 08 => the eighth"
);
if (
!day ||
day.length > 2 ||
daysInMonth(parseInt(month), parseInt(year)) < parseInt(day)
) {
return notADateMessage.showMessage();
}
const formattedDate =
dateFormat?.[0] === "MM-DD-YYYY"
? `${month}-${day}-${year}`
: `${day}-${month}-${year}`;
workspaceEdit.delete(document.uri, currentLine.range);
workspaceEdit.insert(
document.uri,
currentLine.range.start,
`${currentLine.text} SCHEDULED: [${formattedDate}]`
);
await vscode.workspace.applyEdit(workspaceEdit);
vscode.commands.executeCommand("workbench.action.files.save");
})();
function daysInMonth(month: number, year: number): number {
return new Date(year, month, 0).getDate();
}
};