Skip to content

Conversation

@asukaminato0721
Copy link
Contributor

fix #1490

Adjusted so insert_import_edit* returns an ImportEdit with both the actual edit and a stable display string, and added logic to append new symbols to an existing matching from ... import ... statement instead of duplicating the import line. The helper now shares this richer info with callers so UI text stays human-readable.

Updated LSP code to consume the new struct, ensuring code-action titles and auto-import completion details still show from module import name while the actual edit mutates the existing line in place.

Taught the infer command to handle the new edit type and to skip empty/duplicate insertions when mutating files.

Revised the LSP code-action test to assert the merged-import behavior.

@asukaminato0721 asukaminato0721 marked this pull request as ready for review November 18, 2025 10:54
@asukaminato0721 asukaminato0721 marked this pull request as draft November 18, 2025 12:36
@meta-codesync
Copy link

meta-codesync bot commented Nov 20, 2025

@jvansch1 has imported this pull request. If you are a Meta employee, you can view this in D87254329.

@asukaminato0721 asukaminato0721 marked this pull request as ready for review November 21, 2025 11:57
report.push_str(" with text edit: ");
report.push_str(&format!("{:?}", &text_edit));
}
if let Some(documentation) = documentation {

Choose a reason for hiding this comment

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

Why were these changes needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We were trying to match the historical golden strings for completion reports.

Previously, every entry, even plain keywords or literal values, had a blank line appended because the reporter always added one after the optional docstring block. That produced extra empty lines compared to the expected fixtures, which is why dozens of completion tests started failing.

The adjustment keeps the docstring formatting logic intact, but stops unconditionally inserting that trailing blank line unless there was actually extra content to separate. This wa,y the rendered report matches the snapshots again without altering runtime behavior.

{
return edit;
}
let position = if let Some(first_stmt) = ast.body.iter().find(|stmt| !is_docstring_stmt(stmt)) {

Choose a reason for hiding this comment

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

I wonder if there is some way we can avoid traversing the entire AST again? It might lead to slow performance for large modules.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

One way to avoid the second walk would be to fold both concerns into a single pass (track the insertion position while scanning for compatible imports)

Or to compute the insertion point once before calling try_extend_existing_from_import and pass it in.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Pyrefly inserted typing import in sub-optimal location

2 participants