1
1
import type { Memento , Uri , WorkspaceConfiguration } from "vscode" ;
2
2
import { window , workspace as vsWorkspace } from "vscode" ;
3
3
import { extName , getProjectPath , settingsStubsBasePath } from "./api.mjs" ;
4
- import { join , relative } from "path" ;
4
+ import { dirname , join , relative } from "path" ;
5
5
import { PicoMpyCom } from "@paulober/pico-mpy-com" ;
6
+ import { searchFile } from "./osHelper.mjs" ;
6
7
7
8
export enum SettingsKey {
8
9
autoConnect = "autoConnect" ,
@@ -75,6 +76,10 @@ export default class Settings {
75
76
return this . config . update ( key , value , true ) ;
76
77
}
77
78
79
+ public updateWorkspaceFolder < T > ( key : string , value : T ) : Thenable < void > {
80
+ return this . config . update ( key , value , null ) ;
81
+ }
82
+
78
83
public updatePython < T > ( key : string , value : T ) : Thenable < void > {
79
84
return this . pythonConfig . update ( key , value , null ) ;
80
85
}
@@ -126,22 +131,22 @@ export default class Settings {
126
131
* Returns the absolute path to the sync folder. If the sync folder is undefined,
127
132
* the project path is returned.
128
133
*
129
- * @returns the absolute path to the sync folder
134
+ * @returns The absolute path to the sync folder and if the setting is undefined
130
135
*/
131
- public getSyncFolderAbsPath ( ) : string | undefined {
136
+ public getSyncFolderAbsPath ( ) : [ string | undefined , boolean ] {
132
137
const syncDir = this . getString ( SettingsKey . syncFolder ) ;
133
138
const projectDir = getProjectPath ( ) ;
134
139
135
- if ( syncDir === undefined ) {
136
- return projectDir ;
140
+ if ( syncDir === undefined || syncDir . length === 0 ) {
141
+ return [ projectDir , true ] ;
137
142
}
138
143
139
144
if ( projectDir === undefined ) {
140
145
// How can this ever happen??!
141
- return undefined ;
146
+ return [ undefined , false ] ;
142
147
}
143
148
144
- return join ( projectDir , syncDir ) ;
149
+ return [ join ( projectDir , syncDir ) , false ] ;
145
150
}
146
151
147
152
/**
@@ -158,14 +163,41 @@ export default class Settings {
158
163
public async requestSyncFolder (
159
164
actionTitle : string
160
165
) : Promise < [ string , string ] | undefined > {
161
- const syncFolder = this . getSyncFolderAbsPath ( ) ;
166
+ let [ syncFolder , syncSettingNotSet ] = this . getSyncFolderAbsPath ( ) ;
162
167
const projectDir = getProjectPath ( ) ;
163
168
164
169
if ( projectDir === undefined ) {
165
170
// How can this ever happen??!
166
171
return ;
167
172
}
168
173
174
+ // sync folder setting not set
175
+ if ( syncSettingNotSet ) {
176
+ const activationFile = searchFile ( projectDir , ".micropico" ) ;
177
+ const actParent = activationFile ? dirname ( activationFile ) : undefined ;
178
+
179
+ // check if activation file is not in project root
180
+ if ( activationFile && actParent && actParent !== projectDir ) {
181
+ syncFolder = actParent ;
182
+
183
+ // update transparent to the user
184
+ await this . updateWorkspaceFolder (
185
+ SettingsKey . syncFolder ,
186
+ relative ( projectDir , actParent )
187
+ ) ;
188
+
189
+ void window . showWarningMessage (
190
+ `Sync folder has been set to \`${ relative (
191
+ projectDir ,
192
+ actParent
193
+ ) } \` ` +
194
+ "because the `.micropico` file was found in a subdirectory " +
195
+ "and no sync folder was set. To disable this behavior, " +
196
+ "set a sync folder in the settings to `.` for the project root."
197
+ ) ;
198
+ }
199
+ }
200
+
169
201
let additionalSyncFolders = this . getArray (
170
202
SettingsKey . additionalSyncFolders
171
203
) ?. map ( sf => join ( projectDir , sf ) ) ;
0 commit comments