diff --git a/CHANGELOG.md b/CHANGELOG.md index fed7395..71da370 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Change Log +## 0.0.9 +* Fix crash on activation and configuration reload when no text editor is active (`window.activeTextEditor` is `null`). Closes #35, #43. +* Reload settings per-document on every format call, so per-folder and multi-root workspace settings are respected. Closes #36. +* Resolve `${workspaceFolder}` and `${workspaceRoot}` variables in the `phpcbf.standard` setting. Closes #38. +* Use `event.document.uri` (instead of `window.activeTextEditor`) in the `onWillSaveTextDocument` listener to avoid potential null-reference errors. + ## 0.0.8 * Allow configuration from `.vscode/settings.json` when in a Multi-root project. [@WraithKenny](https://github.com/WraithKenny) [#6](https://github.com/soderlind/vscode-phpcbf/pull/6) ## 0.0.7 diff --git a/extension.js b/extension.js index 975f27c..81a61ce 100644 --- a/extension.js +++ b/extension.js @@ -19,11 +19,13 @@ class PHPCBF { this.loadSettings(); } - loadSettings() { - let config = workspace.getConfiguration( - "phpcbf", - window.activeTextEditor.document.uri - ); + 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); + + let config = workspace.getConfiguration("phpcbf", configUri); if (!config.get("enable") === true) { return; } @@ -59,6 +61,17 @@ class PHPCBF { this.standard = config.get("standard", null); + // Resolve ${workspaceFolder} / ${workspaceRoot} in the standard path. + if (this.standard && configUri) { + const folder = workspace.getWorkspaceFolder(configUri); + const rootPath = folder ? folder.uri.fsPath : null; + if (rootPath) { + this.standard = this.standard + .replace("${workspaceFolder}", rootPath) + .replace("${workspaceRoot}", rootPath); + } + } + this.documentFormattingProvider = config.get( "documentFormattingProvider", true @@ -135,6 +148,10 @@ class PHPCBF { } format(document) { + // Reload settings scoped to this document so multi-root workspaces and + // per-folder settings are respected on every format call. + this.loadSettings(document.uri); + if (this.debug) { console.time("phpcbf"); } @@ -266,12 +283,11 @@ exports.activate = context => { context.subscriptions.push( workspace.onWillSaveTextDocument(event => { - const editor = window.activeTextEditor; if ( event.document.languageId == "php" && phpcbf.onsave && workspace - .getConfiguration("editor", editor.document.uri) + .getConfiguration("editor", event.document.uri) .get("formatOnSave") === false ) { event.waitUntil( diff --git a/package.json b/package.json index 762396a..c86425f 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "vscode-phpcbf", "displayName": "phpcbf", "description": "PHP Code Beautifier and Fixer", - "version": "0.0.8", + "version": "0.0.9", "publisher": "persoderlind", "homepage": "https://github.com/soderlind/vscode-phpcbf", "icon": "images/logo.png",