Skip to content

Commit 1522d62

Browse files
lorenzboguhnlobo-ppijulienduchesne
authored
Add continuousEval via config + error to file (#47)
* Add continuousEval via config + error to file * Lint fix * Same thing but for eval expression + bump version --------- Co-authored-by: Lorenz Boguhn <Lorenz.Boguhn@ppi.de> Co-authored-by: Julien Duchesne <julien.duchesne@grafana.com>
1 parent 3d1824f commit 1522d62

File tree

3 files changed

+102
-16
lines changed

3 files changed

+102
-16
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"description": "Full code support (formatting, highlighting, navigation, debugging etc) for Jsonnet",
66
"license": "Apache License Version 2.0",
77
"publisher": "Grafana",
8-
"version": "0.6.1",
8+
"version": "0.7.0",
99
"repository": {
1010
"type": "git",
1111
"url": "https://github.com/grafana/vscode-jsonnet"
@@ -215,6 +215,12 @@
215215
"default": false,
216216
"description": "If enabled, the LSP will publish eval diagnostics. May produce false positives as the evaluator may not have the full evaluation context. Also, if you work with very large jsonnet projects, you may want to disable this for performance reasons"
217217
},
218+
"jsonnet.languageServer.continuousEval": {
219+
"scope": "resource",
220+
"type": "boolean",
221+
"default": true,
222+
"description": "Whether to continuously evaluate the selected file"
223+
},
218224
"jsonnet.languageServer.lint": {
219225
"scope": "resource",
220226
"type": "boolean",

src/extension.ts

Lines changed: 93 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ export async function activate(context: ExtensionContext): Promise<void> {
8989
command: `jsonnet.evalItem`,
9090
arguments: [evalFilePath(editor), editor.selection.active],
9191
};
92-
evalAndDisplay(params, false);
92+
const tempFile = createTmpFile(false);
93+
evalAndDisplay(params, false, tempFile);
9394
}),
9495
commands.registerCommand('jsonnet.evalFile', evalFileFunc(false)),
9596
commands.registerCommand('jsonnet.evalFileYaml', evalFileFunc(true)),
@@ -100,49 +101,122 @@ export async function activate(context: ExtensionContext): Promise<void> {
100101

101102
function evalFileFunc(yaml: boolean) {
102103
return async () => {
103-
const editor = window.activeTextEditor;
104+
const currentFilePath = evalFilePath(window.activeTextEditor);
104105
const params: ExecuteCommandParams = {
105106
command: `jsonnet.evalFile`,
106-
arguments: [evalFilePath(editor)],
107+
arguments: [currentFilePath],
107108
};
108-
evalAndDisplay(params, yaml);
109+
const tempFile = createTmpFile(yaml);
110+
const uri = Uri.file(tempFile);
111+
112+
fs.writeFileSync(tempFile, '"Evaluating..."');
113+
114+
if (workspace.getConfiguration('jsonnet').get('languageServer.continuousEval') === false) {
115+
evalAndDisplay(params, yaml, tempFile);
116+
}
117+
else {
118+
119+
// Initial eval
120+
evalOnDisplay(params, yaml, tempFile);
121+
122+
const watcher = workspace.createFileSystemWatcher(currentFilePath);
123+
124+
window.showTextDocument(uri, {
125+
preview: true,
126+
viewColumn: ViewColumn.Beside,
127+
preserveFocus: true,
128+
});
129+
watcher.onDidChange((e) => {
130+
evalOnDisplay(params, yaml, tempFile);
131+
}
132+
);
133+
}
109134
};
110135
}
111136

137+
function createTmpFile(yaml): string {
138+
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'jsonnet-eval'));
139+
const fileEnding = yaml ? 'yaml' : 'json';
140+
const tempFile = path.join(tempDir, `result.${fileEnding}`);
141+
return tempFile;
142+
}
143+
112144
function evalExpressionFunc(yaml: boolean) {
113145
return async () => {
114-
const editor = window.activeTextEditor;
115146
window.showInputBox({ prompt: 'Expression to evaluate' }).then(async (expr) => {
116147
if (expr) {
148+
const currentFilePath = evalFilePath(window.activeTextEditor);
117149
const params: ExecuteCommandParams = {
118150
command: `jsonnet.evalExpression`,
119-
arguments: [evalFilePath(editor), expr],
151+
arguments: [currentFilePath, expr],
120152
};
121-
evalAndDisplay(params, yaml);
153+
const tempFile = createTmpFile(yaml);
154+
const uri = Uri.file(tempFile);
155+
156+
fs.writeFileSync(tempFile, '"Evaluating..."');
157+
158+
if (workspace.getConfiguration('jsonnet').get('languageServer.continuousEval') === false) {
159+
evalAndDisplay(params, yaml, tempFile);
160+
}
161+
else {
162+
// Initial eval
163+
evalOnDisplay(params, yaml, tempFile);
164+
165+
const watcher = workspace.createFileSystemWatcher(currentFilePath);
166+
167+
window.showTextDocument(uri, {
168+
preview: true,
169+
viewColumn: ViewColumn.Beside,
170+
preserveFocus: true,
171+
});
172+
watcher.onDidChange((e) => {
173+
evalOnDisplay(params, yaml, tempFile);
174+
}
175+
);
176+
}
122177
} else {
123178
window.showErrorMessage('No expression provided');
124179
}
125180
});
126181
};
127182
}
128183

