Skip to content

Merge or delete highlighted text on mouse click

Justin Tracey requested to merge jtracey/meld:replace-match into main

This merge request adds the following functionality: If a user is holding Ctrl and left-clicks on highlighted text within a chunk (i.e., the specific text within a chunk that differs), it applies the opcodes associated with that text to the adjacent pane(s). If a user is holding Ctrl+Shift, it deletes the highlighted text clicked on instead.

I've been using a simplified version of this patch for my own purposes for about a year now, and decided it's useful enough that I should upstream it. It doesn't quite fix #72, but is in a similar vein. Namely, it gives a (quick) way to apply a subset of edits within a chunk. I've found it particularly useful for two main usecases:

  • Editing files with lots of natural language text, such as papers written LaTeX with changes from multiple authors tracked. More generally, it helps merging branches where changes frequently happen to disparate parts of the same/adjacent lines, but in practice I've found this to be most common when natural language is involved (as lines can be very long and editing adjacent lines is frequent).
  • Resolving differences in CSV files (or any other column-oriented text file). Being able to scroll through two revisions of the same CSV and just click on the specific values I want from each row has been a massive time saver.

I've also used it during the course of normal code diffs as well, just not frequently enough to make it a killer feature the way it is in the above two uses.

Conceptually, this feature is implemented by:

  • looking for relevant events on the tagged text
  • if deleting, delete the text
  • if copying:
    • synchronously find the beginning and end of the corresponding tag using the matcher on that chunk
    • replace all of that tagged text with this tagged text

Accessing the matcher synchronously is a bit unfortunate, and not strictly necessary (my original patch was asynchronous), but asynchronously applying edits can not only lead to some UI lag, it also allows for race conditions that can cause things to break (especially in large 3-way diffs).

The hot keys of course correspond to the ones used to modify the actions of the gutter arrows. I originally had Shift clicking to delete the highlighted text, but this collided with Shift clicking to select text.

known bugs:

  • Applying an edit from the middle pane to both of the other panes registers as two distinct edits, so you have to undo twice (once for each side pane) to get back to the state you were in before Ctrl clicking. This is a fairly minor nuisance though, so I don't currently plan to fix it.

Normally I would discuss implementing such a feature before filing a merge request, but since I already had the code for my own purposes, I figured I would just start the discussion with it. Let me know if you'd like any changes or have any questions. :)

Merge request reports