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

REPLCompletions: only complete kwargs in kwarg position #51801

Merged
merged 1 commit into from
Oct 22, 2023

Conversation

IanButterworth
Copy link
Member

@IanButterworth IanButterworth commented Oct 21, 2023

Fixes #51762

This PR

After a ; only kwargs complete

Screenshot 2023-10-20 at 11 03 21 PM

values still complete

Screenshot 2023-10-20 at 11 03 29 PM

Previously

Screenshot 2023-10-20 at 11 06 27 PM

@IanButterworth IanButterworth added the REPL Julia's REPL (Read Eval Print Loop) label Oct 21, 2023
@IanButterworth
Copy link
Member Author

IanButterworth commented Oct 21, 2023

@Liozou #43536 intentionally completes all names whether or not you're after a ;. Do you think only completing kwargs after a ; could make sense?

Splatting a function into kwarg space could be valid, but it's a bit niche to lose the tab complete hinting for?

@IanButterworth IanButterworth requested a review from Liozou October 21, 2023 11:32
@Liozou
Copy link
Member

Liozou commented Oct 21, 2023

I'm fine with limiting suggestions after a ; to only kwarg= completions. I still believe that the kwargs... case in general is not that niche though: splatting a function call is definitely uncommon, however splatting a variable that was inherited from the arguments is actually quite typical in a sequence like this:

foo(...; kwargs...) = bar(...; kwargs...)

I tend to agree that the benefits of limiting suggestions to only the most relevant cases (i.e. only kwarg= completions) outweigh those of being as complete as possible. But actually, there may be a third option: we could also provide suggestions for kwargs..., including the trailing splat, if kwargs is a variable existing in scope and is a NamedTuple (which, I believe, should be the vast majority of splat keyword arguments). What do you think?

Two side notes:

  • if the called method has xxx as a keyword argument, then we may want to provide both xxx= and xxx as suggestions if that xxx is a variable existing in scope. That's because calling foo(; xxx) is syntactic sugar for foo(; xxx=xxx).
  • as you mention, Add REPL-completion for keyword arguments #43536 provides suggestions even before a ;. That's simply because calling foo(..., xxx=...) is valid syntax. Additionally note that foo(xxx=whatever, arg) is also valid syntax for a method defined as foo(y; xxx). So, in my opinion, if we choose to restrict keyword argument completion to after an explicit ;, that's a statement that the official style guide recommends putting keyword arguments after an explicit semicolon. I actually believe that's a very good idea (which is also present in https://github.com/invenia/BlueStyle#keyword-arguments), but then I think we might as well go the extra mile of stating it so in the actual style guide, i.e. here:
    10. **Keyword arguments**.
    In Julia keyword arguments have to come last anyway in function definitions; they're
    listed here for the sake of completeness.

@IanButterworth
Copy link
Member Author

Great suggestions, thanks. Incorporated, except for

we could also provide suggestions for kwargs..., including the trailing splat, if kwargs is a variable existing in scope and is a NamedTuple

Which I couldn't immediately figure out.

Copy link
Member

@Liozou Liozou left a comment

Choose a reason for hiding this comment

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

Great suggestions, thanks. Incorporated, except for

we could also provide suggestions for kwargs..., including the trailing splat, if kwargs is a variable existing in scope and is a NamedTuple

Which I couldn't immediately figure out.

That's fine, it's probably not necessary and it can wait for a future PR anyway if the need arises.

stdlib/REPL/src/REPLCompletions.jl Outdated Show resolved Hide resolved
@@ -1498,7 +1498,7 @@ end
@test "length=" ∈ c
@test "len2=" ∈ c
c, r = test_complete_foo("kwtest3(1+3im; named")
@test "named" c
@test "named" c
# TODO: @test "namedarg=" ∉ c
Copy link
Member Author

Choose a reason for hiding this comment

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

@Liozou is this TODO relevant? I wasn't sure what it meant

Copy link
Member

Choose a reason for hiding this comment

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

Ah so this TODO is meant to be fixed by #44434, like the other TODOs introduced in #43536. It means that kwtest3(1+3im; named#TAB currently suggests namedarg= as a suggestion, but that's actually invalid since the method of kwtest3 that accepts the namedarg keyword argument cannot take a Complex as its first argument.
I made these cases TODOs instead of @test_broken since they are wishful features really, I call them "invalid" but they are not regressions. So I believe the TODO is still relevant, but it's orthogonal to this PR.

@IanButterworth
Copy link
Member Author

@Liozou the example of length completing as both length and length= feels a little awkward. It feels rare that people would use a function/type constructor with the same name as a kwarg after ; in the shorthand form. Perhaps we should filter out callables? Not sure if that's possible reliably

@Liozou
Copy link
Member

Liozou commented Oct 22, 2023

Well yeah, I agree it's definitely a bit unusual, but trying to filter out callables would make the code even more confusing and difficult to maintain for very little additional gain.
Or we can simply revert the suggestion of xxx when xxx= is already provided, that's OK too. Especially since it takes one keystroke to go from xxx= to xxx anyway. I put that option above because I thought it was worth thinking about it, but since we want to reduce the number of completions, I'm fine with strictly sticking to xxx= suggestions.

@IanButterworth IanButterworth merged commit fa66820 into JuliaLang:master Oct 22, 2023
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
REPL Julia's REPL (Read Eval Print Loop)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

REPLCompletions: Only complete kwargs when in kwarg positions
2 participants