@clockblocker/ts7-plugin-sort-import-suggestions
v2.0.0
Published
Customize the priority of TypeScript auto-import suggestions, including TypeScript 7
Readme
@clockblocker/ts7-plugin-sort-import-suggestions
Customize the priority of TypeScript auto-import completions and missing-import quick fixes.
This fork supports both TypeScript generations:
- TypeScript 5.3–6.x uses the original
tsserverlanguage-service plugin. - TypeScript 7 uses a small LSP proxy because the native TypeScript language server does not load JavaScript
tsserverplugins.
The proxy only changes completion sortText values and the order of import code actions. Every other LSP message passes through unchanged to the project’s installed TypeScript 7 server.
| | Before | After | | ------------- | ----------- | ----- | | Auto complete | | | | Code fix | | |
TypeScript 7 setup
npm install -D typescript@^7 @clockblocker/ts7-plugin-sort-import-suggestionsIf TypeScript 7 is already installed:
npm install -D @clockblocker/ts7-plugin-sort-import-suggestionsKeep the sorting configuration in tsconfig.json:
{
"compilerOptions": {
"plugins": [
{
"name": "@clockblocker/ts7-plugin-sort-import-suggestions",
// Promote aliases and relative imports.
"moveUpPatterns": ["@/", "\\.{1,2}/"],
// Demote generated or build-output imports.
"moveDownPatterns": ["dist"]
}
]
}
}For VS Code, select the proxy as the workspace TypeScript 7 SDK in .vscode/settings.json:
{
"js/ts.experimental.useTsgo": true,
"js/ts.tsdk.path": "./node_modules/@clockblocker/ts7-plugin-sort-import-suggestions/bin"
}Reload VS Code and accept the prompt to use the workspace TypeScript version. The workspace must be trusted because this setting launches code from node_modules.
For another LSP-capable editor, configure its TypeScript language-server command as:
node_modules/@clockblocker/ts7-plugin-sort-import-suggestions/bin/tsgo --lsp --stdioThe proxy locates and launches the TypeScript 7 installation belonging to the current project.
TypeScript 5.3–6.x setup
Install the fork and configure the plugin:
npm install -D @clockblocker/ts7-plugin-sort-import-suggestions{
"compilerOptions": {
"plugins": [
{
"name": "@clockblocker/ts7-plugin-sort-import-suggestions",
"moveUpPatterns": ["@/", "\\.{1,2}/"],
"moveDownPatterns": ["dist"]
}
]
}
}Configure the editor to use TypeScript from the project’s node_modules. In VS Code, run TypeScript: Select TypeScript Version and choose Use Workspace Version.
Configuration
moveUpPatterns and moveDownPatterns are arrays of JavaScript regular-expression strings matched against the import module specifier. Invalid patterns are ignored and reported on stderr by the TS7 proxy.
Defaults:
{
"moveUpPatterns": ["@/", "\\.{1,2}/"],
"moveDownPatterns": []
}The nearest tsconfig.json or jsconfig.json is used for each document. Relative and package-based extends configurations are supported.
Development
Requirements: Node.js 18 or newer.
npm install
npm test
npm run typecheckThe test suite exercises the legacy TypeScript 6 plugin, the sorting functions, and a real TypeScript 7 native language-server process.
For background on TypeScript 7’s native language server and plugin transition, see Microsoft’s TypeScript 7 announcement and API extensibility roadmap.
