-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextension.js
More file actions
177 lines (144 loc) · 5.23 KB
/
extension.js
File metadata and controls
177 lines (144 loc) · 5.23 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Create a basic VS Code extension
const vscode = require('vscode');
const path = require("path");
const fs = require('fs');
const { spawn } = require("child_process");
let javaProcess = null; // Persistent Java process
/**
* @param {vscode.ExtensionContext} context
*/
function activate(context) {
const diagnosticCollection = vscode.languages.createDiagnosticCollection('latte');
context.subscriptions.push(diagnosticCollection);
let first = true;
/**
* Starts the Java process once and keeps it running
*/
function startJavaProcess() {
if (!javaProcess) {
javaProcess = spawn("java", ["-XX:+TieredCompilation","-XX:TieredStopAtLevel=1", "-jar", path.join(__dirname, "latte.jar"), "-multi" ], {
stdio: ["pipe", "pipe", "pipe"],
});
javaProcess.stdout.on("data", (data) => {
console.log(`Java Output: ${data.toString()}`);
});
javaProcess.stderr.on("data", (data) => {
console.error(`Java Error: ${data.toString()}`);
});
javaProcess.on("exit", (code) => {
console.log(`Java process exited with code ${code}`);
javaProcess = null; // Restart if needed
});
console.log("Latte Type Checker started!");
}
}
/**
* Runs the type checker on the provided document and updates diagnostics.
* @param {vscode.TextDocument} document
* @param {vscode.DiagnosticCollection} diagnosticCollection
*/
function runTypeChecker(document, diagnosticCollection) {
const filePath = document.fileName;
// Ensure the file exists
if (!fs.existsSync(filePath)) {
vscode.window.showErrorMessage(`File does not exist: ${filePath}`);
return;
}
if (!javaProcess) {
startJavaProcess(); // Start Java process if it's not running
}
console.log(`Validating: ${filePath}`);
javaProcess.stdin.write(filePath + "\n");
javaProcess.stdout.on("data", (data) => {
const stdin = data.toString();
if (stdin.includes("SUCCESS")) {
diagnosticCollection.delete(document.uri);
return;
}
});
javaProcess.stderr.once("data", (data) => {
const stderr = data.toString();
console.log("Type checker stderr:", stderr);
if (stderr === "") {
diagnosticCollection.delete(document.uri);
return;
}
const jsonRegex = /{.*}/s; // Extract JSON errors
const match = stderr.match(jsonRegex);
let errors = [];
if (match) {
try {
errors.push(JSON.parse(match[0]));
updateDiagnostics(document.uri, errors, diagnosticCollection);
} catch (error) {
console.error("Error parsing JSON:", error);
}
} else {
diagnosticCollection.delete(document.uri);
console.log("No JSON found in stderr.");
}
});
}
// Update diagnostics for a file
function updateDiagnostics(fileUri, errors, diagnosticCollection) {
const diagnostics = errors.map(err => {
const range = new vscode.Range(
new vscode.Position(err.startLine - 1, err.startColumn - 1),
new vscode.Position(err.endLine - 1, err.endColumn)
);
let diag = new vscode.Diagnostic(
range,
err.message,
vscode.DiagnosticSeverity.Error
);
diag.source = "Latte";
diag.relatedInformation = [
new vscode.DiagnosticRelatedInformation(
new vscode.Location(fileUri, range),
"See related usage here."
)
];
return diag;
});
diagnosticCollection.set(fileUri, diagnostics);
}
// Check if the file imports `specification.*`
function shouldValidate(document) {
const content = document.getText();
return /\bimport\s+specification/.test(content); // Returns true if `import specification.*` is found
}
vscode.workspace.onDidChangeTextDocument((changedDocument) => {
let document = changedDocument.document;
if (document.languageId === 'java') {
// Ensure the file exists
let x = document.uri.fsPath;
if(shouldValidate(document)){
if(first){
first = false;
vscode.window.showInformationMessage("Latte running: Be Unique!");
}
runTypeChecker(document, diagnosticCollection);
}
}
})
context.subscriptions.push({
dispose: () => {
if (javaProcess) {
javaProcess.kill();
}
},
});
startJavaProcess(); // Start Java process when extension activates
}
/**
* Deactivates the extension
*/
function deactivate() {
if (javaProcess) {
javaProcess.kill();
}
}
module.exports = {
activate,
deactivate
};