diff --git a/extension.js b/extension.js index 6457e71..73de2b2 100644 --- a/extension.js +++ b/extension.js @@ -17,15 +17,32 @@ const TmpDir = os.tmpdir(); class PHPCBF { constructor() { + // Tracks the last workspace-folder key for which settings were loaded. + // Cleared whenever configuration changes so the next format reloads. + this._lastSettingsKey = null; this.loadSettings(); } + /** Invalidate the per-folder settings cache (call on config changes). */ + clearSettingsCache() { + this._lastSettingsKey = null; + } + loadSettings(uri) { // Use the provided URI (e.g. the document being formatted), fall back to // the active editor, or null (global settings) if neither is available. const configUri = uri || (window.activeTextEditor ? window.activeTextEditor.document.uri : null); + // Derive a stable key from the workspace folder so that repeated saves + // within the same folder (the common case) skip redundant config reads. + const folder = configUri ? workspace.getWorkspaceFolder(configUri) : null; + const settingsKey = folder ? folder.uri.toString() : ''; + if (settingsKey !== '' && settingsKey === this._lastSettingsKey) { + return; + } + this._lastSettingsKey = settingsKey; + let config = workspace.getConfiguration("phpcbf", configUri); if (!config.get("enable") === true) { return; @@ -287,6 +304,7 @@ exports.activate = context => { context.subscriptions.push( workspace.onDidChangeConfiguration(() => { + phpcbf.clearSettingsCache(); phpcbf.loadSettings(); }) );