forked from irinazheltisheva/vscode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscriptHover.ts
More file actions
118 lines (103 loc) · 3.8 KB
/
scriptHover.ts
File metadata and controls
118 lines (103 loc) · 3.8 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import {
ExtensionContext, TextDocument, commands, ProviderResult, CancellationToken,
workspace, tasks, Range, HoverProvider, Hover, Position, MarkdownString, Uri
} from 'vscode';
import {
createTask, startDebugging, findAllScriptRanges
} from './tasks';
import * as nls from 'vscode-nls';
const localize = nls.loadMessageBundle();
let cachedDocument: Uri | undefined = undefined;
let cachedScriptsMap: Map<string, [number, number, string]> | undefined = undefined;
export function invalidateHoverScriptsCache(document?: TextDocument) {
if (!document) {
cachedDocument = undefined;
return;
}
if (document.uri === cachedDocument) {
cachedDocument = undefined;
}
}
export class NpmScriptHoverProvider implements HoverProvider {
constructor(context: ExtensionContext) {
context.subscriptions.push(commands.registerCommand('npm.runScriptFromHover', this.runScriptFromHover, this));
context.subscriptions.push(commands.registerCommand('npm.debugScriptFromHover', this.debugScriptFromHover, this));
context.subscriptions.push(workspace.onDidChangeTextDocument((e) => {
invalidateHoverScriptsCache(e.document);
}));
}
public provideHover(document: TextDocument, position: Position, _token: CancellationToken): ProviderResult<Hover> {
let hover: Hover | undefined = undefined;
if (!cachedDocument || cachedDocument.fsPath !== document.uri.fsPath) {
cachedScriptsMap = findAllScriptRanges(document.getText());
cachedDocument = document.uri;
}
cachedScriptsMap!.forEach((value, key) => {
let start = document.positionAt(value[0]);
let end = document.positionAt(value[0] + value[1]);
let range = new Range(start, end);
if (range.contains(position)) {
let contents: MarkdownString = new MarkdownString();
contents.isTrusted = true;
contents.appendMarkdown(this.createRunScriptMarkdown(key, document.uri));
contents.appendMarkdown(this.createDebugScriptMarkdown(key, document.uri));
hover = new Hover(contents);
}
});
return hover;
}
private createRunScriptMarkdown(script: string, documentUri: Uri): string {
let args = {
documentUri: documentUri,
script: script,
};
return this.createMarkdownLink(
localize('runScript', 'Run Script'),
'npm.runScriptFromHover',
args,
localize('runScript.tooltip', 'Run the script as a task')
);
}
private createDebugScriptMarkdown(script: string, documentUri: Uri): string {
const args = {
documentUri: documentUri,
script: script,
};
return this.createMarkdownLink(
localize('debugScript', 'Debug Script'),
'npm.debugScriptFromHover',
args,
localize('debugScript.tooltip', 'Runs the script under the debugger'),
'|'
);
}
private createMarkdownLink(label: string, cmd: string, args: any, tooltip: string, separator?: string): string {
let encodedArgs = encodeURIComponent(JSON.stringify(args));
let prefix = '';
if (separator) {
prefix = ` ${separator} `;
}
return `${prefix}[${label}](command:${cmd}?${encodedArgs} "${tooltip}")`;
}
public runScriptFromHover(args: any) {
let script = args.script;
let documentUri = args.documentUri;
let folder = workspace.getWorkspaceFolder(documentUri);
if (folder) {
let task = createTask(script, `run ${script}`, folder, documentUri);
tasks.executeTask(task);
}
}
public debugScriptFromHover(args: any) {
let script = args.script;
let documentUri = args.documentUri;
let folder = workspace.getWorkspaceFolder(documentUri);
if (folder) {
startDebugging(script, folder);
}
}
}