Skip to content
This repository was archived by the owner on Jan 10, 2019. It is now read-only.

Commit 8542447

Browse files
committed
added onWillSave setting
1 parent 1b9002d commit 8542447

File tree

6 files changed

+45
-3
lines changed

6 files changed

+45
-3
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Change Log (vs-script-commands)
22

3+
# 3.0.0 (April 21st, 2017; execute command before save document)
4+
5+
* added `onWillSave` setting for commands, which indicates to invoke commands if a file is going to be saved
6+
37
## 2.0.1 (April 11th, 2017; improved execution of scripts)
48

59
* the behavior of executing scripts has been improved ... if you come from version 1.x, have a look at the [wiki](https://github.com/mkloubert/vs-script-commands/wiki#since-version-2x-) first

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,14 +199,15 @@ A command entry provides the following properties:
199199
| `onNewFile` | Is invoked when a file has been created. Default `(false)` |
200200
| `onSaved` | Is invoked when a file has been saved. Default `(false)` |
201201
| `onStartup` | Executes the command on startup or not. Default `(false)` |
202+
| `onWillSave` | Is invoked when a file is going to be saved. Default `(false)` |
202203
| `options` | Additional data for the execution. |
203204
| `script` | The path to the script to execute. IF YOU USE A RELATIVE PATH: The path is relative to your workspace. |
204205
| `sortOrder` | The sort order (for the GUI). Default `0` |
205206
| `suppressArguments` | Supress own arguments of the extension or not. Default `(false)` |
206207

207208
### Key bindinds [[↑](#how-to-use-)]
208209

209-
After defining one or more commands, you can open your [keybindings.json](https://code.visualstudio.com/docs/customization/keybindings#_customizing-shortcuts) file and set shortcuts for them, by selecting `File > Preferences > Keyboard Shortcuts` (`Code > Preferences > Keyboard Shortcuts` on Mac) in your editor:
210+
After defining one or more commands, you can open your [keybindings.json](https://code.visualstudio.com/docs/getstarted/keybindings#_advanced-customization) file and set shortcuts for them, by selecting `File > Preferences > Keyboard Shortcuts` (`Code > Preferences > Keyboard Shortcuts` on Mac) in your editor:
210211

211212
![Demo Key bindinds](https://raw.githubusercontent.com/mkloubert/vs-script-commands/master/img/demo1.gif)
212213

package.json

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
"name": "vs-script-commands",
33
"displayName": "Script Commands",
44
"description": "Adds additional commands to Visual Studio Code that uses scripts (JavaScript) for execution.",
5-
"version": "2.1.2",
5+
"version": "3.0.0",
66
"publisher": "mkloubert",
77
"engines": {
8-
"vscode": "^1.5.0"
8+
"vscode": "^1.6.0"
99
},
1010
"license": "MIT",
1111
"categories": [
@@ -171,6 +171,11 @@
171171
"description": "Executes the command on startup or not.",
172172
"default": false
173173
},
174+
"onWillSave": {
175+
"type": "boolean",
176+
"description": "Is invoked when a file is going to be saved.",
177+
"default": false
178+
},
174179
"options": {
175180
"description": "Additional data for the execution."
176181
},
@@ -324,6 +329,11 @@
324329
"description": "Executes the command on startup or not.",
325330
"default": false
326331
},
332+
"onWillSave": {
333+
"type": "boolean",
334+
"description": "Is invoked when a file is going to be saved.",
335+
"default": false
336+
},
327337
"options": {
328338
"description": "Additional data for the execution."
329339
},

src/contracts.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,10 @@ export enum FileChangeType {
138138
* File has been changed in text editor.
139139
*/
140140
EditorChanged = 6,
141+
/**
142+
* File is going to be saved.
143+
*/
144+
WillSave = 7,
141145
}
142146

143147
/**
@@ -272,6 +276,10 @@ export interface ScriptCommand {
272276
* Executes the command on startup or not.
273277
*/
274278
onStartup?: boolean;
279+
/**
280+
* Is invoked when a file is being to be saved.
281+
*/
282+
onWillSave?: boolean;
275283
/**
276284
* Additional data for the execution.
277285
*/

src/controller.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,10 @@ export class ScriptCommandController extends Events.EventEmitter implements vsco
550550
case sc_contracts.FileChangeType.EditorChanged:
551551
doesMatch = sc_helpers.toBooleanSafe(c.onEditorChanged);
552552
break;
553+
554+
case sc_contracts.FileChangeType.WillSave:
555+
doesMatch = sc_helpers.toBooleanSafe(c.onWillSave);
556+
break;
553557
}
554558

555559
return doesMatch;
@@ -584,6 +588,16 @@ export class ScriptCommandController extends Events.EventEmitter implements vsco
584588
});;
585589
}
586590

591+
/**
592+
* Is invoked when a document is being to be saved.
593+
*
594+
* @param {vscode.TextDocumentWillSaveEvent} e The event data.
595+
*/
596+
public onWillSaveTextDocument(e: vscode.TextDocumentWillSaveEvent) {
597+
this.onFileChange(e.document.uri, sc_contracts.FileChangeType.WillSave,
598+
() => [ e ]);
599+
}
600+
587601
/**
588602
* Gets the global output channel.
589603
*/

src/extension.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,11 @@ export function activate(context: vscode.ExtensionContext) {
121121
// notifiy on document has been saved
122122
context.subscriptions.push(vscode.workspace.onDidSaveTextDocument(controller.onDidSaveTextDocument,
123123
controller));
124+
125+
// notifiy on document is going to be saved
126+
context.subscriptions.push(vscode.workspace.onWillSaveTextDocument(controller.onWillSaveTextDocument,
127+
controller));
128+
124129
// notfiy open text editor
125130
context.subscriptions.push(vscode.workspace.onDidOpenTextDocument(controller.onDidOpenTextDocument,
126131
controller));

0 commit comments

Comments
 (0)