-
Notifications
You must be signed in to change notification settings - Fork 730
Add fourslash support for LSP formatting requests #1995
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
Draft
Copilot
wants to merge
6
commits into
main
Choose a base branch
from
copilot/add-fourslash-formatting-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
01bc829
Initial plan
Copilot 6befa0b
Add fourslash support for formatting requests
Copilot a77e23b
Fix API consistency in formatting verification methods
Copilot 8455858
Address code review feedback
Copilot a0c8f66
Add formatting methods that modify file content in-place
Copilot b05216e
Address PR feedback: remove unused markers and fix nil pointer crash
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package fourslash_test | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/microsoft/typescript-go/internal/fourslash" | ||
| "github.com/microsoft/typescript-go/internal/lsp/lsproto" | ||
| "github.com/microsoft/typescript-go/internal/testutil" | ||
| ) | ||
|
|
||
| func TestBasicFormatDocument(t *testing.T) { | ||
| t.Parallel() | ||
| defer testutil.RecoverAndFail(t, "Panic on fourslash test") | ||
| const content = `const x = 1 ; | ||
| function foo ( a , b ) { | ||
| return a + b ; | ||
| } | ||
| const y = foo( 2 , 3 ) ;` | ||
| f := fourslash.NewFourslash(t, nil /*capabilities*/, content) | ||
| f.VerifyFormatDocument(t, &lsproto.FormattingOptions{ | ||
| TabSize: 4, | ||
| InsertSpaces: true, | ||
| }) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| package fourslash_test | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/microsoft/typescript-go/internal/fourslash" | ||
| "github.com/microsoft/typescript-go/internal/lsp/lsproto" | ||
| "github.com/microsoft/typescript-go/internal/testutil" | ||
| ) | ||
|
|
||
| func TestBasicFormatOnType(t *testing.T) { | ||
| t.Parallel() | ||
| defer testutil.RecoverAndFail(t, "Panic on fourslash test") | ||
| const content = `function foo() {/*a*/ | ||
| const x=1; | ||
| }` | ||
| f := fourslash.NewFourslash(t, nil /*capabilities*/, content) | ||
| // Verify formatting after typing opening curly brace | ||
| f.VerifyFormatOnType(t, "a", "{", &lsproto.FormattingOptions{ | ||
| TabSize: 4, | ||
| InsertSpaces: true, | ||
| }) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package fourslash_test | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/microsoft/typescript-go/internal/fourslash" | ||
| "github.com/microsoft/typescript-go/internal/lsp/lsproto" | ||
| "github.com/microsoft/typescript-go/internal/testutil" | ||
| ) | ||
|
|
||
| func TestBasicFormatSelection(t *testing.T) { | ||
| t.Parallel() | ||
| defer testutil.RecoverAndFail(t, "Panic on fourslash test") | ||
| const content = `const x = 1; | ||
| [|function foo(a,b){return a+b;}|] | ||
| const y = 2;` | ||
| f := fourslash.NewFourslash(t, nil /*capabilities*/, content) | ||
| // Format only the function declaration | ||
| rangeToFormat := f.Ranges()[0] | ||
| f.VerifyFormatSelection(t, rangeToFormat, &lsproto.FormattingOptions{ | ||
| TabSize: 4, | ||
| InsertSpaces: true, | ||
| }) | ||
| } |
14 changes: 14 additions & 0 deletions
14
testdata/baselines/reference/fourslash/formatDocument/basicFormatDocument.baseline
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| // === formatDocument === | ||
| // Original (/basicFormatDocument.ts): | ||
| // const x = 1 ; | ||
| // function foo ( a , b ) { | ||
| // return a + b ; | ||
| // } | ||
| // const y = foo( 2 , 3 ) ; | ||
|
|
||
| // Formatted: | ||
| // const x = 1; | ||
| // function foo(a, b) { | ||
| // return a + b; | ||
| // } | ||
| // const y = foo(2, 3); | ||
10 changes: 10 additions & 0 deletions
10
testdata/baselines/reference/fourslash/formatOnType/basicFormatOnType.baseline
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| // === formatOnType === | ||
| // Original (/basicFormatOnType.ts): | ||
| // function foo() { | ||
| // const x=1; | ||
| // } | ||
|
|
||
| // Formatted: | ||
| // function foo() { | ||
| // const x=1; | ||
| // } |
10 changes: 10 additions & 0 deletions
10
testdata/baselines/reference/fourslash/formatSelection/basicFormatSelection.baseline
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| // === formatSelection === | ||
| // Original (/basicFormatSelection.ts): | ||
| // const x = 1; | ||
| // function foo(a,b){return a+b;} | ||
| // const y = 2; | ||
|
|
||
| // Formatted: | ||
| // const x = 1; | ||
| // function foo(a, b) { return a + b; } | ||
| // const y = 2; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Is there a reason why each line of the original and formatted context is prefixed with
//?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
//prefix makes baseline files valid code comments, making them easier to read when viewed directly. This follows the pattern used by other baseline-generating methods in fourslash (e.g.,getBaselineForLocationsWithFileContents). The approach keeps baselines human-readable while ensuring they're syntactically valid.