From ace47fdc037e7040c86a1abd05d80f45c64fee0b Mon Sep 17 00:00:00 2001 From: Jonas Jing <932166095@qq.com> Date: Wed, 27 Nov 2024 15:12:18 +0800 Subject: [PATCH] feat: python language features (#991) Signed-off-by: Jonas(Jiajie) Jing --- CHANGELOG.md | 4 + client/package-lock.json | 62 +- client/package.json | 4 +- client/src/browser/extension.ts | 2 +- client/src/commands/run.ts | 3 +- package-lock.json | 67 +- package.json | 3 +- server/package-lock.json | 452 ++++++++++- server/package.json | 8 +- server/src/browser/server.ts | 10 +- server/src/node/server.ts | 11 +- server/src/python/PyrightLanguageProvider.ts | 8 + .../browser/PyrightLanguageProviderBrowser.ts | 278 +++++++ server/src/python/browser/fakeFileSystem.ts | 83 ++ server/src/python/browser/typeShed.ts | 10 + .../python/browser/typeshed-loader/index.js | 50 ++ .../node/PyrightLanguageProviderNode.ts | 297 ++++++++ server/src/python/utils.ts | 136 ++++ server/src/sas/CodeZoneManager.ts | 102 +-- server/src/sas/CompletionProvider.ts | 48 +- server/src/sas/LanguageServiceProvider.ts | 11 +- server/src/sas/Lexer.ts | 13 +- server/src/sas/LexerEx.ts | 13 +- server/src/sas/utils.ts | 8 + server/src/server.ts | 717 +++++++++++++++--- syntaxes/sas.tmLanguage.json | 4 +- tools/build.mjs | 19 +- webpack.config.js | 37 +- 28 files changed, 2228 insertions(+), 232 deletions(-) create mode 100644 server/src/python/PyrightLanguageProvider.ts create mode 100644 server/src/python/browser/PyrightLanguageProviderBrowser.ts create mode 100644 server/src/python/browser/fakeFileSystem.ts create mode 100644 server/src/python/browser/typeShed.ts create mode 100644 server/src/python/browser/typeshed-loader/index.js create mode 100644 server/src/python/node/PyrightLanguageProviderNode.ts create mode 100644 server/src/python/utils.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 0474a5238..5c898734f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), ## [v1.12.0] - 2024-11-25 +### Changed + +- Required VS Code version 1.89 at minimum + ### Added - Added username and password support for SSH connection type ([#1126](https://github.com/sassoftware/vscode-sas-extension/pull/1126)) diff --git a/client/package-lock.json b/client/package-lock.json index ba8c938a2..a24aadeb8 100644 --- a/client/package-lock.json +++ b/client/package-lock.json @@ -17,7 +17,7 @@ "react-dom": "^18.3.1", "ssh2": "^1.15.0", "uuid": "^11.0.2", - "vscode-languageclient": "^9.0.1", + "vscode-languageclient": "^10.0.0-next.2", "zustand": "^5.0.0" }, "devDependencies": { @@ -30,7 +30,7 @@ "@vscode/test-electron": "^2.4.1" }, "engines": { - "vscode": "^1.82.0" + "vscode": "^1.89.0" } }, "node_modules/@types/node": { @@ -183,7 +183,8 @@ "node_modules/balanced-match": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz", - "integrity": "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==" + "integrity": "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==", + "license": "MIT" }, "node_modules/base64-js": { "version": "1.5.1", @@ -242,6 +243,7 @@ "version": "2.0.1", "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-2.0.1.tgz", "integrity": "sha512-XnAIvQ8eM+kC6aULx6wuQiwVsnzsi9d3WxzV3FpWTGA19F621kwdbsAcFKXgKUHZWsy+mY6iL1sHTxWEFCytDA==", + "license": "MIT", "dependencies": { "balanced-match": "^1.0.0" } @@ -602,14 +604,18 @@ } }, "node_modules/minimatch": { - "version": "5.1.6", - "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-5.1.6.tgz", - "integrity": "sha512-lKwV/1brpG6mBUFHtb7NUmtABCb2WZZmm2wNiOA5hAb8VdCS4B3dtMWyvcoViccwAW/COERjXLt0zP1zXUN26g==", + "version": "9.0.5", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-9.0.5.tgz", + "integrity": "sha512-G6T0ZX48xgozx7587koeX9Ys2NYy6Gmv//P89sEte9V9whIapMNF4idKxnW2QtCcLiTWlb/wfCabAtAFWhhBow==", + "license": "ISC", "dependencies": { "brace-expansion": "^2.0.1" }, "engines": { - "node": ">=10" + "node": ">=16 || 14 >=14.17" + }, + "funding": { + "url": "https://github.com/sponsors/isaacs" } }, "node_modules/ms": { @@ -905,39 +911,43 @@ } }, "node_modules/vscode-jsonrpc": { - "version": "8.2.0", - "resolved": "https://registry.npmjs.org/vscode-jsonrpc/-/vscode-jsonrpc-8.2.0.tgz", - "integrity": "sha512-C+r0eKJUIfiDIfwJhria30+TYWPtuHJXHtI7J0YlOmKAo7ogxP20T0zxB7HZQIFhIyvoBPwWskjxrvAtfjyZfA==", + "version": "9.0.0-next.4", + "resolved": "https://registry.npmjs.org/vscode-jsonrpc/-/vscode-jsonrpc-9.0.0-next.4.tgz", + "integrity": "sha512-zSVIr58lJSMYKIsZ5P7GtBbv1eEx25eNyOf0NmEzxmn1GhUNJAVAb5hkA1poKUwj1FRMwN6CeyWxZypmr8SsQQ==", + "license": "MIT", "engines": { "node": ">=14.0.0" } }, "node_modules/vscode-languageclient": { - "version": "9.0.1", - "resolved": "https://registry.npmjs.org/vscode-languageclient/-/vscode-languageclient-9.0.1.tgz", - "integrity": "sha512-JZiimVdvimEuHh5olxhxkht09m3JzUGwggb5eRUkzzJhZ2KjCN0nh55VfiED9oez9DyF8/fz1g1iBV3h+0Z2EA==", + "version": "10.0.0-next.8", + "resolved": "https://registry.npmjs.org/vscode-languageclient/-/vscode-languageclient-10.0.0-next.8.tgz", + "integrity": "sha512-D9inIHgqKayO9Tv0MeLb3XIL76yTuWmKdHqcGZKzjtQrMGJgASJDYWTapu+yAjEpDp0gmVOaCYyIlLB86ncDoQ==", + "license": "MIT", "dependencies": { - "minimatch": "^5.1.0", - "semver": "^7.3.7", - "vscode-languageserver-protocol": "3.17.5" + "minimatch": "^9.0.3", + "semver": "^7.6.0", + "vscode-languageserver-protocol": "3.17.6-next.6" }, "engines": { - "vscode": "^1.82.0" + "vscode": "^1.89.0" } }, "node_modules/vscode-languageserver-protocol": { - "version": "3.17.5", - "resolved": "https://registry.npmjs.org/vscode-languageserver-protocol/-/vscode-languageserver-protocol-3.17.5.tgz", - "integrity": "sha512-mb1bvRJN8SVznADSGWM9u/b07H7Ecg0I3OgXDuLdn307rl/J3A9YD6/eYOssqhecL27hK1IPZAsaqh00i/Jljg==", + "version": "3.17.6-next.6", + "resolved": "https://registry.npmjs.org/vscode-languageserver-protocol/-/vscode-languageserver-protocol-3.17.6-next.6.tgz", + "integrity": "sha512-naxM9kc/phpl0kAFNVPejMUWUtzFXdPYY/BtQTYtfbBbHf8sceHOrKkmf6yynZRu1A4oFtRZNqV3wyFRTWqUHw==", + "license": "MIT", "dependencies": { - "vscode-jsonrpc": "8.2.0", - "vscode-languageserver-types": "3.17.5" + "vscode-jsonrpc": "9.0.0-next.4", + "vscode-languageserver-types": "3.17.6-next.4" } }, "node_modules/vscode-languageserver-types": { - "version": "3.17.5", - "resolved": "https://registry.npmjs.org/vscode-languageserver-types/-/vscode-languageserver-types-3.17.5.tgz", - "integrity": "sha512-Ld1VelNuX9pdF39h2Hgaeb5hEZM2Z3jUrrMgWQAu82jMtZp7p3vJT3BzToKtZI7NgQssZje5o0zryOrhQvzQAg==" + "version": "3.17.6-next.4", + "resolved": "https://registry.npmjs.org/vscode-languageserver-types/-/vscode-languageserver-types-3.17.6-next.4.tgz", + "integrity": "sha512-SeJTpH/S14EbxOAVaOUoGVqPToqpRTld5QO5Ghig3AlbFJTFF9Wu7srHMfa85L0SX1RYAuuCSFKJVVCxDIk1/Q==", + "license": "MIT" }, "node_modules/zustand": { "version": "5.0.0", @@ -968,4 +978,4 @@ } } } -} +} \ No newline at end of file diff --git a/client/package.json b/client/package.json index 38da0a2d5..6b564374a 100644 --- a/client/package.json +++ b/client/package.json @@ -6,7 +6,7 @@ "version": "0.0.1", "publisher": "SAS", "engines": { - "vscode": "^1.82.0" + "vscode": "^1.89.0" }, "dependencies": { "ag-grid-community": "^32.3.2", @@ -17,7 +17,7 @@ "react-dom": "^18.3.1", "ssh2": "^1.15.0", "uuid": "^11.0.2", - "vscode-languageclient": "^9.0.1", + "vscode-languageclient": "^10.0.0-next.2", "zustand": "^5.0.0" }, "devDependencies": { diff --git a/client/src/browser/extension.ts b/client/src/browser/extension.ts index 5920b2b81..e90103008 100644 --- a/client/src/browser/extension.ts +++ b/client/src/browser/extension.ts @@ -36,8 +36,8 @@ function createWorkerLanguageClient( return new LanguageClient( "sas-lsp", "SAS Language Server", - clientOptions, worker, + clientOptions, ); } diff --git a/client/src/commands/run.ts b/client/src/commands/run.ts index 55d0a31f7..8ccb0ad15 100644 --- a/client/src/commands/run.ts +++ b/client/src/commands/run.ts @@ -47,8 +47,7 @@ async function getSelectedRegions( "sas/getFoldingBlock", { textDocument: { uri: window.activeTextEditor.document.uri.toString() }, - line, - col, + position: { line, col }, }, ); if (block) { diff --git a/package-lock.json b/package-lock.json index 705765953..8dd55093d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -19,6 +19,7 @@ "@typescript-eslint/eslint-plugin": "^8.13.0", "@typescript-eslint/parser": "^8.13.0", "@vscode/l10n-dev": "^0.0.35", + "buffer": "6.0.3", "chai": "^4.4.1", "concurrently": "^9.0.1", "cross-env": "^7.0.3", @@ -41,7 +42,7 @@ "webpack-cli": "^5.1.4" }, "engines": { - "vscode": "^1.82.0" + "vscode": "^1.89.0" } }, "node_modules/@aashutoshrathi/word-wrap": { @@ -2320,6 +2321,26 @@ "dev": true, "license": "MIT" }, + "node_modules/base64-js": { + "version": "1.5.1", + "resolved": "https://registry.npmjs.org/base64-js/-/base64-js-1.5.1.tgz", + "integrity": "sha512-AKpaYlHn8t4SVbOHCy+b5+KKgvR4vrsD8vbvrbiQJps7fKDTkjkDry6ji0rUJjC0kzbNePLwzxq8iypo41qeWA==", + "dev": true, + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/feross" + }, + { + "type": "patreon", + "url": "https://www.patreon.com/feross" + }, + { + "type": "consulting", + "url": "https://feross.org/support" + } + ] + }, "node_modules/binary-extensions": { "version": "2.2.0", "dev": true, @@ -2392,6 +2413,30 @@ "node": "^6 || ^7 || ^8 || ^9 || ^10 || ^11 || ^12 || >=13.7" } }, + "node_modules/buffer": { + "version": "6.0.3", + "resolved": "https://registry.npmjs.org/buffer/-/buffer-6.0.3.tgz", + "integrity": "sha512-FTiCpNxtwiZZHEZbcbTIcZjERVICn9yq/pDFkTl95/AxzD1naBctN7YO68riM/gLSDY7sdrMby8hofADYuuqOA==", + "dev": true, + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/feross" + }, + { + "type": "patreon", + "url": "https://www.patreon.com/feross" + }, + { + "type": "consulting", + "url": "https://feross.org/support" + } + ], + "dependencies": { + "base64-js": "^1.3.1", + "ieee754": "^1.2.1" + } + }, "node_modules/buffer-from": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/buffer-from/-/buffer-from-1.1.2.tgz", @@ -3990,6 +4035,26 @@ "node": ">= 6" } }, + "node_modules/ieee754": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/ieee754/-/ieee754-1.2.1.tgz", + "integrity": "sha512-dcyqhDvX1C46lXZcVqCpK+FtMRQVdIMN6/Df5js2zouUsqG7I6sFxitIC+7KYK29KdXOLHdu9zL4sFnoVQnqaA==", + "dev": true, + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/feross" + }, + { + "type": "patreon", + "url": "https://www.patreon.com/feross" + }, + { + "type": "consulting", + "url": "https://feross.org/support" + } + ] + }, "node_modules/ignore": { "version": "5.3.1", "resolved": "https://registry.npmjs.org/ignore/-/ignore-5.3.1.tgz", diff --git a/package.json b/package.json index b81abbf1b..fc6e01fa9 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,7 @@ "license": "Apache-2.0", "icon": "icons/sas.png", "engines": { - "vscode": "^1.82.0" + "vscode": "^1.89.0" }, "activationEvents": [ "onWebviewPanel:SASResultPanel", @@ -1275,6 +1275,7 @@ "@typescript-eslint/eslint-plugin": "^8.13.0", "@typescript-eslint/parser": "^8.13.0", "@vscode/l10n-dev": "^0.0.35", + "buffer": "6.0.3", "chai": "^4.4.1", "concurrently": "^9.0.1", "esbuild": "^0.24.0", diff --git a/server/package-lock.json b/server/package-lock.json index 4a4434473..4708840db 100644 --- a/server/package-lock.json +++ b/server/package-lock.json @@ -9,39 +9,451 @@ "version": "0.0.1", "license": "Apache-2.0", "dependencies": { - "vscode-languageserver": "^9.0.1", - "vscode-languageserver-textdocument": "^1.0.11" + "pyright-internal-browser": "^1.1.367", + "pyright-internal-node": "^1.1.367", + "vscode-languageserver": "^10.0.0-next.2", + "vscode-languageserver-textdocument": "1.0.11" }, "engines": { "node": "*" } }, + "node_modules/@iarna/toml": { + "version": "2.2.5", + "resolved": "https://registry.npmjs.org/@iarna/toml/-/toml-2.2.5.tgz", + "integrity": "sha512-trnsAYxU3xnS1gPHPyU961coFyLkh4gAD/0zQ5mymY4yOZ+CYvsPqUbOFSw0aDM4y0tV7tiFxL/1XfXPNC6IPg==" + }, + "node_modules/@types/emscripten": { + "version": "1.39.12", + "resolved": "https://registry.npmjs.org/@types/emscripten/-/emscripten-1.39.12.tgz", + "integrity": "sha512-AQImDBgudQfMqUBfrjZYilRxoHDzTBp+ejh+g1fY67eSMalwIKtBXofjpyI0JBgNpHGzxeGAR2QDya0wxW9zbA==" + }, + "node_modules/@yarnpkg/fslib": { + "version": "2.10.4", + "resolved": "https://registry.npmjs.org/@yarnpkg/fslib/-/fslib-2.10.4.tgz", + "integrity": "sha512-WhaLwvXEMjCjGxOraQx+Qtmst13iAPOlSElSZfQFdLohva5owlqACRapJ78zZFEW6M9ArqdQlZaHKVN5/mM+SA==", + "dependencies": { + "@yarnpkg/libzip": "^2.3.0", + "tslib": "^1.13.0" + }, + "engines": { + "node": ">=12 <14 || 14.2 - 14.9 || >14.10.0" + } + }, + "node_modules/@yarnpkg/libzip": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/@yarnpkg/libzip/-/libzip-2.3.0.tgz", + "integrity": "sha512-6xm38yGVIa6mKm/DUCF2zFFJhERh/QWp1ufm4cNUvxsONBmfPg8uZ9pZBdOmF6qFGr/HlT6ABBkCSx/dlEtvWg==", + "dependencies": { + "@types/emscripten": "^1.39.6", + "tslib": "^1.13.0" + }, + "engines": { + "node": ">=12 <14 || 14.2 - 14.9 || >14.10.0" + } + }, + "node_modules/ansi-styles": { + "version": "4.3.0", + "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.3.0.tgz", + "integrity": "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg==", + "dependencies": { + "color-convert": "^2.0.1" + }, + "engines": { + "node": ">=8" + }, + "funding": { + "url": "https://github.com/chalk/ansi-styles?sponsor=1" + } + }, + "node_modules/anymatch": { + "version": "3.1.3", + "resolved": "https://registry.npmjs.org/anymatch/-/anymatch-3.1.3.tgz", + "integrity": "sha512-KMReFUr0B4t+D+OBkjR3KYqvocp2XaSzO55UcB6mgQMd3KbcE+mWTyvVV7D/zsdEbNnV6acZUutkiHQXvTr1Rw==", + "dependencies": { + "normalize-path": "^3.0.0", + "picomatch": "^2.0.4" + }, + "engines": { + "node": ">= 8" + } + }, + "node_modules/array-back": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/array-back/-/array-back-3.1.0.tgz", + "integrity": "sha512-TkuxA4UCOvxuDK6NZYXCalszEzj+TLszyASooky+i742l9TqsOdYCMJJupxRic61hwquNtppB3hgcuq9SVSH1Q==", + "engines": { + "node": ">=6" + } + }, + "node_modules/binary-extensions": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/binary-extensions/-/binary-extensions-2.3.0.tgz", + "integrity": "sha512-Ceh+7ox5qe7LJuLHoY0feh3pHuUDHAcRUeyL2VYghZwfpkNIy/+8Ocg0a3UuSoYzavmylwuLWQOf3hl0jjMMIw==", + "engines": { + "node": ">=8" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/braces": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/braces/-/braces-3.0.2.tgz", + "integrity": "sha512-b8um+L1RzM3WDSzvhm6gIz1yfTbBt6YTlcEKAvsmqCZZFw46z626lVj9j1yEPW33H5H+lBQpZMP1k8l+78Ha0A==", + "dependencies": { + "fill-range": "^7.0.1" + }, + "engines": { + "node": ">=8" + } + }, + "node_modules/buffer-from": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/buffer-from/-/buffer-from-1.1.2.tgz", + "integrity": "sha512-E+XQCRwSbaaiChtv6k6Dwgc+bx+Bs6vuKJHHl5kox/BaKbhiXzqQOwK4cO22yElGp2OCmjwVhT3HmxgyPGnJfQ==" + }, + "node_modules/chalk": { + "version": "4.1.2", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.2.tgz", + "integrity": "sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA==", + "dependencies": { + "ansi-styles": "^4.1.0", + "supports-color": "^7.1.0" + }, + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/chalk/chalk?sponsor=1" + } + }, + "node_modules/chokidar": { + "version": "3.6.0", + "resolved": "https://registry.npmjs.org/chokidar/-/chokidar-3.6.0.tgz", + "integrity": "sha512-7VT13fmjotKpGipCW9JEQAusEPE+Ei8nl6/g4FBAmIm0GOOLMua9NDDo/DWp0ZAxCr3cPq5ZpBqmPAQgDda2Pw==", + "dependencies": { + "anymatch": "~3.1.2", + "braces": "~3.0.2", + "glob-parent": "~5.1.2", + "is-binary-path": "~2.1.0", + "is-glob": "~4.0.1", + "normalize-path": "~3.0.0", + "readdirp": "~3.6.0" + }, + "engines": { + "node": ">= 8.10.0" + }, + "funding": { + "url": "https://paulmillr.com/funding/" + }, + "optionalDependencies": { + "fsevents": "~2.3.2" + } + }, + "node_modules/color-convert": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz", + "integrity": "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==", + "dependencies": { + "color-name": "~1.1.4" + }, + "engines": { + "node": ">=7.0.0" + } + }, + "node_modules/color-name": { + "version": "1.1.4", + "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz", + "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==" + }, + "node_modules/command-line-args": { + "version": "5.2.1", + "resolved": "https://registry.npmjs.org/command-line-args/-/command-line-args-5.2.1.tgz", + "integrity": "sha512-H4UfQhZyakIjC74I9d34fGYDwk3XpSr17QhEd0Q3I9Xq1CETHo4Hcuo87WyWHpAF1aSLjLRf5lD9ZGX2qStUvg==", + "dependencies": { + "array-back": "^3.1.0", + "find-replace": "^3.0.0", + "lodash.camelcase": "^4.3.0", + "typical": "^4.0.0" + }, + "engines": { + "node": ">=4.0.0" + } + }, + "node_modules/fill-range": { + "version": "7.0.1", + "resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.0.1.tgz", + "integrity": "sha512-qOo9F+dMUmC2Lcb4BbVvnKJxTPjCm+RRpe4gDuGrzkL7mEVl/djYSu2OdQ2Pa302N4oqkSg9ir6jaLWJ2USVpQ==", + "dependencies": { + "to-regex-range": "^5.0.1" + }, + "engines": { + "node": ">=8" + } + }, + "node_modules/find-replace": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/find-replace/-/find-replace-3.0.0.tgz", + "integrity": "sha512-6Tb2myMioCAgv5kfvP5/PkZZ/ntTpVK39fHY7WkWBgvbeE+VHd/tZuZ4mrC+bxh4cfOZeYKVPaJIZtZXV7GNCQ==", + "dependencies": { + "array-back": "^3.0.1" + }, + "engines": { + "node": ">=4.0.0" + } + }, + "node_modules/fsevents": { + "version": "2.3.3", + "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.3.tgz", + "integrity": "sha512-5xoDfX+fL7faATnagmWPpbFtwh/R77WmMMqqHGS65C3vvB0YHrgF+B1YmZ3441tMj5n63k0212XNoJwzlhffQw==", + "hasInstallScript": true, + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": "^8.16.0 || ^10.6.0 || >=11.0.0" + } + }, + "node_modules/glob-parent": { + "version": "5.1.2", + "resolved": "https://registry.npmjs.org/glob-parent/-/glob-parent-5.1.2.tgz", + "integrity": "sha512-AOIgSQCepiJYwP3ARnGx+5VnTu2HBYdzbGP45eLw1vr3zB3vZLeyed1sC9hnbcOc9/SrMyM5RPQrkGz4aS9Zow==", + "dependencies": { + "is-glob": "^4.0.1" + }, + "engines": { + "node": ">= 6" + } + }, + "node_modules/has-flag": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz", + "integrity": "sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==", + "engines": { + "node": ">=8" + } + }, + "node_modules/is-binary-path": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/is-binary-path/-/is-binary-path-2.1.0.tgz", + "integrity": "sha512-ZMERYes6pDydyuGidse7OsHxtbI7WVeUEozgR/g7rd0xUimYNlvZRE/K2MgZTjWy725IfelLeVcEM97mmtRGXw==", + "dependencies": { + "binary-extensions": "^2.0.0" + }, + "engines": { + "node": ">=8" + } + }, + "node_modules/is-extglob": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/is-extglob/-/is-extglob-2.1.1.tgz", + "integrity": "sha512-SbKbANkN603Vi4jEZv49LeVJMn4yGwsbzZworEoyEiutsN3nJYdbO36zfhGJ6QEDpOZIFkDtnq5JRxmvl3jsoQ==", + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/is-glob": { + "version": "4.0.3", + "resolved": "https://registry.npmjs.org/is-glob/-/is-glob-4.0.3.tgz", + "integrity": "sha512-xelSayHH36ZgE7ZWhli7pW34hNbNl8Ojv5KVmkJD4hBdD3th8Tfk9vYasLM+mXWOZhFkgZfxhLSnrwRr4elSSg==", + "dependencies": { + "is-extglob": "^2.1.1" + }, + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/is-number": { + "version": "7.0.0", + "resolved": "https://registry.npmjs.org/is-number/-/is-number-7.0.0.tgz", + "integrity": "sha512-41Cifkg6e8TylSpdtTpeLVMqvSBEVzTttHvERD741+pnZ8ANv0004MRL43QKPDlK9cGvNp6NZWZUBlbGXYxxng==", + "engines": { + "node": ">=0.12.0" + } + }, + "node_modules/jsonc-parser": { + "version": "3.2.1", + "resolved": "https://registry.npmjs.org/jsonc-parser/-/jsonc-parser-3.2.1.tgz", + "integrity": "sha512-AilxAyFOAcK5wA1+LeaySVBrHsGQvUFCDWXKpZjzaL0PqW+xfBOttn8GNtWKFWqneyMZj41MWF9Kl6iPWLwgOA==" + }, + "node_modules/leven": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/leven/-/leven-3.1.0.tgz", + "integrity": "sha512-qsda+H8jTaUaN/x5vzW2rzc+8Rw4TAQ/4KjB46IwK5VH+IlVeeeje/EoZRpiXvIqjFgK84QffqPztGI3VBLG1A==", + "engines": { + "node": ">=6" + } + }, + "node_modules/lodash.camelcase": { + "version": "4.3.0", + "resolved": "https://registry.npmjs.org/lodash.camelcase/-/lodash.camelcase-4.3.0.tgz", + "integrity": "sha512-TwuEnCnxbc3rAvhf/LbG7tJUDzhqXyFnv3dtzLOPgCG/hODL7WFnsbwktkD7yUV0RrreP/l1PALq/YSg6VvjlA==" + }, + "node_modules/normalize-path": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/normalize-path/-/normalize-path-3.0.0.tgz", + "integrity": "sha512-6eZs5Ls3WtCisHWp9S2GUy8dqkpGi4BVSz3GaqiE6ezub0512ESztXUwUB6C6IKbQkY2Pnb/mD4WYojCRwcwLA==", + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/picomatch": { + "version": "2.3.1", + "resolved": "https://registry.npmjs.org/picomatch/-/picomatch-2.3.1.tgz", + "integrity": "sha512-JU3teHTNjmE2VCGFzuY8EXzCDVwEqB2a8fsIvwaStHhAWJEeVd1o1QD80CU6+ZdEXXSLbSsuLwJjkCBWqRQUVA==", + "engines": { + "node": ">=8.6" + }, + "funding": { + "url": "https://github.com/sponsors/jonschlinkert" + } + }, + "node_modules/pyright-internal-browser": { + "version": "1.1.367", + "resolved": "https://registry.npmjs.org/pyright-internal-browser/-/pyright-internal-browser-1.1.367.tgz", + "integrity": "sha512-mIbtpRRDZQ9j9NgP3oaKRI2usc2xfQ7xKrCMk/+wLprLintkFuQqkZX+VVDq3nF/QlzFE37+UiLMbKqeYTsIbw==", + "dependencies": { + "@iarna/toml": "2.2.5", + "@yarnpkg/fslib": "2.10.4", + "@yarnpkg/libzip": "2.3.0", + "chalk": "^4.1.2", + "chokidar": "^3.6.0", + "command-line-args": "^5.2.1", + "jsonc-parser": "^3.2.1", + "leven": "3.1.0", + "source-map-support": "^0.5.21", + "tmp": "^0.2.1", + "vscode-jsonrpc": "^9.0.0-next.2", + "vscode-languageserver": "^10.0.0-next.2", + "vscode-languageserver-textdocument": "1.0.11", + "vscode-languageserver-types": "^3.17.6-next.3", + "vscode-uri": "^3.0.8" + } + }, + "node_modules/pyright-internal-node": { + "version": "1.1.367", + "resolved": "https://registry.npmjs.org/pyright-internal-node/-/pyright-internal-node-1.1.367.tgz", + "integrity": "sha512-23v82uTBC8XmRvXrGQQvz0wW4thfaiAIzmV8SBhuMZn6tcllUvlv5BEJ6YFENBSkwWZgguS+p47JZvuxR0TKoQ==", + "dependencies": { + "@iarna/toml": "2.2.5", + "@yarnpkg/fslib": "2.10.4", + "@yarnpkg/libzip": "2.3.0", + "chalk": "^4.1.2", + "chokidar": "^3.6.0", + "command-line-args": "^5.2.1", + "jsonc-parser": "^3.2.1", + "leven": "3.1.0", + "source-map-support": "^0.5.21", + "tmp": "^0.2.1", + "vscode-jsonrpc": "^9.0.0-next.2", + "vscode-languageserver": "^10.0.0-next.2", + "vscode-languageserver-textdocument": "1.0.11", + "vscode-languageserver-types": "^3.17.6-next.3", + "vscode-uri": "^3.0.8" + } + }, + "node_modules/readdirp": { + "version": "3.6.0", + "resolved": "https://registry.npmjs.org/readdirp/-/readdirp-3.6.0.tgz", + "integrity": "sha512-hOS089on8RduqdbhvQ5Z37A0ESjsqz6qnRcffsMU3495FuTdqSm+7bhJ29JvIOsBDEEnan5DPu9t3To9VRlMzA==", + "dependencies": { + "picomatch": "^2.2.1" + }, + "engines": { + "node": ">=8.10.0" + } + }, + "node_modules/source-map": { + "version": "0.6.1", + "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz", + "integrity": "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==", + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/source-map-support": { + "version": "0.5.21", + "resolved": "https://registry.npmjs.org/source-map-support/-/source-map-support-0.5.21.tgz", + "integrity": "sha512-uBHU3L3czsIyYXKX88fdrGovxdSCoTGDRZ6SYXtSRxLZUzHg5P/66Ht6uoUlHu9EZod+inXhKo3qQgwXUT/y1w==", + "dependencies": { + "buffer-from": "^1.0.0", + "source-map": "^0.6.0" + } + }, + "node_modules/supports-color": { + "version": "7.2.0", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz", + "integrity": "sha512-qpCAvRl9stuOHveKsn7HncJRvv501qIacKzQlO/+Lwxc9+0q2wLyv4Dfvt80/DPn2pqOBsJdDiogXGR9+OvwRw==", + "dependencies": { + "has-flag": "^4.0.0" + }, + "engines": { + "node": ">=8" + } + }, + "node_modules/tmp": { + "version": "0.2.3", + "resolved": "https://registry.npmjs.org/tmp/-/tmp-0.2.3.tgz", + "integrity": "sha512-nZD7m9iCPC5g0pYmcaxogYKggSfLsdxl8of3Q/oIbqCqLLIO9IAF0GWjX1z9NZRHPiXv8Wex4yDCaZsgEw0Y8w==", + "engines": { + "node": ">=14.14" + } + }, + "node_modules/to-regex-range": { + "version": "5.0.1", + "resolved": "https://registry.npmjs.org/to-regex-range/-/to-regex-range-5.0.1.tgz", + "integrity": "sha512-65P7iz6X5yEr1cwcgvQxbbIw7Uk3gOy5dIdtZ4rDveLqhrdJP+Li/Hx6tyK0NEb+2GCyneCMJiGqrADCSNk8sQ==", + "dependencies": { + "is-number": "^7.0.0" + }, + "engines": { + "node": ">=8.0" + } + }, + "node_modules/tslib": { + "version": "1.14.1", + "resolved": "https://registry.npmjs.org/tslib/-/tslib-1.14.1.tgz", + "integrity": "sha512-Xni35NKzjgMrwevysHTCArtLDpPvye8zV/0E4EyYn43P7/7qvQwPh9BGkHewbMulVntbigmcT7rdX3BNo9wRJg==" + }, + "node_modules/typical": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/typical/-/typical-4.0.0.tgz", + "integrity": "sha512-VAH4IvQ7BDFYglMd7BPRDfLgxZZX4O4TFcRDA6EN5X7erNJJq+McIEp8np9aVtxrCJ6qx4GTYVfOWNjcqwZgRw==", + "engines": { + "node": ">=8" + } + }, "node_modules/vscode-jsonrpc": { - "version": "8.2.0", - "resolved": "https://registry.npmjs.org/vscode-jsonrpc/-/vscode-jsonrpc-8.2.0.tgz", - "integrity": "sha512-C+r0eKJUIfiDIfwJhria30+TYWPtuHJXHtI7J0YlOmKAo7ogxP20T0zxB7HZQIFhIyvoBPwWskjxrvAtfjyZfA==", + "version": "9.0.0-next.4", + "resolved": "https://registry.npmjs.org/vscode-jsonrpc/-/vscode-jsonrpc-9.0.0-next.4.tgz", + "integrity": "sha512-zSVIr58lJSMYKIsZ5P7GtBbv1eEx25eNyOf0NmEzxmn1GhUNJAVAb5hkA1poKUwj1FRMwN6CeyWxZypmr8SsQQ==", + "license": "MIT", "engines": { "node": ">=14.0.0" } }, "node_modules/vscode-languageserver": { - "version": "9.0.1", - "resolved": "https://registry.npmjs.org/vscode-languageserver/-/vscode-languageserver-9.0.1.tgz", - "integrity": "sha512-woByF3PDpkHFUreUa7Hos7+pUWdeWMXRd26+ZX2A8cFx6v/JPTtd4/uN0/jB6XQHYaOlHbio03NTHCqrgG5n7g==", + "version": "10.0.0-next.6", + "resolved": "https://registry.npmjs.org/vscode-languageserver/-/vscode-languageserver-10.0.0-next.6.tgz", + "integrity": "sha512-0Lh1nhQfSxo5Ob+ayYO1QTIsDix2/Lc72Urm1KZrCFxK5zIFYaEh3QFeM9oZih4Rzs0ZkQPXXnoHtpvs5GT+Zw==", + "license": "MIT", "dependencies": { - "vscode-languageserver-protocol": "3.17.5" + "vscode-languageserver-protocol": "3.17.6-next.6" }, "bin": { "installServerIntoExtension": "bin/installServerIntoExtension" } }, "node_modules/vscode-languageserver-protocol": { - "version": "3.17.5", - "resolved": "https://registry.npmjs.org/vscode-languageserver-protocol/-/vscode-languageserver-protocol-3.17.5.tgz", - "integrity": "sha512-mb1bvRJN8SVznADSGWM9u/b07H7Ecg0I3OgXDuLdn307rl/J3A9YD6/eYOssqhecL27hK1IPZAsaqh00i/Jljg==", + "version": "3.17.6-next.6", + "resolved": "https://registry.npmjs.org/vscode-languageserver-protocol/-/vscode-languageserver-protocol-3.17.6-next.6.tgz", + "integrity": "sha512-naxM9kc/phpl0kAFNVPejMUWUtzFXdPYY/BtQTYtfbBbHf8sceHOrKkmf6yynZRu1A4oFtRZNqV3wyFRTWqUHw==", + "license": "MIT", "dependencies": { - "vscode-jsonrpc": "8.2.0", - "vscode-languageserver-types": "3.17.5" + "vscode-jsonrpc": "9.0.0-next.4", + "vscode-languageserver-types": "3.17.6-next.4" } }, "node_modules/vscode-languageserver-textdocument": { @@ -50,9 +462,15 @@ "integrity": "sha512-X+8T3GoiwTVlJbicx/sIAF+yuJAqz8VvwJyoMVhwEMoEKE/fkDmrqUgDMyBECcM2A2frVZIUj5HI/ErRXCfOeA==" }, "node_modules/vscode-languageserver-types": { - "version": "3.17.5", - "resolved": "https://registry.npmjs.org/vscode-languageserver-types/-/vscode-languageserver-types-3.17.5.tgz", - "integrity": "sha512-Ld1VelNuX9pdF39h2Hgaeb5hEZM2Z3jUrrMgWQAu82jMtZp7p3vJT3BzToKtZI7NgQssZje5o0zryOrhQvzQAg==" + "version": "3.17.6-next.4", + "resolved": "https://registry.npmjs.org/vscode-languageserver-types/-/vscode-languageserver-types-3.17.6-next.4.tgz", + "integrity": "sha512-SeJTpH/S14EbxOAVaOUoGVqPToqpRTld5QO5Ghig3AlbFJTFF9Wu7srHMfa85L0SX1RYAuuCSFKJVVCxDIk1/Q==", + "license": "MIT" + }, + "node_modules/vscode-uri": { + "version": "3.0.8", + "resolved": "https://registry.npmjs.org/vscode-uri/-/vscode-uri-3.0.8.tgz", + "integrity": "sha512-AyFQ0EVmsOZOlAnxoFOGOq1SQDWAB7C6aqMGS23svWAllfOaxbuFvcT8D1i8z3Gyn8fraVeZNNmN6e9bxxXkKw==" } } } diff --git a/server/package.json b/server/package.json index 32d96e04d..3609e4fef 100644 --- a/server/package.json +++ b/server/package.json @@ -8,7 +8,9 @@ "node": "*" }, "dependencies": { - "vscode-languageserver": "^9.0.1", - "vscode-languageserver-textdocument": "^1.0.11" + "pyright-internal-node": "^1.1.367", + "pyright-internal-browser": "^1.1.367", + "vscode-languageserver": "^10.0.0-next.2", + "vscode-languageserver-textdocument": "1.0.11" } -} +} \ No newline at end of file diff --git a/server/src/browser/server.ts b/server/src/browser/server.ts index 0742cd2e1..82f4a96ac 100644 --- a/server/src/browser/server.ts +++ b/server/src/browser/server.ts @@ -1,19 +1,19 @@ // Copyright © 2022, SAS Institute Inc., Cary, NC, USA. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 +import { Connection } from "vscode-languageserver"; import { BrowserMessageReader, BrowserMessageWriter, createConnection, } from "vscode-languageserver/browser"; -import { init } from "../server"; +import { PyrightLanguageProviderBrowser } from "../python/browser/PyrightLanguageProviderBrowser"; +import { runServer } from "../server"; /* browser specific setup code */ - const messageReader = new BrowserMessageReader(self); const messageWriter = new BrowserMessageWriter(self); -const connection = createConnection(messageReader, messageWriter); +const connection: Connection = createConnection(messageReader, messageWriter); -/* from here on, all code is non-browser specific and could be shared with a regular extension */ -init(connection); +runServer(connection, new PyrightLanguageProviderBrowser(connection, 1)); diff --git a/server/src/node/server.ts b/server/src/node/server.ts index 2099dceb3..c01771b0c 100644 --- a/server/src/node/server.ts +++ b/server/src/node/server.ts @@ -1,12 +1,11 @@ // Copyright © 2022, SAS Institute Inc., Cary, NC, USA. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 +import { Connection } from "vscode-languageserver"; import { ProposedFeatures, createConnection } from "vscode-languageserver/node"; -import { init } from "../server"; +import { PyrightLanguageProviderNode } from "../python/node/PyrightLanguageProviderNode"; +import { runServer } from "../server"; -// Create a connection for the server, using Node's IPC as a transport. -// Also include all preview / proposed LSP features. -const connection = createConnection(ProposedFeatures.all); +const connection: Connection = createConnection(ProposedFeatures.all); -// platform independant initialization -init(connection); +runServer(connection, new PyrightLanguageProviderNode(connection, 1)); diff --git a/server/src/python/PyrightLanguageProvider.ts b/server/src/python/PyrightLanguageProvider.ts new file mode 100644 index 000000000..1ef554183 --- /dev/null +++ b/server/src/python/PyrightLanguageProvider.ts @@ -0,0 +1,8 @@ +// Copyright © 2024, SAS Institute Inc., Cary, NC, USA. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 +import type { PyrightLanguageProviderBrowser } from "./browser/PyrightLanguageProviderBrowser"; +import type { PyrightLanguageProviderNode } from "./node/PyrightLanguageProviderNode"; + +export type PyrightLanguageProvider = + | PyrightLanguageProviderNode + | PyrightLanguageProviderBrowser; diff --git a/server/src/python/browser/PyrightLanguageProviderBrowser.ts b/server/src/python/browser/PyrightLanguageProviderBrowser.ts new file mode 100644 index 000000000..836b84e8c --- /dev/null +++ b/server/src/python/browser/PyrightLanguageProviderBrowser.ts @@ -0,0 +1,278 @@ +// Copyright © 2022-2024, SAS Institute Inc., Cary, NC, USA. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 +import { + CallHierarchyIncomingCallsParams, + CallHierarchyItem, + CallHierarchyOutgoingCall, + CallHierarchyOutgoingCallsParams, + CallHierarchyPrepareParams, + CancellationToken, + CodeAction, + CodeActionParams, + Command, + CompletionItem, + CompletionList, + CompletionParams, + Connection, + Declaration, + DeclarationLink, + Definition, + DefinitionLink, + DidChangeConfigurationParams, + DidChangeWatchedFilesParams, + DidCloseTextDocumentParams, + DidOpenTextDocumentParams, + DocumentHighlight, + DocumentHighlightParams, + DocumentSymbol, + DocumentSymbolParams, + ExecuteCommandParams, + HoverParams, + InitializeParams, + InitializeResult, + LSPAny, + Location, + PrepareRenameParams, + ReferenceParams, + RenameParams, + ResultProgressReporter, + SignatureHelpParams, + SymbolInformation, + TextDocumentPositionParams, + WorkDoneProgressReporter, + WorkspaceSymbol, + WorkspaceSymbolParams, +} from "vscode-languageserver"; +import { TextDocument } from "vscode-languageserver-textdocument"; + +import { IPythonMode } from "pyright-internal-browser/dist/packages/pyright-internal/src/analyzer/sourceFile"; +import { FileSystem } from "pyright-internal-browser/dist/packages/pyright-internal/src/common/fileSystem"; +import { PyrightServer } from "pyright-internal-browser/dist/packages/pyright-internal/src/server"; + +import { LanguageServiceProvider } from "../../sas/LanguageServiceProvider"; +import { extractPythonCodes } from "../utils"; + +export class PyrightLanguageProviderBrowser extends PyrightServer { + protected sasLspProvider?: (uri: string) => LanguageServiceProvider; + + constructor( + connection: Connection, + maxWorkers: number, + realFileSystem?: FileSystem, + ) { + // eslint-disable-next-line @typescript-eslint/consistent-type-assertions + super({ ...connection, listen() {} } as LSPAny, maxWorkers, realFileSystem); + } + + public setSasLspProvider( + provider: (uri: string) => LanguageServiceProvider, + ): void { + this.sasLspProvider = provider; + } + + public getClientCapabilities() { + return this.client; + } + + protected setupConnection( + // eslint-disable-next-line @typescript-eslint/no-unused-vars + supportedCommands: string[], + // eslint-disable-next-line @typescript-eslint/no-unused-vars + supportedCodeActions: string[], + ): void { + return; + } + + public onInitialized(): void { + this.updateSettingsForAllWorkspaces(); + super.onInitialized(); + } + + public async initialize( + params: InitializeParams, + supportedCommands: string[], + supportedCodeActions: string[], + ): Promise { + return super.initialize(params, supportedCommands, supportedCodeActions); + } + + public addContentChange(doc: TextDocument): void { + const languageService = this.sasLspProvider!(doc.uri); + const pythonDoc = extractPythonCodes(doc, languageService); + + this.onDidChangeTextDocument({ + textDocument: doc, + contentChanges: [{ text: pythonDoc }], + }); + } + + public async onHover(params: HoverParams, token: CancellationToken) { + return await super.onHover(params, token); + } + + public async onDidOpenTextDocument( + params: DidOpenTextDocumentParams, + ipythonMode = IPythonMode.None, + ) { + const doc = TextDocument.create( + params.textDocument.uri, + "sas", + params.textDocument.version, + params.textDocument.text, + ); + const languageService = this.sasLspProvider!(params.textDocument.uri); + const pythonDocContent = extractPythonCodes(doc, languageService); + const newParams: DidOpenTextDocumentParams = { ...params }; + newParams.textDocument = { ...params.textDocument }; + newParams.textDocument.languageId = "python"; + newParams.textDocument.text = pythonDocContent; + await super.onDidOpenTextDocument(newParams, ipythonMode); + } + + public async onDidCloseTextDocument(params: DidCloseTextDocumentParams) { + await super.onDidCloseTextDocument(params); + } + + public onDidChangeConfiguration(params: DidChangeConfigurationParams): void { + super.onDidChangeConfiguration(params); + } + + public async onDefinition( + params: TextDocumentPositionParams, + token: CancellationToken, + ): Promise { + return await super.onDefinition(params, token); + } + + public async onDeclaration( + params: TextDocumentPositionParams, + token: CancellationToken, + ): Promise { + return await super.onDeclaration(params, token); + } + + public async onTypeDefinition( + params: TextDocumentPositionParams, + token: CancellationToken, + ): Promise { + return await super.onTypeDefinition(params, token); + } + + public async onReferences( + params: ReferenceParams, + token: CancellationToken, + workDoneReporter: WorkDoneProgressReporter, + resultReporter: ResultProgressReporter | undefined, + ): Promise { + return await super.onReferences( + params, + token, + workDoneReporter, + resultReporter, + ); + } + + public async onDocumentSymbol( + params: DocumentSymbolParams, + token: CancellationToken, + ): Promise { + return await super.onDocumentSymbol(params, token); + } + + public async onWorkspaceSymbol( + params: WorkspaceSymbolParams, + token: CancellationToken, + resultReporter: ResultProgressReporter | undefined, + ): Promise { + return await super.onWorkspaceSymbol(params, token, resultReporter); + } + + public async onDocumentHighlight( + params: DocumentHighlightParams, + token: CancellationToken, + ): Promise { + if (params.position.character < 0) { + return null; + } + return await super.onDocumentHighlight(params, token); + } + + public async onSignatureHelp( + params: SignatureHelpParams, + token: CancellationToken, + ) { + return await super.onSignatureHelp(params, token); + } + + public async onCompletion( + params: CompletionParams, + token: CancellationToken, + ): Promise { + return await super.onCompletion(params, token); + } + + public async onCompletionResolve( + params: CompletionItem, + token: CancellationToken, + ): Promise { + return await super.onCompletionResolve(params, token); + } + + public async onPrepareRenameRequest( + params: PrepareRenameParams, + token: CancellationToken, + ) { + return await super.onPrepareRenameRequest(params, token); + } + + public async onRenameRequest(params: RenameParams, token: CancellationToken) { + if (params.position.character < 0) { + return null; + } + return await super.onRenameRequest(params, token); + } + + public async onCallHierarchyPrepare( + params: CallHierarchyPrepareParams, + token: CancellationToken, + ): Promise { + return await super.onCallHierarchyPrepare(params, token); + } + + public async onCallHierarchyIncomingCalls( + params: CallHierarchyIncomingCallsParams, + token: CancellationToken, + ) { + return await super.onCallHierarchyIncomingCalls(params, token); + } + + public async onCallHierarchyOutgoingCalls( + params: CallHierarchyOutgoingCallsParams, + token: CancellationToken, + ): Promise { + return await super.onCallHierarchyOutgoingCalls(params, token); + } + + public onDidChangeWatchedFiles(params: DidChangeWatchedFilesParams): void { + super.onDidChangeWatchedFiles(params); + } + + public async onExecuteCommand( + params: ExecuteCommandParams, + token: CancellationToken, + reporter: WorkDoneProgressReporter, + ) { + return await super.onExecuteCommand(params, token, reporter); + } + + public async executeCodeAction( + params: CodeActionParams, + token: CancellationToken, + ): Promise<(Command | CodeAction)[] | undefined | null> { + return await super.executeCodeAction(params, token); + } + + public async onShutdown(token: CancellationToken): Promise { + return await super.onShutdown(token); + } +} diff --git a/server/src/python/browser/fakeFileSystem.ts b/server/src/python/browser/fakeFileSystem.ts new file mode 100644 index 000000000..778f759c8 --- /dev/null +++ b/server/src/python/browser/fakeFileSystem.ts @@ -0,0 +1,83 @@ +// Copyright © 2022-2024, SAS Institute Inc., Cary, NC, USA. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +/* eslint-disable @typescript-eslint/no-explicit-any */ + +/* eslint-disable @typescript-eslint/no-this-alias */ + +/* + * fakeFileSystem.ts + * + * simulate real fs access. + */ +import { CaseSensitivityDetector } from "pyright-internal-browser/dist/packages/pyright-internal/src/common/caseSensitivityDetector"; +import { ConsoleInterface } from "pyright-internal-browser/dist/packages/pyright-internal/src/common/console"; +import type { + FileSystem, + TempFile, + TmpfileOptions, +} from "pyright-internal-browser/dist/packages/pyright-internal/src/common/fileSystem"; +import { FileWatcherProvider } from "pyright-internal-browser/dist/packages/pyright-internal/src/common/fileWatcher"; +import { + directoryExists, + getDirectoryPath, +} from "pyright-internal-browser/dist/packages/pyright-internal/src/common/pathUtils"; +import { Uri } from "pyright-internal-browser/dist/packages/pyright-internal/src/common/uri/uri"; +import { UriEx } from "pyright-internal-browser/dist/packages/pyright-internal/src/common/uri/uriUtils"; +import { TestFileSystem } from "pyright-internal-browser/dist/packages/pyright-internal/src/tests/harness/vfs/filesystem"; + +import { typeShed } from "./typeShed"; + +const fs = new TestFileSystem(false, { cwd: "/" }); + +export function createFromRealFileSystem( + // eslint-disable-next-line @typescript-eslint/no-unused-vars + console?: ConsoleInterface, + // eslint-disable-next-line @typescript-eslint/no-unused-vars + fileWatcherProvider?: FileWatcherProvider, +): FileSystem { + // install builtin types + for (const entry of typeShed) { + const dir = getDirectoryPath(entry.filePath); + if (!directoryExists(fs, dir)) { + fs.mkdirpSync(dir); + } + fs.writeFileSync(UriEx.parse(entry.filePath), entry.content); + } + + return fs; +} + +export class RealTempFile implements TempFile, CaseSensitivityDetector { + private tmpDirPath: string = "pyright-tmp"; + private tmpFilePath: string = this.tmpDirPath + "/pyright-tmp-file"; + + constructor() { + // Empty + } + + tmpdir(): Uri { + if (!fs.existsSync(UriEx.parse(this.tmpDirPath))) { + fs.mkdirpSync(this.tmpDirPath); + } + return Uri.file(this.tmpDirPath, this); + } + + // eslint-disable-next-line @typescript-eslint/no-unused-vars + tmpfile(options?: TmpfileOptions): Uri { + const file = UriEx.parse(this.tmpFilePath); + if (!fs.existsSync(file)) { + fs.writeFileSync(file, ""); + } + return Uri.file(file.fileName, this); + } + + // eslint-disable-next-line @typescript-eslint/no-unused-vars + isCaseSensitive(uri: string): boolean { + // if (uri.startsWith(FileUriSchema)) { + // return this._isLocalFileSystemCaseSensitive(); + // } + + return true; + } +} diff --git a/server/src/python/browser/typeShed.ts b/server/src/python/browser/typeShed.ts new file mode 100644 index 000000000..3053cda7e --- /dev/null +++ b/server/src/python/browser/typeShed.ts @@ -0,0 +1,10 @@ +// Copyright © 2022-2024, SAS Institute Inc., Cary, NC, USA. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +// typeshed-loader will fill in real content when bundling +export const typeShed = [ + { + filePath: "", + content: "", + }, +]; diff --git a/server/src/python/browser/typeshed-loader/index.js b/server/src/python/browser/typeshed-loader/index.js new file mode 100644 index 000000000..7be34a0c7 --- /dev/null +++ b/server/src/python/browser/typeshed-loader/index.js @@ -0,0 +1,50 @@ +// Copyright © 2022-2024, SAS Institute Inc., Cary, NC, USA. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +/* eslint-disable @typescript-eslint/no-var-requires */ + +/** + * A webpack loader gathering typeshed for intellisense features + */ +const fs = require("fs/promises"); +const path = require("path"); + +const dirs = ["stdlib"]; +const result = []; + +async function* walk(dir) { + for await (const d of await fs.opendir(dir)) { + const entry = path.join(dir, d.name); + if (d.isDirectory()) { + yield* walk(entry); + } else if (d.isFile()) { + yield entry; + } + } +} + +async function loader() { + const callback = this.async(); + + for (const dir of dirs) { + const entry = path.resolve( + __dirname, + "../../../../node_modules/pyright-internal-node/dist/packages/pyright-internal/typeshed-fallback", + dir, + ); + const prefixLength = entry.indexOf("typeshed-fallback") - 1; + for await (const filename of walk(entry)) { + if (filename.endsWith(".pyi")) { + const content = await fs.readFile(filename); + result.push({ + content: content.toString(), + filePath: filename.slice(prefixLength).replace(/\\/g, "/"), + }); + } + } + } + + callback(null, `export const typeShed = ${JSON.stringify(result)}`); +} + +module.exports = loader; diff --git a/server/src/python/node/PyrightLanguageProviderNode.ts b/server/src/python/node/PyrightLanguageProviderNode.ts new file mode 100644 index 000000000..4db1cd926 --- /dev/null +++ b/server/src/python/node/PyrightLanguageProviderNode.ts @@ -0,0 +1,297 @@ +// Copyright © 2022-2024, SAS Institute Inc., Cary, NC, USA. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 +import { + CallHierarchyIncomingCallsParams, + CallHierarchyItem, + CallHierarchyOutgoingCall, + CallHierarchyOutgoingCallsParams, + CallHierarchyPrepareParams, + CancellationToken, + CodeAction, + CodeActionParams, + Command, + CompletionItem, + CompletionList, + CompletionParams, + Connection, + Declaration, + DeclarationLink, + Definition, + DefinitionLink, + DidChangeConfigurationParams, + DidChangeWatchedFilesParams, + DidCloseTextDocumentParams, + DidOpenTextDocumentParams, + DocumentHighlight, + DocumentHighlightParams, + DocumentSymbol, + DocumentSymbolParams, + ExecuteCommandParams, + HoverParams, + InitializeParams, + InitializeResult, + LSPAny, + Location, + PrepareRenameParams, + ReferenceParams, + RenameParams, + ResultProgressReporter, + SignatureHelpParams, + SymbolInformation, + TextDocumentPositionParams, + WorkDoneProgressReporter, + WorkspaceSymbol, + WorkspaceSymbolParams, +} from "vscode-languageserver"; +import { TextDocument } from "vscode-languageserver-textdocument"; + +import { IPythonMode } from "pyright-internal-node/dist/packages/pyright-internal/src/analyzer/sourceFile"; +import { + FileSystem, + ReadOnlyFileSystem, +} from "pyright-internal-node/dist/packages/pyright-internal/src/common/fileSystem"; +import { ClientCapabilities } from "pyright-internal-node/dist/packages/pyright-internal/src/common/languageServerInterface"; +import { DocumentRange } from "pyright-internal-node/dist/packages/pyright-internal/src/common/textRange"; +import { Uri } from "pyright-internal-node/dist/packages/pyright-internal/src/common/uri/uri"; +import { CollectionResult } from "pyright-internal-node/dist/packages/pyright-internal/src/languageService/documentSymbolCollector"; +import { ParseFileResults } from "pyright-internal-node/dist/packages/pyright-internal/src/parser/parser"; +import { PyrightServer } from "pyright-internal-node/dist/packages/pyright-internal/src/server"; + +import { LanguageServiceProvider } from "../../sas/LanguageServiceProvider"; +import { extractPythonCodes } from "../utils"; + +export class PyrightLanguageProviderNode extends PyrightServer { + protected sasLspProvider?: (uri: string) => LanguageServiceProvider; + + constructor( + connection: Connection, + maxWorkers: number, + realFileSystem?: FileSystem, + ) { + // eslint-disable-next-line @typescript-eslint/consistent-type-assertions + super({ ...connection, listen() {} } as LSPAny, maxWorkers, realFileSystem); + } + + public setSasLspProvider( + provider: (uri: string) => LanguageServiceProvider, + ): void { + this.sasLspProvider = provider; + } + + public getClientCapabilities(): ClientCapabilities { + return this.client; + } + + protected setupConnection( + // eslint-disable-next-line @typescript-eslint/no-unused-vars + supportedCommands: string[], + // eslint-disable-next-line @typescript-eslint/no-unused-vars + supportedCodeActions: string[], + ): void { + return; + } + + public onInitialized(): void { + this.updateSettingsForAllWorkspaces(); + super.onInitialized(); + } + + public async initialize( + params: InitializeParams, + supportedCommands: string[], + supportedCodeActions: string[], + ): Promise { + return super.initialize(params, supportedCommands, supportedCodeActions); + } + + public addContentChange(doc: TextDocument): void { + const languageService = this.sasLspProvider!(doc.uri); + const pythonDoc = extractPythonCodes(doc, languageService); + + this.onDidChangeTextDocument({ + textDocument: doc, + contentChanges: [{ text: pythonDoc }], + }); + } + + public async onHover(params: HoverParams, token: CancellationToken) { + return await super.onHover(params, token); + } + + public async onDidOpenTextDocument( + params: DidOpenTextDocumentParams, + ipythonMode = IPythonMode.None, + ) { + const doc = TextDocument.create( + params.textDocument.uri, + "sas", + params.textDocument.version, + params.textDocument.text, + ); + const languageService = this.sasLspProvider!(params.textDocument.uri); + const pythonDocContent = extractPythonCodes(doc, languageService); + const newParams: DidOpenTextDocumentParams = { ...params }; + newParams.textDocument = { ...params.textDocument }; + newParams.textDocument.languageId = "python"; + newParams.textDocument.text = pythonDocContent; + await super.onDidOpenTextDocument(newParams, ipythonMode); + } + + public async onDidCloseTextDocument(params: DidCloseTextDocumentParams) { + await super.onDidCloseTextDocument(params); + } + + public onDidChangeConfiguration(params: DidChangeConfigurationParams): void { + super.onDidChangeConfiguration(params); + } + + public async onDefinition( + params: TextDocumentPositionParams, + token: CancellationToken, + ): Promise { + return await super.onDefinition(params, token); + } + + public async onDeclaration( + params: TextDocumentPositionParams, + token: CancellationToken, + ): Promise { + return await super.onDeclaration(params, token); + } + + public async onTypeDefinition( + params: TextDocumentPositionParams, + token: CancellationToken, + ): Promise { + return await super.onTypeDefinition(params, token); + } + + public async onReferences( + params: ReferenceParams, + token: CancellationToken, + workDoneReporter: WorkDoneProgressReporter, + resultReporter: ResultProgressReporter | undefined, + createDocumentRange?: ( + uri: Uri, + result: CollectionResult, + parseResults: ParseFileResults, + ) => DocumentRange, + convertToLocation?: ( + fs: ReadOnlyFileSystem, + ranges: DocumentRange, + ) => Location | undefined, + ): Promise { + return await super.onReferences( + params, + token, + workDoneReporter, + resultReporter, + createDocumentRange, + convertToLocation, + ); + } + + public async onDocumentSymbol( + params: DocumentSymbolParams, + token: CancellationToken, + ): Promise { + return await super.onDocumentSymbol(params, token); + } + + public async onWorkspaceSymbol( + params: WorkspaceSymbolParams, + token: CancellationToken, + resultReporter: ResultProgressReporter | undefined, + ): Promise { + return await super.onWorkspaceSymbol(params, token, resultReporter); + } + + public async onDocumentHighlight( + params: DocumentHighlightParams, + token: CancellationToken, + ): Promise { + if (params.position.character < 0) { + return null; + } + return await super.onDocumentHighlight(params, token); + } + + public async onSignatureHelp( + params: SignatureHelpParams, + token: CancellationToken, + ) { + return await super.onSignatureHelp(params, token); + } + + public async onCompletion( + params: CompletionParams, + token: CancellationToken, + ): Promise { + return await super.onCompletion(params, token); + } + + public async onCompletionResolve( + params: CompletionItem, + token: CancellationToken, + ): Promise { + return await super.onCompletionResolve(params, token); + } + + public async onPrepareRenameRequest( + params: PrepareRenameParams, + token: CancellationToken, + ) { + return await super.onPrepareRenameRequest(params, token); + } + + public async onRenameRequest(params: RenameParams, token: CancellationToken) { + if (params.position.character < 0) { + return null; + } + return await super.onRenameRequest(params, token); + } + + public async onCallHierarchyPrepare( + params: CallHierarchyPrepareParams, + token: CancellationToken, + ): Promise { + return await super.onCallHierarchyPrepare(params, token); + } + + public async onCallHierarchyIncomingCalls( + params: CallHierarchyIncomingCallsParams, + token: CancellationToken, + ) { + return await super.onCallHierarchyIncomingCalls(params, token); + } + + public async onCallHierarchyOutgoingCalls( + params: CallHierarchyOutgoingCallsParams, + token: CancellationToken, + ): Promise { + return await super.onCallHierarchyOutgoingCalls(params, token); + } + + public onDidChangeWatchedFiles(params: DidChangeWatchedFilesParams): void { + super.onDidChangeWatchedFiles(params); + } + + public async onExecuteCommand( + params: ExecuteCommandParams, + token: CancellationToken, + reporter: WorkDoneProgressReporter, + ) { + return await super.onExecuteCommand(params, token, reporter); + } + + public async onShutdown(token: CancellationToken): Promise { + return await super.onShutdown(token); + } + + public async executeCodeAction( + params: CodeActionParams, + token: CancellationToken, + ): Promise<(Command | CodeAction)[] | undefined | null> { + return await super.executeCodeAction(params, token); + } +} diff --git a/server/src/python/utils.ts b/server/src/python/utils.ts new file mode 100644 index 000000000..6e3c91f5a --- /dev/null +++ b/server/src/python/utils.ts @@ -0,0 +1,136 @@ +// Copyright © 2022-2024, SAS Institute Inc., Cary, NC, USA. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 +import { DocumentSymbol } from "vscode-languageserver"; +import { TextDocument } from "vscode-languageserver-textdocument"; + +import { CodeZoneManager } from "../sas/CodeZoneManager"; +import { LanguageServiceProvider } from "../sas/LanguageServiceProvider"; +import { isCustomRegionStartComment } from "../sas/utils"; + +export const extractPythonCodes = ( + doc: TextDocument, + languageService: LanguageServiceProvider, +): string => { + const codeZoneManager = languageService.getCodeZoneManager(); + const pythonDocLines = []; + const symbols: DocumentSymbol[] = languageService.getDocumentSymbols(); + for (let i = 0; i < symbols.length; i++) { + const symbol = symbols[i]; + if (isCustomRegionStartComment(symbol.name)) { + symbols.splice(i + 1, 0, ...(symbol.children ?? [])); + } + if (symbol.name?.toUpperCase() !== "PROC PYTHON") { + continue; + } + let pythonCodeStart = undefined; + let pythonCodeEnd = undefined; + const pos = { ...symbol.range.start }; + while (pos.line <= symbol.range.end.line) { + if ( + !pythonCodeStart && + codeZoneManager.getCurrentZone(pos.line, pos.character) === + CodeZoneManager.ZONE_TYPE.EMBEDDED_LANG + // // a codezone bug + // pos.line >= symbol.range.start.line + 2 + ) { + pythonCodeStart = { ...pos }; + } + if ( + pythonCodeStart && + codeZoneManager.getCurrentZone(pos.line, pos.character) !== + CodeZoneManager.ZONE_TYPE.EMBEDDED_LANG + ) { + pythonCodeEnd = { ...pos }; + if (pythonCodeEnd.character === 0) { + pythonCodeEnd.line--; + pythonCodeEnd.character = + languageService.model.getColumnCount(pythonCodeEnd.line) - 1; + if (pythonCodeEnd.character < 0) { + pythonCodeEnd.character = 0; + } + } + break; + } + pos.character++; + if (pos.character >= languageService.model.getLine(pos.line).length) { + pos.line++; + pos.character = 0; + } + } + if (!pythonCodeStart) { + continue; + } + const pythonCodeLines = doc + .getText({ + start: pythonCodeStart, + end: pythonCodeEnd ?? symbol.range.end, + }) + .split("\n"); + + let startingEmptyLineCount = 0; + let firstNotEmptyLine: string | undefined = undefined; + for (const line of pythonCodeLines) { + if (line.trim().length > 0 && !line.trim().startsWith("#")) { + firstNotEmptyLine = line; + break; + } else { + startingEmptyLineCount++; + } + } + if (startingEmptyLineCount > 0) { + pythonCodeLines.splice(0, startingEmptyLineCount); + pythonCodeStart.line += startingEmptyLineCount; + pythonCodeStart.character = 0; + } + + let tailingEmptyLineCount = 0; + for (let i = pythonCodeLines.length - 1; i >= 0; i--) { + if ( + pythonCodeLines[i].trim().length === 0 || + pythonCodeLines[i].trim().startsWith("#") + ) { + tailingEmptyLineCount++; + } else { + break; + } + } + if (tailingEmptyLineCount > 0) { + pythonCodeLines.splice(pythonCodeLines.length - tailingEmptyLineCount); + if (pythonCodeEnd) { + pythonCodeEnd.line -= startingEmptyLineCount; + pythonCodeEnd.character = + pythonCodeLines[pythonCodeLines.length - 1].length; + } + } + + const shouldAddDummyBlock: boolean = + !!firstNotEmptyLine && [" ", "\t"].includes(firstNotEmptyLine[0]); + const lineGap = pythonCodeStart.line - pythonDocLines.length; + // must be: proc python;submit; + if (lineGap === 0) { + let line = "if True:"; + line += " ".repeat(pythonCodeStart.character - line.length); + line += pythonCodeLines[0]; + if (firstNotEmptyLine) { + line += ";pass"; + } + pythonDocLines.push(line); + } else { + for (let i = 0; i < lineGap; i++) { + if (shouldAddDummyBlock && i === lineGap - 1) { + pythonDocLines.push("if True:"); + } else { + pythonDocLines.push(""); + } + } + for (const line of pythonCodeLines) { + pythonDocLines.push(line); + } + if (firstNotEmptyLine) { + pythonDocLines.push("pass"); + } + } + } + const pythonDoc = pythonDocLines.join("\n"); + return pythonDoc; +}; diff --git a/server/src/sas/CodeZoneManager.ts b/server/src/sas/CodeZoneManager.ts index 9531f92f6..de4dd71e9 100644 --- a/server/src/sas/CodeZoneManager.ts +++ b/server/src/sas/CodeZoneManager.ts @@ -90,6 +90,7 @@ export class CodeZoneManager { private _stmtName = ""; private _optName = ""; private _subOptName = ""; + private _embeddedCodeStarted = false; private _topZone = 0; private _sectionMode: { @@ -122,6 +123,7 @@ export class CodeZoneManager { this._stmtName = ""; this._optName = ""; this._subOptName = ""; + this._embeddedCodeStarted = false; } private _needOptionDelimiter() { if (!this._stmtWithOptionDelimiter[this._procName]) { @@ -249,57 +251,56 @@ export class CodeZoneManager { } private _token(line: number, col: number): TokenEx | null { let syntax = this._syntaxProvider.getSyntax(line); - const len = syntax.length; - let i = 1, - j = -1, + let foundSyntaxIdx = -1, type: Token["type"] = "text", currLine = line; - for (; i < len; i++) { - if (syntax[i].start >= col) { - if (syntax[i - 1].start <= col) { - j = i - 1; - type = syntax[j].style; + + for (let i = 0; i < syntax.length; i++) { + if (syntax[i].start <= col) { + if (i === syntax.length - 1 || syntax[i + 1].start > col) { + foundSyntaxIdx = i; + type = syntax[i].style; break; - } else { - break; //not found, not continue } } } if (Lexer.isComment[type] || Lexer.isLiteral[type]) { while (currLine >= 0) { - if (syntax[j].state instanceof Object) { + if (syntax[foundSyntaxIdx].state instanceof Object) { return { type: type, text: "", line: currLine, - col: syntax[j].start, - endLine: syntax[j].state.line, - endCol: syntax[j].state.col, + col: syntax[foundSyntaxIdx].start, + endLine: syntax[foundSyntaxIdx].state.line, + endCol: syntax[foundSyntaxIdx].state.col, }; - } else if (syntax[j].state === 1) { + } else if (syntax[foundSyntaxIdx].state === 1) { //met the start of the node break; } else { - j--; - if (j < 0) { + foundSyntaxIdx--; + if (foundSyntaxIdx < 0) { do { currLine--; syntax = this._syntaxProvider.getSyntax(currLine); //skip the line without syntax } while (currLine >= 0 && syntax.length === 0); - j = syntax.length - 1; + foundSyntaxIdx = syntax.length - 1; } } } } - if (i > 0) { + if (foundSyntaxIdx >= 0) { const lineText = this._model.getLine(line); let endCol = 0, startCol = 0; syntax = this._syntaxProvider.getSyntax(line); if (syntax.length !== 0) { - endCol = syntax[i] ? syntax[i].start : lineText.length; - startCol = syntax[i - 1].start; + endCol = syntax[foundSyntaxIdx + 1] + ? syntax[foundSyntaxIdx + 1].start + : lineText.length; + startCol = syntax[foundSyntaxIdx].start; } return { type: type, @@ -318,8 +319,7 @@ export class CodeZoneManager { text = "", col = 0, type: Token["type"] = "text", - i = 0, - token = null; + i = 0; const lineCount = this._model.getLineCount(); if (context.line >= lineCount) { @@ -355,26 +355,23 @@ export class CodeZoneManager { if ( /*syntax[syntaxLen-1].state !== 0 && */ // for the line without normal end mark. syntax[syntaxLen - 1].start <= context.col && - lineLen >= context.col + lineLen >= context.col && + syntax[syntaxLen - 1].start < lineLen // to prevent "" ) { i = syntaxLen - 1; } else { for (i = context.syntaxIdx; i >= 0; i--) { if ( syntax[i].start <= context.col && - syntax[i + 1].start >= context.col + syntax[i + 1].start >= context.col && + syntax[i].start !== syntax[i + 1].start // to prevent "" ) { break; } } } - // get the text and type - if (context.syntaxIdx < 0 || i < 0) { - //this line is special, no normal end mark, - col = 0; - type = syntax[0].style; - text = line.substring(0, lineLen); - } else { + + if (i >= 0) { col = syntax[i].start; type = syntax[i].style; text = line.substring( @@ -382,18 +379,22 @@ export class CodeZoneManager { syntax[i + 1] ? syntax[i + 1].start : lineLen, ); } + // adjust pointer - if (i < 1) { - context.col = -1; - context.syntaxIdx = -1; - } else { + if (i >= 1) { context.col = syntax[i - 1].start; context.syntaxIdx = i - 1; + } else { + context.col = -1; + context.syntaxIdx = -1; } } while (/^\s*$/.test(text)); if (Lexer.isComment[type] || Lexer.isLiteral[type]) { - token = this._token(context.line, col + 1)!; + const token = this._token(context.line, col)!; + if (!token) { + return null; + } if ( token.endLine && (token.line !== context.line || token.col !== context.col) @@ -692,9 +693,6 @@ export class CodeZoneManager { len = 0; const tokens = []; - const initLine = context.line; - const initCol = context.col; - context.syntaxIdx = -1; context.tokens = null; while (true) { @@ -1136,14 +1134,19 @@ export class CodeZoneManager { const zone = this._stmtEx(context, stmt); type = zone.type; if (["PYTHON", "LUA"].includes(this._procName)) { - if ( - ["SUBMIT", "ENDSUBMIT", "INTERACTIVE", "ENDINTERACTIVE"].includes( - stmt.text, - ) - ) { + if (!this._embeddedCodeStarted) { + if (["SUBMIT", "INTERACTIVE", "I"].includes(stmt.text)) { + this._embeddedCodeStarted = true; + } return CodeZoneManager.ZONE_TYPE.PROC_STMT; } else { - return CodeZoneManager.ZONE_TYPE.EMBEDDED_LANG; + // this._embeddedCodeStarted is true + if (["ENDSUBMIT", "ENDINTERACTIVE", "RUN"].includes(stmt.text)) { + this._embeddedCodeStarted = false; + return CodeZoneManager.ZONE_TYPE.PROC_STMT; + } else { + return CodeZoneManager.ZONE_TYPE.EMBEDDED_LANG; + } } } else if (this._isCall(zone)) { type = CodeZoneManager.ZONE_TYPE.RESTRICTED; @@ -2456,8 +2459,11 @@ export class CodeZoneManager { } } private _currentZone(line: number, col: number) { - const newToken = this._token(line, col)!, - type = newToken.type; //self.type(line,col), + const newToken = this._token(line, col)!; + const type = newToken.type; + if (type === "embedded-code") { + return CodeZoneManager.ZONE_TYPE.EMBEDDED_LANG; + } let context = null, pos: any = this._normalize(line, col - 1); const tmpLine = pos.line, diff --git a/server/src/sas/CompletionProvider.ts b/server/src/sas/CompletionProvider.ts index f27e20be4..73af9d311 100644 --- a/server/src/sas/CompletionProvider.ts +++ b/server/src/sas/CompletionProvider.ts @@ -612,9 +612,7 @@ export class CompletionProvider { }); } - getCompleteItems( - position: Position, - ): Promise { + getCompleteItems(position: Position): Promise { return new Promise((resolve) => { this._getZone(position); const prefix = this.popupContext.prefix; @@ -686,20 +684,36 @@ export class CompletionProvider { getCompleteItemHelp(item: CompletionItem): Promise { return new Promise((resolve) => { - this._loadHelp({ - keyword: item.label, - type: "tooltip", - ...this.popupContext, - cb: (data) => { - if (data && data.data) { - item.documentation = { - kind: MarkupKind.Markdown, - value: this._addLinkContext(this.popupContext.zone, data), - }; - } - resolve(item); - }, - }); + if (["endsubmit", "endinteractive"].includes(item.label?.toLowerCase())) { + this.loader.getProcedureStatementHelp( + "PYTHON", + item.label.toUpperCase(), + (data) => { + if (data && data.data) { + item.documentation = { + kind: MarkupKind.Markdown, + value: this._addLinkContext(515, data), + }; + } + resolve(item); + }, + ); + } else { + this._loadHelp({ + keyword: item.label, + type: "tooltip", + ...this.popupContext, + cb: (data) => { + if (data && data.data) { + item.documentation = { + kind: MarkupKind.Markdown, + value: this._addLinkContext(this.popupContext.zone, data), + }; + } + resolve(item); + }, + }); + } }); } diff --git a/server/src/sas/LanguageServiceProvider.ts b/server/src/sas/LanguageServiceProvider.ts index 70e936923..2c90c16f9 100644 --- a/server/src/sas/LanguageServiceProvider.ts +++ b/server/src/sas/LanguageServiceProvider.ts @@ -1,8 +1,11 @@ // Copyright © 2022-2023, SAS Institute Inc., Cary, NC, USA. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 -import { FoldingRange } from "vscode-languageserver"; +import { + DocumentSymbol, + FoldingRange, + SymbolKind, +} from "vscode-languageserver"; import { Range, TextDocument } from "vscode-languageserver-textdocument"; -import { DocumentSymbol, SymbolKind } from "vscode-languageserver-types"; import { CodeZoneManager } from "./CodeZoneManager"; import { CompletionProvider } from "./CompletionProvider"; @@ -52,8 +55,8 @@ const SymbolKinds = [ ]; export class LanguageServiceProvider { - private model; - private syntaxProvider; + public model; + public syntaxProvider; public completionProvider; public formatOnTypeProvider; public formatter; diff --git a/server/src/sas/Lexer.ts b/server/src/sas/Lexer.ts index f78490cb2..12c2e1d3c 100644 --- a/server/src/sas/Lexer.ts +++ b/server/src/sas/Lexer.ts @@ -304,7 +304,7 @@ export class Lexer { } if ( token.type === "text" && - ["SUBMIT", "INTERACTIVE"].includes(token.text) + ["SUBMIT", "INTERACTIVE", "I"].includes(token.text) ) { this.context.embeddedLangState = EmbeddedLangState.PROC_PYTHON_SUBMIT_OR_INTERACTIVE; @@ -318,7 +318,7 @@ export class Lexer { } if ( token.type === "text" && - ["SUBMIT", "INTERACTIVE"].includes(token.text) + ["SUBMIT", "INTERACTIVE", "I"].includes(token.text) ) { this.context.embeddedLangState = EmbeddedLangState.PROC_LUA_SUBMIT_OR_INTERACTIVE; @@ -358,7 +358,7 @@ export class Lexer { const stringReg = /'''|"""|("[^"]*?("|$))|('[^']*?('|$))/; const commentReg = /#.*$/; const secReg = - /(\b((endsubmit|endinteractive)(\s+|\/\*.*?\*\/)*;|(data|proc|%macro)\b[^'";]*;)(\s+|\/\*.*?\*\/)*$)/; + /(\b((endsubmit|endinteractive)(\s+|\/\*.*?\*\/)*;|(data|proc|%macro)\b[^'";]*;))/; match = new RegExp( `${stringReg.source}|${commentReg.source}|${secReg.source}`, "m", @@ -376,7 +376,7 @@ export class Lexer { } else { token = this._foundEmbeddedCodeToken(this.curr, { line: line, - column: match.index, + column: pos + match.index, }); break SWITCH; } @@ -429,7 +429,7 @@ export class Lexer { const stringReg = /("[^"]*("|$))|('[^']*('|$))|\[\[/; const commentReg = /--[^[].*$|--\[\[|\/\*/; const secReg = - /(\b((endsubmit|endinteractive)(\s+|\/\*.*?\*\/)*;|(data|proc|%macro)\b[^'";]*;)(\s+|\/\*.*?\*\/)*$)/; + /(\b((endsubmit|endinteractive)(\s+|\/\*.*?\*\/)*;|(data|proc|%macro)\b[^'";]*;))/; const reg = new RegExp( `${stringReg.source}|${commentReg.source}|${secReg.source}`, "m", @@ -452,7 +452,7 @@ export class Lexer { } else { token = this._foundEmbeddedCodeToken(this.curr, { line: line, - column: match.index, + column: pos + match.index, }); break SWITCH; } @@ -987,6 +987,7 @@ export class Lexer { this.curr.line = line; this.curr.column = col; this.context.lastNoncommentToken = null; + this.context.embeddedLangState = EmbeddedLangState.NONE; this.quoting = -1; this.bquoting = -1; this.ignoreFormat = false; diff --git a/server/src/sas/LexerEx.ts b/server/src/sas/LexerEx.ts index c330b4f23..425cc1bb6 100644 --- a/server/src/sas/LexerEx.ts +++ b/server/src/sas/LexerEx.ts @@ -6,7 +6,12 @@ import { Lexer, Token } from "./Lexer"; import { Model } from "./Model"; import { SyntaxDataProvider } from "./SyntaxDataProvider"; import { Change } from "./SyntaxProvider"; -import { TextPosition, arrayToMap } from "./utils"; +import { + TextPosition, + arrayToMap, + isCustomRegionEndComment, + isCustomRegionStartComment, +} from "./utils"; /** * LexerEx to handle basic semantic related problems. @@ -1700,14 +1705,14 @@ export class LexerEx { } private isCustomBlockStart_(token: Token): boolean { - if (token && /^\s*[%/]?\*\s*region\b/i.test(token.text)) { + if (token && isCustomRegionStartComment(token.text)) { return true; } return false; } private isCustomBlockEnd_(token: Token): boolean { - if (token && /^\s*[%/]?\*\s*endregion\b/i.test(token.text)) { + if (token && isCustomRegionEndComment(token.text)) { return true; } return false; @@ -3095,7 +3100,7 @@ export class LexerEx { generalProcStmt = false; } } else if (procName === "LUA" || procName === "PYTHON") { - if (["SUBMIT", "INTERACTIVE"].includes(word)) { + if (["SUBMIT", "INTERACTIVE", "I"].includes(word)) { const next = this.prefetch_({ pos: 1 }); if (next && next.text === ";" && next.type === "sep") { this.stack.push({ diff --git a/server/src/sas/utils.ts b/server/src/sas/utils.ts index b99fc40d2..c1477aada 100644 --- a/server/src/sas/utils.ts +++ b/server/src/sas/utils.ts @@ -103,3 +103,11 @@ export function getText(key: string, arg?: string): string { } return result; } + +export const isCustomRegionStartComment = (commmentText?: string) => { + return /^\s*[%/]?\*\s*region\b/i.test(commmentText ?? ""); +}; + +export const isCustomRegionEndComment = (commmentText?: string) => { + return /^\s*[%/]?\*\s*endregion\b/i.test(commmentText ?? ""); +}; diff --git a/server/src/server.ts b/server/src/server.ts index 1db45f264..d98b31b05 100644 --- a/server/src/server.ts +++ b/server/src/server.ts @@ -1,30 +1,65 @@ -// Copyright © 2022-2023, SAS Institute Inc., Cary, NC, USA. All Rights Reserved. +// Copyright © 2022-2024, SAS Institute Inc., Cary, NC, USA. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 import { + CancellationToken, + CodeActionKind, + CompletionItemKind, + CompletionTriggerKind, Connection, + DidChangeConfigurationParams, + DidChangeWatchedFilesParams, + DocumentHighlightParams, + DocumentSymbol, + ExecuteCommandParams, InitializeResult, + Location, + Position, + PrepareRenameParams, + Range, + ReferenceParams, + Registration, + RegistrationRequest, + RenameParams, + ResultProgressReporter, SemanticTokensRequest, + TextDocumentPositionParams, TextDocumentSyncKind, - TextDocuments, + Unregistration, + UnregistrationRequest, + WorkDoneProgressReporter, + WorkspaceSymbolParams, } from "vscode-languageserver"; import { TextDocument } from "vscode-languageserver-textdocument"; -import { CompletionProvider } from "./sas/CompletionProvider"; +import { ReadOnlyFileSystem } from "pyright-internal-node/dist/packages/pyright-internal/src/common/fileSystem"; +import { DocumentRange } from "pyright-internal-node/dist/packages/pyright-internal/src/common/textRange"; +import { Uri } from "pyright-internal-node/dist/packages/pyright-internal/src/common/uri/uri"; +import { CollectionResult } from "pyright-internal-node/dist/packages/pyright-internal/src/languageService/documentSymbolCollector"; +import { ParseFileResults } from "pyright-internal-node/dist/packages/pyright-internal/src/parser/parser"; + +import { PyrightLanguageProvider } from "./python/PyrightLanguageProvider"; +import { CodeZoneManager } from "./sas/CodeZoneManager"; import { LanguageServiceProvider, legend } from "./sas/LanguageServiceProvider"; import type { LibCompleteItem } from "./sas/SyntaxDataProvider"; +import { isCustomRegionStartComment } from "./sas/utils"; + +interface DocumentInfo { + document: TextDocument; + changed: boolean; + service?: LanguageServiceProvider; +} -const servicePool: Record = {}; -const documentPool: Record = {}; +export const runServer = ( + connection: Connection, + _pyrightLanguageProvider: PyrightLanguageProvider, +) => { + const documentPool: Record = {}; -let completionProvider: CompletionProvider; -let connection: Connection; -let supportSASGetLibList = false; + let supportSASGetLibList = false; + let registeredAdvancedCapabilities = false; -// Create a simple text document manager. -const documents: TextDocuments = new TextDocuments(TextDocument); + _pyrightLanguageProvider.setSasLspProvider(getLanguageService); -export const init = (conn: Connection): void => { - connection = conn; connection.onInitialize((params) => { if ( params.initializationOptions && @@ -32,6 +67,8 @@ export const init = (conn: Connection): void => { ) { supportSASGetLibList = true; } + _pyrightLanguageProvider.initialize(params, [], []); + const result: InitializeResult = { capabilities: { textDocumentSync: TextDocumentSyncKind.Incremental, @@ -39,96 +76,263 @@ export const init = (conn: Connection): void => { legend, full: true, }, - documentSymbolProvider: true, documentFormattingProvider: true, foldingRangeProvider: true, - hoverProvider: true, - completionProvider: { - triggerCharacters: [" "], - resolveProvider: true, - }, documentOnTypeFormattingProvider: { firstTriggerCharacter: "\n", moreTriggerCharacter: [";"], }, + documentSymbolProvider: { workDoneProgress: true }, + workspaceSymbolProvider: { workDoneProgress: true }, + hoverProvider: { workDoneProgress: true }, + completionProvider: { + triggerCharacters: _pyrightLanguageProvider.getClientCapabilities() + .hasVisualStudioExtensionsCapability + ? [".", "[", "@", '"', "'", " "] + : [".", "[", '"', "'", " "], + resolveProvider: true, + workDoneProgress: true, + completionItem: { + labelDetailsSupport: true, + }, + }, signatureHelpProvider: { - triggerCharacters: ["(", ","], + triggerCharacters: ["(", ",", ")"], + workDoneProgress: true, + }, + workspace: { + workspaceFolders: { + supported: true, + changeNotifications: true, + }, + }, + codeActionProvider: { + codeActionKinds: [ + CodeActionKind.QuickFix, + CodeActionKind.SourceOrganizeImports, + ], }, }, }; return result; }); + connection.onInitialized(() => _pyrightLanguageProvider.onInitialized()); + connection.onRequest(SemanticTokensRequest.type, (params) => { + syncIfDocChange(params.textDocument.uri); const languageService = getLanguageService(params.textDocument.uri); return { data: languageService.getTokens() }; }); - connection.onHover((params) => { - const languageService = getLanguageService(params.textDocument.uri); - - return languageService.completionProvider.getHelp(params.position); + connection.onHover(async (params, token) => { + return await dispatch(params, { + async sas(languageService) { + return await languageService.completionProvider.getHelp( + params.position, + ); + }, + async python(pyrightLanguageService) { + return await pyrightLanguageService.onHover(params, token); + }, + }); }); - connection.onCompletion((params) => { - const languageService = getLanguageService(params.textDocument.uri); - completionProvider = languageService.completionProvider; - - return completionProvider.getCompleteItems(params.position); + connection.onCompletion(async (params, token) => { + return await dispatch(params, { + async sas(languageService) { + if ( + params.context?.triggerCharacter && + params.context.triggerCharacter !== " " + ) { + return undefined; + } + const complitionList = + await languageService.completionProvider.getCompleteItems( + params.position, + ); + if (complitionList) { + for (const item of complitionList.items) { + if (!item.data) { + item.data = {}; + } + item.data._languageService = "sas"; + item.data._uri = params.textDocument.uri; + } + } + return complitionList; + }, + async python(pyrightLanguageService) { + const complitionList = await pyrightLanguageService.onCompletion( + params, + token, + ); + if (complitionList) { + if ( + params.context?.triggerKind === CompletionTriggerKind.Invoked || + params.context?.triggerKind === + CompletionTriggerKind.TriggerForIncompleteCompletions + ) { + if ( + complitionList.items.findIndex( + (item) => item.label === "endsubmit", + ) === -1 + ) { + const endsubmitItem = { + insertText: undefined, + kind: CompletionItemKind.Keyword, + label: "endsubmit", + }; + complitionList.items.push(endsubmitItem); + } + if ( + complitionList.items.findIndex( + (item) => item.label === "endinteractive", + ) === -1 + ) { + const endinteractiveItem = { + insertText: undefined, + kind: CompletionItemKind.Keyword, + label: "endinteractive", + }; + complitionList.items.push(endinteractiveItem); + } + } + for (const item of complitionList.items) { + if (!item.data) { + item.data = {}; + } + item.data._languageService = "python"; + item.data._uri = params.textDocument.uri; + } + } + return complitionList; + }, + }); }); - connection.onCompletionResolve((params) => { - return completionProvider.getCompleteItemHelp(params); + connection.onCompletionResolve(async (completionItem, token) => { + const lang = completionItem.data._languageService; + const label = completionItem.label; + const kind = completionItem.kind; + if ( + lang === "sas" || + (lang === "python" && + kind === CompletionItemKind.Keyword && + ["endsubmit", "endinteractive"].includes(label?.toLowerCase())) + ) { + const languageService = getLanguageService(completionItem.data._uri); + return await languageService.completionProvider.getCompleteItemHelp( + completionItem, + ); + } else if (lang === "python") { + return await _pyrightLanguageProvider.onCompletionResolve( + completionItem, + token, + ); + } else { + return completionItem; + } }); - connection.onDocumentSymbol((params) => { + connection.onDocumentSymbol(async (params, token) => { + syncIfDocChange(params.textDocument.uri); const languageService = getLanguageService(params.textDocument.uri); - return languageService.getDocumentSymbols(); + const sasSymbols = languageService.getDocumentSymbols(); + const pythonSymbols = + (await _pyrightLanguageProvider.onDocumentSymbol(params, token)) ?? []; + let hasPythonCode = false; + const symbolList = [...sasSymbols]; + for (let i = 0; i < symbolList.length; i++) { + const curSymbol = symbolList[i]; + if (isCustomRegionStartComment(curSymbol.name)) { + symbolList.splice(i + 1, 0, ...(curSymbol.children ?? [])); + } else if (curSymbol.name?.toUpperCase() === "PROC PYTHON") { + hasPythonCode = true; + for (const pythonSymbol of pythonSymbols) { + if (!("range" in pythonSymbol)) { + continue; + } + if (isRangeIncluded(curSymbol.range, pythonSymbol.range)) { + curSymbol.children?.push(pythonSymbol); + } + } + } + } + if (registeredAdvancedCapabilities) { + if (!hasPythonCode) { + unregisterAdvancedCapabilities(connection); + } + } else { + if (hasPythonCode) { + registerAdvancedCapabilities(connection); + } + } + return sasSymbols; }); + // todo connection.onFoldingRanges((params) => { + syncIfDocChange(params.textDocument.uri); const languageService = getLanguageService(params.textDocument.uri); return languageService.getFoldingRanges(); }); - connection.onRequest("sas/getFoldingBlock", (params) => { - const languageService = getLanguageService(params.textDocument.uri); - const block = languageService.getFoldingBlock( - params.line, - params.col, - params.strict ?? true, - params.ignoreCustomBlock, - params.ignoreGlobalBlock, - ); - if (!block) { - return undefined; - } else { - return { ...block, outerBlock: undefined, innerBlocks: undefined }; - } + connection.onRequest("sas/getFoldingBlock", async (params) => { + return await dispatch(params, { + async default(languageServices) { + const block = languageServices.sasLanguageService.getFoldingBlock( + params.position.line, + params.position.col, + params.strict ?? true, + params.ignoreCustomBlock, + params.ignoreGlobalBlock, + ); + if (!block) { + return undefined; + } else { + return { ...block, outerBlock: undefined, innerBlocks: undefined }; + } + }, + }); }); - connection.onDocumentOnTypeFormatting((params) => { - const languageService = getLanguageService(params.textDocument.uri); - return languageService.formatOnTypeProvider.getIndentEdit( - params.position.line, - params.position.character, - params.ch, - params.options.tabSize, - params.options.insertSpaces, - ); + connection.onDocumentOnTypeFormatting(async (params) => { + return await dispatch(params, { + async sas(languageService) { + return languageService.formatOnTypeProvider.getIndentEdit( + params.position.line, + params.position.character, + params.ch, + params.options.tabSize, + params.options.insertSpaces, + ); + }, + }); }); - connection.onSignatureHelp((params) => { - const languageService = getLanguageService(params.textDocument.uri); - completionProvider = languageService.completionProvider; - return completionProvider.getSignatureHelp( - params.position, - params.context?.activeSignatureHelp?.activeSignature, - ); + connection.onSignatureHelp(async (params, token) => { + return await dispatch(params, { + async sas(languageService) { + return await languageService.completionProvider.getSignatureHelp( + params.position, + params.context?.activeSignatureHelp?.activeSignature, + ); + }, + async python(pyrightLanguageService) { + // eslint-disable-next-line @typescript-eslint/consistent-type-assertions, @typescript-eslint/no-explicit-any + return (await pyrightLanguageService.onSignatureHelp( + params, + token, + // eslint-disable-next-line @typescript-eslint/no-explicit-any + )) as any; + }, + }); }); connection.onDocumentFormatting((params) => { + syncIfDocChange(params.textDocument.uri); const languageService = getLanguageService(params.textDocument.uri); return languageService.formatter.format({ tabWidth: params.options.tabSize, @@ -136,37 +340,380 @@ export const init = (conn: Connection): void => { }); }); - documents.onDidChangeContent((event) => { - if (servicePool[event.document.uri]) { - documentPool[event.document.uri] = event.document; - return; - } - servicePool[event.document.uri] = new LanguageServiceProvider( - event.document, + connection.onDidOpenTextDocument(async (params) => { + const doc = TextDocument.create( + params.textDocument.uri, + "sas", + params.textDocument.version, + params.textDocument.text, + ); + documentPool[doc.uri] = { document: doc, changed: false }; + await _pyrightLanguageProvider.onDidOpenTextDocument(params); + }); + + connection.onDidCloseTextDocument(async (params) => { + const uri = params.textDocument.uri; + delete documentPool[uri]; + await _pyrightLanguageProvider.onDidCloseTextDocument(params); + }); + + connection.onDidChangeTextDocument((params) => { + const uri = params.textDocument.uri; + const docInfo = documentPool[uri]; + TextDocument.update( + docInfo.document, + params.contentChanges, + params.textDocument.version, + ); + docInfo.changed = true; + docInfo.service = undefined; + }); + + connection.onDidChangeConfiguration( + async (params: DidChangeConfigurationParams) => { + return await _pyrightLanguageProvider.onDidChangeConfiguration(params); + }, + ); + + connection.onDefinition( + async (params: TextDocumentPositionParams, token: CancellationToken) => { + return await dispatch(params, { + async python(pyrightLanguageService) { + return await pyrightLanguageService.onDefinition(params, token); + }, + }); + }, + ); + + connection.onDeclaration( + async (params: TextDocumentPositionParams, token: CancellationToken) => { + return await dispatch(params, { + async python(pyrightLanguageService) { + return await pyrightLanguageService.onDeclaration(params, token); + }, + }); + }, + ); + + connection.onTypeDefinition( + async (params: TextDocumentPositionParams, token: CancellationToken) => { + return await dispatch(params, { + async python(pyrightLanguageService) { + return await pyrightLanguageService.onTypeDefinition(params, token); + }, + }); + }, + ); + + connection.onReferences( + async ( + params: ReferenceParams, + token: CancellationToken, + workDoneReporter: WorkDoneProgressReporter, + resultReporter: ResultProgressReporter | undefined, + createDocumentRange?: ( + uri: Uri, + result: CollectionResult, + parseResults: ParseFileResults, + ) => DocumentRange, + convertToLocation?: ( + fs: ReadOnlyFileSystem, + ranges: DocumentRange, + ) => Location | undefined, + ) => { + return await dispatch(params, { + async python(pyrightLanguageService) { + return await pyrightLanguageService.onReferences( + params, + token, + workDoneReporter, + resultReporter, + createDocumentRange, + convertToLocation, + ); + }, + }); + }, + ); + + connection.onWorkspaceSymbol( + async ( + params: WorkspaceSymbolParams, + token: CancellationToken, + workDoneProgress, + resultProgress, + ) => { + syncAllChangedDoc(); + return await _pyrightLanguageProvider.onWorkspaceSymbol( + params, + token, + resultProgress, + ); + }, + ); + + connection.onDocumentHighlight( + async (params: DocumentHighlightParams, token: CancellationToken) => { + return await dispatch(params, { + async python(pyrightLanguageService) { + return await pyrightLanguageService.onDocumentHighlight( + params, + token, + ); + }, + }); + }, + ); + + connection.onPrepareRename( + async (params: PrepareRenameParams, token: CancellationToken) => { + return await dispatch(params, { + async python(pyrightLanguageService) { + return await pyrightLanguageService.onPrepareRenameRequest( + params, + token, + ); + }, + }); + }, + ); + + connection.onRenameRequest( + async (params: RenameParams, token: CancellationToken) => { + return await dispatch(params, { + async python(pyrightLanguageService) { + // eslint-disable-next-line @typescript-eslint/consistent-type-assertions + return (await pyrightLanguageService.onRenameRequest( + params, + token, + // eslint-disable-next-line @typescript-eslint/no-explicit-any + )) as any; + }, + }); + }, + ); + + connection.onDidChangeWatchedFiles( + async (params: DidChangeWatchedFilesParams) => { + params.changes.forEach((item) => { + if (item.uri in documentPool) { + syncIfDocChange(item.uri); + } + }); + _pyrightLanguageProvider.onDidChangeWatchedFiles(params); + }, + ); + + connection.onExecuteCommand( + async ( + params: ExecuteCommandParams, + token: CancellationToken, + reporter: WorkDoneProgressReporter, + ) => { + syncAllChangedDoc(); + return await _pyrightLanguageProvider.onExecuteCommand( + params, + token, + reporter, + ); + }, + ); + + const callHierarchy = connection.languages.callHierarchy; + + callHierarchy.onPrepare(async (params, token) => { + return ( + (await dispatch(params, { + async python(pyrightLanguageService) { + return await pyrightLanguageService.onCallHierarchyPrepare( + params, + token, + ); + }, + })) ?? [] + ); + }); + + callHierarchy.onIncomingCalls(async (params, token) => { + return await _pyrightLanguageProvider.onCallHierarchyIncomingCalls( + params, + token, ); }); - // Make the text document manager listen on the connection - // for open, change and close text document events - documents.listen(connection); + callHierarchy.onOutgoingCalls(async (params, token) => { + return await _pyrightLanguageProvider.onCallHierarchyOutgoingCalls( + params, + token, + ); + }); + + connection.onCodeAction(async (params, token) => { + return await dispatch( + { textDocument: params.textDocument, position: params.range.start }, + { + async python(pyrightLanguageService) { + return await pyrightLanguageService.executeCodeAction(params, token); + }, + }, + ); + }); + + connection.onShutdown(async (token) => { + await _pyrightLanguageProvider.onShutdown(token); + }); // Listen on the connection connection.listen(); -}; -function getLanguageService(uri: string) { - if (documentPool[uri]) { + function getLanguageService(uri: string) { + const docInfo = documentPool[uri]; // re-create LanguageServer if document changed - servicePool[uri] = new LanguageServiceProvider(documentPool[uri]); + if (!docInfo.service) { + docInfo.service = new LanguageServiceProvider(docInfo.document); - if (supportSASGetLibList) { - servicePool[uri].setLibService((libId, resolve) => - connection - .sendRequest("sas/getLibList", { libId: libId }) - .then(resolve), - ); + if (supportSASGetLibList) { + docInfo.service.setLibService((libId, resolve) => + connection + .sendRequest("sas/getLibList", { libId: libId }) + .then(resolve), + ); + } } - delete documentPool[uri]; + return docInfo.service!; } - return servicePool[uri]; -} + + const dispatch = async ( + params: { textDocument: { uri: string }; position: Position }, + callbacks: { + sas?: (languageService: LanguageServiceProvider) => Promise; + python?: ( + pyrightLanguageService: PyrightLanguageProvider, + ) => Promise; + default?: (languageServices: { + sasLanguageService: LanguageServiceProvider; + pythonLanguageService?: PyrightLanguageProvider; + }) => Promise; + }, + ) => { + syncIfDocChange(params.textDocument.uri); + const languageService = getLanguageService(params.textDocument.uri); + const codeZoneManager = languageService.getCodeZoneManager(); + const pos = params.position; + const symbols: DocumentSymbol[] = languageService.getDocumentSymbols(); + for (let i = 0; i < symbols.length; i++) { + const symbol = symbols[i]; + if (isCustomRegionStartComment(symbol.name)) { + symbols.splice(i + 1, 0, ...(symbol.children ?? [])); + continue; + } + const start = symbol.range.start; + const end = symbol.range.end; + if ( + (start.line < pos.line || + (start.line === pos.line && start.character <= pos.character)) && + (end.line > pos.line || + (end.line === pos.line && end.character >= pos.character)) + ) { + if ( + symbol.name?.toUpperCase() === "PROC PYTHON" && + codeZoneManager.getCurrentZone(pos.line, pos.character) === + CodeZoneManager.ZONE_TYPE.EMBEDDED_LANG + ) { + if (callbacks.python) { + return await callbacks.python(_pyrightLanguageProvider); + } else if (callbacks.default) { + return await callbacks.default({ + sasLanguageService: languageService, + pythonLanguageService: _pyrightLanguageProvider, + }); + } else { + return undefined; + } + } + } + } + if (callbacks.sas) { + return await callbacks.sas(languageService); + } else if (callbacks.default) { + return await callbacks.default({ + sasLanguageService: languageService, + pythonLanguageService: _pyrightLanguageProvider, + }); + } + { + return undefined; + } + }; + + const isRangeIncluded = (a: Range, b: Range) => { + if ( + (b.start.line > a.start.line || + (b.start.line === a.start.line && + b.start.character >= a.start.character)) && + (b.end.line < a.end.line || + (b.end.line === a.end.line && b.end.character <= a.end.character)) + ) { + return true; + } + return false; + }; + + const syncIfDocChange = (uri: string) => { + const docInfo = documentPool[uri]; + if (!docInfo.changed) { + return; + } + docInfo.changed = false; + _pyrightLanguageProvider.addContentChange(docInfo.document); + }; + + const syncAllChangedDoc = () => { + for (const uri in documentPool) { + syncIfDocChange(uri); + } + }; + + const advancedCapabilities = [ + "textDocument/declaration", + "textDocument/definition", + "textDocument/typeDefinition", + "textDocument/references", + "textDocument/rename", + "textDocument/documentHighlight", + "textDocument/prepareCallHierarchy", + ]; + + const registerAdvancedCapabilities = async (conn: Connection) => { + if (registeredAdvancedCapabilities) { + return; + } + registeredAdvancedCapabilities = true; + const registerOptions = { + documentSelector: [{ language: "sas" }], + }; + const registrations: Registration[] = []; + for (const capability of advancedCapabilities) { + registrations.push({ + id: capability, + method: capability, + registerOptions, + }); + } + await conn.sendRequest(RegistrationRequest.type, { registrations }); + }; + + const unregisterAdvancedCapabilities = async (conn: Connection) => { + if (!registeredAdvancedCapabilities) { + return; + } + registeredAdvancedCapabilities = false; + const unregisterations: Unregistration[] = []; + for (const capability of advancedCapabilities) { + unregisterations.push({ id: capability, method: capability }); + } + await conn.sendRequest(UnregistrationRequest.type, { + unregisterations: unregisterations, + }); + }; +}; diff --git a/syntaxes/sas.tmLanguage.json b/syntaxes/sas.tmLanguage.json index b18039af3..834732e1b 100644 --- a/syntaxes/sas.tmLanguage.json +++ b/syntaxes/sas.tmLanguage.json @@ -35,7 +35,7 @@ "include": "#strings-or-comments" }, { - "begin": "(?i)(?<=submit|interactive)(\\s|/\\*.*?\\*/)*;", + "begin": "(?i)(?<=submit|interactive|i)(\\s|/\\*.*?\\*/)*;", "end": "(?i)(endsubmit|endinteractive)(\\s|/\\*.*?\\*/)*;", "name": "source.lua", "beginCaptures": { @@ -60,7 +60,7 @@ "include": "#strings-or-comments" }, { - "begin": "(?i)(?<=submit|interactive)(\\s|/\\*.*?\\*/)*;", + "begin": "(?i)(?<=submit|interactive|i)(\\s|/\\*.*?\\*/)*;", "end": "(?i)(endsubmit|endinteractive)(\\s|/\\*.*?\\*/)*;", "name": "source.python", "beginCaptures": { diff --git a/tools/build.mjs b/tools/build.mjs index ae9b34f0b..8bba61939 100644 --- a/tools/build.mjs +++ b/tools/build.mjs @@ -1,5 +1,7 @@ import concurrently from "concurrently"; import esbuild from "esbuild"; +import fs from "fs"; +import path from "path"; console.log("start"); const dev = process.argv[2]; @@ -79,7 +81,22 @@ if (process.env.npm_config_webviews || process.env.npm_config_client) { ]); await result.then( - () => {}, + () => { + const foldersToCopy = [ + { + src: "./server/node_modules/jsonc-parser/lib/umd/impl", + dest: "./server/dist/node/impl", + }, + { + src: "./server/node_modules/pyright-internal-node/dist/packages/pyright-internal/typeshed-fallback", + dest: "./server/dist/node/typeshed-fallback", + }, + ]; + for (const item of foldersToCopy) { + fs.cpSync(item.src, item.dest, { recursive: true }); + console.log(`${item.src} was copied to ${item.dest}`); + } + }, () => console.error("Assets failed to build successfully"), ); } diff --git a/webpack.config.js b/webpack.config.js index c5a5f505e..4d8e995d7 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -8,6 +8,7 @@ /** @typedef {import('webpack').Configuration} WebpackConfig **/ const path = require("path"); +const { ProvidePlugin, DefinePlugin } = require("webpack"); /** @type WebpackConfig */ const browserClientConfig = { @@ -71,8 +72,22 @@ const browserServerConfig = { extensions: [".ts", ".js"], // support ts-files and js-files alias: { "../node": path.resolve(__dirname, "server/src/browser"), + "./common/realFileSystem": path.resolve( + __dirname, + "server/src/python/browser/fakeFileSystem", + ), + }, + fallback: { + path: require.resolve("path-browserify"), + os: false, + crypto: false, + // buffer: require.resolve("buffer/"), + stream: false, + child_process: false, + fs: false, + assert: false, + util: false, }, - fallback: {}, }, module: { rules: [ @@ -85,6 +100,17 @@ const browserServerConfig = { }, ], }, + { + test: /python[\\|/]browser[\\|/]typeShed\.ts$/, + use: [ + { + loader: path.resolve( + __dirname, + "server/src/python/browser/typeshed-loader", + ), + }, + ], + }, { test: /\.properties$/, exclude: /node_modules/, @@ -94,11 +120,20 @@ const browserServerConfig = { }, externals: { vscode: "commonjs vscode", // ignored because it doesn't exist + fsevents: "commonjs2 fsevents", }, performance: { hints: false, }, devtool: "source-map", + plugins: [ + new DefinePlugin({ + process: "{ env: {}, execArgv: [], cwd: () => '/' }", + }), + new ProvidePlugin({ + Buffer: ["buffer", "Buffer"], + }), + ], }; module.exports = [browserClientConfig, browserServerConfig];