Changeset - e6034764387e
[Not reviewed]
default
0 1 0
Mads Kiilerich (mads) - 5 years ago 2021-02-22 11:44:23
mads@kiilerich.com
diffs: let DiffProcessor inject context_lineno in chunk changes

Make it possible to use these numbers reliably, also outside as_html.
1 file changed with 10 insertions and 4 deletions:
0 comments (0 inline, 0 general)
kallithea/lib/diffs.py
Show inline comments
 
@@ -56,49 +56,48 @@ def _safe_id(idstring):
 
    matches \w (alphanumerics and underscore) is removed.
 

	
 
    """
 
    # Transform all whitespace to underscore
 
    idstring = re.sub(r'\s', "_", idstring)
 
    # Remove everything that is not a hyphen or a member of \w
 
    idstring = re.sub(r'(?!-)\W', "", idstring).lower()
 
    return idstring
 

	
 

	
 
def as_html(parsed_lines, table_class='code-difftable', line_class='line',
 
            old_lineno_class='lineno old', new_lineno_class='lineno new',
 
            no_lineno_class='lineno',
 
            code_class='code'):
 
    """
 
    Return given diff as html table with customized css classes
 
    """
 
    _html_empty = True
 
    _html = []
 
    _html.append('''<table class="%(table_class)s">\n''' % {
 
        'table_class': table_class
 
    })
 

	
 
    for file_info in parsed_lines:
 
        count_no_lineno = 0  # counter to allow comments on lines without new/old line numbers
 
        for chunk in file_info['chunks']:
 
            _html_empty = False
 
            for change in chunk:
 
                _html.append('''<tr class="%(lc)s %(action)s">\n''' % {
 
                    'lc': line_class,
 
                    'action': change['action']
 
                })
 
                if change['old_lineno'] or change['new_lineno']:
 
                    ###########################################################
 
                    # OLD LINE NUMBER
 
                    ###########################################################
 
                    anchor_old = "%(filename)s_o%(oldline_no)s" % {
 
                        'filename': _safe_id(file_info['filename']),
 
                        'oldline_no': change['old_lineno']
 
                    }
 
                    anchor_old_id = ''
 
                    if change['old_lineno']:
 
                        anchor_old_id = 'id="%s"' % anchor_old
 
                    _html.append('''\t<td %(a_id)s class="%(olc)s">''' % {
 
                        'a_id': anchor_old_id,
 
                        'olc': old_lineno_class,
 
                    })
 
                    _html.append('''<a href="%(url)s" data-pseudo-content="%(label)s"></a>''' % {
 
                        'label': change['old_lineno'],
 
@@ -107,53 +106,52 @@ def as_html(parsed_lines, table_class='c
 
                    _html.append('''</td>\n''')
 
                    ###########################################################
 
                    # NEW LINE NUMBER
 
                    ###########################################################
 
                    anchor_new = "%(filename)s_n%(newline_no)s" % {
 
                        'filename': _safe_id(file_info['filename']),
 
                        'newline_no': change['new_lineno']
 
                    }
 
                    anchor_new_id = ''
 
                    if change['new_lineno']:
 
                        anchor_new_id = 'id="%s"' % anchor_new
 
                    _html.append('''\t<td %(a_id)s class="%(nlc)s">''' % {
 
                        'a_id': anchor_new_id,
 
                        'nlc': new_lineno_class
 
                    })
 
                    _html.append('''<a href="%(url)s" data-pseudo-content="%(label)s"></a>''' % {
 
                        'label': change['new_lineno'],
 
                        'url': '#%s' % anchor_new,
 
                    })
 
                    _html.append('''</td>\n''')
 
                else:
 
                    ###########################################################
 
                    # NO LINE NUMBER
 
                    ###########################################################
 
                    anchor = "%(filename)s_%(count_no_lineno)s" % {
 
                    anchor = "%(filename)s_%(context_lineno)s" % {
 
                        'filename': _safe_id(file_info['filename']),
 
                        'count_no_lineno': count_no_lineno,
 
                        'context_lineno': change['context_lineno'],
 
                    }
 
                    count_no_lineno += 1
 
                    _html.append('''\t<td id="%(anchor)s" class="%(olc)s" colspan="2">''' % {
 
                        'anchor': anchor,
 
                        'olc': no_lineno_class,
 
                    })
 
                    _html.append('''</td>\n''')
 
                ###########################################################
 
                # CODE
 
                ###########################################################
 
                _html.append('''\t<td class="%(cc)s">''' % {
 
                    'cc': code_class,
 
                })
 
                _html.append('''\n\t\t<div class="add-bubble"><div>&nbsp;</div></div><pre>%(code)s</pre>\n''' % {
 
                    'code': change['line']
 
                })
 

	
 
                _html.append('''\t</td>''')
 
                _html.append('''\n</tr>\n''')
 
    _html.append('''</table>''')
 
    if _html_empty:
 
        return None
 
    return ''.join(_html)
 

	
 

	
 
def wrap_to_table(html):
 
@@ -374,48 +372,56 @@ class DiffProcessor(object):
 
                # Git binary patch
 
                if head['bin_patch']:
 
                    stats['ops'][BIN_FILENODE] = 'binary diff not shown'
 
                chunks = []
 

	
 
            if op == 'removed' and chunks:
 
                # a way of seeing deleted content could perhaps be nice - but
 
                # not with the current UI
 
                chunks = []
 

	
 
            # show helpful additional texts for mode change and renaming, but not for plain 'modified file'
 
            msgs = [
 
                {
 
                    'old_lineno': '',
 
                    'new_lineno': '',
 
                    'action': 'context',
 
                    'line': msg,
 
                }
 
                for op_, msg in stats['ops'].items()
 
                if op_ != MOD_FILENODE
 
            ]
 
            if msgs:
 
                chunks.insert(0, msgs)
 

	
 
            # enumerate 'context' lines that don't have new/old line numbers so they can be commented on
 
            context_lineno = 0
 
            for chunk in chunks:
 
                for change in chunk:
 
                    if not change['old_lineno'] and not change['new_lineno']:
 
                        change['context_lineno'] = context_lineno
 
                        context_lineno += 1
 

	
 
            _files.append({
 
                'old_filename':     head['a_path'],
 
                'filename':         head['b_path'],
 
                'old_revision':     head['a_blob_id'],
 
                'new_revision':     head['b_blob_id'],
 
                'chunks':           chunks,
 
                'operation':        op,
 
                'stats':            stats,
 
            })
 

	
 
        if not html:
 
            return _files
 

	
 
        for diff_data in _files:
 
            for chunk in diff_data['chunks']:
 
                for change in chunk:
 
                    change['line'] = _escaper(change['line'])
 
                # highlight inline changes when one del is followed by one add
 
                lineiter = iter(chunk)
 
                try:
 
                    peekline = next(lineiter)
 
                    while True:
 
                        # find a first del line
 
                        while peekline['action'] != 'del':
0 comments (0 inline, 0 general)