Skip to content

Commit 577e521

Browse files
committed
Added a new setting platformio-ide.activateProjectOnTextEditorChange to enable automatic project activation depending on an active opened text editor // Resolve platformio#2410
1 parent 9f05c4d commit 577e521

File tree

4 files changed

+77
-19
lines changed

4 files changed

+77
-19
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## 2.3.1 (2021-??-??)
44

55
- Added a new setting ``platformio-ide.autoOpenPlatformIOIniFile`` to control an automatic opening of the `platformio.ini` file from a project when no other editors are opened (issue [#2419](https://github.com/platformio/platformio-vscode-ide/issues/2419))
6+
- Added a new setting ``platformio-ide.activateProjectOnTextEditorChange`` to enable automatic project activation depending on an active opened text editor (issue [#2410](https://github.com/platformio/platformio-vscode-ide/issues/2410))
67
- Automatically activate project environment opened via "PIO Home > New Project / Open Project" (issue [#2414](https://github.com/platformio/platformio-vscode-ide/issues/2414))
78
- Do not show "Default" environment when a project does not have any build environments (issue [#2450](https://github.com/platformio/platformio-vscode-ide/issues/2450))
89

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,11 @@
517517
"default": false,
518518
"description": "Activate the PlatformIO extension only when a PlatformIO-based project (with `platformio.ini`) is opened in the workspace"
519519
},
520+
"platformio-ide.activateProjectOnTextEditorChange": {
521+
"type": "boolean",
522+
"default": false,
523+
"description": "Automatically activate project depending on an active opened text editor"
524+
},
520525
"platformio-ide.autoOpenPlatformIOIniFile": {
521526
"type": "boolean",
522527
"default": true,

src/home.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ export default class PIOHome {
9999
if (command === 'open_project') {
100100
if (extension.projectObservable) {
101101
extension.projectObservable.saveProjectStateItem(
102-
vscode.Uri.file(params).path,
102+
vscode.Uri.file(params).fsPath,
103103
'activeEnv',
104104
undefined
105105
);

src/project/observable.js

Lines changed: 70 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,15 @@ export default class ProjectObservable {
6666

6767
this.subscriptions = [
6868
this._pool,
69-
// vscode.window.onDidChangeActiveTextEditor(() => this.switchToProject()),
69+
vscode.window.onDidChangeActiveTextEditor(() => {
70+
if (!extension.getSetting('activateProjectOnTextEditorChange')) {
71+
return;
72+
}
73+
const projectDir = this.getActiveEditorProjectDir();
74+
if (projectDir) {
75+
this.switchToProject(projectDir);
76+
}
77+
}),
7078
vscode.workspace.onDidChangeWorkspaceFolders(() =>
7179
this.switchToProject(this.findActiveProjectDir())
7280
),
@@ -105,6 +113,14 @@ export default class ProjectObservable {
105113
}
106114

107115
findActiveProjectDir() {
116+
let projectDir = undefined;
117+
if (extension.getSetting('activateProjectOnTextEditorChange')) {
118+
projectDir = this.getActiveEditorProjectDir();
119+
}
120+
return projectDir || this.getSelectedProjectDir();
121+
}
122+
123+
getSelectedProjectDir() {
108124
const pioProjectDirs = ProjectObservable.getPIOProjectDirs();
109125
const currentActiveDir = this._pool.getActiveProjectDir();
110126
if (pioProjectDirs.length < 1) {
@@ -126,6 +142,27 @@ export default class ProjectObservable {
126142
return pioProjectDirs[0];
127143
}
128144

145+
getActiveEditorProjectDir() {
146+
const pioProjectDirs = ProjectObservable.getPIOProjectDirs();
147+
if (pioProjectDirs.length < 1) {
148+
return undefined;
149+
}
150+
const editor = vscode.window.activeTextEditor;
151+
if (!editor) {
152+
return undefined;
153+
}
154+
const resource = editor.document.uri;
155+
if (resource.scheme !== 'file') {
156+
return undefined;
157+
}
158+
const folder = vscode.workspace.getWorkspaceFolder(resource);
159+
if (!folder || !ProjectObservable.isPIOProjectSync(folder.uri.fsPath)) {
160+
// outside workspace
161+
return undefined;
162+
}
163+
return folder.uri.fsPath;
164+
}
165+
129166
loadProjectStateItem(projectDir, name) {
130167
const data = (this._persistentState.getValue('projects') || {})[projectDir] || {};
131168
return data[name];
@@ -169,30 +206,45 @@ export default class ProjectObservable {
169206
}
170207
this._sbEnvSwitcher.text = '$(root-folder) Loading...';
171208

209+
let currentProjectDir = undefined;
210+
let currentEnvName = undefined;
211+
if (this._pool.getActiveObserver()) {
212+
currentProjectDir = this._pool.getActiveObserver().projectDir;
213+
currentEnvName = this._pool.getActiveObserver().getActiveEnvName();
214+
}
215+
172216
const observer = this._pool.getObserver(projectDir);
173-
let envName = undefined;
174217
if ('envName' in options) {
175-
envName = options.envName;
218+
await observer.switchProjectEnv(options.envName);
176219
} else if (!observer.getActiveEnvName()) {
177-
envName = this.loadProjectStateItem(projectDir, 'activeEnv');
178-
}
179-
await observer.switchProjectEnv(envName);
180-
this._pool.switch(projectDir);
181-
182-
if (this._taskManager) {
183-
this._taskManager.dispose();
184-
this._taskManager = undefined;
220+
await observer.switchProjectEnv(
221+
this.loadProjectStateItem(projectDir, 'activeEnv')
222+
);
185223
}
186-
this._taskManager = new ProjectTaskManager(projectDir, observer);
187224

188-
// open "platformio.ini" if no visible editors
225+
// ignore active project and & env
189226
if (
190-
vscode.window.visibleTextEditors.length === 0 &&
191-
extension.getSetting('autoOpenPlatformIOIniFile')
227+
!currentProjectDir ||
228+
currentProjectDir !== projectDir ||
229+
currentEnvName !== observer.getActiveEnvName()
192230
) {
193-
vscode.window.showTextDocument(
194-
vscode.Uri.file(path.join(projectDir, 'platformio.ini'))
195-
);
231+
this._pool.switch(projectDir);
232+
233+
if (this._taskManager) {
234+
this._taskManager.dispose();
235+
this._taskManager = undefined;
236+
}
237+
this._taskManager = new ProjectTaskManager(projectDir, observer);
238+
239+
// open "platformio.ini" if no visible editors
240+
if (
241+
vscode.window.visibleTextEditors.length === 0 &&
242+
extension.getSetting('autoOpenPlatformIOIniFile')
243+
) {
244+
vscode.window.showTextDocument(
245+
vscode.Uri.file(path.join(projectDir, 'platformio.ini'))
246+
);
247+
}
196248
}
197249

198250
this.updateEnvSwitcher();

0 commit comments

Comments
 (0)