From 6f95b38f3893480a37d95c3ef0d2f69e43ff9e48 Mon Sep 17 00:00:00 2001 From: Julien Vincent Date: Sun, 8 Jan 2023 22:18:57 +0000 Subject: [PATCH] Truncate repl output as length crosses threshold Calvas performance can be drastically affected as the size of the repl output window grows. This commit adds a config entry `replOutputMaxLines` which if set to a non-0 number will cause the output window to be truncated if it grows beyond this threshold. Fixes #804 --- CHANGELOG.md | 3 +++ package.json | 5 +++++ src/config.ts | 2 ++ src/results-output/results-doc.ts | 16 ++++++++++++++++ src/results-output/util.ts | 2 +- 5 files changed, 27 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e26f622f7..a10308ccd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,9 @@ Changes to Calva. ## [Unreleased] - Fix: [Rogue loops that print output to stdout cause Calva to hang](https://github.com/BetterThanTomorrow/calva/issues/2010) +- Fix: [Output window becomes very slow when number of lines of content is very high](https://github.com/BetterThanTomorrow/calva/issues/804) +- Partial Fix: [Output window becomes very slow when number of lines of content is very high](https://github.com/BetterThanTomorrow/calva/issues/942) + ## [2.0.323] - 2023-01-07 - Fix: [Provider completions not handling errors gracefully](https://github.com/BetterThanTomorrow/calva/issues/2006) diff --git a/package.json b/package.json index 67818a754..d0add65a8 100644 --- a/package.json +++ b/package.json @@ -775,6 +775,11 @@ "type": "number", "default": 100 }, + "calva.replOutputMaxLines": { + "markdownDescription": "The maximum number of lines to retain in the repl output window. Having the repl output window grow too large can significantly affect performance. Setting this to 0 will disable truncating", + "type": "number", + "default": 1000 + }, "calva.depsEdnJackInExecutable": { "markdownDescription": "Which executable should Calva Jack-in use for starting a deps.edn project? The default is to let Calva choose. It will choose `clojure` if that is installed and working. Otherwise `deps.clj`, which is bundled with Calva, will be used. (This settings has no effect on Windows, where `deps.clj` will always be used.)", "enum": [ diff --git a/src/config.ts b/src/config.ts index c8745413b..2561c954a 100644 --- a/src/config.ts +++ b/src/config.ts @@ -15,6 +15,7 @@ const KEYBINDINGS_ENABLED_CONFIG_KEY = 'calva.keybindingsEnabled'; const KEYBINDINGS_ENABLED_CONTEXT_KEY = 'calva:keybindingsEnabled'; const REPL_OUTPUT_THROTTLE_RATE_CONFIG_KEY = 'calva.replOutputThrottleRate'; +const REPL_OUTPUT_MAX_LINES_CONFIG_KEY = 'calva.replOutputMaxLines'; type ReplSessionType = 'clj' | 'cljs'; @@ -237,6 +238,7 @@ export { KEYBINDINGS_ENABLED_CONFIG_KEY, KEYBINDINGS_ENABLED_CONTEXT_KEY, REPL_OUTPUT_THROTTLE_RATE_CONFIG_KEY, + REPL_OUTPUT_MAX_LINES_CONFIG_KEY, documentSelector, ReplSessionType, getConfig, diff --git a/src/results-output/results-doc.ts b/src/results-output/results-doc.ts index 952416787..ff822085e 100644 --- a/src/results-output/results-doc.ts +++ b/src/results-output/results-doc.ts @@ -20,6 +20,10 @@ const RESULTS_DOC_NAME = `output.${config.REPL_FILE_EXT}`; const REPL_OUTPUT_THROTTLE_RATE = vscode.workspace .getConfiguration() .get(config.REPL_OUTPUT_THROTTLE_RATE_CONFIG_KEY); +const REPL_OUTPUT_MAX_LINES = vscode.workspace + .getConfiguration() + .get(config.REPL_OUTPUT_MAX_LINES_CONFIG_KEY); + const PROMPT_HINT = 'Use `alt+enter` to evaluate'; const START_GREETINGS = [ @@ -306,10 +310,22 @@ async function writeToResultsDoc({ text, onAppended }: ResultsBufferEntry): Prom const insertPosition = doc.positionAt(Infinity); const edit = new vscode.WorkspaceEdit(); const editText = util.stripAnsi(text); + + if (REPL_OUTPUT_MAX_LINES > 0 && doc.lineCount > REPL_OUTPUT_MAX_LINES) { + edit.delete( + docUri, + new vscode.Range( + new vscode.Position(0, 0), + new vscode.Position(doc.lineCount - REPL_OUTPUT_MAX_LINES, 0) + ) + ); + } + edit.insert(docUri, insertPosition, editText); if (!((await vscode.workspace.applyEdit(edit)) && (await doc.save()))) { return; } + onAppended?.( new vscode.Location(docUri, insertPosition), new vscode.Location(docUri, doc.positionAt(Infinity)) diff --git a/src/results-output/util.ts b/src/results-output/util.ts index 09d757d9a..3fc740bbd 100644 --- a/src/results-output/util.ts +++ b/src/results-output/util.ts @@ -24,7 +24,7 @@ function splitEditQueueForTextBatching( const nextBatch = takeWhile(editQueue, (value, index) => { return index < maxBatchSize && !value.onAppended; }).map((x) => x.text); - const remainingEditQueue = [...editQueue].slice(nextBatch.length); + const remainingEditQueue = editQueue.slice(nextBatch.length); return [nextBatch, remainingEditQueue]; }