-
Notifications
You must be signed in to change notification settings - Fork 709
Organize imports for TypeScript and JavaScript #1770
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Add import sorting and grouping functionality: - Compare imports by module specifiers (absolute vs relative) - Sort by import kind (side-effect, type-only, namespace, default, named) - Group imports separated by blank lines - Binary search for efficient insertion point calculation
Test coverage includes: - Module specifier comparison (absolute vs relative, alphabetical) - Import kind ordering (side-effect, type-only, namespace, default, named) - Binary search insertion index calculation - Module specifier expression extraction - Mixed import type sorting Achieves 82%+ coverage for core sorting functions
Register workspace/executeCommand handler for typescript-go.organizeImports: - Declare command in ExecuteCommandProvider capabilities - Handle command execution and dispatch to language service - Apply text edits via workspace/applyEdit request
Integrate organize imports feature: - Add executeCommand method to LSP client - Register typescript.native-preview.sortImports command - Add Sort Imports to command palette and quick pick menu - Support for TypeScript and JavaScript files
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR implements a comprehensive "organize imports" feature for TypeScript and JavaScript files in the TypeScript-Go port. The feature sorts imports while preserving blank line groupings to respect user organization.
Key changes:
- Implements import comparison logic that sorts by module type (absolute vs relative), import kind (side-effect, type-only, namespace, default, named), and alphabetically within categories
- Adds LSP server support for the
typescript-go.organizeImports
command with workspace edit capabilities - Integrates the feature into the VS Code extension as a "Sort Imports" command
Reviewed Changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.
Show a summary per file
File | Description |
---|---|
internal/lsp/server.go |
Adds LSP command handler registration and workspace edit execution for organize imports |
internal/ls/organizeimports_test.go |
Comprehensive test suite covering import comparison logic and module specifier extraction |
internal/ls/organizeimports_service.go |
Core service implementation that groups imports by blank lines and sorts within groups |
internal/ls/organizeimports.go |
Import comparison algorithms and utility functions for sorting different import types |
_extension/src/commands.ts |
VS Code extension command registration and UI integration for sort imports |
_extension/src/client.ts |
LSP client method for executing server commands |
_extension/package.json |
VS Code extension manifest updates for the new sort imports command |
@microsoft-github-policy-service agree |
The loop condition was incorrectly scanning forward from pos to stmt.End(). Fixed to scan backwards from pos-1 to find preceding newlines before the statement, which correctly identifies blank lines in the leading trivia.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.
Comments suppressed due to low confidence (1)
internal/ls/organizeimports.go:1
- The old implementation contains a placeholder comment '!!!' and always returns len(sortedImports), which indicates incomplete functionality. This has been replaced with a proper binary search implementation.
package ls
Rewrite the comment to be more direct and specific about why parameters are reversed: we want absolute imports (isRelative=false) to come before relative imports (isRelative=true).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.
Comments suppressed due to low confidence (1)
internal/ls/organizeimports.go:1
- The old function implementation with commented placeholder code should be removed since it has been replaced by the new
GetImportDeclarationInsertIndex
function.
package ls
// Reverse parameter order because we want absolute imports (isRelative=false) before relative imports (isRelative=true) | ||
if comparison := compareBooleans(isRelative2, isRelative1); comparison != 0 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The parameter reversal in compareBooleans(isRelative2, isRelative1)
is confusing and error-prone. Consider using compareBooleans(isRelative1, isRelative2)
and negating the result, or creating a clearer helper function like compareAbsoluteBeforeRelative
.
Copilot uses AI. Check for mistakes.
- Replace fmt.Errorf with errors.New for non-formatted error (perfsprint) - Fix variable shadowing by reusing err instead of declaring new one (customlint)
Run dprint fmt to fix formatting issues: - Add trailing comma in TypeScript function arguments - Move catch keyword to new line - Remove extra blank lines in Go test file - Add newline at end of files
I don't think this is the right approach. The language server protocol already has organize imports. We should not require anything custom to make this work. Adding anything to the client implies we are going outside the LSP spec. |
In this PR:
Imports are sorted by:
The feature is exposed via LSP
workspace/executeCommand
with commandtypescript-go.organizeImports
and integrated into the VS Code extension as the "Sort Imports" command.Fixes #1724