juji-io / editscript

A library to diff and patch Clojure/ClojureScript data structures
Eclipse Public License 1.0
472 stars 23 forks source link

Support for structural editing operations #27

Open or opened 2 years ago

or commented 2 years ago

Hey, great library, thanks for your work!

I'd like to create edit scripts for Clojure code patches. Would it be feasible to add operations for structural editing? I'm thinking mainly about the following ones, tho there are some others.

  1. wrap: expr -> (expr)
  2. raise: (foo expr bar) -> expr
  3. slurp: (foo) expr -> (foo expr)
  4. barf: (foo expr) -> (foo) expr

Each could work for vectors and sets as well.

Example:

(e/diff
   '(do
      (let [a 6])
      (+ a 9))
   '(do
      (let [a 6]
        (+ a 10))))
-> [[[1 2] :+ (+ a 10)] 
    [[2] :-]]

would result in a cheaper sequence of operations:

[[[1] :slurp]
 [[1 2 2] :r 10]]

Another example:

  (e/diff
   '(do
      (large-expr))
   '(do
      (let [a 6]
       (large-expr))))
-> [[[1] :r (let [a 6] (large-expr))]]

would become something like

[[[1] :wrap :list]
 [[1 0] :+ let]
 [[1 1] :+ [a 6]]]
huahaiy commented 2 years ago

interesting idea, I will think about it.

huahaiy commented 2 years ago

Our current definition of diff size is the number of nodes in the diff data structure. The structural editing examples you show above do not actually reduce the diff size, but increase them.