Skip to content

Port isInComment #1397

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

Merged
merged 7 commits into from
Jul 15, 2025
Merged

Port isInComment #1397

merged 7 commits into from
Jul 15, 2025

Conversation

gabritto
Copy link
Member

Port isInComment/getRangeOfEnclosingComment.
This fixes some completion tests, but some tests now fail because JSDoc completions are unimplemented, and we always return nil for completions in a comment position.

// `precedingToken` and `tokenAtPosition`.
// It is the caller's responsibility to call `astnav.GetTokenAtPosition` to compute a default `tokenAtPosition`,
// or `astnav.FindPrecedingToken` to compute a default `precedingToken`.
func getRangeOfEnclosingComment(
Copy link
Member Author

Choose a reason for hiding this comment

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

This function in Strada used to differentiate between a null precedingToken (which would be left undefined) and an undefined precedingToken (which would get replaced with a default computed value). We can't easily do this in Go, so, I'm simply not doing anything to the parameters here.

Copy link
Member

Choose a reason for hiding this comment

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

Maybe a separate shouldFindPrecedingToken bool?

Copy link
Member Author

Choose a reason for hiding this comment

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

I kinda dislike the original behavior, and there was only one place which needed it in Strada, so I'd rather leave it like this.

@gabritto gabritto marked this pull request as ready for review July 11, 2025 23:38
Copy link
Member

@DanielRosenwasser DanielRosenwasser left a comment

Choose a reason for hiding this comment

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

Looks good, but I had a few nits. I am curious to hear what others say about needing an allocator for that method though.

// `precedingToken` and `tokenAtPosition`.
// It is the caller's responsibility to call `astnav.GetTokenAtPosition` to compute a default `tokenAtPosition`,
// or `astnav.FindPrecedingToken` to compute a default `precedingToken`.
func getRangeOfEnclosingComment(
Copy link
Member

Choose a reason for hiding this comment

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

Maybe a separate shouldFindPrecedingToken bool?

tokenAtPosition *ast.Node,
) *ast.CommentRange {
jsdoc := ast.FindAncestor(tokenAtPosition, func(n *ast.Node) bool {
return n.Kind == ast.KindJSDoc
Copy link
Member

Choose a reason for hiding this comment

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

We're probably missing an IsJSDoc method, which you could just pass here as (*ast.Node).IsJSDoc`

Copy link
Member

Choose a reason for hiding this comment

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

I could have sworn we had that already...

Copy link
Member Author

Choose a reason for hiding this comment

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

I thought we could be avoiding adding it on purpose, to avoid confusion with isJSDocNode, but I'll add it.

if node.Kind == ast.KindJsxText {
return nil
}
return scanner.GetLeadingCommentRanges(&ast.NodeFactory{}, file.Text(), node.Pos())
Copy link
Member

Choose a reason for hiding this comment

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

It's so weird that this needs a factory because we only ever really need to look at a single comment range at a time.

@@ -11,7 +11,7 @@ import (

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

t.Skip()
Copy link
Member

Choose a reason for hiding this comment

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

do we have an easy way to eyeball these Skip tests for the future? Do we just assume every Skip in fourslash is a bug?

Copy link
Member Author

Choose a reason for hiding this comment

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

Some are bugs, some are missing features, e.g. JSDoc tests. The way to find out why they fail is to re-generate the tests without Skip and look at the error messages.

Copy link
Member

@DanielRosenwasser DanielRosenwasser left a comment

Choose a reason for hiding this comment

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

I just realized the comment range methods don't actually allocate and just return values - so it's even weirder that they need a factory! But I feel better about it because it's not just allocating slabs of memory needlessly.

gabritto and others added 2 commits July 14, 2025 11:14
Co-authored-by: Daniel Rosenwasser <[email protected]>
Co-authored-by: Daniel Rosenwasser <[email protected]>
@gabritto gabritto enabled auto-merge July 14, 2025 20:28
@gabritto
Copy link
Member Author

Hopefully this will be the last reapproval needed. 🙁

@gabritto gabritto added this pull request to the merge queue Jul 15, 2025
Merged via the queue into main with commit e62a044 Jul 15, 2025
22 checks passed
@gabritto gabritto deleted the gabritto/isInComment branch July 15, 2025 16:00
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.

3 participants