Add a setting for mapping file extensions to syntax highlighters for diffs

Review Request #11821 — Created Sept. 24, 2021 and submitted — Latest diff uploaded

Information

Review Board
release-5.0.x

Reviewers

Previously, we guess what syntax highlighting to apply to files in the diff
viewer given their data and file extension. Sometimes this guess would be wrong,
and we have no way of setting what type of syntax highlighting gets applied to
files. This change adds a setting for mapping file extensions to pygments
lexers (the syntax highlighters), namely
reviewboard.admin.siteconfig.diffviewer_custom_pygments_lexers. If a mapping
is not defined for a certain file extension, we revert to the old behaviour
of guessing which lexer to use given the filename and file contents.

Adds reviewboard.admin.form_widgets.LexersMappingWidget which partly
handles the UI for modifying this mapping.

  • Created unit tests for the mapping in
    reviewboard.diffviewer.tests.test_raw_diff_chunk_generator.
    Ran all unit tests in reviewboard.diffviewer with success
  • Created the unit tests for the LexersMappingWidget in
    reviewboard.admin.tests.test_lexers_mapping_widget.
    Ran all unit tests in reviewboard.admin.tests with success.

Changes between revision 1 and 2

orig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

Commits

Summary ID Author
Add a setting for mapping file extensions to syntax highlighters for
diffs Adds a default mapping of file extensions to pygments lexers, which administrators may modify to define their own custom mapping. If a mapping is not defined for a certain file extension, we revert to the old behaviour of guessing which lexer to use given the filename and file contents.
3e4ddeda899f0e5d97f02ed070d7525662ee33c5 Michelle
Uses for else clause and .items() instead of .keys() for searching the
mapping
97a6bd3547ff2c3af0708640538d75ef4a55f1c8 Michelle
Logs an error if a mapped lexer does not exist in Pygments
13f279fa09f30803a02621e13192942b95e80460 Michelle
Removed trailing whitespace
d27cc3d581a53a7fd414db5efc820cba92ba9fb3 Michelle
Properly imports mapping from settings.py
8eb445508de1a38aae178bc59477b585f681bbd6 Michelle
fixed string formatting for logged error
dafacdae4c204636121480ab9b3821ffec9ff889 Michelle
Made CUSTOM_PYGMENTS_LEXERS a class attribute (this makes it easier
to modify during testing)
45754bd19bf4ae4ae6f9582e725557ddf0e3c976 Michelle
took out print statement from testing
5fb349b3f4d0adb8a4f0c0b25d3df3611a446220 Michelle
Added unit tests for apply_pygment, testing that default
custom mapping works and that mapping to a non existant class logs an error
c29591ba2ab98206ea65f12b248826d2e5ad5eac Michelle
Modified doc formatting and changed default test to instead
test if using a custom mapping overrides the guessing function to determine a lexer
f853203d6bcadd51d4e9dcfe9561607034023ab7 Michelle
reviewboard/settings.py
reviewboard/diffviewer/chunk_generator.py
reviewboard/diffviewer/tests/test_raw_diff_chunk_generator.py
Loading...