Add prettier and its SDK integration

pull/373/head
Kelvin Schoofs 2 years ago
parent 2673c726c8
commit e95c16a91e

@ -3,6 +3,7 @@
// for the documentation about the extensions.json format // for the documentation about the extensions.json format
"recommendations": [ "recommendations": [
"amodio.tsl-problem-matcher", "amodio.tsl-problem-matcher",
"arcanis.vscode-zipfs" "arcanis.vscode-zipfs",
"esbenp.prettier-vscode"
] ]
} }

@ -24,5 +24,6 @@
"**/.yarn": true, "**/.yarn": true,
"**/.pnp.*": true "**/.pnp.*": true
}, },
"typescript.enablePromptUseWorkspaceTsdk": true "typescript.enablePromptUseWorkspaceTsdk": true,
"prettier.prettierPath": ".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`);

@ -0,0 +1,6 @@
{
"name": "prettier",
"version": "2.6.2-sdk",
"main": "./index.js",
"type": "commonjs"
}

@ -18,6 +18,7 @@ const moduleWrapper = tsserver => {
const pnpApi = require(`pnpapi`); const pnpApi = require(`pnpapi`);
const isVirtual = str => str.match(/\/(\$\$virtual|__virtual__)\//); const isVirtual = str => str.match(/\/(\$\$virtual|__virtual__)\//);
const isPortal = str => str.startsWith("portal:/");
const normalize = str => str.replace(/\\/g, `/`).replace(/^\/?/, `/`); const normalize = str => str.replace(/\\/g, `/`).replace(/^\/?/, `/`);
const dependencyTreeRoots = new Set(pnpApi.getDependencyTreeRoots().map(locator => { const dependencyTreeRoots = new Set(pnpApi.getDependencyTreeRoots().map(locator => {
@ -44,7 +45,7 @@ const moduleWrapper = tsserver => {
const resolved = isVirtual(str) ? pnpApi.resolveVirtual(str) : str; const resolved = isVirtual(str) ? pnpApi.resolveVirtual(str) : str;
if (resolved) { if (resolved) {
const locator = pnpApi.findPackageLocator(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; str = resolved;
} }
} }
@ -60,18 +61,26 @@ const moduleWrapper = tsserver => {
// //
// Ref: https://github.com/microsoft/vscode/issues/105014#issuecomment-686760910 // 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 // Before | ^zip:/c:/foo/bar.zip/package.json
// After | ^/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`: { case `vscode <1.61`: {
str = `^zip:${str}`; str = `^zip:${str}`;
} break; } break;
case `vscode`: { case `vscode <1.66`: {
str = `^/zip/${str}`; str = `^/zip/${str}`;
} break; } break;
case `vscode`: {
str = `^/zip${str}`;
} break;
// To make "go to definition" work, // To make "go to definition" work,
// We have to resolve the actual file system path from virtual path // 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) // 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 // everything else is up to neovim
case `neovim`: { case `neovim`: {
str = normalize(resolved).replace(/\.zip\//, `.zip::`); str = normalize(resolved).replace(/\.zip\//, `.zip::`);
str = `zipfile:${str}`; str = `zipfile://${str}`;
} break; } break;
default: { default: {
@ -100,8 +109,7 @@ const moduleWrapper = tsserver => {
function fromEditorPath(str) { function fromEditorPath(str) {
switch (hostInfo) { switch (hostInfo) {
case `coc-nvim`: case `coc-nvim`: {
case `neovim`: {
str = str.replace(/\.zip::/, `.zip/`); str = str.replace(/\.zip::/, `.zip/`);
// The path for coc-nvim is in format of /<pwd>/zipfile:/<pwd>/.yarn/... // The path for coc-nvim is in format of /<pwd>/zipfile:/<pwd>/.yarn/...
// So in order to convert it back, we use .* to match all the thing // So in order to convert it back, we use .* to match all the thing
@ -111,6 +119,12 @@ const moduleWrapper = tsserver => {
: str.replace(/^.*zipfile:/, ``); : str.replace(/^.*zipfile:/, ``);
} break; } break;
case `neovim`: {
str = str.replace(/\.zip::/, `.zip/`);
// The path for neovim is in format of zipfile:///<pwd>/.yarn/...
return str.replace(/^zipfile:\/\//, ``);
} break;
case `vscode`: case `vscode`:
default: { default: {
return process.platform === `win32` return process.platform === `win32`
@ -143,8 +157,9 @@ const moduleWrapper = tsserver => {
let hostInfo = `unknown`; let hostInfo = `unknown`;
Object.assign(Session.prototype, { Object.assign(Session.prototype, {
onMessage(/** @type {string} */ message) { onMessage(/** @type {string | object} */ message) {
const parsedMessage = JSON.parse(message) const isStringMessage = typeof message === 'string';
const parsedMessage = isStringMessage ? JSON.parse(message) : message;
if ( if (
parsedMessage != null && parsedMessage != null &&
@ -153,14 +168,23 @@ const moduleWrapper = tsserver => {
typeof parsedMessage.arguments.hostInfo === `string` typeof parsedMessage.arguments.hostInfo === `string`
) { ) {
hostInfo = parsedMessage.arguments.hostInfo; 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)\./)) { if (hostInfo === `vscode` && process.env.VSCODE_IPC_HOOK) {
hostInfo += ` <1.61`; 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) => { const processedMessageJSON = JSON.stringify(parsedMessage, (key, value) => {
return typeof value === `string` ? fromEditorPath(value) : value; return typeof value === 'string' ? fromEditorPath(value) : value;
})); });
return originalOnMessage.call(
this,
isStringMessage ? processedMessageJSON : JSON.parse(processedMessageJSON)
);
}, },
send(/** @type {any} */ msg) { send(/** @type {any} */ msg) {

@ -18,6 +18,7 @@ const moduleWrapper = tsserver => {
const pnpApi = require(`pnpapi`); const pnpApi = require(`pnpapi`);
const isVirtual = str => str.match(/\/(\$\$virtual|__virtual__)\//); const isVirtual = str => str.match(/\/(\$\$virtual|__virtual__)\//);
const isPortal = str => str.startsWith("portal:/");
const normalize = str => str.replace(/\\/g, `/`).replace(/^\/?/, `/`); const normalize = str => str.replace(/\\/g, `/`).replace(/^\/?/, `/`);
const dependencyTreeRoots = new Set(pnpApi.getDependencyTreeRoots().map(locator => { const dependencyTreeRoots = new Set(pnpApi.getDependencyTreeRoots().map(locator => {
@ -44,7 +45,7 @@ const moduleWrapper = tsserver => {
const resolved = isVirtual(str) ? pnpApi.resolveVirtual(str) : str; const resolved = isVirtual(str) ? pnpApi.resolveVirtual(str) : str;
if (resolved) { if (resolved) {
const locator = pnpApi.findPackageLocator(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; str = resolved;
} }
} }
@ -60,18 +61,26 @@ const moduleWrapper = tsserver => {
// //
// Ref: https://github.com/microsoft/vscode/issues/105014#issuecomment-686760910 // 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 // Before | ^zip:/c:/foo/bar.zip/package.json
// After | ^/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`: { case `vscode <1.61`: {
str = `^zip:${str}`; str = `^zip:${str}`;
} break; } break;
case `vscode`: { case `vscode <1.66`: {
str = `^/zip/${str}`; str = `^/zip/${str}`;
} break; } break;
case `vscode`: {
str = `^/zip${str}`;
} break;
// To make "go to definition" work, // To make "go to definition" work,
// We have to resolve the actual file system path from virtual path // 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) // 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 // everything else is up to neovim
case `neovim`: { case `neovim`: {
str = normalize(resolved).replace(/\.zip\//, `.zip::`); str = normalize(resolved).replace(/\.zip\//, `.zip::`);
str = `zipfile:${str}`; str = `zipfile://${str}`;
} break; } break;
default: { default: {
@ -100,8 +109,7 @@ const moduleWrapper = tsserver => {
function fromEditorPath(str) { function fromEditorPath(str) {
switch (hostInfo) { switch (hostInfo) {
case `coc-nvim`: case `coc-nvim`: {
case `neovim`: {
str = str.replace(/\.zip::/, `.zip/`); str = str.replace(/\.zip::/, `.zip/`);
// The path for coc-nvim is in format of /<pwd>/zipfile:/<pwd>/.yarn/... // The path for coc-nvim is in format of /<pwd>/zipfile:/<pwd>/.yarn/...
// So in order to convert it back, we use .* to match all the thing // So in order to convert it back, we use .* to match all the thing
@ -111,6 +119,12 @@ const moduleWrapper = tsserver => {
: str.replace(/^.*zipfile:/, ``); : str.replace(/^.*zipfile:/, ``);
} break; } break;
case `neovim`: {
str = str.replace(/\.zip::/, `.zip/`);
// The path for neovim is in format of zipfile:///<pwd>/.yarn/...
return str.replace(/^zipfile:\/\//, ``);
} break;
case `vscode`: case `vscode`:
default: { default: {
return process.platform === `win32` return process.platform === `win32`
@ -143,8 +157,9 @@ const moduleWrapper = tsserver => {
let hostInfo = `unknown`; let hostInfo = `unknown`;
Object.assign(Session.prototype, { Object.assign(Session.prototype, {
onMessage(/** @type {string} */ message) { onMessage(/** @type {string | object} */ message) {
const parsedMessage = JSON.parse(message) const isStringMessage = typeof message === 'string';
const parsedMessage = isStringMessage ? JSON.parse(message) : message;
if ( if (
parsedMessage != null && parsedMessage != null &&
@ -153,14 +168,23 @@ const moduleWrapper = tsserver => {
typeof parsedMessage.arguments.hostInfo === `string` typeof parsedMessage.arguments.hostInfo === `string`
) { ) {
hostInfo = parsedMessage.arguments.hostInfo; 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)\./)) { if (hostInfo === `vscode` && process.env.VSCODE_IPC_HOOK) {
hostInfo += ` <1.61`; 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) => { const processedMessageJSON = JSON.stringify(parsedMessage, (key, value) => {
return typeof value === `string` ? fromEditorPath(value) : value; return typeof value === 'string' ? fromEditorPath(value) : value;
})); });
return originalOnMessage.call(
this,
isStringMessage ? processedMessageJSON : JSON.parse(processedMessageJSON)
);
}, },
send(/** @type {any} */ msg) { send(/** @type {any} */ msg) {

@ -1,6 +1,6 @@
{ {
"name": "typescript", "name": "typescript",
"version": "4.4.3-sdk", "version": "4.5.5-sdk",
"main": "./lib/typescript.js", "main": "./lib/typescript.js",
"type": "commonjs" "type": "commonjs"
} }

@ -7653,6 +7653,15 @@ fsevents@~2.3.2:
languageName: node languageName: node
linkType: hard 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": "pretty-error@npm:^4.0.0":
version: 4.0.0 version: 4.0.0
resolution: "pretty-error@npm:4.0.0" resolution: "pretty-error@npm:4.0.0"
@ -9410,6 +9419,7 @@ resolve@^2.0.0-next.3:
common: "workspace:*" common: "workspace:*"
event-stream: ^4.0.1 event-stream: ^4.0.1
jsonc-parser: ^2.0.0 jsonc-parser: ^2.0.0
prettier: ^2.6.2
semver: ^7.3.5 semver: ^7.3.5
socks: ^2.2.0 socks: ^2.2.0
source-map: ^0.7.3 source-map: ^0.7.3

@ -34,6 +34,7 @@
- Also ditched `@yarnpkg/plugin-version` which wasn't even really used in the first place - 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` - Created a `common` module which now holds `fileSystemConfig.ts` and `webviewMessages.ts`
- Improve webview ESLint setup, namely update `@typescript-eslint/*` and remove unused plugins - 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) ## v1.24.1 (2021-12-07)

@ -414,6 +414,7 @@
"@types/vscode": "~1.49.0", "@types/vscode": "~1.49.0",
"@types/webpack": "^4.4.25", "@types/webpack": "^4.4.25",
"@types/winreg": "^1.2.30", "@types/winreg": "^1.2.30",
"prettier": "^2.6.2",
"source-map": "^0.7.3", "source-map": "^0.7.3",
"source-map-support": "^0.5.19", "source-map-support": "^0.5.19",
"ts-loader": "^9.2.3", "ts-loader": "^9.2.3",

Loading…
Cancel
Save