11import type { Memento , Uri , WorkspaceConfiguration } from "vscode" ;
22import { window , workspace as vsWorkspace } from "vscode" ;
33import { PyboardRunner } from "@paulober/pyboard-serial-com" ;
4- import { extName , getProjectPath } from "./api.mjs" ;
4+ import { extName , getProjectPath , settingsStubsBasePath } from "./api.mjs" ;
55import { join , relative } from "path" ;
66
77export enum SettingsKey {
@@ -24,10 +24,12 @@ export type Setting = string | boolean | string[] | null | undefined;
2424
2525export default class Settings {
2626 private config : WorkspaceConfiguration ;
27+ private pythonConfig : WorkspaceConfiguration ;
2728 public context : Memento ;
2829
2930 constructor ( context : Memento ) {
3031 this . config = vsWorkspace . getConfiguration ( extName ) ;
32+ this . pythonConfig = vsWorkspace . getConfiguration ( "python.analysis" ) ;
3133
3234 this . context = context ;
3335 }
@@ -36,7 +38,11 @@ export default class Settings {
3638 this . config = vsWorkspace . getConfiguration ( extName ) ;
3739 }
3840
39- public get ( key : SettingsKey ) : Setting {
41+ public reloadPython ( ) : void {
42+ this . pythonConfig = vsWorkspace . getConfiguration ( "python.analysis" ) ;
43+ }
44+
45+ public get ( key : SettingsKey | string ) : Setting {
4046 return this . config . get ( key ) ;
4147 }
4248
@@ -58,10 +64,20 @@ export default class Settings {
5864 return Array . isArray ( value ) ? value : undefined ;
5965 }
6066
61- public update < T > ( key : SettingsKey , value : T ) : Thenable < void > {
67+ public getArrayPython ( key : string ) : string [ ] | undefined {
68+ const value = this . pythonConfig . get ( key ) ;
69+
70+ return Array . isArray ( value ) ? value : undefined ;
71+ }
72+
73+ public update < T > ( key : SettingsKey | string , value : T ) : Thenable < void > {
6274 return this . config . update ( key , value , true ) ;
6375 }
6476
77+ public updatePython < T > ( key : string , value : T ) : Thenable < void > {
78+ return this . pythonConfig . update ( key , value , null ) ;
79+ }
80+
6581 // helpers
6682 /**
6783 * Get the COM port to connect to.
@@ -200,6 +216,40 @@ export default class Settings {
200216 public getIngoredSyncItems ( ) : string [ ] {
201217 return this . getArray ( SettingsKey . pyIgnore ) || [ ] ;
202218 }
219+
220+ public async updateStubsPath ( newStubs : string ) : Promise < boolean > {
221+ // catch if stubs where updated before after starting the extension
222+ this . reloadPython ( ) ;
223+
224+ const typeshedPaths = this . getArrayPython ( "typeshedPaths" ) ;
225+ const extraPaths = this . getArrayPython ( "extraPaths" ) ;
226+
227+ if ( typeshedPaths === undefined || extraPaths === undefined ) {
228+ return false ;
229+ }
230+
231+ // Remove paths starting with '~/.micropico-stubs'
232+ const filteredTypeshedPaths = typeshedPaths . filter (
233+ path =>
234+ ! path . startsWith ( settingsStubsBasePath ( ) ) &&
235+ ! path . includes ( "Pico-W-Stub" )
236+ ) ;
237+ const filteredExtraPaths = extraPaths . filter (
238+ path =>
239+ ! path . startsWith ( settingsStubsBasePath ( ) ) &&
240+ ! path . includes ( "Pico-W-Stub" )
241+ ) ;
242+
243+ // Add newStubs to both arrays
244+ filteredTypeshedPaths . push ( newStubs ) ;
245+ filteredExtraPaths . push ( newStubs ) ;
246+
247+ // Update the settings with the modified arrays
248+ await this . updatePython ( "typeshedPaths" , filteredTypeshedPaths ) ;
249+ await this . updatePython ( "extraPaths" , filteredExtraPaths ) ;
250+
251+ return true ;
252+ }
203253}
204254
205255/**
0 commit comments