Skip to content

Commit

Permalink
Truncate repl output as length crosses threshold
Browse files Browse the repository at this point in the history
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
  • Loading branch information
julienvincent committed Jan 8, 2023
1 parent ac022a4 commit 99b6e9d
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
2 changes: 2 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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,
Expand Down
16 changes: 16 additions & 0 deletions src/results-output/results-doc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ const RESULTS_DOC_NAME = `output.${config.REPL_FILE_EXT}`;
const REPL_OUTPUT_THROTTLE_RATE = vscode.workspace
.getConfiguration()
.get<number>(config.REPL_OUTPUT_THROTTLE_RATE_CONFIG_KEY);
const REPL_OUTPUT_MAX_LINES = vscode.workspace
.getConfiguration()
.get<number>(config.REPL_OUTPUT_MAX_LINES_CONFIG_KEY);

const PROMPT_HINT = 'Use `alt+enter` to evaluate';

const START_GREETINGS = [
Expand Down Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion src/results-output/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}

Expand Down

0 comments on commit 99b6e9d

Please sign in to comment.