@@ -27,10 +27,14 @@ import { is } from './Utility/System/guards';
2727let remoteConnection : RemoteConnection | undefined ;
2828
2929// In the main thread, grab the logger and localize functions directly.
30- const logFn = isMainThread ? require ( './logger' ) . log : ( ) => { } ;
31- const noteFn = isMainThread ? require ( './logger' ) . note : ( ) => { } ;
32- const localizeFn = isMainThread ? require ( './localization' ) . localize : ( info : { key : string ; comment : string [ ] } | string , message : string , ...args : ( string | number | boolean | undefined | null ) [ ] ) => message . replace ( / \{ ( \d + ) \} / g, ( _ , index ) => String ( args [ Number ( index ) ] ?? 'undefined' ) ) ;
33- const getOutputChannelLoggerFn = isMainThread ? require ( './logger' ) . getOutputChannelLogger : ( ) => undefined ;
30+ import { localize as localizationLocalize } from './localization' ;
31+ import * as logger from './logger' ;
32+ import { getOutputChannelLogger , note as loggerNote } from './logger' ;
33+
34+ const logFn = isMainThread ? logger . log : ( ) => { } ;
35+ const noteFn = isMainThread ? loggerNote : ( ) => { } ;
36+ const localizeFn = isMainThread ? localizationLocalize : ( info : { key : string ; comment : string [ ] } | string , message : string , ...args : ( string | number | boolean | undefined | null ) [ ] ) => message . replace ( / \{ ( \d + ) \} / g, ( _ , index ) => String ( args [ Number ( index ) ] ?? 'undefined' ) ) ;
37+ const getOutputChannelLoggerFn = isMainThread ? getOutputChannelLogger : ( ) => undefined ;
3438
3539/**
3640 * Used when this is imported in a module running in a worker thread.
@@ -79,17 +83,17 @@ export function note(message: string | Promise<string>) {
7983 * @param message The message to localize.
8084 * @param args The arguments for the message.
8185 */
82- export function localize ( info : { key : string ; comment : string [ ] } | string , message : string , ...args : ( string | number | boolean | undefined | null ) [ ] ) : Promise < string > | string {
83- return isMainThread ?
84- // main thread uses localize directly
85- localizeFn ( info , message , ...args ) :
86-
87- // worker (non-main) thread requests localization via remote connection
88- remoteConnection ?. request ( 'localize' , info , message , ... args ) ||
89-
90- // fallback to simple replacement if no remote connection is available
86+ export const localize : AsyncLocalizeFunc = ( infoOrKey , message , ...args ) => {
87+ if ( isMainThread ) {
88+ if ( typeof infoOrKey === 'string' ) {
89+ return localizeFn ( infoOrKey , message , ...args ) ;
90+ } else {
91+ return localizeFn ( infoOrKey as LocalizeInfo , message , ... args ) ;
92+ }
93+ }
94+ return remoteConnection ?. request ( 'localize' , infoOrKey , message , ... args ) ??
9195 message . replace ( / \{ ( \d + ) \} / g, ( _ , index ) => String ( args [ Number ( index ) ] ?? 'undefined' ) ) ;
92- }
96+ } ;
9397
9498export function appendLineAtLevel ( level : number , message : string | Promise < string > ) : void {
9599 if ( is . promise ( message ) ) {
@@ -168,10 +172,11 @@ export interface LocalizeInfo {
168172}
169173
170174/** The type for the localize function for string localization, but can work asynchronously (can be called via remoting). */
171- export interface AsyncLocalizeFunc {
172- ( info : LocalizeInfo , message : string , ...args : ( string | number | boolean | undefined | null ) [ ] ) : string | Promise < string > ;
173- ( key : string , message : string , ...args : ( string | number | boolean | undefined | null ) [ ] ) : string | Promise < string > ;
174- }
175+ export type AsyncLocalizeFunc = (
176+ infoOrKey : LocalizeInfo | string ,
177+ message : string ,
178+ ...args : ( string | number | boolean | undefined | null ) [ ]
179+ ) => string | Promise < string > ;
175180
176181/**
177182 * A cancellation token is passed to an asynchronous or long running
@@ -436,7 +441,7 @@ export async function searchFolders(folders: string[], filename: string | RegExp
436441
437442 // If it is a file, check for a match.
438443 case stats . isFile ( ) :
439- if ( nameMatches ( item ) && ( predicate ? await predicate ! ( fullPath ) : true ) ) {
444+ if ( nameMatches ( item ) && ( predicate ? await predicate ( fullPath ) : true ) ) {
440445 return options . result ??= fullPath ;
441446 }
442447 break ;
0 commit comments