Skip to content

Commit

Permalink
fix: get config on every invoke of formatter
Browse files Browse the repository at this point in the history
change retrieval of extension configuration from instantiation of extension to every invoke of formatter.

fixes #12
  • Loading branch information
JoseVSeb committed Feb 8, 2024
1 parent 74d0b68 commit 8ece27e
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 32 deletions.
25 changes: 25 additions & 0 deletions src/ExtensionConfiguration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { getJavaConfiguration } from "./utils";

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

export class ExtensionConfiguration implements GoogleJavaFormatConfiguration {
readonly executable?: string;
readonly version?: string;
readonly extra?: string;

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

private handler: ProxyHandler<ExtensionConfiguration> = {
get(target, prop) {
return getJavaConfiguration().get(
`format.settings.google.${String(prop)}`,
);
},
};
}
7 changes: 4 additions & 3 deletions src/GoogleJavaFormatterSync.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { execSync } from "child_process";
import { IGoogleJavaFormatter } from "./IGoogleJavaFormatter";
import { LogOutputChannel } from "vscode";
import { GoogleJavaFormatConfiguration } from "./ExtensionConfiguration";

export default class GoogleJavaFormatterSync implements IGoogleJavaFormatter {
constructor(
private executable: string,
private extra: string | undefined,
private config: GoogleJavaFormatConfiguration,
private log: LogOutputChannel,
) {}

Expand All @@ -22,8 +23,8 @@ export default class GoogleJavaFormatterSync implements IGoogleJavaFormatter {
try {
let command = `java -jar ${this.executable}`;

if (this.extra) {
command += ` ${this.extra}`;
if (this.config.extra) {
command += ` ${this.config.extra}`;
}

if (range) {
Expand Down
16 changes: 3 additions & 13 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ExtensionContext, window } from "vscode";
import getExtensionCacheFolder from "./getExtensionCacheFolder";
import getExtensionConfiguration from "./getExtensionConfiguration";
import { ExtensionConfiguration } from "./ExtensionConfiguration";
import getJarLocalPathFromConfig from "./getJarLocalPathFromConfig";
import GoogleJavaFormatEditProvider from "./GoogleJavaFormatEditProvider";
import GoogleJavaFormatEditService from "./GoogleJavaFormatEditService";
Expand All @@ -13,28 +13,18 @@ export async function activate(context: ExtensionContext) {
});
context.subscriptions.push(log);

const config = getExtensionConfiguration();
const config = new ExtensionConfiguration();
const cacheDir = getExtensionCacheFolder(context);

if (!config) {
const message =
"Google Java Format for VS Code extension configuration not found.";
log.error(message);
window.showErrorMessage(message);
return;
}

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

const { extra } = config;

const formatter: IGoogleJavaFormatter = new GoogleJavaFormatterSync(
jarLocalPath,
extra,
config,
log,
);
context.subscriptions.push(formatter.init());
Expand Down
15 changes: 0 additions & 15 deletions src/getExtensionConfiguration.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/getJarLocalPathFromConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
downloadGoogleJavaFormatJar,
downloadGoogleJavaFormatJarByVersion,
} from "./downloadGoogleJavaFormatJar";
import { GoogleJavaFormatConfiguration } from "./getExtensionConfiguration";
import { GoogleJavaFormatConfiguration } from "./ExtensionConfiguration";
import { getListOfGoogleJavaFormatVersions } from "./getListOfGoogleJavaFormatVersions";
import { getUriFromString } from "./utils";

Expand Down

0 comments on commit 8ece27e

Please sign in to comment.