-
-
Notifications
You must be signed in to change notification settings - Fork 213
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Print results and output to a tmp .calva-out file
Fixes #681 TODO: - There are still problems with printing several times during an eval. - If the output doc is visible it should be scrolled immediately - Stop printing to Calva says. - Possibly, open the output doc where it was last placed
- Loading branch information
Showing
6 changed files
with
116 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -86,7 +86,8 @@ | |
".clojure", | ||
".edn", | ||
".joke", | ||
".boot" | ||
".boot", | ||
".calva-out" | ||
] | ||
} | ||
], | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import * as os from 'os'; | ||
import * as path from 'path'; | ||
import * as vscode from 'vscode'; | ||
import * as state from './state'; | ||
|
||
const RESULTS_DOC_NAME = 'eval-results.calva-out'; | ||
|
||
function getResultsUri(untitled: boolean): vscode.Uri { | ||
return vscode.Uri.parse((untitled ? 'untitled:' : '') + path.join(os.tmpdir(), RESULTS_DOC_NAME)); | ||
} | ||
|
||
export async function openResultsDoc(clear: boolean = false): Promise<vscode.TextDocument> { | ||
let exists: boolean = false; | ||
let resultsDoc: vscode.TextDocument; | ||
try { | ||
const stat = await vscode.workspace.fs.stat(getResultsUri(false)); | ||
exists = true; | ||
} catch { | ||
exists = false; | ||
} | ||
await vscode.workspace.openTextDocument(getResultsUri(!exists)).then(async doc => { | ||
resultsDoc = doc; | ||
if (clear) { | ||
var edit = new vscode.WorkspaceEdit(); | ||
edit.delete(getResultsUri(false), new vscode.Range( | ||
new vscode.Position(0, 0), | ||
doc.positionAt(doc.getText().length) | ||
)); | ||
const success = await vscode.workspace.applyEdit(edit); | ||
if (!success) { | ||
state.deref().outputChannel().appendLine('Error clearing output document.') | ||
} | ||
} | ||
vscode.window.showTextDocument(doc, 1, true); | ||
}); | ||
return resultsDoc; | ||
} | ||
|
||
export function revealResultsDoc() { | ||
openResultsDoc().then(doc => { | ||
vscode.window.showTextDocument(doc); | ||
}); | ||
} | ||
|
||
let scrollToBottomSub: vscode.Disposable; | ||
|
||
export function appendToResultsDoc(text: string, reveal: boolean = false) { | ||
const edit = new vscode.WorkspaceEdit(); | ||
vscode.workspace.openTextDocument(getResultsUri(false)).then(doc => { | ||
edit.insert(getResultsUri(false), doc.positionAt(Infinity), `${text}\n`); | ||
if (scrollToBottomSub) { | ||
scrollToBottomSub.dispose(); | ||
} | ||
scrollToBottomSub = vscode.window.onDidChangeActiveTextEditor((editor) => { | ||
if (path.basename(editor.document.fileName) === RESULTS_DOC_NAME) { | ||
const lastPos = editor.document.positionAt(Infinity); | ||
editor.selection = new vscode.Selection(lastPos, lastPos); | ||
editor.revealRange(new vscode.Range(lastPos, lastPos)); | ||
console.log("Scrolled to bottom") | ||
scrollToBottomSub.dispose(); | ||
} | ||
}); | ||
state.extensionContext.subscriptions.push(scrollToBottomSub); | ||
vscode.workspace.applyEdit(edit).then( | ||
success => { | ||
if (!success) { | ||
console.log("Sad puppy") | ||
} | ||
}, | ||
reason => { | ||
console.error(`Error appending output to: ${getResultsUri(false).path}`); | ||
console.error(reason) | ||
} | ||
); | ||
}) | ||
} | ||
|
||
// function createFileWithContent(filename, content) { | ||
// var newFile = vscode.Uri.parse('untitled:' + path.join(os.homedir(), filename)); | ||
// vscode.workspace.openTextDocument(newFile).then(function (document) { | ||
// var edit = new vscode.WorkspaceEdit(); | ||
// edit.delete(newFile, new vscode.Range( | ||
// document.positionAt(0), | ||
// document.positionAt(document.getText().length - 1) | ||
// )); | ||
// return vscode.workspace.applyEdit(edit).then(function (success) { | ||
// var edit = new vscode.WorkspaceEdit(); | ||
// edit.insert(newFile, new vscode.Position(0, 0), content); | ||
// return vscode.workspace.applyEdit(edit).then(function (success) { | ||
// if (success) { | ||
// vscode.window.showTextDocument(document); | ||
// } | ||
// }); | ||
// }); | ||
// }); | ||
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters