Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,16 @@ const TmpDir = os.tmpdir();

class PHPCBF {
constructor() {
// Cache configSearch results keyed by "workspaceRoot\0fileDir" to avoid
// repeated filesystem walks (fs.existsSync calls) on every format operation.
this._configCache = new Map();
this.loadSettings();
}

clearConfigCache() {
this._configCache.clear();
}

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.
Expand Down Expand Up @@ -117,7 +124,18 @@ class PHPCBF {
];

const fileDir = path.relative(workspaceRoot, path.dirname(filePath));
const confFile = findFiles(workspaceRoot, fileDir, confFileNames);

// Avoid repeated filesystem walks for the same directory.
// The cache maps "workspaceRoot\0fileDir" -> absolute config path (or null).
// It is cleared whenever phpcbf configuration changes.
const cacheKey = workspaceRoot + "\0" + fileDir;
let confFile;
if (this._configCache.has(cacheKey)) {
confFile = this._configCache.get(cacheKey);
} else {
confFile = findFiles(workspaceRoot, fileDir, confFileNames);
this._configCache.set(cacheKey, confFile);
}

standard = confFile || this.standard;
} else {
Expand Down Expand Up @@ -287,6 +305,7 @@ exports.activate = context => {

context.subscriptions.push(
workspace.onDidChangeConfiguration(() => {
phpcbf.clearConfigCache();
phpcbf.loadSettings();
})
);
Expand Down