( )⚙ D4997 absorb: use a formatter to generate output

This is an archive of the discontinued Mercurial Phabricator instance.

absorb: use a formatter to generate output
ClosedPublic

Authored by mbthomas on Oct 12 2018, 11:49 AM.

Details

Summary

Change absorb to use a formatter to generate its output. This allows the use
of templates to customize the output.

Diff Detail

Repository
rHG Mercurial
Branch
default
Lint
No Linters Available
Unit
No Unit Test Coverage

Event Timeline

mbthomas created this revision.Oct 12 2018, 11:49 AM
indygreg accepted this revision.Oct 12 2018, 1:35 PM
This revision is now accepted and ready to land.Oct 12 2018, 1:35 PM
This revision was automatically updated to reflect the committed changes.
yuja added a subscriber: yuja.Oct 13 2018, 1:43 AM

+ fm.startitem()
+ fm.write('hunk', ' %s\n',
+ '@@ -%d,%d +%d,%d @@'
+ % (a1, a2 - a1, b1, b2 - b1), label='diff.hunk')
+ fm.data(path=self.path, linetype='hunk')
+
+ def writeline(idx, diffchar, line, linetype, linelabel):
+ fm.startitem()
+ node = ''
+ if idx:
+ ctx = self.fctxs[idx]
+ fm.context(fctx=ctx)
+ node = ctx.hex()
+ fm.write('node', '%-7.7s ', node, label='absorb.node')
+ fm.write('diffchar ' + linetype, '%s%s\n', diffchar, line,
+ label=linelabel)

Nit: maybe rename "diffchar" to "change"? It's called as such in "hg grep".
https://www.mercurial-scm.org/wiki/GenericTemplatingPlan#Dictionary

+ fm.data(path=self.path, linetype=linetype)
+
+ for i in pycompat.xrange(a1, a2):
+ writeline(aidxs[i - a1], '-', trim(alines[i]), 'deleted',
+ 'diff.deleted')
+ for i in pycompat.xrange(b1, b2):
+ writeline(bidxs[i - b1], '+', trim(blines[i]), 'inserted',
+ 'diff.inserted')

Perhaps, it's better to make deleted/inserted lines nested to preserve the
original data structure.

{"hunk", ...,
 "deletedlines": [{"line": ...}, ...]
 "insertedlines": ...}

or

{"hunk", ...,
 "lines": [{"line": ..., "deleted": True}, ...]}

instead of

{"hunk", ...},
{"deleted": ...},
{"inserted": ...},