@@ -152,11 +152,11 @@ async function languageActivate(context: IContext) {
152
152
// Find first document to focus
153
153
const editor = window . activeTextEditor ;
154
154
if ( isTypstDocument ( editor ?. document ) ) {
155
- commandActivateDoc ( editor . document ) ;
155
+ focusDocPath ( editor . document ) ;
156
156
} else {
157
157
window . visibleTextEditors . forEach ( ( editor ) => {
158
158
if ( isTypstDocument ( editor . document ) ) {
159
- commandActivateDoc ( editor . document ) ;
159
+ focusDocPath ( editor . document ) ;
160
160
}
161
161
} ) ;
162
162
}
@@ -172,27 +172,26 @@ async function languageActivate(context: IContext) {
172
172
// }
173
173
if ( ! isTypstDocument ( editor ?. document ) ) {
174
174
// console.log("not typst", langId, editor?.document.uri.fsPath);
175
- return commandActivateDoc ( undefined ) ;
175
+ return focusDocPath ( undefined ) ;
176
176
}
177
- return commandActivateDoc ( editor ?. document ) ;
177
+ return focusDocPath ( editor ?. document ) ;
178
178
} ) ,
179
179
) ;
180
180
context . subscriptions . push (
181
181
vscode . workspace . onDidOpenTextDocument ( ( doc : vscode . TextDocument ) => {
182
182
if ( doc . isUntitled && window . activeTextEditor ?. document === doc ) {
183
183
if ( isTypstDocument ( doc ) ) {
184
- return commandActivateDocPath ( doc , "/untitled/" + doc . uri . fsPath ) ;
184
+ return focusDocPath ( doc ) ;
185
185
} else {
186
- return commandActivateDoc ( undefined ) ;
186
+ return focusDocPath ( undefined ) ;
187
187
}
188
188
}
189
189
} ) ,
190
190
) ;
191
191
context . subscriptions . push (
192
192
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 ) ;
196
195
}
197
196
} ) ,
198
197
) ;
@@ -245,6 +244,24 @@ async function openExternal(target: string): Promise<void> {
245
244
await vscode . env . openExternal ( uri ) ;
246
245
}
247
246
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
+
248
265
async function commandGetCurrentDocumentMetrics ( ) : Promise < any > {
249
266
const activeEditor = window . activeTextEditor ;
250
267
if ( activeEditor === undefined ) {
@@ -507,34 +524,6 @@ async function initTemplate(context: vscode.ExtensionContext, inPlace: boolean,
507
524
}
508
525
}
509
526
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
-
538
527
async function commandRunCodeLens ( ...args : string [ ] ) : Promise < void > {
539
528
if ( args . length === 0 ) {
540
529
return ;
0 commit comments