Skip to content

add Go branch scope support #1854

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 14 commits into from
Nov 30, 2023
Merged

add Go branch scope support #1854

merged 14 commits into from
Nov 30, 2023

Conversation

josharian
Copy link
Collaborator

@josharian josharian commented Sep 5, 2023

Not all the behavior is perfect (I'm looking at you, pour and drink), but it's better than not existing.

The perfect is the enemy of the good.

Checklist

  • I have added tests
  • [-] I have updated the docs and cheatsheet
  • [-] I have not broken the cheatsheet

@josharian
Copy link
Collaborator Author

josharian commented Sep 5, 2023

outstanding:

  • figure out how to get drink, pour, and chuck all to do the right thing at the same time (sigh)
  • add tests once behavior is correct
  • move TODOs to some personal list, or just implement; maybe move over condition at the same time?

note also this is a potentially interesting case for #1631

@pokey
Copy link
Member

pokey commented Sep 6, 2023

Looking at your scope visualizer screenshots from slack, it appears that the problem is that tree-sitter-go includes the trailing newline in the node's range. I'd recommend using our shrink regex predicate operator to exclude it. Hopefully that will fix your various issues.

Keep in mind that "chuck branch" will leave an empty line, but pretty much every other scope type has the same problem, and we're planning to fix all tree-sitter whitespace removal using the #855 hammer

@josharian josharian changed the title add branch support to Go add switch case clauses to Go's branch scope Sep 7, 2023
@josharian
Copy link
Collaborator Author

Range:

Screenshot 2023-09-06 at 8 07 56 PM

Removal:

Screenshot 2023-09-06 at 8 08 07 PM

Iteration:

Screenshot 2023-09-06 at 8 08 17 PM

@josharian josharian marked this pull request as ready for review September 7, 2023 03:10
@josharian josharian requested a review from pokey as a code owner September 7, 2023 03:10
Copy link
Member

@pokey pokey left a comment

Choose a reason for hiding this comment

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

From your screenshots, it appears the newline is still being included.

Fwiw I do wonder if we want to create a "trim" query predicate operator so we don't have to keep reinventing the wheel with whitespace / newline trimming

@auscompgeek auscompgeek added the lang-go Issues related to Go programming language support label Sep 9, 2023
josharian added a commit that referenced this pull request Sep 12, 2023
The same thing can be accomplished with #shrink-to-match!,
but this is considerably simpler to use, and meets a common need.

Suggested by Pokey during review of #1854.
@josharian
Copy link
Collaborator Author

PTAL at these visualizer screenshots. If it looks OK, I'll add more tests and then you can review the code.

I remain unhappy with the pour/drink behavior, but I'm realizing that fixing that is a much, much larger project that touches other scopes, and I'm pretty ready to get the basic functionality in.

Screenshot 2023-09-14 at 5 42 55 PM Screenshot 2023-09-14 at 5 44 02 PM Screenshot 2023-09-14 at 5 43 48 PM

@josharian josharian changed the title add switch case clauses to Go's branch scope add Go branch scope support Sep 15, 2023
Copy link
Member

@pokey pokey left a comment

Choose a reason for hiding this comment

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

had a quick look and tests / visualizer looks good! left some minor comments; didn't review the .scm thoroughly as it's a bit tough to read without autoformatter

@josharian
Copy link
Collaborator Author

added some tests. unfortunately, the behavior in

packages/cursorless-vscode-e2e/src/suite/fixtures/recorded/languages/go/cloneBranch2.yml

and

packages/cursorless-vscode-e2e/src/suite/fixtures/recorded/languages/go/cloneBranch3.yml

is bad: it generates invalid code.

I am not sure how to fix it. Looks like a line number off-by-one?

Because they're hard to read (maybe I really should finish up #1805? there was so little enthusiasm for it), here's an example.

Start:

if x {
	x++
} else if y {
	y++
} else {
	x--
}

Then "clone branch" with cursor at the beginning of the y++ line. Result:

if x {
	x++
else if y {
	y++
}
} else if y {
	y++
} else {
	x--
}

Best would be something like:

if x {
	x++
} else if y {
	y++
} else if y {
	y++
} else {
	x--
}

But at a minimum, I'd expect valid syntax, so maybe:

if x {
	x++
}
else if y {
	y++
} else if y {
	y++
} else {
	x--
}

@josharian
Copy link
Collaborator Author

Sigh. The precommit failure is due to typescript code I added to the playground to experiment with branch scope behavior. Can we extent the playground exemption to include typescript?

josharian added a commit that referenced this pull request Sep 15, 2023
It is unused. I believe that it also contains a latent bug,
when the range contains entirely whitespace.

Noticed during discussion at

#1854 (comment)
@josharian josharian mentioned this pull request Sep 15, 2023
@josharian
Copy link
Collaborator Author

Fixed clone. It's not quite perfect, but it's close enough. The weird setting of the insertion delimiter on @branch.start.startOf instead of just @branch is because we evaluate the insertion delimiter query before we rewrite .start.startOf, and it complains because plain old @branch doesn't exist at that point. We could reorder, or be more forgiving...the relevant code says:

      // FIXME: We could allow some predicates to be forgiving,
     // because it's possible to have a capture on an optional nodeInfo.
     // In that case we'd prob just return `true` if any capture was
     // `null`, but we should check that the given capture name
     // appears statically in the given pattern.  But we don't yet
     // have a use case so let's leave it for now.

But let's just get this in for now?

@josharian
Copy link
Collaborator Author

PTAL

@josharian
Copy link
Collaborator Author

I believe this is ready for review.

@josharian
Copy link
Collaborator Author

Ping.

github-merge-queue bot pushed a commit that referenced this pull request Nov 1, 2023
It is unused. I believe that it also contains a latent bug,
when the range contains entirely whitespace.

Noticed during discussion at


#1854 (comment)



## Checklist

- [-] I have added
[tests](https://www.cursorless.org/docs/contributing/test-case-recorder/)
- [-] I have updated the
[docs](https://github.com/cursorless-dev/cursorless/tree/main/docs) and
[cheatsheet](https://github.com/cursorless-dev/cursorless/tree/main/cursorless-talon/src/cheatsheet)
- [-] I have not broken the cheatsheet

---------

Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
fidgetingbits pushed a commit to fidgetingbits/cursorless that referenced this pull request Nov 3, 2023
It is unused. I believe that it also contains a latent bug,
when the range contains entirely whitespace.

Noticed during discussion at


cursorless-dev#1854 (comment)



## Checklist

- [-] I have added
[tests](https://www.cursorless.org/docs/contributing/test-case-recorder/)
- [-] I have updated the
[docs](https://github.com/cursorless-dev/cursorless/tree/main/docs) and
[cheatsheet](https://github.com/cursorless-dev/cursorless/tree/main/cursorless-talon/src/cheatsheet)
- [-] I have not broken the cheatsheet

---------

Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
Copy link
Member

@pokey pokey 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 with some minor comments!

consequence: (block) @branch.end.endOf
) @_if @branch.start.startOf
(#not-parent-type? @_if if_statement)
(#insertion-delimiter! @branch.start.startOf " ")
Copy link
Member

Choose a reason for hiding this comment

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

Is this necessary? I think a space is the default insertion delimiter.

Suggested change
(#insertion-delimiter! @branch.start.startOf " ")

queries/go.scm Outdated
alternative: (if_statement
consequence: (block) @branch.end.endOf
)
(#insertion-delimiter! @branch.start.startOf " ")
Copy link
Member

Choose a reason for hiding this comment

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

As above

Suggested change
(#insertion-delimiter! @branch.start.startOf " ")

queries/go.scm Outdated
Comment on lines 256 to 258
"else" @branch.start.startOf
alternative: (if_statement
consequence: (block) @branch.end.endOf
Copy link
Member

Choose a reason for hiding this comment

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

I'm pretty sure you don't need the .startOf / .endOf if they're not overlapping and you can rely on the order

Suggested change
"else" @branch.start.startOf
alternative: (if_statement
consequence: (block) @branch.end.endOf
"else" @branch.start
alternative: (if_statement
consequence: (block) @branch.end

"else" @branch.start.startOf
alternative: (block)
) @branch.end.endOf
(#insertion-delimiter! @branch.start.startOf " ")
Copy link
Member

Choose a reason for hiding this comment

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

as above

Suggested change
(#insertion-delimiter! @branch.start.startOf " ")

Copy link
Member

@pokey pokey left a comment

Choose a reason for hiding this comment

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

Made the final small tweaks to get this one merged in

@pokey pokey enabled auto-merge November 30, 2023 18:11
@pokey pokey mentioned this pull request Nov 30, 2023
1 task
@pokey pokey added this pull request to the merge queue Nov 30, 2023
Merged via the queue into main with commit 6660914 Nov 30, 2023
@pokey pokey deleted the josh/go/branch branch November 30, 2023 18:31
@josharian
Copy link
Collaborator Author

Made the final small tweaks to get this one merged in

Thanks! Sorry I'm such a mess at the moment. Really appreciated.

@pokey
Copy link
Member

pokey commented Dec 4, 2023

Not at all!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lang-go Issues related to Go programming language support
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants