-
Notifications
You must be signed in to change notification settings - Fork 1
/
extension.js
90 lines (76 loc) · 2.7 KB
/
extension.js
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
const vscode = require('vscode');
const script = require('./lib/index');
function activate(context) {
// Use the console to output diagnostic information (console.log) and errors (console.error)
// This line of code will only be executed once when your extension is activated
console.log('Congratulations, your extension "remotetolocalimagevscode" is now active!');
// Register the command for replacing in workspaces
let disposable = vscode.commands.registerCommand('remotetolocalimagevscode.replaceinworkspaces', function () {
try {
script.runAllWorkspaces();
vscode.window.showInformationMessage('Images have been replaced');
} catch (err) {
console.log(err.stack);
}
});
// Register the command for replacing in the current file
let disposable2 = vscode.commands.registerCommand('remotetolocalimagevscode.replaceinfile', function () {
try {
script.runCurrentFile();
vscode.window.showInformationMessage('Images have been replaced');
} catch (err) {
console.log(err.stack);
}
});
// Register the command for running with a single URL
let disposable4 = vscode.commands.registerCommand('remotetolocalimagevscode.singleurl', function (match) {
try {
script.runSingleUrl(match);
vscode.window.showInformationMessage('Image has been replaced');
} catch (err) {
console.log(err.stack);
}
});
// Register the code lens provider
let disposable3 = vscode.languages.registerCodeLensProvider({ scheme: 'file' }, {
provideCodeLenses
});
context.subscriptions.push(disposable);
context.subscriptions.push(disposable2);
context.subscriptions.push(disposable3);
context.subscriptions.push(disposable4);
}
function provideCodeLenses(document) {
const codeLenses = [];
const content = document.getText();
const urlRegex = /\b(?:https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|]/gi;
const imageExtensionRegex = /\.(jpg|jpeg|png|gif|bmp|svg|webp)/i;
let match;
let count = 0;
function createCodeLens(match) {
const startPosition = document.positionAt(match.index);
const endPosition = document.positionAt(match.index + match[0].length);
const range = new vscode.Range(startPosition, endPosition);
const codeLens = new vscode.CodeLens(range);
codeLens.command = {
title: "Make image local",
command: "remotetolocalimagevscode.singleurl",
arguments: [match[0]]
};
return codeLens;
}
while ((match = urlRegex.exec(content))) {
count++;
if (imageExtensionRegex.test(match[0])) {
const codeLens = createCodeLens(match);
codeLenses.push(codeLens);
}
}
return codeLenses;
}
// this method is called when your extension is deactivated
function deactivate() { }
module.exports = {
activate,
deactivate
}