Skip to content

impl Safe Delete #2207#2415

Closed
asukaminato0721 wants to merge 5 commits intofacebook:mainfrom
asukaminato0721:2207-safe-delete
Closed

impl Safe Delete #2207#2415
asukaminato0721 wants to merge 5 commits intofacebook:mainfrom
asukaminato0721:2207-safe-delete

Conversation

@asukaminato0721
Copy link
Copy Markdown
Contributor

@asukaminato0721 asukaminato0721 commented Feb 14, 2026

Summary

Fixes part of #2207

Added a file-level Safe Delete code action that deletes a Python file only when no import usages are found, using a workspace edit DeleteFile operation and a new refactor.delete kind.

Implemented an AST-based import usage scan across reverse dependencies to decide safety.

note: for simplicity, I didn't impl find ref and delete anyway yet. will add in the future.

Test Plan

Added LSP interaction tests and fixtures for safe delete.

@meta-cla meta-cla Bot added the cla signed label Feb 14, 2026
@asukaminato0721 asukaminato0721 changed the title impl Safe Delete impl Safe Delete #2207 Feb 14, 2026
@asukaminato0721 asukaminato0721 marked this pull request as ready for review February 14, 2026 04:05
Copilot AI review requested due to automatic review settings February 14, 2026 04:05
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR implements the Safe Delete feature for Pyrefly, addressing part of issue #2207. The implementation provides two levels of safe deletion:

  1. Symbol-level safe delete: Allows deletion of unused functions, methods, classes, variables, constants, and type aliases within a file. Uses global reference finding across all reverse dependencies to ensure the symbol is truly unused.

  2. File-level safe delete: Enables deletion of entire Python files when no import usages are detected. Uses AST-based import scanning across reverse dependencies to verify safety.

Changes:

  • Added symbol-level safe delete using global reference checking
  • Added file-level safe delete using import usage analysis
  • Introduced refactor.delete CodeActionKind for LSP clients
  • Added comprehensive test coverage for both deletion types

Reviewed changes

Copilot reviewed 13 out of 13 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
pyrefly/lib/state/lsp/quick_fixes/safe_delete.rs Implements symbol-level safe delete with reference checking
pyrefly/lib/lsp/non_wasm/safe_delete_file.rs Implements file-level safe delete with import usage scanning
pyrefly/lib/state/lsp.rs Adds find_global_references_from_definition and supporting methods
pyrefly/lib/lsp/non_wasm/server.rs Integrates safe delete actions into LSP server and registers refactor.delete kind
pyrefly/lib/test/lsp/code_actions.rs Unit tests for symbol-level safe delete
pyrefly/lib/test/lsp/lsp_interaction/safe_delete_file.rs LSP interaction tests for file-level safe delete
Test fixtures Example Python files for testing safe delete scenarios
Module files Updates to module structure to include new safe delete modules

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread pyrefly/lib/state/lsp/quick_fixes/safe_delete.rs Outdated
Comment thread pyrefly/lib/state/lsp.rs Outdated
@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

1 similar comment
@github-actions

This comment has been minimized.

@asukaminato0721 asukaminato0721 marked this pull request as draft February 14, 2026 05:49
@asukaminato0721 asukaminato0721 marked this pull request as ready for review February 14, 2026 05:54
@github-actions

This comment has been minimized.

@meta-codesync
Copy link
Copy Markdown
Contributor

meta-codesync Bot commented Feb 18, 2026

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

Copy link
Copy Markdown
Contributor

@stroxler stroxler left a comment

Choose a reason for hiding this comment

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

Review automatically exported from Phabricator review in Meta.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@jvansch1
Copy link
Copy Markdown
Contributor

jvansch1 commented Mar 4, 2026

@asukaminato0721 I am working on getting this in now. Could you just resolve the merge conflict here first?

asukaminato0721 and others added 4 commits March 4, 2026 21:04
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Mar 4, 2026

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Mar 4, 2026

No diffs to classify.

@meta-codesync
Copy link
Copy Markdown
Contributor

meta-codesync Bot commented Mar 4, 2026

@jvansch1 merged this pull request in 067c7d9.

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.

5 participants