@@ -13,6 +13,7 @@ const extensionId = 'write-your-python-program';
1313const python3ConfigKey = 'python3Cmd' ;
1414const verboseConfigKey = 'verbose' ;
1515const debugConfigKey = 'debug' ;
16+ const languageKey = 'language' ;
1617const disableTypecheckingConfigKey = 'disableTypechecking' ;
1718const isWindows = process . platform === "win32" ;
1819const exeExt = isWindows ? ".exe" : "" ;
@@ -217,6 +218,19 @@ function disableTypechecking(context: vscode.ExtensionContext): boolean {
217218 return ! ! config [ disableTypecheckingConfigKey ] ;
218219}
219220
221+ function getLanguage ( context : vscode . ExtensionContext ) : 'en' | 'de' | undefined {
222+ const config = vscode . workspace . getConfiguration ( extensionId ) ;
223+ const lang = config [ languageKey ] ;
224+ if ( lang === 'english' ) {
225+ return 'en' ;
226+ } else if ( lang === 'german' ) {
227+ return 'de' ;
228+ } else {
229+ return undefined ;
230+ }
231+ }
232+
233+
220234async function fixPylanceConfig (
221235 context : vscode . ExtensionContext ,
222236 folder ?: vscode . WorkspaceFolder
@@ -231,6 +245,30 @@ async function fixPylanceConfig(
231245 const target = folder ? vscode . ConfigurationTarget . WorkspaceFolder
232246 : vscode . ConfigurationTarget . Workspace ;
233247
248+ const errors : string [ ] = [ ] ;
249+
250+ // Two special errors
251+ const noWorkspaceOpen = '__NO_WORKSPACE_OPEN__' ;
252+
253+ // helper to update config and collect errors; treat "no folder open" specially
254+ async function tryUpdate ( key : string , value : any ) {
255+ // If target is workspace-wide and there is no workspace open at all
256+ const workspaceFolders = vscode . workspace . workspaceFolders ;
257+ if ( ! workspaceFolders || workspaceFolders . length === 0 ) {
258+ // nothing to update at workspace level
259+ errors . push ( noWorkspaceOpen ) ;
260+ return ;
261+ }
262+
263+ try {
264+ await cfg . update ( key , value , target ) ;
265+ } catch ( e ) {
266+ // If the error message explicitly mentions the workspace/folder, capture it, but prefer the proactive checks above
267+ const msg = e instanceof Error ? e . message : String ( e ) ;
268+ errors . push ( `Failed to update ${ key } : ${ msg } ` ) ;
269+ }
270+ }
271+
234272 // wildcard warnings
235273 const keyOverride = 'analysis.diagnosticSeverityOverrides' ;
236274 const overrides = cfg . get < Record < string , string > > ( keyOverride ) ?? { } ;
@@ -239,33 +277,36 @@ async function fixPylanceConfig(
239277 ...overrides ,
240278 reportWildcardImportFromLibrary : 'none' ,
241279 } ;
242- await cfg . update (
243- 'analysis.diagnosticSeverityOverrides' ,
244- updated ,
245- target
246- ) ;
280+ await tryUpdate ( keyOverride , updated ) ;
247281 }
248282
249283 // extraPaths
250284 const keyExtraPaths = 'analysis.extraPaths' ;
251285 const extra = cfg . get < string [ ] > ( keyExtraPaths ) ?? [ ] ;
252286 if ( extra . length !== 1 || extra [ 0 ] !== libDir ) {
253- await cfg . update (
254- keyExtraPaths ,
255- [ libDir ] ,
256- target
257- ) ;
287+ await tryUpdate ( keyExtraPaths , [ libDir ] ) ;
258288 }
259289
260290 // typechecking off
261291 const keyMode = 'analysis.typeCheckingMode' ;
262292 const mode = cfg . get < string > ( keyMode ) ?? '' ;
263293 if ( mode !== 'off' ) {
264- await cfg . update (
265- 'analysis.typeCheckingMode' ,
266- 'off' ,
267- target
268- ) ;
294+ await tryUpdate ( keyMode , 'off' ) ;
295+ }
296+
297+ if ( errors . length > 0 ) {
298+ let msg : string ;
299+ if ( errors . every ( e => e === noWorkspaceOpen ) ) {
300+ msg = 'Write Your Python Program: settings were not changed because no folder is open. Open a folder to apply wypp settings.' ;
301+ } else {
302+ const sanitized = errors . map ( e => e === noWorkspaceOpen ? 'Skipped workspace update because no folder is open' : e ) ;
303+ msg = `Write Your Python Program: failed to update settings. ` + sanitized . join ( '. ' ) ;
304+ }
305+ try {
306+ vscode . window . showWarningMessage ( msg ) ;
307+ } catch ( _e ) {
308+ // ignore any error while showing the warning
309+ }
269310 }
270311}
271312
@@ -386,7 +427,6 @@ export async function activate(context: vscode.ExtensionContext) {
386427 "run" ,
387428 "▶ RUN" ,
388429 async ( cmdId ) => {
389- await fixPylanceConfig ( context ) ;
390430 const file =
391431 ( vscode . window . activeTextEditor ) ?
392432 vscode . window . activeTextEditor . document . fileName :
@@ -399,6 +439,7 @@ export async function activate(context: vscode.ExtensionContext) {
399439 vscode . window . showWarningMessage ( 'Not a python file' ) ;
400440 return ;
401441 }
442+ await fixPylanceConfig ( context ) ;
402443 await vscode . window . activeTextEditor ?. document . save ( ) ;
403444 const pyCmd = getPythonCmd ( pyExt ) ;
404445 let verboseOpt = "" ;
@@ -410,14 +451,19 @@ export async function activate(context: vscode.ExtensionContext) {
410451 if ( verboseOpt !== "" ) {
411452 verboseOpt = " " + verboseOpt + " --no-clear" ;
412453 }
454+ const lang = getLanguage ( context ) ;
455+ let langOpt = "" ;
456+ if ( lang ) {
457+ langOpt = " --lang " + lang ;
458+ }
413459 const disableOpt = disableTypechecking ( context ) ? " --no-typechecking" : "" ;
414460 if ( pyCmd . kind !== "error" ) {
415461 const pythonCmd = commandListToArgument ( pyCmd . cmd ) ;
416462 const cmdTerm = await startTerminal (
417463 terminals [ cmdId ] ?. terminal ,
418464 "WYPP - RUN" ,
419465 pythonCmd + " " + fileToCommandArgument ( runProg ) + verboseOpt +
420- disableOpt +
466+ disableOpt + langOpt +
421467 " --interactive " +
422468 " --change-directory " +
423469 fileToCommandArgument ( file )
0 commit comments