Skip to content

Add similar extraction occurrence count - #947

Open
mcepl wants to merge 1 commit into
masterfrom
issue-168-extract-match-count
Open

mcepl wants to merge 1 commit into
masterfrom
issue-168-extract-match-count

Conversation

@mcepl

@mcepl mcepl commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

Add get_similar_occurrences() to extract-method and extract-variable
refactorings. It runs the existing validation and match collection and
returns the number of matching regions without constructing a ChangeSet or
applying replacements. This lets editor integrations decide whether to
offer the similar-code option before calculating the full refactoring.

Document the API and test counts for both refactoring types, including a
selection with no other matches.

Tests: python3 -m pytest -q (2172 passed, 7 skipped, 5 xfailed).

Closes #168

Expose get_similar_occurrences() so clients can decide whether to offer
similar-code extraction without constructing the resulting Change. Reuse
the existing validation and match collection, and document the count for
API callers.

Fixes: #168
@codecov

codecov Bot commented Nov 9, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.44%. Comparing base (1f102bb) to head (517c871).

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #947   +/-   ##
=======================================
  Coverage   95.43%   95.44%           
=======================================
  Files         134      134           
  Lines       26843    26874   +31     
=======================================
+ Hits        25618    25649   +31     
  Misses       1225     1225           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment thread docs/library.rst
that option. It returns the number of regions similar extraction would
process, without creating a ``Change``. In the usual case, a result greater
than one means that similar extraction would affect additional regions.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Rather than adding an API that's specific to just extract-similar refactoring, I was actually thinking that this should be more of a general-purpose API to ask the refactoring class to compute a preview object.

An API that could be used, for example, for rename/extract similar operation to preview how many files/occurrences would be affected, listing valid target scopes where extract operation would or could place the extracted method/variable, previewing the function arguments of extract method, how many occurrences would be affected by inline refactoring, how many things organise import would remove/move, etc, without fully calculating the changeset. Integrations could display these info in the UI so users can make more informed decision before performing them, or they could use it to enable/disable actions like similar that doesn't always make sense.

The availability of what info is available is of course going to be specific to each operation, but the API for requesting these preview information objects should have a somewhat consistent contract between different refactoring operations.

I think the implementation here for get_similar_occurrences() looks fine but the API should be left undocumented and considered private for now until we can sketch out the API contract in a separate ticket. I'd approve the PR if we leave out the documentation for now.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Question: Determine number of occurrences when extracting a variable/method

2 participants