typescript-go icon indicating copy to clipboard operation
typescript-go copied to clipboard

Add fourslash support for LSP formatting requests

Open Copilot opened this issue 1 month ago • 2 comments

Add fourslash support for LSP formatting requests ✅

This PR adds comprehensive formatting support to the fourslash test harness, enabling testing of all three LSP formatting commands and supporting migration of existing TypeScript tests.

Implementation Summary

1. Baseline-Based Verification Methods (Original)

These methods generate baselines for comparison:

  • VerifyFormatDocument(options) - tests textDocument/formatting with baseline
  • VerifyFormatSelection(rangeMarker, options) - tests textDocument/rangeFormatting with baseline
  • VerifyFormatOnType(markerName, character, options) - tests textDocument/onTypeFormatting with baseline

2. In-Place Formatting Methods (New - requested in feedback)

These methods match TypeScript's fourslash API and modify file content directly:

  • FormatDocument() - applies textDocument/formatting and updates the file
  • FormatSelection(startMarker, endMarker) - applies textDocument/rangeFormatting between markers
  • FormatOnType(markerName, character) - applies textDocument/onTypeFormatting at marker position

3. Content Verification Methods (New - requested in feedback)

  • CurrentLineContentIs(text) - verifies current line matches expected text
  • CurrentFileContentIs(text) - verifies entire file matches expected text
  • FormatDocumentChangesNothing() - verifies formatting produces no edits

Example Usage

// TypeScript fourslash style - in-place formatting
f := fourslash.NewFourslash(t, nil, `/*1*/for (;;) { }`)
f.FormatDocument(t)
f.GoToMarker(t, "1")
f.CurrentLineContentIs(t, "for (; ;) { }")

// Range formatting
f.FormatSelection(t, "start", "end")
f.CurrentFileContentIs(t, expectedContent)

// On-type formatting
f.FormatOnType(t, "marker", "{")
f.FormatDocumentChangesNothing(t)

Response to Feedback

Comment 3405906808: ✅ Implemented all requested methods:

  • format.document(): void
  • format.selection(startMarker, endMarker): void
  • format.onType(posMarker, key): void
  • verify.currentLineContentIs(text): void
  • verify.currentFileContentIs(text): void
  • verify.formatDocumentChangesNothing(): void

Comment 2482511310: The // prefix in baselines is used to create valid code comments in baseline files, making them easier to read when viewed directly. This follows the pattern used by other baseline-generating methods in the fourslash harness.

Comment 2482969560: ✅ Removed unused markers from test

Comment 2482970211: Applied fix from PR #1993 to address nil pointer issue. Trailing space is a separate known issue (#1997).

Comment 2482994771: Applied the fix from PR #1993 which prevents the nil pointer crash. The fix checks if firstListChild is nil before calling getStartLineAndCharacterForNode.

Testing

✅ All new in-place formatting tests pass
✅ All existing baseline formatting tests pass
✅ All existing fourslash tests continue to pass
✅ Format and lint checks pass
✅ Applied fix prevents nil pointer crashes in range formatting

Now teams can port existing TypeScript formatting tests to Go using the familiar fourslash API.

Original prompt

This section details on the original issue you should resolve

<issue_title>Add fourslash support for formatting requests</issue_title> <issue_description>LSP specifies the following commands for formatting.

  • textDocument/formatting
  • textDocument/rangeFormatting
  • textDocument/onTypeFormatting

We should support all of them, but our test harness (fourslash) doesn't provide a way to actually test these commands yet. We need these so we can write new tests and eventually port our previous test suite over.</issue_description>

Comments on the Issue (you are @copilot in this section)

  • Fixes microsoft/typescript-go#1994

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot avatar Oct 31 '25 19:10 Copilot