From 4b8a6ee3e1f38938ca863eb94a865bfb681696d1 Mon Sep 17 00:00:00 2001 From: Peter Date: Sat, 22 Feb 2020 22:49:54 +0100 Subject: [PATCH] Use current namespace for custom snippts w/o ns Fixes #558 --- CHANGELOG.md | 1 + docs/readthedocs/source/custom-commands.md | 2 +- src/repl-window.ts | 22 ++++++++++++---------- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7172d214f..3496185a2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ Changes to Calva. - [Fix: Some character literals throws paredit out of whack](https://github.com/BetterThanTomorrow/calva/issues/563) - [Fix: Initial expand selection sometimes fails](https://github.com/BetterThanTomorrow/calva/issues/549) - [Change line comment characters to ;;](https://github.com/BetterThanTomorrow/calva/issues/564) +- [Use editor namespace for custom REPL commands w/o `ns` specified](https://github.com/BetterThanTomorrow/calva/issues/558) ## [2.0.76] - 2020-02-12 - [Fix Calva locking up when opening files with very long lines](https://github.com/BetterThanTomorrow/calva/issues/556) diff --git a/docs/readthedocs/source/custom-commands.md b/docs/readthedocs/source/custom-commands.md index 665f5ac82..c2d058709 100644 --- a/docs/readthedocs/source/custom-commands.md +++ b/docs/readthedocs/source/custom-commands.md @@ -6,7 +6,7 @@ The `calva.customREPLCommandSnippets` is an object/dictionary with the following * `name`: The name of the snippet as it will appear in the picker menu * `snippet`: The code that will be evaluated -* `ns`: (optional) Namespace to evaluate the command in. If omitted the command will be executed in whatever namespace the REPL window has at the moment, which probably is only useful for running code in the `user` namespace. +* `ns`: (optional) Namespace to evaluate the command in. If omitted the command will be executed in the namespace of the current editor. * `replType`: Which REPL window to use for the evaluation. Either `"clj"` or `"cljs"` E.g. with these settings: diff --git a/src/repl-window.ts b/src/repl-window.ts index f2eb6b1de..cdf371db1 100644 --- a/src/repl-window.ts +++ b/src/repl-window.ts @@ -42,7 +42,7 @@ export function activeReplWindow() { } export function isReplWindowOpen(mode: "clj" | "cljs" = "clj") { - // If we find `mode` in the `replWindows` + // If we find `mode` in the `replWindows` // dictionary, then it is open. if (!replWindows[mode]) { return (false); @@ -182,7 +182,7 @@ class REPLWindow { } reconnect() { - // evaluate something that really test + // evaluate something that really test // the ability of the connected repl. let res = this.session.eval("(+ 1 1)"); res.value.then((v) => { @@ -458,11 +458,11 @@ export function sendTextToREPLWindow(sessionType: "clj" | "cljs", text: string, let wnd = replWindows[sessionType]; if (wnd) { let inNs = ns ? ns : wnd.ns; - if (ns && ns !== wnd.ns) { - const requireEvaluation = wnd.session.eval(`(require '${ns})`); + if (inNs && inNs !== wnd.ns) { + const requireEvaluation = wnd.session.eval(inNs !== 'user' ? `(require '${inNs})` : 'nil'); requireEvaluation.value .then((v) => { - const inNSEvaluation = wnd.session.eval(`(in-ns '${ns})`) + const inNSEvaluation = wnd.session.eval(`(in-ns '${inNs})`) inNSEvaluation.value .then((v) => { wnd.setNamespace(inNSEvaluation.ns).then((v) => { @@ -562,7 +562,9 @@ function sendCustomCommandSnippetToREPLCommand() { }).then(async (pick) => { if (pick && snippetsDict[pick] && snippetsDict[pick].snippet) { const command = snippetsDict[pick].snippet, - ns = snippetsDict[pick].ns ? snippetsDict[pick].ns : "user", + editor = vscode.window.activeTextEditor, + editorNS = editor && editor.document && editor.document.languageId === 'clojure' ? util.getNamespace(editor.document) : undefined, + ns = snippetsDict[pick].ns ? snippetsDict[pick].ns : editorNS, repl = snippetsDict[pick].repl ? snippetsDict[pick].repl : "clj"; sendTextToREPLWindow(repl ? repl : "clj", command, ns); } @@ -586,16 +588,16 @@ export function activate(context: vscode.ExtensionContext) { context.subscriptions.push(vscode.commands.registerCommand('calva.clearClojureScriptREPLWindow', clearClojureScriptREPLWindowAndHistory)); context.subscriptions.push(vscode.commands.registerCommand('calva.replWindow.newLine', () => { activeReplWindow().executeCommand('new-line'); })); context.subscriptions.push(vscode.commands.registerCommand('calva.replWindow.submitPrompt', () => { activeReplWindow().executeCommand('submit'); })); - context.subscriptions.push(vscode.commands.registerCommand('calva.replWindow.historyUp', util.debounce(() => { + context.subscriptions.push(vscode.commands.registerCommand('calva.replWindow.historyUp', util.debounce(() => { activeReplWindow().executeCommand('history-up'); }, 10, true))); - context.subscriptions.push(vscode.commands.registerCommand('calva.replWindow.historyDown', util.debounce(() => { + context.subscriptions.push(vscode.commands.registerCommand('calva.replWindow.historyDown', util.debounce(() => { activeReplWindow().executeCommand('history-down'); }, 10, true))); - context.subscriptions.push(vscode.commands.registerCommand('calva.replWindow.cursorUp', util.debounce(() => { + context.subscriptions.push(vscode.commands.registerCommand('calva.replWindow.cursorUp', util.debounce(() => { activeReplWindow().executeCommand('cursor-up'); }, 10, true))); - context.subscriptions.push(vscode.commands.registerCommand('calva.replWindow.cursorDown', util.debounce(() => { + context.subscriptions.push(vscode.commands.registerCommand('calva.replWindow.cursorDown', util.debounce(() => { activeReplWindow().executeCommand('cursor-down'); }, 10, true))); }