pyrefly icon indicating copy to clipboard operation
pyrefly copied to clipboard

Literal autocomplete should take care of existing quote marks

Open fangyi-zhou opened this issue 4 months ago • 7 comments

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

fangyi-zhou avatar Sep 12 '25 23:09 fangyi-zhou

cc @kinto0

fangyi-zhou avatar Sep 12 '25 23:09 fangyi-zhou

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"] = "<>"

kinto0 avatar Sep 13 '25 14:09 kinto0

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)

fangyi-zhou avatar Sep 13 '25 23:09 fangyi-zhou

Image

fangyi-zhou avatar Sep 13 '25 23:09 fangyi-zhou

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).

kinto0 avatar Sep 14 '25 18:09 kinto0

current status:

  • quotes are still duplicated on acceptance of completion

kinto0 avatar Nov 11 '25 19:11 kinto0

hi, so i made a pr for this issue pr #1666 which fixed it and also added a test case for verification

Karman-singh15 avatar Nov 22 '25 18:11 Karman-singh15

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!

github-actions[bot] avatar Dec 09 '25 00:12 github-actions[bot]

hi @Karman-singh15 please reopen the PR if you would like to continue working on this

kinto0 avatar Dec 10 '25 00:12 kinto0

hi sorry for the delay i will be working on this tomorrow

Karman-singh15 avatar Dec 10 '25 18:12 Karman-singh15