@@ -43,12 +43,25 @@ function fixUpError(err: unknown): Error {
43
43
return error ;
44
44
}
45
45
46
+ /**
47
+ * Naively converts a file system path to a python module name.
48
+ *
49
+ * Assumes `.py` file extension and no invalid characters.
50
+ *
51
+ * @param path The path.
52
+ */
53
+ function pathToModule ( path : string ) : string {
54
+ return path . slice ( 0 , path . length - 3 ) . replaceAll ( '/' , '.' ) ;
55
+ }
56
+
46
57
const setUpPythonEnvironment = `
47
58
import jedi
48
59
import pybricks_jedi
49
60
50
61
print('preloading pybricks_jedi...')
51
62
pybricks_jedi.initialize()
63
+ # TODO: this could be moved to pybricks_jedi.initialize()
64
+ pybricks_jedi.complete("from ", 1, 6)
52
65
print('preloading done.')
53
66
` ;
54
67
@@ -68,16 +81,20 @@ async function init(): Promise<void> {
68
81
pyodide . FS . mkdir ( mountDir ) ;
69
82
pyodide . FS . mount ( pyodide . FS . filesystems . MEMFS , { root : '.' } , mountDir ) ;
70
83
84
+ const userModules = new Set < string > ( ) ;
85
+
71
86
self . addEventListener ( 'message' , async ( e ) => {
72
87
if ( pythonMessageWriteUserFile . matches ( e . data ) ) {
73
88
pyodide . FS . writeFile ( `${ mountDir } /${ e . data . path } ` , e . data . contents ) ;
74
89
console . debug ( 'copied' , e . data . path , 'to emscripten fs' ) ;
90
+ userModules . add ( pathToModule ( e . data . path ) ) ;
75
91
return ;
76
92
}
77
93
78
94
if ( pythonMessageDeleteUserFile . matches ( e . data ) ) {
79
95
pyodide . FS . unlink ( `${ mountDir } /${ e . data . path } ` ) ;
80
96
console . debug ( 'removed' , e . data . path , ' from emscripten fs' ) ;
97
+ userModules . delete ( pathToModule ( e . data . path ) ) ;
81
98
return ;
82
99
}
83
100
} ) ;
@@ -103,6 +120,7 @@ async function init(): Promise<void> {
103
120
104
121
const complete = pyodide . runPython ( 'pybricks_jedi.complete' ) ;
105
122
const getSignatures = pyodide . runPython ( 'pybricks_jedi.get_signatures' ) ;
123
+ const updateUserModules = pyodide . runPython ( 'pybricks_jedi.update_user_modules' ) ;
106
124
107
125
self . addEventListener ( 'message' , async ( e ) => {
108
126
if ( pythonMessageSetInterruptBuffer . matches ( e . data ) ) {
@@ -113,6 +131,7 @@ async function init(): Promise<void> {
113
131
if ( pythonMessageComplete . matches ( e . data ) ) {
114
132
console . debug ( 'worker received complete message' ) ;
115
133
try {
134
+ updateUserModules ( userModules ) ;
116
135
const { code, lineNumber, column } = e . data ;
117
136
const list = complete ( code , lineNumber , column ) ;
118
137
self . postMessage ( pythonMessageDidComplete ( list ) ) ;
@@ -125,6 +144,7 @@ async function init(): Promise<void> {
125
144
if ( pythonMessageGetSignature . matches ( e . data ) ) {
126
145
console . debug ( 'worker received getSignatures message' ) ;
127
146
try {
147
+ updateUserModules ( userModules ) ;
128
148
const { code, lineNumber, column } = e . data ;
129
149
const list = getSignatures ( code , lineNumber , column ) ;
130
150
self . postMessage ( pythonMessageDidGetSignature ( list ) ) ;
0 commit comments