Skip to content

feat(ClickSuggestions): better pasting logic - #45571

Open
JovanGerb wants to merge 1 commit into
leanprover-community:masterfrom
JovanGerb:Jovan-click_suggestions-insert_logic
Open

JovanGerb wants to merge 1 commit into
leanprover-community:masterfrom
JovanGerb:Jovan-click_suggestions-insert_logic

Conversation

@JovanGerb

@JovanGerb JovanGerb commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

This PR makes #click_suggestions insert its suggestions in the correct place, even if the cursor is not in the place where the tactic should go.

In particular, if the cursor is placed after a tactic, or at the start of a tactic, then the position of the insertion is made to align with that tactic syntax, instead of using the cursor position.

What is more difficult is to determine the appropriate indentation level after a loose by. So, the user still needs to manually add the newline after a by before using #click_suggestions. This might be improved in a future PR.

This PR adds a test file that tests the insertion behaviour. In addition to testing the new behaviour, we also tests some other behaviours that weren't previously tested.


@github-actions

github-actions Bot commented Nov 2, 2026 •

Copy link
Copy Markdown

PR summary a0c68dba51

Import changes for modified files

No significant changes to the import graph

Import changes for all files
Files Import difference

Declarations diff (regex)

+ String.insertCursor
+ String.replaceRange
+ newCursorPos
+ testInsertionLogic

You can run this locally as follows
## from your `mathlib4` directory:
git clone https://github.com/leanprover-community/mathlib-ci.git ../mathlib-ci

## summary with just the declaration names:
../mathlib-ci/scripts/pr_summary/declarations_diff.sh <optional_commit>

## more verbose report:
../mathlib-ci/scripts/pr_summary/declarations_diff.sh long <optional_commit>

The doc-module for scripts/pr_summary/declarations_diff.sh in the mathlib-ci repository contains some details about this script.

Declarations diff (Lean)

✅ Lean-aware diff — post-build, computed from the Lean environment (commit a0c68db).

  • +5 new declarations
  • −3 removed declarations
+Mathlib.Tactic.ClickSuggestions.Context.fileMap
+Mathlib.Tactic.ClickSuggestions.Context.fileVersion
-Mathlib.Tactic.ClickSuggestions.Context.focus
+Mathlib.Tactic.ClickSuggestions.Context.goalsAt
-Mathlib.Tactic.ClickSuggestions.Context.meta
+Mathlib.Tactic.ClickSuggestions.Context.numGoals
-Mathlib.Tactic.ClickSuggestions.Context.stx
+Mathlib.Tactic.ClickSuggestions.newCursorPos

No changes to strong technical debt.
No changes to weak technical debt.

Current commit a0c68dba51
Reference commit b6dfe53cdc

This script lives in the mathlib-ci repository. To run it locally, from your mathlib4 directory:

git clone https://github.com/leanprover-community/mathlib-ci.git ../mathlib-ci
../mathlib-ci/scripts/reporting/technical-debt-metrics.py pr_summary
  • The relative value is the weighted sum of the differences with weight given by the inverse of the current value of the statistic.
  • The absolute value is the relative value divided by the total sum of the inverses of the current values (i.e. the weighted average of the differences).

@github-actions github-actions Bot added the t-meta Tactics, attributes or user commands label Nov 2, 2026
@JovanGerb JovanGerb changed the title feat(ClickSuggestions): better insertion logic feat(ClickSuggestions): better pasting logic Nov 12, 2026

@joneugster joneugster left a comment

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.

Thanks! Seems to work nicely.

maintainer merge

- Otherwise, for example if the cursor is after a `by` or a `=>`,
simply insert it at the cursor position.

TODO: we might want to add support for inserting in the middle of a `rw [...]` sequence.

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.

(review note) this isn't currently supported either, it inserts rw[..., rw[suggested_lemma], ...], so this is just documenting an existing issue and is not a regression.

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.

I've observed a bug where the text was something like

lemma ...
    ... := by
  induction ...

and click-suggestion wrote

lemma ...
    ... := by
  rw [...]induction ...

without a new line, but I am unable to reproduce this again...

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.

since I can't reproduce this, I'm happy to ignore it :)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could it be this?

module

public import Mathlib.Tactic.ClickSuggestions
public import ProofWidgets.Component.HtmlDisplay

#click_suggestions

example (n : Nat) : n + 0 = n := by
  induction n <;> simp

Then put the cursor at the start of the line with induction n <;> simp.

Now, Shift-click n + 0 and click any(?) suggestion, such as rw [add_zero] to get to

example (n : Nat) : n + 0 = n := by
rw [Nat.add_zero]  induction n <;> simp

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Indeed, when the cursor is before the first tactic in a by block (or if there is no tactic in the by block yet) then the syntax associated to that position is the by token itself, which means that the syntax doesn't allow us to determine the correct alignment with the other tactics.

In that scenario, we simply do the insertion at the cursor position, which indeed can be wrong.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This behaviour is also visible in the tests. I should add a comment there pointing this out.

@joneugster joneugster assigned joneugster and unassigned thorimur Nov 20, 2026
@github-actions

Copy link
Copy Markdown

🚀 Pull request has been placed on the maintainer queue by joneugster.

@mathlib-triage mathlib-triage Bot added the maintainer-merge A reviewer has approved the changed; awaiting maintainer approval. label Nov 20, 2026

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

maintainer-merge A reviewer has approved the changed; awaiting maintainer approval. t-meta Tactics, attributes or user commands

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants