Skip to content

Commit 3ec074e

Browse files
author
Tim Etchells
committed
Fix command enablement
Connections are filtered by active-ness when appropriate If Starting projects are accepted, so are Starting - Debug projects Refresh command now wipes cached projects
1 parent d57a9bc commit 3ec074e

File tree

7 files changed

+49
-21
lines changed

7 files changed

+49
-21
lines changed

dev/src/command/CommandUtil.ts

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import * as vscode from "vscode";
22

3-
import { Log } from "../Logger";
3+
import Log from "../Logger";
44
import Project from "../microclimate/project/Project";
55
import Connection from "../microclimate/connection/Connection";
66
import ConnectionManager from "../microclimate/connection/ConnectionManager";
7-
import { ProjectState } from "../microclimate/project/ProjectState";
7+
import ProjectState from "../microclimate/project/ProjectState";
88

99
import Commands from "../constants/Commands";
1010

@@ -84,7 +84,7 @@ export function createCommands(): vscode.Disposable[] {
8484
* @param acceptableStates - If at least one state is passed, only projects in one of these states will be presented to the user.
8585
*/
8686
export async function promptForProject(...acceptableStates: ProjectState.AppStates[]): Promise<Project | undefined> {
87-
const project = await promptForResourceInner(false, true, ...acceptableStates);
87+
const project = await promptForResourceInner(false, true, false, ...acceptableStates);
8888
if (project instanceof Project) {
8989
return project as Project;
9090
}
@@ -97,12 +97,16 @@ export async function promptForProject(...acceptableStates: ProjectState.AppStat
9797
return undefined;
9898
}
9999

100-
export async function promptForConnection(): Promise<Connection | undefined> {
100+
export async function promptForConnection(activeOnly: boolean): Promise<Connection | undefined> {
101101
if (ConnectionManager.instance.connections.length === 1) {
102-
return ConnectionManager.instance.connections[0];
102+
const onlyConnection = ConnectionManager.instance.connections[0];
103+
if (onlyConnection.isConnected || !activeOnly) {
104+
return onlyConnection;
105+
}
106+
// else continue to promptForResource, which will report if there are no suitable connections.
103107
}
104108

105-
const connection = await promptForResourceInner(true, false);
109+
const connection = await promptForResourceInner(true, false, activeOnly);
106110
if (connection instanceof Connection) {
107111
return connection as Connection;
108112
}
@@ -115,38 +119,56 @@ export async function promptForConnection(): Promise<Connection | undefined> {
115119
return undefined;
116120
}
117121

118-
export async function promptForResource(...acceptableStates: ProjectState.AppStates[]): Promise<Project | Connection | undefined> {
119-
return promptForResourceInner(true, true, ...acceptableStates);
122+
export async function promptForResource(activeConnectionsOnly: boolean, ...acceptableStates: ProjectState.AppStates[]):
123+
Promise<Project | Connection | undefined> {
124+
125+
return promptForResourceInner(true, true, activeConnectionsOnly, ...acceptableStates);
120126
}
121127

122-
async function promptForResourceInner(includeConnections: boolean, includeProjects: boolean, ...acceptableStates: ProjectState.AppStates[]):
123-
Promise<Project | Connection | undefined> {
128+
/**
129+
* If !includeConnections, activeConnectionsOnly is ignored.
130+
* If !includeProjects, acceptableStates is ignored.
131+
*/
132+
async function promptForResourceInner(includeConnections: boolean, includeProjects: boolean, activeConnectionsOnly: boolean,
133+
...acceptableStates: ProjectState.AppStates[]):
134+
Promise<Project | Connection | undefined> {
124135

125136
if (!includeConnections && !includeProjects) {
126137
// One of these must always be set
127138
Log.e("Neither connection or projects are to be included!");
128139
return undefined;
129140
}
130141
else if (!includeProjects && acceptableStates.length > 0) {
131-
// This is a misuse of this function
142+
// This doesn't actually matter, but we're going to log this misuse anyway
132143
Log.e("Not including projects, but acceptable states were specified!");
133144
acceptableStates = [];
134145
}
146+
else if (!includeConnections && activeConnectionsOnly) {
147+
// This doesn't actually matter, but we're going to log this misuse anyway
148+
Log.e("Not including connections, but activeConnectionsOnly is set!");
149+
}
135150

136151
const choices: vscode.QuickPickItem[] = [];
137152

138153
const connections = ConnectionManager.instance.connections;
139154
if (includeConnections) {
140-
// Convert each Connected Connection into a QuickPickItem
141-
// choices.push(... (connections.filter( (conn) => conn.isConnected)));
142-
choices.push(...connections);
155+
if (activeConnectionsOnly) {
156+
choices.push(...(connections.filter( (conn) => conn.isConnected)));
157+
}
158+
else {
159+
choices.push(...connections);
160+
}
143161
}
144162

145163
if (includeProjects) {
146164
// for now, assume if they want Started, they also accept Debugging. This may change.
147165
if (acceptableStates.includes(ProjectState.AppStates.STARTED) && !acceptableStates.includes(ProjectState.AppStates.DEBUGGING)) {
148166
acceptableStates.push(ProjectState.AppStates.DEBUGGING);
149167
}
168+
// same for Starting / Starting - Debug
169+
if (acceptableStates.includes(ProjectState.AppStates.STARTING) && !acceptableStates.includes(ProjectState.AppStates.DEBUG_STARTING)) {
170+
acceptableStates.push(ProjectState.AppStates.DEBUG_STARTING);
171+
}
150172

151173
// Logger.log("Accept states", acceptableStates);
152174

dev/src/command/NewMCProjectCmd.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import Endpoints from "../constants/Endpoints";
99
export default async function newMCProjectCmd(connection: Connection): Promise<void> {
1010
Log.d("newMCProjectCmd invoked");
1111
if (connection == null) {
12-
const selected = await promptForConnection();
12+
const selected = await promptForConnection(true);
1313
if (selected == null) {
1414
Log.d("User cancelled prompt for resource");
1515
// user cancelled

dev/src/command/OpenInBrowserCmd.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const STRING_NS = StringNamespaces.CMD_OPEN_IN_BROWSER;
1414
export default async function openInBrowserCmd(resource: Project | Connection): Promise<void> {
1515
Log.d("OpenInBrowserCmd invoked");
1616
if (resource == null) {
17-
const selected = await promptForResource(ProjectState.AppStates.STARTED);
17+
const selected = await promptForResource(true, ProjectState.AppStates.STARTED);
1818
if (selected == null) {
1919
Log.d("User cancelled prompt for resource");
2020
// user cancelled

dev/src/command/OpenWorkspaceFolderCmd.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import StringNamespaces from "../constants/strings/StringNamespaces";
1111
export default async function openWorkspaceFolderCmd(resource: Project | Connection): Promise<void> {
1212
Log.d(`Go to folder command invoked on ${resource}`);
1313
if (resource == null) {
14-
const selected = await promptForResource();
14+
const selected = await promptForResource(false);
1515
if (selected == null) {
1616
Log.d("User cancelled prompt for resource");
1717
// user cancelled

dev/src/command/RefreshConnectionCmd.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import StringNamespaces from "../constants/strings/StringNamespaces";
99
export default async function refreshConnectionCmd(connection: Connection): Promise<void> {
1010
Log.d("refreshConnectionCmd");
1111
if (connection == null) {
12-
const selected = await promptForConnection();
12+
const selected = await promptForConnection(true);
1313
if (selected == null) {
1414
// user cancelled
1515
Log.d("User cancelled project prompt");
@@ -19,5 +19,5 @@ export default async function refreshConnectionCmd(connection: Connection): Prom
1919
}
2020

2121
vscode.window.showInformationMessage(Translator.t(StringNamespaces.CMD_MISC, "refreshingConnection", { uri: connection.mcUri }));
22-
return connection.forceUpdateProjectList();
22+
return connection.forceUpdateProjectList(true);
2323
}

dev/src/command/RemoveConnectionCmd.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import StringNamespaces from "../constants/strings/StringNamespaces";
1010
export default async function removeConnectionCmd(connection: Connection): Promise<void> {
1111
Log.d("removeConnectionCmd invoked");
1212
if (connection == null) {
13-
const selected = await promptForConnection();
13+
const selected = await promptForConnection(false);
1414
if (selected == null) {
1515
// user cancelled
1616
Log.d("User cancelled project prompt");

dev/src/microclimate/connection/Connection.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ export default class Connection implements ITreeItemAdaptable, vscode.QuickPickI
8484
}
8585
this.connected = true;
8686
Log.d(`${this} is now connected`);
87+
await this.forceUpdateProjectList();
8788
this.logManager.onConnectionReconnect();
8889

8990
this.onChange();
@@ -96,6 +97,7 @@ export default class Connection implements ITreeItemAdaptable, vscode.QuickPickI
9697
return;
9798
}
9899
this.connected = false;
100+
this.projects = [];
99101
Log.d(`${this} is now disconnected`);
100102
this.logManager.onConnectionDisconnect();
101103

@@ -192,8 +194,12 @@ export default class Connection implements ITreeItemAdaptable, vscode.QuickPickI
192194
return Connection.CONTEXT_ID;
193195
}
194196

195-
public async forceUpdateProjectList(): Promise<void> {
197+
public async forceUpdateProjectList(wipeProjects: boolean = false): Promise<void> {
196198
Log.d("forceUpdateProjectList");
199+
if (wipeProjects) {
200+
Log.d(`Wiping ${this.projects.length} projects`);
201+
this.projects = [];
202+
}
197203
this.needProjectUpdate = true;
198204
this.getProjects();
199205
}

0 commit comments

Comments
 (0)