Show more clear TS Version picker when reinstalling a different TS version locally

- Make sure that `TypeScriptVersion` is immutable by getting and caching `apiVersion` on init
- Only show dot next to currently active version if both path and api versions match
This commit is contained in:
Matt Bierner 2020-02-18 14:19:38 -08:00
parent c7a0ff3694
commit 2aced89ae1
4 changed files with 39 additions and 17 deletions

View file

@ -414,15 +414,14 @@ export default class TypeScriptServiceClient extends Disposable implements IType
}
public onVersionStatusClicked(): Thenable<void> {
return this.showVersionPicker(false);
return this.showVersionPicker();
}
private showVersionPicker(firstRun: boolean): Thenable<void> {
return this.versionPicker.show(firstRun).then(change => {
if (firstRun || !change.newVersion || !change.oldVersion || change.oldVersion.path === change.newVersion.path) {
return;
private showVersionPicker(): Thenable<void> {
return this.versionPicker.show().then(change => {
if (change.newVersion && change.oldVersion && change.oldVersion.eq(change.newVersion)) {
this.restartTsServer();
}
this.restartTsServer();
});
}

View file

@ -65,6 +65,10 @@ export default class API {
public readonly fullVersionString: string,
) { }
public eq(other: API): boolean {
return semver.eq(this.version, other.version);
}
public gte(other: API): boolean {
return semver.gte(this.version, other.version);
}

View file

@ -66,7 +66,7 @@ export class TypeScriptVersionPicker {
for (const version of this.versionProvider.localVersions) {
pickOptions.push({
label: (this.useWorkspaceTsdkSetting && this.currentVersion.path === version.path
label: (this.useWorkspaceTsdkSetting && this.currentVersion.eq(version)
? '• '
: '') + localize('useWorkspaceVersionOption', "Use Workspace Version"),
description: version.displayName,

View file

@ -21,11 +21,16 @@ export const enum TypeScriptVersionSource {
}
export class TypeScriptVersion {
public readonly apiVersion: API | undefined;
constructor(
public readonly source: TypeScriptVersionSource,
public readonly path: string,
private readonly _pathLabel?: string
) { }
) {
this.apiVersion = TypeScriptVersion.getApiVersion(this.tsServerPath);
}
public get tsServerPath(): string {
return path.join(this.path, 'tsserver.js');
@ -39,8 +44,28 @@ export class TypeScriptVersion {
return this.apiVersion !== undefined;
}
public get apiVersion(): API | undefined {
const version = this.getTypeScriptVersion(this.tsServerPath);
public eq(other: TypeScriptVersion): boolean {
if (this.path !== other.path) {
return false;
}
if (this.apiVersion === other.apiVersion) {
return true;
}
if (!this.apiVersion || !other.apiVersion) {
return false;
}
return this.apiVersion.eq(other.apiVersion);
}
public get displayName(): string {
const version = this.apiVersion;
return version ? version.displayName : localize(
'couldNotLoadTsVersion', 'Could not load the TypeScript version at this path');
}
public static getApiVersion(serverPath: string): API | undefined {
const version = TypeScriptVersion.getTypeScriptVersion(serverPath);
if (version) {
return version;
}
@ -54,13 +79,7 @@ export class TypeScriptVersion {
return undefined;
}
public get displayName(): string {
const version = this.apiVersion;
return version ? version.displayName : localize(
'couldNotLoadTsVersion', 'Could not load the TypeScript version at this path');
}
private getTypeScriptVersion(serverPath: string): API | undefined {
private static getTypeScriptVersion(serverPath: string): API | undefined {
if (!fs.existsSync(serverPath)) {
return undefined;
}