-
Notifications
You must be signed in to change notification settings - Fork 0
/
extension.js
110 lines (96 loc) · 3.13 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
const vscode = require("vscode");
const { languages, commands, workspace } = vscode;
const decodeFunction = require("./src/decodeFunction.bs");
class CodelensProvider {
constructor() {
this.codeLenses = [];
this.regex = /type\s+(\w+)\s+=\s+\{/g;
this._onDidChangeCodeLenses = new vscode.EventEmitter();
this.onDidChangeCodeLenses = this._onDidChangeCodeLenses.event;
vscode.workspace.onDidChangeConfiguration((_) => {
this._onDidChangeCodeLenses.fire();
});
}
provideCodeLenses(document, token) {
if (
vscode.workspace
.getConfiguration("rescript-decode")
.get("enableDecodeButton", true)
) {
this.codeLenses = [];
const regex = new RegExp(this.regex);
const text = document.getText();
let matches;
while ((matches = regex.exec(text)) !== null) {
const line = document.lineAt(document.positionAt(matches.index).line);
const indexOf = line.text.indexOf(matches[0]);
const position = new vscode.Position(line.lineNumber, indexOf);
const range = document.getWordRangeAtPosition(
position,
new RegExp(this.regex)
);
if (range) {
this.codeLenses.push(new vscode.CodeLens(range));
}
}
return this.codeLenses;
}
return [];
}
resolveCodeLens(codeLens, token) {
if (
vscode.workspace
.getConfiguration("rescript-decode")
.get("enableDecodeButton", true)
) {
const document = vscode.window.activeTextEditor.document;
const currentLine = document.lineAt(codeLens.range.start.line).text;
codeLens.command = {
title: "Generate Decode",
tooltip: "Click to generate decode function",
command: "rescript-decode.generateDecode",
arguments: [currentLine, false],
};
return codeLens;
}
return null;
}
}
function activate(context) {
const codelensProvider = new CodelensProvider();
languages.registerCodeLensProvider("*", codelensProvider);
commands.registerCommand("rescript-decode.enableDecodeButton", () => {
workspace
.getConfiguration("rescript-decode")
.update("enableDecodeButton", true, true);
});
commands.registerCommand("rescript-decode.generateDecode", (args) => {
const regex = /type\s+(\w+)\s+=/; // Regular expression to match the type name
const editor = vscode.window.activeTextEditor;
if (!editor) {
vscode.window.showErrorMessage("No active editor found.");
return;
}
const match = args.match(regex);
if (match) {
const typeName = match[1];
const fileContents = editor.document.getText();
let result = decodeFunction.generateDecode(typeName, fileContents);
let res = `open LogicUtils // Import Utilities from your Utils file
${result}
`;
vscode.env.clipboard.writeText(res).then(() => {
vscode.window.showInformationMessage(
`${typeName} : Generated code copied to clipboard.`
);
});
} else {
vscode.window.showErrorMessage("No Type Found");
}
});
}
function deactivate() {}
module.exports = {
activate,
deactivate,
};