129-
function evalAndDisplay(params: ExecuteCommandParams, yaml: boolean): void {
184+
function evalOnDisplay(params: ExecuteCommandParams, yaml: boolean, tempFile: string): void {
185+
channel.appendLine(`Sending eval request: ${JSON.stringify(params)}`);
186+
client
187+
.sendRequest(ExecuteCommandRequest.type, params)
188+
.then((result) => {
189+
let uri = Uri.file(tempFile);
190+
fs.writeFileSync(tempFile, result);
191+
192+
if (yaml) {
193+
const file = fs.readFileSync(tempFile, 'utf8');
194+
const parsed = JSON.parse(file);
195+
const yamlString = stringifyYaml(parsed);
196+
uri = Uri.file(tempFile);
197+
fs.writeFileSync(tempFile, yamlString);
198+
}
199+
})
200+
.catch((err) => {
201+
window.showErrorMessage(err.message);
202+
fs.writeFileSync(tempFile, err.message);
203+
});
204+
}
205+
206+
function evalAndDisplay(params: ExecuteCommandParams, yaml: boolean, tempFile: string): void {
130207
channel.appendLine(`Sending eval request: ${JSON.stringify(params)}`);
131208
client
132209
.sendRequest(ExecuteCommandRequest.type, params)
133210
.then((result) => {
134-
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'jsonnet-eval'));
135-
const tempFile = path.join(tempDir, 'result.json');
136211
let uri = Uri.file(tempFile);
137212
fs.writeFileSync(tempFile, result);
138213

139214
if (yaml) {
140215
const file = fs.readFileSync(tempFile, 'utf8');
141216
const parsed = JSON.parse(file);
142217
const yamlString = stringifyYaml(parsed);
143-
const tempYamlFile = path.join(tempDir, 'result.yaml');
144-
uri = Uri.file(tempYamlFile);
145-
fs.writeFileSync(tempYamlFile, yamlString);
218+
uri = Uri.file(tempFile);
219+
fs.writeFileSync(tempFile, yamlString);
146220
}
147221
window.showTextDocument(uri, {
148222
preview: true,
@@ -151,6 +225,12 @@ function evalAndDisplay(params: ExecuteCommandParams, yaml: boolean): void {
151225
})
152226
.catch((err) => {
153227
window.showErrorMessage(err.message);
228+
fs.writeFileSync(tempFile, err.message);
229+
const uri = Uri.file(tempFile);
230+
window.showTextDocument(uri, {
231+
preview: true,
232+
viewColumn: ViewColumn.Beside,
233+
});
154234
});
155235
}
156236

0 commit comments

Comments
 (0)