From e95c16a91ead936bb27efc56c196bf6c27795382 Mon Sep 17 00:00:00 2001 From: Kelvin Schoofs Date: Tue, 31 May 2022 19:48:54 +0200 Subject: [PATCH] Add prettier and its SDK integration --- .vscode/extensions.json | 3 +- .vscode/settings.json | 3 +- .yarn/sdks/prettier/index.js | 20 ++++++++ .yarn/sdks/prettier/package.json | 6 +++ .yarn/sdks/typescript/lib/tsserver.js | 50 +++++++++++++++----- .yarn/sdks/typescript/lib/tsserverlibrary.js | 50 +++++++++++++++----- .yarn/sdks/typescript/package.json | 2 +- .yarn/yarn.lock | 10 ++++ CHANGELOG.md | 1 + package.json | 1 + 10 files changed, 117 insertions(+), 29 deletions(-) create mode 100644 .yarn/sdks/prettier/index.js create mode 100644 .yarn/sdks/prettier/package.json diff --git a/.vscode/extensions.json b/.vscode/extensions.json index 4966560..d10bdec 100644 --- a/.vscode/extensions.json +++ b/.vscode/extensions.json @@ -3,6 +3,7 @@ // for the documentation about the extensions.json format "recommendations": [ "amodio.tsl-problem-matcher", - "arcanis.vscode-zipfs" + "arcanis.vscode-zipfs", + "esbenp.prettier-vscode" ] } diff --git a/.vscode/settings.json b/.vscode/settings.json index 5ded7c3..3c64ad7 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -24,5 +24,6 @@ "**/.yarn": true, "**/.pnp.*": true }, - "typescript.enablePromptUseWorkspaceTsdk": true + "typescript.enablePromptUseWorkspaceTsdk": true, + "prettier.prettierPath": ".yarn/sdks/prettier/index.js" } diff --git a/.yarn/sdks/prettier/index.js b/.yarn/sdks/prettier/index.js new file mode 100644 index 0000000..f6882d8 --- /dev/null +++ b/.yarn/sdks/prettier/index.js @@ -0,0 +1,20 @@ +#!/usr/bin/env node + +const {existsSync} = require(`fs`); +const {createRequire, createRequireFromPath} = require(`module`); +const {resolve} = require(`path`); + +const relPnpApiPath = "../../../.pnp.cjs"; + +const absPnpApiPath = resolve(__dirname, relPnpApiPath); +const absRequire = (createRequire || createRequireFromPath)(absPnpApiPath); + +if (existsSync(absPnpApiPath)) { + if (!process.versions.pnp) { + // Setup the environment to be able to require prettier/index.js + require(absPnpApiPath).setup(); + } +} + +// Defer to the real prettier/index.js your application uses +module.exports = absRequire(`prettier/index.js`); diff --git a/.yarn/sdks/prettier/package.json b/.yarn/sdks/prettier/package.json new file mode 100644 index 0000000..bce279f --- /dev/null +++ b/.yarn/sdks/prettier/package.json @@ -0,0 +1,6 @@ +{ + "name": "prettier", + "version": "2.6.2-sdk", + "main": "./index.js", + "type": "commonjs" +} diff --git a/.yarn/sdks/typescript/lib/tsserver.js b/.yarn/sdks/typescript/lib/tsserver.js index 71e35cf..830ad9f 100644 --- a/.yarn/sdks/typescript/lib/tsserver.js +++ b/.yarn/sdks/typescript/lib/tsserver.js @@ -18,6 +18,7 @@ const moduleWrapper = tsserver => { const pnpApi = require(`pnpapi`); const isVirtual = str => str.match(/\/(\$\$virtual|__virtual__)\//); + const isPortal = str => str.startsWith("portal:/"); const normalize = str => str.replace(/\\/g, `/`).replace(/^\/?/, `/`); const dependencyTreeRoots = new Set(pnpApi.getDependencyTreeRoots().map(locator => { @@ -44,7 +45,7 @@ const moduleWrapper = tsserver => { const resolved = isVirtual(str) ? pnpApi.resolveVirtual(str) : str; if (resolved) { const locator = pnpApi.findPackageLocator(resolved); - if (locator && dependencyTreeRoots.has(`${locator.name}@${locator.reference}`)) { + if (locator && (dependencyTreeRoots.has(`${locator.name}@${locator.reference}`) || isPortal(locator.reference))) { str = resolved; } } @@ -60,18 +61,26 @@ const moduleWrapper = tsserver => { // // Ref: https://github.com/microsoft/vscode/issues/105014#issuecomment-686760910 // - // Update Oct 8 2021: VSCode changed their format in 1.61. + // Update 2021-10-08: VSCode changed their format in 1.61. // Before | ^zip:/c:/foo/bar.zip/package.json // After | ^/zip//c:/foo/bar.zip/package.json // + // Update 2022-04-06: VSCode changed the format in 1.66. + // Before | ^/zip//c:/foo/bar.zip/package.json + // After | ^/zip/c:/foo/bar.zip/package.json + // case `vscode <1.61`: { str = `^zip:${str}`; } break; - case `vscode`: { + case `vscode <1.66`: { str = `^/zip/${str}`; } break; + case `vscode`: { + str = `^/zip${str}`; + } break; + // To make "go to definition" work, // We have to resolve the actual file system path from virtual path // and convert scheme to supported by [vim-rzip](https://github.com/lbrayner/vim-rzip) @@ -85,7 +94,7 @@ const moduleWrapper = tsserver => { // everything else is up to neovim case `neovim`: { str = normalize(resolved).replace(/\.zip\//, `.zip::`); - str = `zipfile:${str}`; + str = `zipfile://${str}`; } break; default: { @@ -100,8 +109,7 @@ const moduleWrapper = tsserver => { function fromEditorPath(str) { switch (hostInfo) { - case `coc-nvim`: - case `neovim`: { + case `coc-nvim`: { str = str.replace(/\.zip::/, `.zip/`); // The path for coc-nvim is in format of //zipfile://.yarn/... // So in order to convert it back, we use .* to match all the thing @@ -111,6 +119,12 @@ const moduleWrapper = tsserver => { : str.replace(/^.*zipfile:/, ``); } break; + case `neovim`: { + str = str.replace(/\.zip::/, `.zip/`); + // The path for neovim is in format of zipfile:////.yarn/... + return str.replace(/^zipfile:\/\//, ``); + } break; + case `vscode`: default: { return process.platform === `win32` @@ -143,8 +157,9 @@ const moduleWrapper = tsserver => { let hostInfo = `unknown`; Object.assign(Session.prototype, { - onMessage(/** @type {string} */ message) { - const parsedMessage = JSON.parse(message) + onMessage(/** @type {string | object} */ message) { + const isStringMessage = typeof message === 'string'; + const parsedMessage = isStringMessage ? JSON.parse(message) : message; if ( parsedMessage != null && @@ -153,14 +168,23 @@ const moduleWrapper = tsserver => { typeof parsedMessage.arguments.hostInfo === `string` ) { hostInfo = parsedMessage.arguments.hostInfo; - if (hostInfo === `vscode` && process.env.VSCODE_IPC_HOOK && process.env.VSCODE_IPC_HOOK.match(/Code\/1\.([1-5][0-9]|60)\./)) { - hostInfo += ` <1.61`; + if (hostInfo === `vscode` && process.env.VSCODE_IPC_HOOK) { + if (/(\/|-)1\.([1-5][0-9]|60)\./.test(process.env.VSCODE_IPC_HOOK)) { + hostInfo += ` <1.61`; + } else if (/(\/|-)1\.(6[1-5])\./.test(process.env.VSCODE_IPC_HOOK)) { + hostInfo += ` <1.66`; + } } } - return originalOnMessage.call(this, JSON.stringify(parsedMessage, (key, value) => { - return typeof value === `string` ? fromEditorPath(value) : value; - })); + const processedMessageJSON = JSON.stringify(parsedMessage, (key, value) => { + return typeof value === 'string' ? fromEditorPath(value) : value; + }); + + return originalOnMessage.call( + this, + isStringMessage ? processedMessageJSON : JSON.parse(processedMessageJSON) + ); }, send(/** @type {any} */ msg) { diff --git a/.yarn/sdks/typescript/lib/tsserverlibrary.js b/.yarn/sdks/typescript/lib/tsserverlibrary.js index 7a2d65e..0f7084c 100644 --- a/.yarn/sdks/typescript/lib/tsserverlibrary.js +++ b/.yarn/sdks/typescript/lib/tsserverlibrary.js @@ -18,6 +18,7 @@ const moduleWrapper = tsserver => { const pnpApi = require(`pnpapi`); const isVirtual = str => str.match(/\/(\$\$virtual|__virtual__)\//); + const isPortal = str => str.startsWith("portal:/"); const normalize = str => str.replace(/\\/g, `/`).replace(/^\/?/, `/`); const dependencyTreeRoots = new Set(pnpApi.getDependencyTreeRoots().map(locator => { @@ -44,7 +45,7 @@ const moduleWrapper = tsserver => { const resolved = isVirtual(str) ? pnpApi.resolveVirtual(str) : str; if (resolved) { const locator = pnpApi.findPackageLocator(resolved); - if (locator && dependencyTreeRoots.has(`${locator.name}@${locator.reference}`)) { + if (locator && (dependencyTreeRoots.has(`${locator.name}@${locator.reference}`) || isPortal(locator.reference))) { str = resolved; } } @@ -60,18 +61,26 @@ const moduleWrapper = tsserver => { // // Ref: https://github.com/microsoft/vscode/issues/105014#issuecomment-686760910 // - // Update Oct 8 2021: VSCode changed their format in 1.61. + // Update 2021-10-08: VSCode changed their format in 1.61. // Before | ^zip:/c:/foo/bar.zip/package.json // After | ^/zip//c:/foo/bar.zip/package.json // + // Update 2022-04-06: VSCode changed the format in 1.66. + // Before | ^/zip//c:/foo/bar.zip/package.json + // After | ^/zip/c:/foo/bar.zip/package.json + // case `vscode <1.61`: { str = `^zip:${str}`; } break; - case `vscode`: { + case `vscode <1.66`: { str = `^/zip/${str}`; } break; + case `vscode`: { + str = `^/zip${str}`; + } break; + // To make "go to definition" work, // We have to resolve the actual file system path from virtual path // and convert scheme to supported by [vim-rzip](https://github.com/lbrayner/vim-rzip) @@ -85,7 +94,7 @@ const moduleWrapper = tsserver => { // everything else is up to neovim case `neovim`: { str = normalize(resolved).replace(/\.zip\//, `.zip::`); - str = `zipfile:${str}`; + str = `zipfile://${str}`; } break; default: { @@ -100,8 +109,7 @@ const moduleWrapper = tsserver => { function fromEditorPath(str) { switch (hostInfo) { - case `coc-nvim`: - case `neovim`: { + case `coc-nvim`: { str = str.replace(/\.zip::/, `.zip/`); // The path for coc-nvim is in format of //zipfile://.yarn/... // So in order to convert it back, we use .* to match all the thing @@ -111,6 +119,12 @@ const moduleWrapper = tsserver => { : str.replace(/^.*zipfile:/, ``); } break; + case `neovim`: { + str = str.replace(/\.zip::/, `.zip/`); + // The path for neovim is in format of zipfile:////.yarn/... + return str.replace(/^zipfile:\/\//, ``); + } break; + case `vscode`: default: { return process.platform === `win32` @@ -143,8 +157,9 @@ const moduleWrapper = tsserver => { let hostInfo = `unknown`; Object.assign(Session.prototype, { - onMessage(/** @type {string} */ message) { - const parsedMessage = JSON.parse(message) + onMessage(/** @type {string | object} */ message) { + const isStringMessage = typeof message === 'string'; + const parsedMessage = isStringMessage ? JSON.parse(message) : message; if ( parsedMessage != null && @@ -153,14 +168,23 @@ const moduleWrapper = tsserver => { typeof parsedMessage.arguments.hostInfo === `string` ) { hostInfo = parsedMessage.arguments.hostInfo; - if (hostInfo === `vscode` && process.env.VSCODE_IPC_HOOK && process.env.VSCODE_IPC_HOOK.match(/Code\/1\.([1-5][0-9]|60)\./)) { - hostInfo += ` <1.61`; + if (hostInfo === `vscode` && process.env.VSCODE_IPC_HOOK) { + if (/(\/|-)1\.([1-5][0-9]|60)\./.test(process.env.VSCODE_IPC_HOOK)) { + hostInfo += ` <1.61`; + } else if (/(\/|-)1\.(6[1-5])\./.test(process.env.VSCODE_IPC_HOOK)) { + hostInfo += ` <1.66`; + } } } - return originalOnMessage.call(this, JSON.stringify(parsedMessage, (key, value) => { - return typeof value === `string` ? fromEditorPath(value) : value; - })); + const processedMessageJSON = JSON.stringify(parsedMessage, (key, value) => { + return typeof value === 'string' ? fromEditorPath(value) : value; + }); + + return originalOnMessage.call( + this, + isStringMessage ? processedMessageJSON : JSON.parse(processedMessageJSON) + ); }, send(/** @type {any} */ msg) { diff --git a/.yarn/sdks/typescript/package.json b/.yarn/sdks/typescript/package.json index 1a10512..34ba1c4 100644 --- a/.yarn/sdks/typescript/package.json +++ b/.yarn/sdks/typescript/package.json @@ -1,6 +1,6 @@ { "name": "typescript", - "version": "4.4.3-sdk", + "version": "4.5.5-sdk", "main": "./lib/typescript.js", "type": "commonjs" } diff --git a/.yarn/yarn.lock b/.yarn/yarn.lock index 0499b51..6b04d3a 100644 --- a/.yarn/yarn.lock +++ b/.yarn/yarn.lock @@ -7653,6 +7653,15 @@ fsevents@~2.3.2: languageName: node linkType: hard +"prettier@npm:^2.6.2": + version: 2.6.2 + resolution: "prettier@npm:2.6.2" + bin: + prettier: bin-prettier.js + checksum: 48d08dde8e9fb1f5bccdd205baa7f192e9fc8bc98f86e1b97d919de804e28c806b0e6cc685e4a88211aa7987fa9668f30baae19580d87ced3ed0f2ec6572106f + languageName: node + linkType: hard + "pretty-error@npm:^4.0.0": version: 4.0.0 resolution: "pretty-error@npm:4.0.0" @@ -9410,6 +9419,7 @@ resolve@^2.0.0-next.3: common: "workspace:*" event-stream: ^4.0.1 jsonc-parser: ^2.0.0 + prettier: ^2.6.2 semver: ^7.3.5 socks: ^2.2.0 source-map: ^0.7.3 diff --git a/CHANGELOG.md b/CHANGELOG.md index 99eec4a..de6eca0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -34,6 +34,7 @@ - Also ditched `@yarnpkg/plugin-version` which wasn't even really used in the first place - Created a `common` module which now holds `fileSystemConfig.ts` and `webviewMessages.ts` - Improve webview ESLint setup, namely update `@typescript-eslint/*` and remove unused plugins +- Add prettier and its Yarn PnP SDK integration + VS Code settings ## v1.24.1 (2021-12-07) diff --git a/package.json b/package.json index 7f75f93..6026ff6 100644 --- a/package.json +++ b/package.json @@ -414,6 +414,7 @@ "@types/vscode": "~1.49.0", "@types/webpack": "^4.4.25", "@types/winreg": "^1.2.30", + "prettier": "^2.6.2", "source-map": "^0.7.3", "source-map-support": "^0.5.19", "ts-loader": "^9.2.3",