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

Commit d208936

Browse files
committed
added 'quick execution' feature
1 parent 8542447 commit d208936

File tree

10 files changed

+812
-20
lines changed

10 files changed

+812
-20
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+
# 4.0.0 (April 29th, 2017; quick JavaScript execution)
4+
5+
* added `Script commands: Quick execution` command that can [execute JavaScript code quickly](https://github.com/mkloubert/vs-script-commands#quick-execution-) ... enter `$help` as first action to get information about available functions and variables
6+
37
# 3.0.0 (April 21st, 2017; execute command before save document)
48

59
* added `onWillSave` setting for commands, which indicates to invoke commands if a file is going to be saved

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Adds additional commands to [Visual Studio Code](https://code.visualstudio.com/)
1818
* [Key bindinds](#key-bindinds-)
1919
* [Invoke manually](#invoke-manually-)
2020
* [Status bar buttons](#status-bar-buttons-)
21+
* [Quick execution](#quick-execution-)
2122
3. [Documentation](#documentation-)
2223

2324
## Install [[↑](#table-of-contents)]
@@ -221,6 +222,7 @@ Press `F1` to open the list of commands and enter one of the following commands:
221222
| ---- | ---- | --------- |
222223
| `Script commands: Execute command` | Executes a command defined by that extension. | `extension.scriptCommands.execute` |
223224
| `Script commands: Execute VSCode command` | Executes another command that is available in VSCode. | `extension.scriptCommands.executeVSCode` |
225+
| `Script commands: Quick execution` | Executes a JavaScript expression quickly. | `extension.scriptCommands.quickExecution` |
224226

225227
### Status bar buttons [[↑](#how-to-use-)]
226228

@@ -255,6 +257,12 @@ You can activate buttons for your commands in the status bar, by defining the `b
255257
| `text` | The caption for the button. |
256258
| `tooltip` | The tooltip for the button. |
257259

260+
### Quick execution [[↑](#how-to-use-)]
261+
262+
Press `F1` to open the list of commands and select `Script commands: Quick execution` to execute any JavaScript expression (execute `$help` to open a help tab which lists all available features, like functions and variables):
263+
264+
![Demo Quick execution](https://raw.githubusercontent.com/mkloubert/vs-script-commands/master/img/demo4.gif)
265+
258266
## Documentation [[↑](#table-of-contents)]
259267

260268
The full API documentation can be found [here](https://mkloubert.github.io/vs-script-commands/).

img/demo4.gif

2.12 MB
Loading

package.json

Lines changed: 54 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
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": "3.0.0",
5+
"version": "4.0.0",
66
"publisher": "mkloubert",
77
"engines": {
88
"vscode": "^1.6.0"
@@ -33,6 +33,11 @@
3333
"command": "extension.scriptCommands.executeVSCode",
3434
"title": "Execute VSCode command",
3535
"category": "Script commands"
36+
},
37+
{
38+
"command": "extension.scriptCommands.quickExecution",
39+
"title": "Quick execution",
40+
"category": "Script commands"
3641
}
3742
],
3843
"configuration": {
@@ -142,10 +147,10 @@
142147
"default": false
143148
},
144149
"onFileClosed": {
145-
"type": "boolean",
146-
"description": "Is invoked when a file has been closed.",
147-
"default": false
148-
},
150+
"type": "boolean",
151+
"description": "Is invoked when a file has been closed.",
152+
"default": false
153+
},
149154
"onFileDeleted": {
150155
"type": "boolean",
151156
"description": "Is invoked when a file has been deleted.",
@@ -195,8 +200,9 @@
195200
"default": false
196201
}
197202
},
198-
"required":[
199-
"id", "script"
203+
"required": [
204+
"id",
205+
"script"
200206
]
201207
},
202208
{
@@ -353,8 +359,9 @@
353359
"default": false
354360
}
355361
},
356-
"required":[
357-
"id", "script"
362+
"required": [
363+
"id",
364+
"script"
358365
]
359366
}
360367
}
@@ -363,6 +370,29 @@
363370
"globals": {
364371
"description": "Global data available for ALL commands defined by that extension."
365372
},
373+
"quick": {
374+
"description": "Settings for 'quick execution'.",
375+
"type": "object",
376+
"properties": {
377+
"cwd": {
378+
"description": "The initial current directory for the executions.",
379+
"type": "string"
380+
},
381+
"noResultInfo": {
382+
"description": "Show result of execution or not.",
383+
"type": "boolean",
384+
"default": false
385+
},
386+
"showResultInTab": {
387+
"description": "Show results in tab instead of a popup or not.",
388+
"type": "boolean",
389+
"default": false
390+
},
391+
"state": {
392+
"description": "The initial state value."
393+
}
394+
}
395+
},
366396
"showOutput": {
367397
"type": "boolean",
368398
"description": "Show output on startup or not.",
@@ -385,11 +415,16 @@
385415
"test": "node ./node_modules/vscode/bin/test"
386416
},
387417
"devDependencies": {
388-
"typescript": "^2.0.3",
389-
"vscode": "^1.0.0",
390-
"mocha": "^2.3.3",
418+
"@types/fs-extra": "^2.1.0",
419+
"@types/glob": "^5.0.30",
420+
"@types/html-entities": "^1.2.15",
421+
"@types/marked": "0.0.28",
422+
"@types/mocha": "^2.2.32",
391423
"@types/node": "^6.0.40",
392-
"@types/mocha": "^2.2.32"
424+
"@types/tmp": "0.0.32",
425+
"mocha": "^2.3.3",
426+
"typescript": "^2.0.3",
427+
"vscode": "^1.0.0"
393428
},
394429
"icon": "icon.png",
395430
"author": {
@@ -404,6 +439,11 @@
404439
},
405440
"readmeFilename": "README.md",
406441
"dependencies": {
407-
"moment": "^2.17.1"
442+
"fs-extra": "^3.0.0",
443+
"glob": "^7.1.1",
444+
"html-entities": "^1.2.1",
445+
"marked": "^0.3.6",
446+
"moment": "^2.17.1",
447+
"tmp": "0.0.31"
408448
}
409449
}

src/content.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ export class HtmlTextDocumentContentProvider implements vscode.TextDocumentConte
7878
for (let i = 0; i < htmlDocs.length; i++) {
7979
let d = htmlDocs[i];
8080

81-
if (sc_helpers.toStringSafe(d.id).trim() == id) {
81+
if (sc_helpers.toStringSafe(d.id).trim() === id) {
8282
doc = d;
8383
break;
8484
}
@@ -90,7 +90,7 @@ export class HtmlTextDocumentContentProvider implements vscode.TextDocumentConte
9090
if (doc) {
9191
if (doc.body) {
9292
let enc = sc_helpers.normalizeString(doc.encoding);
93-
if (!enc) {
93+
if ('' === enc) {
9494
enc = 'utf8';
9595
}
9696

src/contracts.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,27 @@ export interface Configuration {
3939
* Global data available for ALL commands defined by that extension.
4040
*/
4141
globals?: GlobalVariables;
42+
/**
43+
* Settings for "quick execution".
44+
*/
45+
quick?: {
46+
/**
47+
* The initial current directory for the executions.
48+
*/
49+
cwd?: string;
50+
/**
51+
* Show result of execution or not.
52+
*/
53+
noResultInfo?: boolean;
54+
/**
55+
* Show results in tab instead of a popup or not.
56+
*/
57+
showResultInTab?: boolean;
58+
/**
59+
* The initial state value.
60+
*/
61+
state?: any;
62+
};
4263
/**
4364
* Open output on startup or not.
4465
*/

src/controller.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import * as Moment from 'moment';
2828
import * as Path from 'path';
2929
import * as sc_contracts from './contracts';
3030
import * as sc_helpers from './helpers';
31+
import * as sc_quick from './quick';
3132
import * as vscode from 'vscode';
3233

3334

@@ -154,6 +155,13 @@ export class ScriptCommandController extends Events.EventEmitter implements vsco
154155
return this._config || {};
155156
}
156157

158+
/**
159+
* Gets the underlying extension context.
160+
*/
161+
public get context(): vscode.ExtensionContext {
162+
return this._CONTEXT;
163+
}
164+
157165
/** @inheritdoc */
158166
public dispose() {
159167
try {
@@ -589,7 +597,7 @@ export class ScriptCommandController extends Events.EventEmitter implements vsco
589597
}
590598

591599
/**
592-
* Is invoked when a document is being to be saved.
600+
* Is invoked when a document is going to be saved.
593601
*
594602
* @param {vscode.TextDocumentWillSaveEvent} e The event data.
595603
*/
@@ -598,6 +606,20 @@ export class ScriptCommandController extends Events.EventEmitter implements vsco
598606
() => [ e ]);
599607
}
600608

609+
/**
610+
* Opens a HTML document in a new tab.
611+
*
612+
* @param {string} html The HTML document (source code).
613+
* @param {string} [title] The custom title for the tab.
614+
* @param {any} [id] The custom ID for the document in the storage.
615+
*
616+
* @returns {Promise<any>} The promise.
617+
*/
618+
public openHtml(html: string, title?: string, id?: any): Promise<any> {
619+
return sc_helpers.openHtmlDocument(this.htmlDocuments,
620+
html, title, id);
621+
}
622+
601623
/**
602624
* Gets the global output channel.
603625
*/
@@ -612,6 +634,14 @@ export class ScriptCommandController extends Events.EventEmitter implements vsco
612634
return this._PACKAGE_FILE;
613635
}
614636

637+
/**
638+
* Does a "quick execution".
639+
*/
640+
public quickExecution() {
641+
return sc_quick.quickExecution
642+
.apply(this, arguments);
643+
}
644+
615645
/**
616646
* Reloads the commands.
617647
*/
@@ -983,6 +1013,10 @@ export class ScriptCommandController extends Events.EventEmitter implements vsco
9831013

9841014
me.reloadCommands();
9851015

1016+
// reset all "quick" stuff
1017+
sc_quick.reset
1018+
.apply(me, []);
1019+
9861020
// startup commands
9871021
let commandsToExecute = me.getCommands().filter(x => {
9881022
return sc_helpers.toBooleanSafe(x.onStartup);

src/extension.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,16 @@ export function activate(context: vscode.ExtensionContext) {
8686
}
8787
});
8888

89+
// quick JavaScript execution
90+
let quickExecution = vscode.commands.registerCommand('extension.scriptCommands.quickExecution', () => {
91+
try {
92+
controller.quickExecution();
93+
}
94+
catch (e) {
95+
vscode.window.showErrorMessage(`[QUICK EXECUTION ERROR]: ${sc_helpers.toStringSafe(e)}`);
96+
}
97+
});
98+
8999
// open HTML document
90100
let openHtmlDoc = vscode.commands.registerCommand('extension.scriptCommands.openHtmlDoc', (doc: sc_contracts.Document) => {
91101
try {
@@ -137,7 +147,7 @@ export function activate(context: vscode.ExtensionContext) {
137147
controller));
138148

139149
context.subscriptions.push(controller,
140-
executeCmd, executeVSCmd, openHtmlDoc,
150+
executeCmd, executeVSCmd, quickExecution, openHtmlDoc,
141151
htmlViewer);
142152

143153
controller.onActivated();

src/helpers.ts

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ export function getUrlParam(params: Object, name: string): string {
148148
name = normalizeString(name);
149149

150150
for (let p in params) {
151-
if (normalizeString(p) == name) {
151+
if (normalizeString(p) === name) {
152152
return toStringSafe(params[p]);
153153
}
154154
}
@@ -164,7 +164,7 @@ export function getUrlParam(params: Object, name: string): string {
164164
* @return {boolean} Is empty or not.
165165
*/
166166
export function isEmptyString(val: any): boolean {
167-
return '' == toStringSafe(val).trim();
167+
return '' === toStringSafe(val).trim();
168168
}
169169

170170
/**
@@ -317,6 +317,24 @@ export function removeDocuments(docs: sc_contracts.Document | sc_contracts.Docum
317317
return removed;
318318
}
319319

320+
/**
321+
* Replaces all occurrences of a string.
322+
*
323+
* @param {any} str The input string.
324+
* @param {any} searchValue The value to search for.
325+
* @param {any} replaceValue The value to replace 'searchValue' with.
326+
*
327+
* @return {string} The output string.
328+
*/
329+
export function replaceAllStrings(str: any, searchValue: any, replaceValue: any) {
330+
str = toStringSafe(str);
331+
searchValue = toStringSafe(searchValue);
332+
replaceValue = toStringSafe(replaceValue);
333+
334+
return str.split(searchValue)
335+
.join(replaceValue);
336+
}
337+
320338
/**
321339
* Sorts a list of commands.
322340
*
@@ -372,6 +390,31 @@ export function sortCommands(pkgs: sc_contracts.ScriptCommand[],
372390
.map(x => x.value);
373391
}
374392

393+
/**
394+
* Returns an array like object as new array.
395+
*
396+
* @param {ArrayLike<T>} arr The input object.
397+
* @param {boolean} [normalize] Returns an empty array, if input object is (null) / undefined.
398+
*
399+
* @return {T[]} The input object as array.
400+
*/
401+
export function toArray<T>(arr: ArrayLike<T>, normalize = true): T[] {
402+
if (isNullOrUndefined(arr)) {
403+
if (toBooleanSafe(normalize)) {
404+
return [];
405+
}
406+
407+
return <any>arr;
408+
}
409+
410+
let newArray: T[] = [];
411+
for (let i = 0; i < arr.length; i++) {
412+
newArray.push(arr[i]);
413+
}
414+
415+
return newArray;
416+
}
417+
375418
/**
376419
* Converts a value to a boolean.
377420
*

0 commit comments

Comments
 (0)