Skip to content
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

Add contiguous scope modifier #1835

Open
AndreasArvidsson opened this issue Aug 26, 2023 · 4 comments · May be fixed by #2101
Open

Add contiguous scope modifier #1835

AndreasArvidsson opened this issue Aug 26, 2023 · 4 comments · May be fixed by #2101
Assignees

Comments

@AndreasArvidsson
Copy link
Member

AndreasArvidsson commented Aug 26, 2023

Could be used for example targeting multiple contiguous line comments.

"comment slurp comments fine"

// foo
// bar

Potential solution

I was thinking that this could be implemented around the existing scope handlers similar to how the every modifier works and just join scopes/targets based on leading and trailing delimiter ranges.

  1. Get all scopes
const scopes = scopeHandler.generateScopes(...);
  1. Iterate scopes and join contiguous targets
const leadingRange = leadingTarget.trailingDelimiterRange ?? leadingTarget.contentRange;
const trailingRange = trailingTarget.leadingDelimiterRange ?? trailingTarget.contentRange;
if (leadingRange.intersects(trailingRange) != null) {
    newContentRange = leadingTarget.contentRange.union(trailingTarget.contentRange);
}

Thinks to discuss

  • One problem is that most leading and trailing delimiter ranges doesn't contain the eol. Eg with comment the leading range would end with the previous line and the trailing range would start with the next line. We could of course say that that is valid for merging, but it would probably make "take slurp tokens" join all tokens in the file and therefore take the entire file.
  • How do we handle nested scopes?
  • Do we want to support "every slurp <scope>"?
@josharian
Copy link
Collaborator

josharian commented Aug 26, 2023

FWIW, in the Go ast, contiguous blocks of comments are called comment groups.

I too would like to be able to operate on comment groups.

General purpose slurp is an interesting idea. It would be useful to think of other scopes that it might apply to / other use cases where it is distinct from “every”.

@josharian
Copy link
Collaborator

josharian commented Aug 26, 2023

If slurp does not ignore commenst, I guess it could be used for things like taking items between one comment and the next comment, which sounds like a reasonably common task, for example, in a list of test cases, organized into groups separated by comments.

@josharian

This comment was marked as resolved.

@AndreasArvidsson
Copy link
Member Author

AndreasArvidsson commented Aug 26, 2023

I've already suggested a definition in my code examples. I propose to define it as when two instances of the same scope have intersecting trailing/leading ranges.

@AndreasArvidsson AndreasArvidsson self-assigned this Dec 5, 2023
@AndreasArvidsson AndreasArvidsson linked a pull request Dec 6, 2023 that will close this issue
2 tasks
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 a pull request may close this issue.

2 participants