Skip to content

Commit

Permalink
Initialize result-doc only once
Browse files Browse the repository at this point in the history
Fixes #1542
  • Loading branch information
PEZ committed Feb 20, 2022
1 parent f5ecad0 commit 128ad52
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
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]
- [Print stacktrace link in REPL gets duplicated](https://github.com/BetterThanTomorrow/calva/issues/1542)

## [2.0.244] - 2022-02-20
- [Add custom hover snippets](https://github.com/BetterThanTomorrow/calva/issues/1471)
Expand Down
34 changes: 21 additions & 13 deletions src/results-output/results-doc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ function outputFileDir() {
}
}

let isInitialized = false;

const DOC_URI = () => {
return vscode.Uri.joinPath(outputFileDir(), RESULTS_DOC_NAME);
};
Expand Down Expand Up @@ -131,24 +133,15 @@ export function setContextForOutputWindowActive(isActive: boolean): void {
}

export async function initResultsDoc(): Promise<vscode.TextDocument> {
const docUri = DOC_URI();
await vscode.workspace.fs.createDirectory(outputFileDir());
let resultsDoc: vscode.TextDocument;
try {
resultsDoc = await vscode.workspace.openTextDocument(DOC_URI());
resultsDoc = await vscode.workspace.openTextDocument(docUri);
} catch (e) {
await util.writeTextToFile(DOC_URI(), '');
resultsDoc = await vscode.workspace.openTextDocument(DOC_URI());
await util.writeTextToFile(docUri, '');
resultsDoc = await vscode.workspace.openTextDocument(docUri);
}
const greetings = `${START_GREETINGS}\n\n`;
const edit = new vscode.WorkspaceEdit();
const fullRange = new vscode.Range(
resultsDoc.positionAt(0),
resultsDoc.positionAt(Infinity)
);
edit.replace(DOC_URI(), fullRange, greetings);
await vscode.workspace.applyEdit(edit);
resultsDoc.save();

if (config.getConfig().autoOpenREPLWindow) {
const resultsEditor = await vscode.window.showTextDocument(
resultsDoc,
Expand All @@ -160,6 +153,20 @@ export async function initResultsDoc(): Promise<vscode.TextDocument> {
resultsEditor.selection = new vscode.Selection(lastPos, lastPos);
resultsEditor.revealRange(new vscode.Range(firstPos, firstPos));
}
if (isInitialized) {
return resultsDoc;
}

const greetings = `${START_GREETINGS}\n\n`;
const edit = new vscode.WorkspaceEdit();
const fullRange = new vscode.Range(
resultsDoc.positionAt(0),
resultsDoc.positionAt(Infinity)
);
edit.replace(docUri, fullRange, greetings);
await vscode.workspace.applyEdit(edit);
resultsDoc.save();

// For some reason onDidChangeTextEditorViewColumn won't fire
state.extensionContext.subscriptions.push(
vscode.window.onDidChangeActiveTextEditor((event) => {
Expand Down Expand Up @@ -222,6 +229,7 @@ export async function initResultsDoc(): Promise<vscode.TextDocument> {
);
}
replHistory.resetState();
isInitialized = true;
return resultsDoc;
}

Expand Down

0 comments on commit 128ad52

Please sign in to comment.