mirror of
https://github.com/microsoft/vscode.git
synced 2026-04-22 17:48:56 +01:00
Support to run the selected script in the editor
This commit is contained in:
@@ -9,10 +9,14 @@ import * as vscode from 'vscode';
|
||||
|
||||
import { addJSONProviders } from './features/jsonContributions';
|
||||
import { NpmScriptsTreeDataProvider } from './npmView';
|
||||
import { provideNpmScripts, invalidateScriptsCache } from './tasks';
|
||||
import { provideNpmScripts, invalidateScriptsCache, findScriptAtPosition, createTask } from './tasks';
|
||||
|
||||
import * as nls from 'vscode-nls';
|
||||
|
||||
let taskProvider: vscode.Disposable | undefined;
|
||||
|
||||
const localize = nls.loadMessageBundle();
|
||||
|
||||
export async function activate(context: vscode.ExtensionContext): Promise<void> {
|
||||
taskProvider = registerTaskProvider(context);
|
||||
const treeDataProvider = registerExplorer(context);
|
||||
@@ -32,6 +36,7 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
|
||||
}
|
||||
});
|
||||
context.subscriptions.push(addJSONProviders(httpRequest.xhr));
|
||||
context.subscriptions.push(vscode.commands.registerCommand('npm.runScriptFromSource', runScriptFromSource));
|
||||
}
|
||||
|
||||
function registerTaskProvider(context: vscode.ExtensionContext): vscode.Disposable | undefined {
|
||||
@@ -70,6 +75,29 @@ function configureHttpRequest() {
|
||||
httpRequest.configure(httpSettings.get<string>('proxy', ''), httpSettings.get<boolean>('proxyStrictSSL', true));
|
||||
}
|
||||
|
||||
async function runScriptFromSource() {
|
||||
let editor = vscode.window.activeTextEditor;
|
||||
if (!editor) {
|
||||
return;
|
||||
}
|
||||
let document = editor.document;
|
||||
let contents = document.getText();
|
||||
let selection = editor.selection;
|
||||
let offset = document.offsetAt(selection.anchor);
|
||||
let script = findScriptAtPosition(contents, offset);
|
||||
if (script) {
|
||||
let uri = document.uri;
|
||||
let folder = vscode.workspace.getWorkspaceFolder(uri);
|
||||
if (folder) {
|
||||
let task = createTask(script, `run ${script}`, folder, uri);
|
||||
vscode.tasks.executeTask(task);
|
||||
}
|
||||
} else {
|
||||
let message = localize('noScriptFound', 'Could not find a script at the selection.');
|
||||
vscode.window.showErrorMessage(message);
|
||||
}
|
||||
}
|
||||
|
||||
export function deactivate(): void {
|
||||
if (taskProvider) {
|
||||
taskProvider.dispose();
|
||||
|
||||
@@ -304,6 +304,45 @@ async function findAllScripts(buffer: string): Promise<StringMap> {
|
||||
return scripts;
|
||||
}
|
||||
|
||||
export function findScriptAtPosition(buffer: string, offset: number): string | undefined {
|
||||
let script: string | undefined = undefined;
|
||||
let inScripts = false;
|
||||
let scriptStart: number | undefined;
|
||||
|
||||
let visitor: JSONVisitor = {
|
||||
onError(_error: ParseErrorCode, _offset: number, _length: number) {
|
||||
// TODO: inform user about the parse error
|
||||
},
|
||||
onObjectEnd() {
|
||||
if (inScripts) {
|
||||
inScripts = false;
|
||||
scriptStart = undefined;
|
||||
}
|
||||
},
|
||||
onLiteralValue(value: any, nodeOffset: number, nodeLength: number) {
|
||||
if (inScripts && scriptStart) {
|
||||
if (offset >= scriptStart && offset < nodeOffset + nodeLength) {
|
||||
// found the script
|
||||
inScripts = false;
|
||||
} else {
|
||||
script = undefined;
|
||||
}
|
||||
}
|
||||
},
|
||||
onObjectProperty(property: string, nodeOffset: number, nodeLength: number) {
|
||||
if (property === 'scripts') {
|
||||
inScripts = true;
|
||||
}
|
||||
else if (inScripts) {
|
||||
scriptStart = nodeOffset;
|
||||
script = property;
|
||||
}
|
||||
}
|
||||
};
|
||||
visit(buffer, visitor);
|
||||
return script;
|
||||
}
|
||||
|
||||
export async function getScripts(packageJsonUri: Uri): Promise<StringMap | undefined> {
|
||||
|
||||
if (packageJsonUri.scheme !== 'file') {
|
||||
|
||||
Reference in New Issue
Block a user