Skip to content

Commit a9d6d1f

Browse files
refactor: collect file state into an object
1 parent e4ed9de commit a9d6d1f

File tree

3 files changed

+67
-49
lines changed

3 files changed

+67
-49
lines changed

editors/vscode/src/extension.ts

Lines changed: 26 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,11 @@ async function languageActivate(context: IContext) {
152152
// Find first document to focus
153153
const editor = window.activeTextEditor;
154154
if (isTypstDocument(editor?.document)) {
155-
commandActivateDoc(editor.document);
155+
focusDocPath(editor.document);
156156
} else {
157157
window.visibleTextEditors.forEach((editor) => {
158158
if (isTypstDocument(editor.document)) {
159-
commandActivateDoc(editor.document);
159+
focusDocPath(editor.document);
160160
}
161161
});
162162
}
@@ -172,27 +172,26 @@ async function languageActivate(context: IContext) {
172172
// }
173173
if (!isTypstDocument(editor?.document)) {
174174
// console.log("not typst", langId, editor?.document.uri.fsPath);
175-
return commandActivateDoc(undefined);
175+
return focusDocPath(undefined);
176176
}
177-
return commandActivateDoc(editor?.document);
177+
return focusDocPath(editor?.document);
178178
}),
179179
);
180180
context.subscriptions.push(
181181
vscode.workspace.onDidOpenTextDocument((doc: vscode.TextDocument) => {
182182
if (doc.isUntitled && window.activeTextEditor?.document === doc) {
183183
if (isTypstDocument(doc)) {
184-
return commandActivateDocPath(doc, "/untitled/" + doc.uri.fsPath);
184+
return focusDocPath(doc);
185185
} else {
186-
return commandActivateDoc(undefined);
186+
return focusDocPath(undefined);
187187
}
188188
}
189189
}),
190190
);
191191
context.subscriptions.push(
192192
vscode.workspace.onDidCloseTextDocument((doc: vscode.TextDocument) => {
193-
if (extensionState.mut.focusingDoc === doc) {
194-
extensionState.mut.focusingDoc = undefined;
195-
commandActivateDoc(undefined);
193+
if (extensionState.mut.focusing.doc === doc) {
194+
focusDocPath(undefined);
196195
}
197196
}),
198197
);
@@ -245,6 +244,24 @@ async function openExternal(target: string): Promise<void> {
245244
await vscode.env.openExternal(uri);
246245
}
247246

247+
function focusDocPath(doc: vscode.TextDocument | undefined) {
248+
const fsPath = doc
249+
? doc.isUntitled
250+
? "/untitled/" + doc.uri.fsPath
251+
: doc.uri.fsPath
252+
: undefined;
253+
254+
// Changes focus state.
255+
extensionState.mut.focusing.focusMain(doc, fsPath);
256+
257+
// Changes status bar.
258+
const formatString = statusBarFormatString();
259+
triggerStatusBar(
260+
// Shows the status bar only the last focusing file is not closed (opened)
261+
!!formatString && !!(fsPath || extensionState.mut.focusing.doc?.isClosed === false),
262+
);
263+
}
264+
248265
async function commandGetCurrentDocumentMetrics(): Promise<any> {
249266
const activeEditor = window.activeTextEditor;
250267
if (activeEditor === undefined) {
@@ -507,34 +524,6 @@ async function initTemplate(context: vscode.ExtensionContext, inPlace: boolean,
507524
}
508525
}
509526

510-
function commandActivateDoc(doc: vscode.TextDocument | undefined) {
511-
commandActivateDocPath(doc, doc?.uri.fsPath);
512-
}
513-
514-
let focusMainTimeout: NodeJS.Timeout | undefined = undefined;
515-
function commandActivateDocPath(doc: vscode.TextDocument | undefined, fsPath: string | undefined) {
516-
// console.log("focus main", fsPath, new Error().stack);
517-
extensionState.mut.focusingFile = fsPath;
518-
if (fsPath) {
519-
extensionState.mut.focusingDoc = doc;
520-
}
521-
if (extensionState.mut.focusingDoc?.isClosed) {
522-
extensionState.mut.focusingDoc = undefined;
523-
}
524-
const formatString = statusBarFormatString();
525-
// remove the status bar until the last focusing file is closed
526-
triggerStatusBar(
527-
!!formatString && !!(fsPath || extensionState.mut.focusingDoc?.isClosed === false),
528-
);
529-
530-
if (focusMainTimeout) {
531-
clearTimeout(focusMainTimeout);
532-
}
533-
focusMainTimeout = setTimeout(() => {
534-
tinymist.executeCommand("tinymist.focusMain", [fsPath]);
535-
}, 100);
536-
}
537-
538527
async function commandRunCodeLens(...args: string[]): Promise<void> {
539528
if (args.length === 0) {
540529
return;

editors/vscode/src/focus.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { tinymist } from "./lsp";
2+
import * as vscode from "vscode";
3+
4+
export class FocusState {
5+
private mainTimeout: NodeJS.Timeout | undefined = undefined;
6+
private mainPath: string | undefined = undefined;
7+
private mainDoc: vscode.TextDocument | undefined = undefined;
8+
9+
reset() {
10+
if (this.mainTimeout) {
11+
clearTimeout(this.mainTimeout);
12+
}
13+
}
14+
15+
// Informs the server after a while. This is not done intemediately to avoid
16+
// the cases that the user removes the file from the editor and then opens
17+
// another one in a short time.
18+
focusMain(doc?: vscode.TextDocument, fsPath?: string) {
19+
if (this.mainTimeout) {
20+
clearTimeout(this.mainTimeout);
21+
}
22+
this.mainDoc = doc;
23+
if (this.mainDoc?.isClosed) {
24+
this.mainDoc = undefined;
25+
}
26+
this.mainPath = fsPath;
27+
this.mainTimeout = setTimeout(async () => {
28+
tinymist.executeCommand("tinymist.focusMain", [fsPath]);
29+
}, 100);
30+
}
31+
32+
get path() {
33+
return this.mainPath;
34+
}
35+
get doc() {
36+
return this.mainDoc;
37+
}
38+
}

editors/vscode/src/state.ts

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as vscode from "vscode";
22
import { PreviewPanelContext } from "./features/preview";
3+
import { FocusState } from "./focus";
34

45
export type ExtensionContext = vscode.ExtensionContext;
56

@@ -24,12 +25,9 @@ interface ExtensionState {
2425
renderDocs: boolean;
2526
};
2627
mut: {
27-
focusingFile: string | undefined;
28-
focusingDoc: vscode.TextDocument | undefined;
28+
focusing: FocusState;
2929
focusingPreviewPanelContext: PreviewPanelContext | undefined;
3030
};
31-
getFocusingFile(): string | undefined;
32-
getFocusingDoc(): vscode.TextDocument | undefined;
3331
getFocusingPreviewPanelContext(): PreviewPanelContext | undefined;
3432
}
3533

@@ -54,16 +52,9 @@ export const extensionState: ExtensionState = {
5452
renderDocs: false,
5553
},
5654
mut: {
57-
focusingFile: undefined,
58-
focusingDoc: undefined,
55+
focusing: new FocusState(),
5956
focusingPreviewPanelContext: undefined,
6057
},
61-
getFocusingFile() {
62-
return extensionState.mut.focusingFile;
63-
},
64-
getFocusingDoc() {
65-
return extensionState.mut.focusingDoc;
66-
},
6758
getFocusingPreviewPanelContext() {
6859
return extensionState.mut.focusingPreviewPanelContext;
6960
},

0 commit comments

Comments
 (0)