diff --git a/CHANGELOG.md b/CHANGELOG.md index 84c1c28a6..29c1875a7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ Changes to Calva. - [Support for custom project/workflow commands](https://github.com/BetterThanTomorrow/calva/issues/281) ## [Unreleased] +- [Better inline evaluation error reports with file context](https://github.com/BetterThanTomorrow/calva/issues/329) - [Escape HTML in stdout and stderr in REPL window](https://github.com/BetterThanTomorrow/calva/issues/321) - [Add hover to inline result display, containing the full results](https://github.com/BetterThanTomorrow/calva/pull/336) diff --git a/calva/evaluate.ts b/calva/evaluate.ts index 698497923..6d0275210 100644 --- a/calva/evaluate.ts +++ b/calva/evaluate.ts @@ -42,9 +42,18 @@ async function evaluateSelection(document = {}, options = {}) { let res = await client.eval("(in-ns '" + util.getNamespace(doc) + ")").value; try { - - let context = client.eval(code, { stdout: m => out.push(m), stderr: m => err.push(m), pprint: !!pprint }) - let value = await context.value + const line = codeSelection.start.line, + column = codeSelection.start.character, + filePath = doc.fileName, + context = client.eval(code, { + file: filePath, + line: line, + column: column, + stdout: m => out.push(m), + stderr: m => err.push(m), + pprint: !!pprint + }); + let value = await context.value; value = context.pprintOut || value; if (replace) { @@ -71,7 +80,7 @@ async function evaluateSelection(document = {}, options = {}) { if (out.length > 0) { chan.appendLine("stdout:"); - chan.appendLine(out.map(x => x.replace(/\n\r?$/, "")).join("\n")); + chan.appendLine(normalizeNewLines(out)); } chan.appendLine('=>'); if (pprint) { @@ -81,15 +90,15 @@ async function evaluateSelection(document = {}, options = {}) { if (err.length > 0) { chan.appendLine("Error:") - chan.appendLine(err.map(x => x.replace(/\n\r?$/, "")).join("\n")); + chan.appendLine(normalizeNewLines(err)); } } catch (e) { if (!err.length) { // venantius/ultra outputs errors on stdout, it seems. err = out; - if (err.length > 0) { - chan.appendLine("Error:") - chan.appendLine(err.map(x => x.replace(/\n\r?$/, "")).join("\n")); - } + } + if (err.length > 0) { + chan.appendLine("Error:") + chan.appendLine(normalizeNewLines(err)); } annotations.decorateSelection(codeSelection, editor, annotations.AnnotationStatus.ERROR); @@ -101,6 +110,10 @@ async function evaluateSelection(document = {}, options = {}) { vscode.window.showErrorMessage("Not connected to a REPL") } +function normalizeNewLines(strings: string[]): string { + return strings.map(x => x.replace(/\n\r?$/, "")).join("\n"); +} + function evaluateSelectionReplace(document = {}, options = {}) { evaluateSelection(document, Object.assign({}, options, { replace: true, pprint: true })); } diff --git a/calva/utilities.ts b/calva/utilities.ts index 955b5cdcc..7b505f39f 100644 --- a/calva/utilities.ts +++ b/calva/utilities.ts @@ -206,7 +206,7 @@ function getFileType(document) { let doc = getDocument(document); if (doc) { - return doc.fileName.substr((doc.fileName.lastIndexOf('.') + 1), doc.fileName.length); + return path.extname(doc.fileName); } else { return 'clj'; @@ -214,8 +214,7 @@ function getFileType(document) { } function getFileName(document) { - let fileNameIndex = (document.fileName.lastIndexOf('\\') + 1); - return document.fileName.substr(fileNameIndex, document.fileName.length) + return path.basename(document.fileName); } function getDocumentNamespace(document = {}) {