Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show code eval in repl option #1470

Merged
merged 3 commits into from
Jan 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Changes to Calva.

## [Unreleased]
- [Show code eval in repl option](https://github.com/BetterThanTomorrow/calva/issues/1465)

## [2.0.235] - 2022-01-22
- [Continue to support -Aalias for jack-in](https://github.com/BetterThanTomorrow/calva/issues/1474)
Expand Down
23 changes: 22 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,11 @@
"default": false,
"description": "Run load-file when opening a new file and on file save"
},
"calva.evaluationSendCodeToOutputWindow": {
"type": "boolean",
"default": false,
"description": "Also send evaluated code to the REPL Window"
},
"calva.testOnSave": {
"type": "boolean",
"default": false,
Expand Down Expand Up @@ -823,6 +828,12 @@
"category": "Calva",
"enablement": "calva:connected"
},
{
"command": "calva.toggleEvaluationSendCodeToOutputWindow",
"title": "Toggle also sending evaluated code to the REPL Window",
"category": "Calva",
"enablement": "calva:connected"
},
{
"command": "calva.jackIn",
"title": "Start a Project REPL and Connect (aka Jack-In)",
Expand Down Expand Up @@ -1656,7 +1667,7 @@
"when": "calva:keybindingsEnabled && editorLangId == clojure && editorTextFocus"
},
{
"command": "calva.evaluateCurrentTopLevelForm",
"command": "calva.evaluateOutputWindowForm",
"key": "enter",
"when": "calva:keybindingsEnabled && calva:outputWindowActive && calva:outputWindowSubmitOnEnter && editorTextFocus"
},
Expand Down Expand Up @@ -1695,6 +1706,11 @@
"key": "ctrl+alt+c p",
"when": "editorLangId == clojure && calva:keybindingsEnabled"
},
{
"command": "calva.toggleEvaluationSendCodeToOutputWindow",
"key": "ctrl+alt+o s",
"when": "editorLangId == clojure && calva:keybindingsEnabled"
},
{
"command": "calva.requireREPLUtilities",
"key": "ctrl+alt+c ctrl+u",
Expand Down Expand Up @@ -2510,6 +2526,11 @@
"command": "calva.togglePrettyPrint",
"group": "calva/b-eval"
},
{
"when": "editorLangId == clojure && calva:showReplUi",
"command": "calva.toggleEvaluationSendCodeToOutputWindow",
"group": "calva/b-eval"
},
{
"command": "calva.debug.instrument",
"enablement": "editorLangId == clojure && calva:connected",
Expand Down
1 change: 1 addition & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ function getConfig() {
customREPLCommandSnippetsWorkspace: commands,
customREPLCommandSnippetsWorkspaceFolder: configOptions.inspect("customREPLCommandSnippets").workspaceFolderValue as customREPLCommandSnippet[],
prettyPrintingOptions: configOptions.get("prettyPrintingOptions") as PrettyPrintingOptions,
evaluationSendCodeToOutputWindow: configOptions.get("evaluationSendCodeToOutputWindow") as boolean,
enableJSCompletions: configOptions.get("enableJSCompletions") as boolean,
autoOpenREPLWindow: configOptions.get("autoOpenREPLWindow") as boolean,
autoOpenJackInTerminal: configOptions.get("autoOpenJackInTerminal") as boolean,
Expand Down
2 changes: 1 addition & 1 deletion src/connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ async function connectToHost(hostname: string, port: number, connectSequence: Re

if (connectSequence.afterCLJReplJackInCode) {
outputWindow.append(`\n; Evaluating 'afterCLJReplJackInCode'`);
await evaluate.evaluateInOutputWindow(connectSequence.afterCLJReplJackInCode, 'clj', outputWindow.getNs());
await evaluate.evaluateInOutputWindow(connectSequence.afterCLJReplJackInCode, 'clj', outputWindow.getNs(), {});
}

outputWindow.appendPrompt();
Expand Down
9 changes: 8 additions & 1 deletion src/custom-snippets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,13 @@ async function evaluateCustomCodeSnippet(codeOrKey?: string): Promise<void> {
replace(/\$top-level-defined-symbol/g, getText.currentTopLevelFunction(editor)[1]).
replace(/\$head/g, getText.toStartOfList(editor)[1]).
replace(/\$tail/g, getText.toEndOfList(editor)[1]);
await evaluate.evaluateInOutputWindow(interpolatedCode, repl, ns);

const options = {};

if (pick !== undefined) {
options['evaluationSendCodeToOutputWindow'] = snippetsDict[pick].evaluationSendCodeToOutputWindow;
}

await evaluate.evaluateInOutputWindow(interpolatedCode, repl, ns, options);
outputWindow.appendPrompt();
}
42 changes: 34 additions & 8 deletions src/evaluate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ async function addAsComment(c: number, result: string, codeSelection: vscode.Sel

async function evaluateCode(code: string, options, selection?: vscode.Selection): Promise<void> {
const pprintOptions = options.pprintOptions || getConfig().prettyPrintingOptions;
// passed options overwrite config options
const evaluationSendCodeToOutputWindow = (options.evaluationSendCodeToOutputWindow === undefined || options.evaluationSendCodeToOutputWindow === true)
&& getConfig().evaluationSendCodeToOutputWindow;
const addToHistory = evaluationSendCodeToOutputWindow;
const line = options.line;
const column = options.column;
const filePath = options.filePath;
Expand All @@ -56,6 +60,11 @@ async function evaluateCode(code: string, options, selection?: vscode.Selection)
const editor = vscode.window.activeTextEditor;

if (code.length > 0) {
if (addToHistory) {
replHistory.addToReplHistory(session.replType, code);
replHistory.resetState();
}

let err: string[] = [];

if (outputWindow.getNs() !== ns) {
Expand All @@ -76,6 +85,11 @@ async function evaluateCode(code: string, options, selection?: vscode.Selection)
try {
let value = await context.value;
value = util.stripAnsi(context.pprintOut || value);

if (evaluationSendCodeToOutputWindow) {
outputWindow.append(code);
}

outputWindow.append(value, async (resultLocation) => {
if (selection) {
const c = selection.start.character;
Expand Down Expand Up @@ -156,11 +170,6 @@ async function evaluateSelection(document: {}, options) {
const filePath = doc.fileName;
const session = replSession.getSession(util.getFileType(doc));

if (outputWindow.isResultsDoc(doc)) {
replHistory.addToReplHistory(session.replType, code);
replHistory.resetState();
}

if (code.length > 0) {
if (options.debug) {
code = '#dbg\n' + code;
Expand Down Expand Up @@ -226,6 +235,14 @@ function evaluateTopLevelForm(document = {}, options = {}) {
})).catch(printWarningForError);
}

function evaluateOutputWindowForm(document = {}, options = {}) {
evaluateSelection(document, Object.assign({}, options, {
pprintOptions: getConfig().prettyPrintingOptions,
selectionFn: getText.currentTopLevelFormText,
evaluationSendCodeToOutputWindow: false
})).catch(printWarningForError);
}

function evaluateCurrentForm(document = {}, options = {}) {
evaluateSelection(document, Object.assign({}, options, {
pprintOptions: getConfig().prettyPrintingOptions,
Expand Down Expand Up @@ -373,6 +390,12 @@ async function togglePrettyPrint() {
statusbar.update();
};

async function toggleEvaluationSendCodeToOutputWindow() {
const config = vscode.workspace.getConfiguration('calva');
await config.update('evaluationSendCodeToOutputWindow', !config.get('evaluationSendCodeToOutputWindow'), vscode.ConfigurationTarget.Global);
statusbar.update();
};

async function instrumentTopLevelForm() {
evaluateSelection({}, {
pprintOptions: getConfig().prettyPrintingOptions,
Expand All @@ -382,7 +405,7 @@ async function instrumentTopLevelForm() {
state.analytics().logEvent(DEBUG_ANALYTICS.CATEGORY, DEBUG_ANALYTICS.EVENT_ACTIONS.INSTRUMENT_FORM).send();
}

export async function evaluateInOutputWindow(code: string, sessionType: string, ns: string) {
export async function evaluateInOutputWindow(code: string, sessionType: string, ns: string, options) {
const outputDocument = await outputWindow.openResultsDoc();
const evalPos = outputDocument.positionAt(outputDocument.getText().length);
try {
Expand All @@ -393,8 +416,9 @@ export async function evaluateInOutputWindow(code: string, sessionType: string,
outputWindow.setSession(session, ns);
outputWindow.appendPrompt();
}
outputWindow.append(code);

await evaluateCode(code, {
...options,
filePath: outputDocument.fileName,
session,
ns,
Expand Down Expand Up @@ -432,6 +456,8 @@ export default {
copyLastResultCommand,
requireREPLUtilitiesCommand,
togglePrettyPrint,
toggleEvaluationSendCodeToOutputWindow,
instrumentTopLevelForm,
evaluateInOutputWindow
evaluateInOutputWindow,
evaluateOutputWindowForm
};
2 changes: 2 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,12 @@ async function activate(context: vscode.ExtensionContext) {
context.subscriptions.push(vscode.commands.registerCommand('calva.evaluateStartOfFileToCursor', eval.evaluateStartOfFileToCursor));
context.subscriptions.push(vscode.commands.registerCommand('calva.evaluateUser', eval.evaluateUser));
context.subscriptions.push(vscode.commands.registerCommand('calva.evaluateCurrentTopLevelForm', eval.evaluateTopLevelForm));
context.subscriptions.push(vscode.commands.registerCommand('calva.evaluateOutputWindowForm', eval.evaluateOutputWindowForm));
context.subscriptions.push(vscode.commands.registerCommand('calva.evaluateSelectionReplace', eval.evaluateSelectionReplace));
context.subscriptions.push(vscode.commands.registerCommand('calva.evaluateSelectionAsComment', eval.evaluateSelectionAsComment));
context.subscriptions.push(vscode.commands.registerCommand('calva.evaluateTopLevelFormAsComment', eval.evaluateTopLevelFormAsComment));
context.subscriptions.push(vscode.commands.registerCommand('calva.togglePrettyPrint', eval.togglePrettyPrint));
context.subscriptions.push(vscode.commands.registerCommand('calva.toggleEvaluationSendCodeToOutputWindow', eval.toggleEvaluationSendCodeToOutputWindow));
context.subscriptions.push(vscode.commands.registerCommand('calva.runTestUnderCursor', () => { testRunner.runTestUnderCursorCommand(controller) }));
context.subscriptions.push(vscode.commands.registerCommand('calva.runNamespaceTests', () => { testRunner.runNamespaceTestsCommand(controller) }));
context.subscriptions.push(vscode.commands.registerCommand('calva.runAllTests', () => { testRunner.runAllTestsCommand(controller) }));
Expand Down