Skip to content

Commit

Permalink
fix: update jar file with confirmation on configuration change
Browse files Browse the repository at this point in the history
fixes #15
  • Loading branch information
JoseVSeb committed Feb 9, 2024
1 parent 0f46a6a commit 9b12115
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 17 deletions.
15 changes: 13 additions & 2 deletions src/ExtensionConfiguration.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,33 @@
import { Uri } from "vscode";
import { getJavaConfiguration } from "./utils";

export type GoogleJavaFormatVersion =
| `${number}.${number}.${number}`
| "latest";

export interface GoogleJavaFormatConfiguration {
executable?: string;
version?: string;
version?: GoogleJavaFormatVersion;
extra?: string;
jarUri: Uri;
}

export class ExtensionConfiguration implements GoogleJavaFormatConfiguration {
readonly executable?: string;
readonly version?: string;
readonly version?: GoogleJavaFormatVersion;
readonly extra?: string;
jarUri: Uri = null!;

constructor() {
return new Proxy(this, this.handler);
}

private handler: ProxyHandler<ExtensionConfiguration> = {
get(target, prop) {
if (prop === "jarUri") {
return target[prop];
}

return getJavaConfiguration().get(
`format.settings.google.${String(prop)}`,
);
Expand Down
3 changes: 1 addition & 2 deletions src/GoogleJavaFormatterSync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { GoogleJavaFormatConfiguration } from "./ExtensionConfiguration";

export default class GoogleJavaFormatterSync implements IGoogleJavaFormatter {
constructor(
private executable: string,
private config: GoogleJavaFormatConfiguration,
private log: LogOutputChannel,
) {}
Expand All @@ -21,7 +20,7 @@ export default class GoogleJavaFormatterSync implements IGoogleJavaFormatter {
public format(text: string, range?: [number, number]): Promise<string> {
return new Promise<string>((resolve, reject) => {
try {
let command = `java -jar ${this.executable}`;
let command = `java -jar ${this.config.jarUri.fsPath}`;

if (this.config.extra) {
command += ` ${this.config.extra}`;
Expand Down
67 changes: 54 additions & 13 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import { ExtensionContext, window } from "vscode";
import getExtensionCacheFolder from "./getExtensionCacheFolder";
import {
ConfigurationChangeEvent,
ExtensionContext,
ProgressLocation,
window,
workspace,
} from "vscode";
import { ExtensionConfiguration } from "./ExtensionConfiguration";
import getJarLocalPathFromConfig from "./getJarLocalPathFromConfig";
import GoogleJavaFormatEditProvider from "./GoogleJavaFormatEditProvider";
import GoogleJavaFormatEditService from "./GoogleJavaFormatEditService";
import GoogleJavaFormatterSync from "./GoogleJavaFormatterSync";
import { IGoogleJavaFormatter } from "./IGoogleJavaFormatter";
import getExtensionCacheFolder from "./getExtensionCacheFolder";
import getJarLocalPathFromConfig from "./getJarLocalPathFromConfig";

export async function activate(context: ExtensionContext) {
const log = window.createOutputChannel("Google Java Format for VS Code", {
Expand All @@ -16,17 +21,53 @@ export async function activate(context: ExtensionContext) {
const config = new ExtensionConfiguration();
const cacheDir = getExtensionCacheFolder(context);

const jarLocalPath = await getJarLocalPathFromConfig({
cacheDir,
log,
config,
}).then((uri) => uri.fsPath);
const configureJarFile = async () => {
config.jarUri = await getJarLocalPathFromConfig({
cacheDir,
log,
config,
});
};
await configureJarFile();

const formatter: IGoogleJavaFormatter = new GoogleJavaFormatterSync(
jarLocalPath,
config,
log,
const configurationChangeListener = async (
event: ConfigurationChangeEvent,
) => {
if (
// check if configuration updated
!event.affectsConfiguration("java.format.settings.google")
) {
// jar update not needed
return;
}

log.info("Configuration change detected.");
const action = await window.showInformationMessage(
"Configuration change detected. Update jar file?",
"Update",
"Ignore",
);

if (action !== "Update") {
log.debug("Change ignored.");
return;
}

log.debug("Updating jar file...");
window.withProgress(
{
location: ProgressLocation.Notification,
title: "Updating jar file...",
cancellable: false,
},
configureJarFile,
);
};
context.subscriptions.push(
workspace.onDidChangeConfiguration(configurationChangeListener),
);

const formatter = new GoogleJavaFormatterSync(config, log);
context.subscriptions.push(formatter.init());

const editProvider = new GoogleJavaFormatEditProvider(formatter, log);
Expand Down

0 comments on commit 9b12115

Please sign in to comment.