Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions internal/format/comment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,105 @@ func TestCommentFormatting(t *testing.T) {
})
}

func TestFormatSelectionPreservesComments(t *testing.T) {
t.Parallel()

t.Run("format selection should not delete block comment when selection ends inside comment", func(t *testing.T) {
t.Parallel()
ctx := format.WithFormatCodeSettings(t.Context(), &lsutil.FormatCodeSettings{
EditorSettings: lsutil.EditorSettings{
TabSize: 4,
IndentSize: 4,
BaseIndentSize: 0,
NewLineCharacter: "\n",
ConvertTabsToSpaces: true,
IndentStyle: lsutil.IndentStyleSmart,
TrimTrailingWhitespace: true,
},
}, "\n")

// Reproduce: const test/* comment */=5;
// When selecting a range that ends inside the comment (before */), format selection should not delete the comment.
originalText := `const test/* comment */=5;`

sourceFile := parser.ParseSourceFile(ast.SourceFileParseOptions{
FileName: "/test.ts",
Path: "/test.ts",
}, originalText, core.ScriptKindTS)

// Select a range that starts at the beginning of the line and ends inside the block comment.
// This covers `const test/* comment`, stopping before the closing `*/`.
commentStart := strings.Index(originalText, "/*")
selectionEnd := commentStart + len("/* comment") // ends inside the comment, before the closing `*/`

edits := format.FormatSelection(ctx, sourceFile, 0, selectionEnd)
formatted := applyBulkEdits(originalText, edits)

// The entire statement should be preserved unchanged
assert.Equal(t, formatted, originalText, "format selection should not delete the block comment or alter the statement")
})
Comment on lines +296 to +301
Copy link

Copilot AI Apr 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The format-selection regression assertions are fairly loose (e.g., they only check that /* comment */ and sometimes test are present). This could still pass if other important parts of the statement (like =5;) are accidentally deleted or reordered. Consider strengthening the assertions to include the rest of the statement (or assert an exact expected result).

Copilot uses AI. Check for mistakes.

t.Run("format selection should not delete block comment when selection starts inside comment", func(t *testing.T) {
t.Parallel()
ctx := format.WithFormatCodeSettings(t.Context(), &lsutil.FormatCodeSettings{
EditorSettings: lsutil.EditorSettings{
TabSize: 4,
IndentSize: 4,
BaseIndentSize: 0,
NewLineCharacter: "\n",
ConvertTabsToSpaces: true,
IndentStyle: lsutil.IndentStyleSmart,
TrimTrailingWhitespace: true,
},
}, "\n")

originalText := `const test/* comment */=5;`

sourceFile := parser.ParseSourceFile(ast.SourceFileParseOptions{
FileName: "/test.ts",
Path: "/test.ts",
}, originalText, core.ScriptKindTS)

// Select from inside the comment to the end
commentStart := strings.Index(originalText, "/*")
selectionStart := commentStart + 3 // inside the comment

edits := format.FormatSelection(ctx, sourceFile, selectionStart, len(originalText))
formatted := applyBulkEdits(originalText, edits)

// The entire statement should be preserved unchanged
assert.Equal(t, formatted, originalText, "format selection should not delete the block comment or alter the statement")
})

t.Run("full document format should preserve block comment", func(t *testing.T) {
t.Parallel()
ctx := format.WithFormatCodeSettings(t.Context(), &lsutil.FormatCodeSettings{
EditorSettings: lsutil.EditorSettings{
TabSize: 4,
IndentSize: 4,
BaseIndentSize: 0,
NewLineCharacter: "\n",
ConvertTabsToSpaces: true,
IndentStyle: lsutil.IndentStyleSmart,
TrimTrailingWhitespace: true,
},
}, "\n")

originalText := `const test/* comment */=5;`

sourceFile := parser.ParseSourceFile(ast.SourceFileParseOptions{
FileName: "/test.ts",
Path: "/test.ts",
}, originalText, core.ScriptKindTS)

edits := format.FormatDocument(ctx, sourceFile)
formatted := applyBulkEdits(originalText, edits)

// Full document format should preserve the comment and the entire statement
assert.Equal(t, formatted, originalText, "full format should preserve the block comment and the statement")
})
}

func TestSliceBoundsPanic(t *testing.T) {
t.Parallel()

Expand Down
10 changes: 10 additions & 0 deletions internal/format/span.go
Original file line number Diff line number Diff line change
Expand Up @@ -1064,6 +1064,16 @@ func (w *formatSpanWorker) consumeTokenAndAdvanceScanner(currentTokenInfo tokenI

if len(currentTokenInfo.trailingTrivia) > 0 {
w.previousRangeTriviaEnd = core.LastOrNil(currentTokenInfo.trailingTrivia).Loc.End()
// If any trailing comment trivia extends past the original range, it won't be
// processed by processTrivia (which skips comments not contained by originalRange).
// Cap previousRangeTriviaEnd before such comments so the trailing edit contiguity
// check in execute() won't pair across unprocessed comment content.
for _, trivia := range currentTokenInfo.trailingTrivia {
if isComment(trivia.Kind) && !trivia.Loc.ContainedBy(w.originalRange) {
w.previousRangeTriviaEnd = trivia.Loc.Pos()
break
}
}
w.processTrivia(currentTokenInfo.trailingTrivia, parent, w.childContextNode, dynamicIndenation)
}

Expand Down
Loading