Literal autocomplete should take care of existing quote marks
Describe the Bug
from typing import Literal
def foo(fruit: Literal["apple", "pear"]):
...
foo(''
Python: 3.12
When the cursor is in between the quotes, the auto-complete populates the literals correctly. Then if I select one of the options, the post-completion state becomes
foo(''apple''
which didn't take into account the existing quote marks and is incorrect.
On a side note, the current auto-complete doesn't handle strings with quotes correctly --- which is an orthogonal issue with lower priority.
Relevant code pointer: https://github.com/facebook/pyrefly/blob/4ffa2e037a105ee284acc2b127ac454944ea96e1/pyrefly/lib/state/lsp.rs#L1934
Presumably this call wraps the string into another layer of single quotes https://github.com/facebook/pyrefly/blob/4ffa2e037a105ee284acc2b127ac454944ea96e1/pyrefly/lib/state/lsp.rs#L1938
https://github.com/facebook/pyrefly/blob/4ffa2e037a105ee284acc2b127ac454944ea96e1/crates/pyrefly_types/src/literal.rs#L57
Sandbox Link
https://pyrefly.org/sandbox/?project=N4IgZglgNgpgziAXKOBDAdgEwEYHsAeAdAA4CeS4ATrgLYAEALqcROgOZ0Q3G6UN0AZCAxiVUUADropmGGDphcuABRhKAV2GJBw0eIDaEkKmLFYRgDR0jxGKkpGAugEpEUuh7qFvUqYpUA5AEgFiDqDNBwJOSIIADEdACqEVDCpArq6ADGEbjocL5Ycgq8NKgMAPro6jTYosr42qwMznQAtAB8dHAMlG7onnSUMAzqlANgRgByNXV9dMD4AL5GUiBLoag5EABuMABi0DAUaFh4RGTrQA&version=3.12
(Only applicable for extension issues) IDE Information
No response
cc @kinto0
Thanks so much for the report! I'll try to get to this on monday. But feel free to take a stab at it in the meantime if you want since you seem to have found the root cause!
First issue: double quotes on completion, makes sense. Second issue:
On a side note, the current auto-complete doesn't handle strings with quotes correctly --- which is an orthogonal issue with lower priority.
What do you mean by this? Do you mean this example doesn't complete options?
foo: Literal["apple"] = "<>"
What do you mean by this? Do you mean this example doesn't complete options?
If there's a quote mark in the string (or other escapeable characters like \n), we should probably escape this quote marks (and/or other things that possibly needs escaping)?
def foo(quote_style: Literal["'", '"']):
...
foo('<>')
Ideally, the autocomplete option inside <> should be " or \'
and similarly
def foo(quote_style: Literal["'", '"']):
...
foo("<>")
Ideally, the autocomplete option inside <> should be \" or '
(This should be filed under a different issue, but possibly can be fixed alongside)
thanks for the detailed report!!
I think both issues should be fixed by switching to a textedit based completion response for literals instead of relying on client interpretation of inserttext. I can't do this immediately, but I've added two tests for this behavior (will merge on monday when the team is back).
in the meantime, literal completions will generally work before quotes are typed by triggering a completion manually (ctrl+space).
current status:
- quotes are still duplicated on acceptance of completion
hi, so i made a pr for this issue pr #1666 which fixed it and also added a test case for verification
This issue has someone assigned, but has not had recent activity for more than 2 weeks.
If you are still working on this issue, please add a comment so everyone knows. Otherwise, please unassign yourself and allow someone else to take over.
Thank you for your contributions!
hi @Karman-singh15 please reopen the PR if you would like to continue working on this
hi sorry for the delay i will be working on this tomorrow