Skip to content

Commit

Permalink
Add stylua selection version command
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnnyMorganz committed Dec 30, 2023
1 parent 6a05e27 commit bb0c505
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 21 deletions.
1 change: 1 addition & 0 deletions stylua-vscode/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ To view the changelog of the StyLua binary, see [here](https://github.com/Johnny

- Added configuration option `stylua.configPath` to provide a direct path to a `stylua.toml` file. Note: this will override any workspace config lookup
- Added configuration option `stylua.verify` to pass `--verify` to StyLua CLI when formatting a file. This enforces output verification
- Added command `StyLua: Select Version` to customize which version of StyLua to install. This command updates the `stylua.targetReleaseVersion` setting

### Changed

Expand Down
11 changes: 3 additions & 8 deletions stylua-vscode/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions stylua-vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@
"command": "stylua.showOutputChannel",
"title": "Show Output Channel",
"category": "StyLua"
},
{
"command": "stylua.selectVersion",
"title": "Select Version",
"category": "StyLua"
}
],
"configuration": {
Expand Down Expand Up @@ -142,6 +147,7 @@
"dependencies": {
"ignore": "^5.1.8",
"node-fetch": "^2.6.1",
"semver": "^7.5.4",
"unzipper": "^0.10.14"
}
}
13 changes: 6 additions & 7 deletions stylua-vscode/src/download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class StyluaDownloader {

if (path === undefined) {
await vscode.workspace.fs.createDirectory(this.storageDirectory);
await this.downloadStyLuaVisual();
await this.downloadStyLuaVisual(util.getDesiredVersion());
return await this.getStyluaPath();
} else {
if (!(await util.fileExists(path))) {
Expand Down Expand Up @@ -69,19 +69,18 @@ export class StyluaDownloader {
}
}

public downloadStyLuaVisual(): Thenable<void> {
public downloadStyLuaVisual(version: string): Thenable<void> {
return vscode.window.withProgress(
{
cancellable: false,
location: vscode.ProgressLocation.Notification,
title: "Downloading StyLua",
title: `Downloading StyLua (${version})`,
},
() => this.downloadStylua()
() => this.downloadStylua(version)
);
}

private async downloadStylua(): Promise<void> {
const version = util.getDesiredVersion();
private async downloadStylua(version: string): Promise<void> {
const release = await this.github.getRelease(version);
const assetFilename = util.getAssetFilenamePattern();
const outputFilename = util.getDownloadOutputFilename();
Expand Down Expand Up @@ -129,7 +128,7 @@ export class StyluaDownloader {
.then((option) => {
switch (option) {
case "Install":
this.downloadStyLuaVisual();
this.downloadStyLuaVisual(release.tagName);
break;
case "Release Notes":
vscode.env.openExternal(vscode.Uri.parse(release.htmlUrl));
Expand Down
47 changes: 45 additions & 2 deletions stylua-vscode/src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import * as vscode from "vscode";
import * as semver from "semver";
import { formatCode, checkIgnored } from "./stylua";
import { GitHub } from "./github";
import { StyluaDownloader } from "./download";
import { getDesiredVersion } from "./util";

/**
* Convert a Position within a Document to a byte offset.
Expand Down Expand Up @@ -42,7 +44,7 @@ export async function activate(context: vscode.ExtensionContext) {

context.subscriptions.push(
vscode.commands.registerCommand("stylua.reinstall", async () => {
await downloader.downloadStyLuaVisual();
await downloader.downloadStyLuaVisual(getDesiredVersion());
styluaBinaryPath = await downloader.getStyluaPath();
})
);
Expand All @@ -59,6 +61,47 @@ export async function activate(context: vscode.ExtensionContext) {
})
);

context.subscriptions.push(
vscode.commands.registerCommand("stylua.selectVersion", async () => {
const versions = (await github.getAllReleases()).sort((a, b) =>
semver.rcompare(a.tagName, b.tagName)
);
if (versions.length === 0) {
return;
}
const latestVersion = versions[0];

const selectedVersion = await vscode.window.showQuickPick(
versions
.sort((a, b) => semver.rcompare(a.tagName, b.tagName))
.map((release) => {
if (release.tagName === latestVersion.tagName) {
return { label: `${release.tagName} (latest)` };
} else {
return { label: release.tagName };
}
}),
{
placeHolder: "Select the version of StyLua to install",
}
);

if (selectedVersion) {
const updateConfigValue = selectedVersion.label.includes("latest")
? "latest"
: selectedVersion.label;
await downloader.downloadStyLuaVisual(selectedVersion.label);
vscode.workspace
.getConfiguration("stylua")
.update(
"targetReleaseVersion",
updateConfigValue,
vscode.ConfigurationTarget.Workspace
);
}
})
);

context.subscriptions.push(
vscode.workspace.onDidChangeConfiguration(async (change) => {
if (change.affectsConfiguration("stylua")) {
Expand Down Expand Up @@ -100,7 +143,7 @@ export async function activate(context: vscode.ExtensionContext) {
)
.then((option) => {
if (option === "Install") {
downloader.downloadStyLuaVisual();
vscode.commands.executeCommand("stylua.reinstall");
}
});
return [];
Expand Down
10 changes: 6 additions & 4 deletions stylua-vscode/src/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,17 +132,19 @@ export class GitHub implements Disposable {
return this.credential.authenticated;
}

public async getAllReleases(): Promise<GitHubRelease[]> {
const json = await fetchJson(RELEASES_URL, this.credential.token);
return Array.isArray(json) ? json.map(releaseFromJson) : [];
}

public async getRelease(version: string): Promise<GitHubRelease> {
if (version === "latest") {
const json = await fetchJson(RELEASES_URL_LATEST, this.credential.token);
return releaseFromJson(json);
}

version = version.startsWith("v") ? version : "v" + version;
const json = await fetchJson(RELEASES_URL, this.credential.token);
const releases: GitHubRelease[] = Array.isArray(json)
? json.map(releaseFromJson)
: [];
const releases = await this.getAllReleases();
for (const release of releases) {
if (release.tagName.startsWith(version)) {
return release;
Expand Down

0 comments on commit bb0c505

Please sign in to comment.