Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wip/inline eval file context #330

Merged
merged 5 commits into from
Sep 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
31 changes: 22 additions & 9 deletions calva/evaluate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here i dont understand the !! isnt it the same as not having those?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a leftover from when we didn't use types. Basically it just forces it to be either true or false from merely being just truthy or falsy. It should be fixed with better typing control, I think.

});
let value = await context.value;
value = context.pprintOut || value;

if (replace) {
Expand All @@ -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) {
Expand All @@ -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);
Expand All @@ -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 }));
}
Expand Down
5 changes: 2 additions & 3 deletions calva/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,16 +206,15 @@ 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';
}
}

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 = {}) {
Expand Down