Skip to content

Commit

Permalink
Merge pull request #1 from BetterThanTomorrow/rayat-amperity-rayat-am…
Browse files Browse the repository at this point in the history
…perity/issue610

Adding the doc->textnotation as a command
  • Loading branch information
rayat-amperity committed Apr 8, 2022
2 parents c79490c + e991540 commit 464f041
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 18 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
Changes to Calva.

## [Unreleased]

- [Multi cursor](https://github.com/BetterThanTomorrow/calva/issues/610)

## [2.0.264] - 2022-04-07
- Fix: [Shadowcljs shows error when running calva.loadFile command](https://github.com/BetterThanTomorrow/calva/issues/1670)

## [2.0.263] - 2022-04-06
- [Improve kondo configuration documentation](https://github.com/BetterThanTomorrow/calva/issues/1282)
- [Require VS Code 1.66+ (and update project node version to 16+)](https://github.com/BetterThanTomorrow/calva/issues/1638#issuecomment-1086726236)
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"displayName": "Calva: Clojure & ClojureScript Interactive Programming",
"description": "Integrated REPL, formatter, Paredit, and more. Powered by cider-nrepl and clojure-lsp.",
"icon": "assets/calva.png",
"version": "2.0.264",
"version": "2.0.265",
"publisher": "betterthantomorrow",
"author": {
"name": "Better Than Tomorrow",
Expand Down Expand Up @@ -917,6 +917,11 @@
"title": "Toggle nREPL Logging Enabled",
"category": "Calva Diagnostics"
},
{
"command": "calva.diagnostics.printTextNotationFromDocument",
"title": "Print TextNotation from the current document to Calva says",
"category": "Calva Diagnostics"
},
{
"command": "calva.linting.resolveMacroAs",
"title": "Resolve Macro As",
Expand Down
8 changes: 4 additions & 4 deletions src/evaluate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ async function evaluateCode(
const err: string[] = [];

if (outputWindow.getNs() !== ns) {
await session.eval(`(in-ns '${ns}) (clojure.core/refer-clojure)`, session.client.ns).value;
await session.switchNS(ns);
}

const context: NReplEvaluation = session.eval(code, ns, {
Expand Down Expand Up @@ -417,7 +417,7 @@ async function loadFile(

outputWindow.append('; Evaluating file: ' + fileName);

await session.eval(`(in-ns '${ns}) (clojure.core/refer-clojure)`, session.client.ns).value;
await session.switchNS(ns);

const res = session.loadFile(fileContents, {
fileName,
Expand Down Expand Up @@ -474,7 +474,7 @@ async function requireREPLUtilitiesCommand() {
if (session) {
try {
await namespace.createNamespaceFromDocumentIfNotExists(util.tryToGetDocument({}));
await session.eval(`(in-ns '${ns}) (clojure.core/refer-clojure)`, session.client.ns).value;
await session.switchNS(ns);
await session.eval(form, ns).value;
chan.appendLine(`REPL utilities are now available in namespace ${ns}.`);
} catch (e) {
Expand Down Expand Up @@ -548,7 +548,7 @@ export async function evaluateInOutputWindow(
const session = replSession.getSession(sessionType);
replSession.updateReplSessionType();
if (outputWindow.getNs() !== ns) {
await session.eval(`(in-ns '${ns}) (clojure.core/refer-clojure)`, session.client.ns).value;
await session.switchNS(ns);
outputWindow.setSession(session, ns);
if (options.evaluationSendCodeToOutputWindow !== false) {
outputWindow.appendPrompt();
Expand Down
10 changes: 6 additions & 4 deletions src/extension-test/unit/common/text-notation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export function docFromTextNotation(s: string): model.StringDocument {
return doc;
}

export function textNotationFromDoc(doc: model.StringDocument): string {
export function textNotationFromDoc(doc: model.EditableDocument): string {
const selections = doc.selections ?? [];
let cursorSymbols: [number, string][] = [];
selections.forEach((s, cursorNumber) => {
Expand All @@ -96,7 +96,9 @@ export function textNotationFromDoc(doc: model.StringDocument): string {

cursorSymbols = orderBy(cursorSymbols, (c) => c[0]);

const text = doc.model.lines.map((l) => l.text).join('•');
const text = getText(doc)
.split(doc.model.lineEndingLength === 1 ? '\n' : '\r\n')
.join('•');

// basically split up the text into chunks separated by where they'd have had cursor symbols, and append cursor symbols after each chunk, before joining back together
// this way we can insert the cursor symbols in the right place without having to worry about the cumulative offsets created by appending the cursor symbols
Expand Down Expand Up @@ -131,7 +133,7 @@ export function textNotationFromDoc(doc: model.StringDocument): string {
* @param doc
* @returns string
*/
export function text(doc: model.StringDocument): string {
export function getText(doc: model.EditableDocument): string {
return doc.model.getText(0, Infinity);
}

Expand All @@ -141,5 +143,5 @@ export function text(doc: model.StringDocument): string {
*/
export function textAndSelection(doc: model.StringDocument): [string, [number, number][]] {
// return [text(doc), [doc.selection.anchor, doc.selection.active]];
return [text(doc), doc.selections.map((s) => [s.anchor, s.active])];
return [getText(doc), doc.selections.map((s) => [s.anchor, s.active])];
}
2 changes: 1 addition & 1 deletion src/extension-test/unit/cursor-doc/indent-test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as expect from 'expect';
import * as model from '../../../cursor-doc/model';
import * as indent from '../../../cursor-doc/indent';
import { docFromTextNotation, textAndSelection, text } from '../common/text-notation';
import { docFromTextNotation, textAndSelection, getText } from '../common/text-notation';
import { ModelEditSelection } from '../../../cursor-doc/model';

model.initScanner(20000);
Expand Down
4 changes: 2 additions & 2 deletions src/extension-test/unit/cursor-doc/paredit-test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as expect from 'expect';
import * as paredit from '../../../cursor-doc/paredit';
import * as model from '../../../cursor-doc/model';
import { docFromTextNotation, textAndSelection, text } from '../common/text-notation';
import { docFromTextNotation, textAndSelection, getText } from '../common/text-notation';
import { ModelEditSelection } from '../../../cursor-doc/model';
import { last, method } from 'lodash';

Expand Down Expand Up @@ -1488,7 +1488,7 @@ describe('paredit', () => {
it.skip('splice string', () => {
const a = docFromTextNotation('"h|ello"');
void paredit.spliceSexp(a);
expect(text(a)).toEqual('hello');
expect(getText(a)).toEqual('hello');
});
});
});
Expand Down
8 changes: 8 additions & 0 deletions src/nrepl/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,14 @@ export class NReplSession {
});
}

async switchNS(ns: any) {
if (this.replType === 'clj') {
await this.eval(`(in-ns '${ns}) (clojure.core/refer-clojure)`, this.client.ns).value;
} else {
await this.eval(`(in-ns '${ns})`, this.client.ns).value;
}
}

private _createEvalOperationMessage(code: string, ns: string, opts: any) {
if (vscode.debug.activeDebugSession && this.replType === 'clj') {
const debugResponse = getStateValue(debug.DEBUG_RESPONSE_KEY);
Expand Down
13 changes: 12 additions & 1 deletion src/paredit/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import * as paredit from '../cursor-doc/paredit';
import * as docMirror from '../doc-mirror/index';
import { EditableDocument, ModelEditResult } from '../cursor-doc/model';
import { assertIsDefined } from '../utilities';

import { textNotationFromDoc } from '../extension-test/unit/common/text-notation';
import * as calvaState from '../state';
const onPareditKeyMapChangedEmitter = new EventEmitter<string>();

const languages = new Set(['clojure', 'lisp', 'scheme']);
Expand Down Expand Up @@ -462,6 +463,16 @@ export function activate(context: ExtensionContext) {
.update('calva.paredit.defaultKeyMap', 'original', vscode.ConfigurationTarget.Global);
}
}),
commands.registerCommand('calva.diagnostics.printTextNotationFromDocument', () => {
const doc = vscode.window.activeTextEditor?.document;
if (doc && doc.languageId === 'clojure') {
const mirrorDoc = docMirror.getDocument(vscode.window.activeTextEditor?.document);
const notation = textNotationFromDoc(mirrorDoc);
const chan = calvaState.outputChannel();
const relPath = vscode.workspace.asRelativePath(doc.uri);
chan.appendLine(`Text notation for: ${relPath}:\n${notation}`);
}
}),
window.onDidChangeActiveTextEditor(
(e) => e && e.document && languages.has(e.document.languageId)
),
Expand Down
4 changes: 2 additions & 2 deletions src/results-output/results-doc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ export async function setNamespaceFromCurrentFile() {
const session = replSession.getSession();
const ns = namespace.getNamespace(util.tryToGetDocument({}));
if (getNs() !== ns && util.isDefined(ns)) {
await session.eval(`(in-ns '${ns}) (clojure.core/refer-clojure)`, session.client.ns).value;
await session.switchNS(ns);
}
setSession(session, ns);
replSession.updateReplSessionType();
Expand All @@ -251,7 +251,7 @@ async function appendFormGrabbingSessionAndNS(topLevel: boolean) {
}
if (code != '') {
if (getNs() !== ns) {
await session.eval(`(in-ns '${ns}) (clojure.core/refer-clojure)`, session.client.ns).value;
await session.switchNS(ns);
}
setSession(session, ns);
append(code, (_) => revealResultsDoc(false));
Expand Down

0 comments on commit 464f041

Please sign in to comment.