Skip to content

Commit

Permalink
Merge pull request #176 from teves-castro/EvaluateAsComment
Browse files Browse the repository at this point in the history
Add evaluate as comment
  • Loading branch information
PEZ authored May 2, 2019
2 parents cd8cd36 + 23ce59b commit 1a328a9
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 6 deletions.
22 changes: 18 additions & 4 deletions calva/evaluate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ async function evaluateSelection(document = {}, options = {}) {
doc = util.getDocument(document),
pprint = options["pprint"] || false,
replace = options["replace"] || false,
topLevel = options["topLevel"] || false;
topLevel = options["topLevel"] || false,
asComment = options["comment"] || false;
if (current.get('connected')) {
let client = util.getSession(util.getFileType(doc));
let editor = vscode.window.activeTextEditor,
selection = editor.selection,
codeSelection = null,
codeSelection: vscode.Selection = null,
code = "";

if (selection.isEmpty) {
Expand All @@ -40,18 +41,26 @@ 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
value = context.pprintOut || value;

if (replace) {
const indent = ' '.repeat(c),
const indent = `${' '.repeat(c)};`,
edit = vscode.TextEdit.replace(codeSelection, value.replace(/\n/gm, "\n" + indent)),
wsEdit = new vscode.WorkspaceEdit();
wsEdit.set(editor.document.uri, [edit]);
vscode.workspace.applyEdit(wsEdit);
chan.appendLine("Replaced inline.")

} else if (asComment) {
const indent = `${' '.repeat(c)};`,
output = value.replace(/\"/gm, "").split("\\n").join(`\n${indent}`),
edit = vscode.TextEdit.insert(codeSelection.end, `\n;; ==> ${output} <== ;;`),
wsEdit = new vscode.WorkspaceEdit();
wsEdit.set(editor.document.uri, [edit]);
vscode.workspace.applyEdit(wsEdit);
chan.appendLine("Evaluated as comment.")
} else {
annotations.decorateSelection(codeSelection, editor, annotations.AnnotationStatus.SUCCESS);
if (!pprint)
Expand Down Expand Up @@ -97,6 +106,10 @@ function evaluateSelectionReplace(document = {}, options = {}) {
evaluateSelection(document, Object.assign({}, options, { replace: true, pprint: true }));
}

function evaluateSelectionAsComment(document = {}, options = {}) {
evaluateSelection(document, Object.assign({}, options, { comment: true, pprint: true }));
}

function evaluateSelectionPrettyPrint(document = {}, options = {}) {
evaluateSelection(document, Object.assign({}, options, { pprint: true }));
}
Expand Down Expand Up @@ -156,5 +169,6 @@ export default {
evaluateSelectionPrettyPrint,
evaluateCurrentTopLevelFormPrettyPrint,
evaluateSelectionReplace,
evaluateSelectionAsComment,
copyLastResultCommand
};
5 changes: 3 additions & 2 deletions calva/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,13 @@ function activate(context) {
context.subscriptions.push(vscode.commands.registerCommand('calva.evaluateSelectionPrettyPrint', EvaluateMiddleWare.evaluateSelectionPrettyPrint));
context.subscriptions.push(vscode.commands.registerCommand('calva.evaluateCurrentTopLevelFormPrettyPrint', EvaluateMiddleWare.evaluateCurrentTopLevelFormPrettyPrint));
context.subscriptions.push(vscode.commands.registerCommand('calva.evaluateSelectionReplace', EvaluateMiddleWare.evaluateSelectionReplace));
context.subscriptions.push(vscode.commands.registerCommand('calva.evaluateSelectionAsComment', EvaluateMiddleWare.evaluateSelectionAsComment));
context.subscriptions.push(vscode.commands.registerCommand('calva.lintFile', LintMiddleWare.lintDocument));
context.subscriptions.push(vscode.commands.registerCommand('calva.runNamespaceTests', TestRunnerMiddleWare.runNamespaceTestsCommand));
context.subscriptions.push(vscode.commands.registerCommand('calva.runAllTests', TestRunnerMiddleWare.runAllTestsCommand));
context.subscriptions.push(vscode.commands.registerCommand('calva.rerunTests', TestRunnerMiddleWare.rerunTestsCommand));

context.subscriptions.push(vscode.commands.registerCommand('calva.clearInlineResults', annotations.clearEvaluationDecorations));
context.subscriptions.push(vscode.commands.registerCommand('calva.clearInlineResults', annotations.clearDecorations));
context.subscriptions.push(vscode.commands.registerCommand('calva.copyLastResults', evaluate.copyLastResultCommand));

context.subscriptions.push(vscode.commands.registerCommand('calva.refresh', refresh.refresh));
Expand Down Expand Up @@ -148,7 +149,7 @@ function activate(context) {

greetings.activationGreetings(chan, lint);


chan.appendLine("Start the REPL with the command *Start Project REPL and connect (aka Jack-in)*. Default keybinding: ctrl+alt+v ctrl+alt+j");
state.analytics().logPath("/activated").logEvent("LifeCycle", "Activated").send();

Expand Down
40 changes: 40 additions & 0 deletions calva/providers/annotations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,44 @@ function clearEvaluationDecorations(editor?: vscode.TextEditor) {
setSelectionDecorations(editor, [], AnnotationStatus.REPL_WINDOW);
}

function allMatches(re: RegExp, text: string) {
const matches: { start: number, end: number }[] = []
let match
while (match = re.exec(text)) {
matches.push({
start: match.index,
end: match.index + match[0].length
})
}
return matches
}

function clearEvaluationCommentDecorations(editor?: vscode.TextEditor) {
if (editor === undefined) {
editor = vscode.window.activeTextEditor
}

const originalText = editor.document.getText()
const comment = /\n;; ==>.*<== ;;/gm

const matches = allMatches(comment, originalText)

if (matches.length > 0) {
const edits = matches.map(
({ start, end }) =>
new vscode.TextEdit(new vscode.Range(editor.document.positionAt(start), editor.document.positionAt(end)), ""),
)
const wsEdit = new vscode.WorkspaceEdit()
wsEdit.set(editor.document.uri, edits)
vscode.workspace.applyEdit(wsEdit)
}
}

function clearDecorations(editor?: vscode.TextEditor) {
clearEvaluationDecorations(editor)
clearEvaluationCommentDecorations(editor)
}

function decorateResults(resultString, hasError, codeSelection: vscode.Range, editor) {
let uri = editor.document.uri,
key = uri + ':resultDecorationRanges',
Expand Down Expand Up @@ -131,7 +169,9 @@ function onDidChangeTextDocument(event: vscode.TextDocumentChangeEvent) {

export default {
AnnotationStatus,
clearEvaluationCommentDecorations,
clearEvaluationDecorations,
clearDecorations,
decorateResults,
decorateSelection,
onDidChangeTextDocument
Expand Down
9 changes: 9 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,11 @@
"title": "Evaluate current form (or selection), and replace it with the result",
"category": "Calva"
},
{
"command": "calva.evaluateSelectionAsComment",
"title": "Evaluate current form (or selection), and append a comment with the result",
"category": "Calva"
},
{
"command": "calva.copyLastResults",
"title": "Copy the result of last evaluation to the clipboard",
Expand Down Expand Up @@ -620,6 +625,10 @@
"command": "calva.evaluateSelectionReplace",
"key": "ctrl+alt+c r"
},
{
"command": "calva.evaluateSelectionAsComment",
"key": "ctrl+alt+c c"
},
{
"command": "calva.copyLastResults",
"key": "ctrl+alt+c ctrl+c"
Expand Down

0 comments on commit 1a328a9

Please sign in to comment.