Skip to content

Add similar extraction occurrence count - #947

Open
mcepl wants to merge 2 commits into
masterfrom
issue-168-extract-match-count
Open

mcepl wants to merge 2 commits into
masterfrom
issue-168-extract-match-count

Conversation

@mcepl

@mcepl mcepl commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

Add a private _get_similar_occurrences() method to the extract-method
and extract-variable refactorings. It runs the existing validation and
match collection and returns the number of regions that similar-code
extraction would affect (the selected region included), without
constructing a ChangeSet or applying replacements. This lets editor
integrations decide whether to offer the "similar" option before
calculating the full refactoring.

As agreed in review, the method is deliberately private and
undocumented: a general, consistent preview API for refactorings is to
be designed separately, and this method may change or be replaced by
it.

Tests cover the counts for both refactoring types, including a
selection with no other matches. A changelog.d/168.md fragment is
included, which is why this PR is based on changelog-news-fragments.

Closes #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.46%. Comparing base (18e7d47) to head (f2fcc8e).

Additional details and impacted files
@@                    Coverage Diff                    @@
##           changelog-news-fragments     #947   +/-   ##
=========================================================
  Coverage                     95.46%   95.46%           
=========================================================
  Files                           134      134           
  Lines                         26946    26977   +31     
=========================================================
+ Hits                          25723    25754   +31     
  Misses                         1223     1223           

☔ 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 Resolved
@mcepl
mcepl force-pushed the issue-168-extract-match-count branch from 517c871 to 1da5b64 Compare November 14, 2026 01:14
@mcepl
mcepl requested a review from lieryan November 16, 2026 17:24
@lieryan lieryan self-assigned this Nov 28, 2026
Nearly every pull request conflicted on the top of CHANGELOG.md,
because all of them appended to the same "Upcoming release"
section. Following a much simplified version of CPython's
blurb/NEWS.d workflow, each PR now adds a small Markdown file
changelog.d/<issue>.md with its entry, so parallel PRs never
touch the same file.

The new bin/news script (plain POSIX sh, so it works on Linux,
the BSDs and macOS) creates a fragment in $VISUAL/$EDITOR from a
template encouraging longer, explanatory entries (`bin/news 123`),
previews the pending release section (`bin/news preview`), and
folds the fragments, sorted by number, into a new "# Release
<version>" section of CHANGELOG.md (`bin/news collect`). A new
portable top-level Makefile wraps it as `make news N=123`,
`make changelog-preview`, `make changelog` and `make release`,
the last of which also builds the documentation via
docs/Makefile; that gains an explicit `html` rule, as its
Sphinx-generated catch-all pattern rule is GNU make only. The
news target passes N and TEXT to bin/news through the
environment ("$$N", "$$TEXT"), so Markdown backticks and quotes
in an entry reach the script literally instead of being
re-parsed by the shell.

The pending entry for the #223 regression tests, previously in
the "Upcoming release" section, has been moved to
changelog.d/223.md, and the PR template, contributing and
release-process docs updated.
@mcepl
mcepl changed the base branch from master to changelog-news-fragments November 30, 2026 09:05
mcepl added a commit that referenced this pull request Nov 30, 2026
Following review on PR #947, rename get_similar_occurrences() to
_get_similar_occurrences() and drop its documentation from
docs/library.rst. The maintainers would rather design a general
preview API covering several refactorings before committing to a
public contract, so this helper stays internal for now. Add the
changelog.d fragment for the change.
Fixes: #168
@mcepl
mcepl force-pushed the issue-168-extract-match-count branch from 8ef13f9 to 79d4f25 Compare November 30, 2026 09:10
Add a private _get_similar_occurrences() helper to ExtractMethod
and ExtractVariable, which returns the number of regions that
similar-code extraction would affect (the selected region
included) without building or applying a ChangeSet. Integrations
can use it to decide whether offering the "similar" option makes
sense. Match collection is split out of _collect_info() into
_collect_matches() and info construction into _create_info(), so
the helper reuses the existing validation and matching logic.

Following review on PR #947, the helper stays private and
undocumented: the maintainers would rather design a general
preview API covering several refactorings before committing to a
public contract. A changelog.d fragment and tests are included.

Fixes: #168
@mcepl
mcepl force-pushed the issue-168-extract-match-count branch from 79d4f25 to f2fcc8e Compare November 30, 2026 09:12
@mcepl

mcepl commented Nov 30, 2026

Copy link
Copy Markdown
Contributor Author

@lieryan, the documentation has been removed and the method is now private (_get_similar_occurrences()), as you suggested; the changelog fragment notes it is undocumented and subject to change pending a general preview API. Could you take another look, please?

Base automatically changed from changelog-news-fragments to master December 1, 2026 22:17

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