Skip to content

Commit

Permalink
Use current namespace for custom snippts w/o ns
Browse files Browse the repository at this point in the history
Fixes #558
  • Loading branch information
PEZ committed Mar 6, 2020
1 parent 60f6e36 commit 4b8a6ee
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion docs/readthedocs/source/custom-commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
22 changes: 12 additions & 10 deletions src/repl-window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -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);
}
Expand All @@ -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)));
}
Expand Down

0 comments on commit 4b8a6ee

Please sign in to comment.