Skip to content

Conversation

saixiaoxi
Copy link

In this PR:

  • Port organize imports functionality for TypeScript and JavaScript
  • Preserve blank line groupings to respect user's logical organization
  • Support all import types: side-effect, type-only, namespace, default, named, and require

Imports are sorted by:

  1. Module type: absolute imports before relative imports
  2. Import kind: side-effect → type-only → namespace → default → named → require
  3. Alphabetically within each category

The feature is exposed via LSP workspace/executeCommand with command typescript-go.organizeImports and integrated into the VS Code extension as the "Sort Imports" command.

Fixes #1724

sutong.527608 added 4 commits September 30, 2025 16:52
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
@Copilot Copilot AI review requested due to automatic review settings September 30, 2025 09:10
Copy link
Contributor

@Copilot Copilot AI left a 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

@saixiaoxi
Copy link
Author

@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.
@saixiaoxi saixiaoxi requested a review from Copilot September 30, 2025 09:21
Copy link
Contributor

@Copilot Copilot AI left a 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).
@saixiaoxi saixiaoxi requested a review from Copilot September 30, 2025 09:23
Copy link
Contributor

@Copilot Copilot AI left a 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

Comment on lines +103 to +104
// Reverse parameter order because we want absolute imports (isRelative=false) before relative imports (isRelative=true)
if comparison := compareBooleans(isRelative2, isRelative1); comparison != 0 {
Copy link
Preview

Copilot AI Sep 30, 2025

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.

sutong.527608 added 2 commits September 30, 2025 17:30
- 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
@jakebailey
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Sort Imports functionality missing in vscode
2 participants