Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added variable substitution commands #18

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 54 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,66 @@ Following commands are available:
- `Insert Timestamp` - Inserts current timestamp in milliseconds at the cursor position.
- `Insert Formatted DateTime` (<kbd>⇧</kbd>+<kbd>⌘</kbd>+<kbd>⌥</kbd>+<kbd>I</kbd> on OS X, <kbd>Ctrl</kbd>+<kbd>Alt</kbd>+<kbd>Shift</kbd>+<kbd>I</kbd> on Windows and Linux) - Prompt user for format and insert formatted date and/or time at the cursor position.

Each command is also available as a variable expansion, which is particularly useful for [task input variables](https://code.visualstudio.com/docs/editor/variables-reference#_input-variables).

- `insertDateString.getDateTime` - Returns current date and/or time according to configured format (`format`).
- `insertDateString.getDate` - Returns current date according to configured format (`formatDate`).
- `insertDateString.getTime` - Returns current time according to configured format (`formatTime`).
- `insertDateString.getTimestamp` - Returns current timestamp in milliseconds.
- `insertDateString.getOwnFormatDateTime` - Returns formatted date and/or time according to provided format.

``` json
{
"version": "2.0.0",
"tasks": [
{
"label": "print default datetime",
"type": "shell",
"command": "echo",
"args": ["${input:dateTime}"]
},
{
"label": "print custom datetime",
"type": "shell",
"command": "echo",
"args": ["${input:formatDateTime}"]
},
{
"label": "print prompted datetime",
"type": "shell",
"command": "echo",
"args": ["${input:promptedDateTime}"]
}
],
"inputs": [
{
"id": "dateTime",
"type": "command",
"command": "insertDateString.getDateTime"
},
{
"id": "formatDateTime",
"type": "command",
"command": "insertDateString.getOwnFormatDateTime",
"args": "hh:mm:ss YYYY-MM-DD"
},
{
"id": "promptedDateTime",
"type": "command",
"command": "insertDateString.getOwnFormatDateTime"
// notice lack of args here
}
]
}
```

## Available settings

- Date and time format string (_this affects `Insert DateTime` output_):
- Date format string (_this affects `Insert Date` output_):
- Time format string (_this affects `Insert Time` output_):

```
``` json
// Date format to be used.
"insertDateString.format": "YYYY-MM-DD hh:mm:ss",
"insertDateString.formatDate": "YYYY-MM-DD",
Expand Down
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,12 @@
"onCommand:insertDateString.insertDate",
"onCommand:insertDateString.insertTime",
"onCommand:insertDateString.insertTimestamp",
"onCommand:insertDateString.insertOwnFormatDateTime"
"onCommand:insertDateString.insertOwnFormatDateTime",
"onCommand:insertDateString.getDateTime",
"onCommand:insertDateString.getDate",
"onCommand:insertDateString.getTime",
"onCommand:insertDateString.getTimestamp",
"onCommand:insertDateString.getOwnFormatDateTime"
],
"categories": [
"Other",
Expand Down Expand Up @@ -127,4 +132,4 @@
"color": "#005696",
"theme": "dark"
}
}
}
57 changes: 51 additions & 6 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ function replaceEditorSelection(text: string) {
}

export function activate(context: ExtensionContext): void {
// User-facing commands (exposed in contributions)
context.subscriptions.push(
commands.registerCommand("insertDateString.insertDateTime", () =>
replaceEditorSelection(getFormattedDateString())
Expand Down Expand Up @@ -61,15 +62,59 @@ export function activate(context: ExtensionContext): void {
);

context.subscriptions.push(
commands.registerCommand("insertDateString.insertOwnFormatDateTime", () => {
window
.showInputBox({
commands.registerCommand("insertDateString.insertOwnFormatDateTime", async () => {
const format = await window.showInputBox({
value: getConfiguredFormat(),
prompt: INPUT_PROMPT,
});

if (format != null) {
replaceEditorSelection(getFormattedDateString(format));
}
})
);

// Can be used in task inputs and other variable substitutions
context.subscriptions.push(
commands.registerCommand("insertDateString.getDateTime", () => {
return getFormattedDateString();
})
);

context.subscriptions.push(
commands.registerCommand("insertDateString.getDate", () => {
return getFormattedDateString(getConfiguredFormat("formatDate"));
})
);

context.subscriptions.push(
commands.registerCommand("insertDateString.getTime", () => {
return getFormattedDateString(getConfiguredFormat("formatTime"));
})
);

context.subscriptions.push(
commands.registerCommand("insertDateString.getTimestamp", () => {
return new Date().getTime().toString();
})
);

context.subscriptions.push(
commands.registerCommand("insertDateString.getOwnFormatDateTime", async format => {
if (format != null) {
return getFormattedDateString(format);
} else {
const format = await window.showInputBox({
value: getConfiguredFormat(),
prompt: INPUT_PROMPT,
})
.then((format) => {
replaceEditorSelection(getFormattedDateString(format));
});

if (format != null) {
return getFormattedDateString(format);
} else {
return undefined;
}
}
})
);
}