default file view mode and some colors now configurable in settings

This commit is contained in:
Vaclav Tvrdik
2026-10-10 12:04:26 +02:00
parent 2d9db745b0
commit f18bad79cd
8 changed files with 260 additions and 58 deletions

View File

@@ -94,6 +94,64 @@ def test_enter_skips_refetch_when_preview_is_current(monkeypatch):
assert shown == ["b.py", "a.py"]
@pytest.mark.parametrize("mode, all_mode", [("file_by_file", False), ("all_in_one", True), (None, False)])
def test_a_new_review_view_starts_in_the_default_view_mode(monkeypatch, mode, all_mode):
option(monkeypatch, "default_view_mode", mode)
monkeypatch.setattr(commands, "resolve_repo", lambda window: "/repo")
monkeypatch.setattr(commands, "find_review_view", lambda window: sublime.View())
_, st = commands.open_review(sublime.Window())
assert st.all_mode is all_mode
def test_panel_in_all_in_one_mode_scrolls_instead_of_loading_files(monkeypatch):
loads, focused = [], []
def fake_show_all(view, st, focus=None):
# load at once: what show_all's done does once the diffs land
st.token += 1
st.rendered_token = st.token
loads.append(st.change.path)
monkeypatch.setattr(commands, "show_all", fake_show_all)
monkeypatch.setattr(commands, "show_file", lambda view, st, c: pytest.fail("loaded a single file"))
monkeypatch.setattr(commands.render, "render_all", lambda view, st, focus=None: focused.append(focus))
st = state.ViewState("/repo", all_mode=True)
st.change = change("kept.py")
st.files = [st.change]
files = [change("a.py"), change("b.py")]
st.collapsed = {"b.py"}
window = sublime.Window()
commands.show_files_panel(window, sublime.View(), st, files, st.kept())
_, on_select, _, on_highlight, _ = window.panel
assert loads == ["a.py"]
# a highlight unfolds and scrolls to its file; Enter on it needs no redraw
on_highlight(1)
assert focused == ["b.py"] and st.change.path == "b.py" and "b.py" not in st.collapsed
on_select(1)
assert focused == ["b.py"] and loads == ["a.py"]
# Esc goes back to the kept file, still in the all-in-one view
on_select(-1)
assert st.all_mode and st.change.path == "kept.py" and loads == ["a.py", "kept.py"]
def test_esc_goes_back_to_the_kept_view_mode(monkeypatch):
calls = []
monkeypatch.setattr(commands, "show_file", lambda view, st, c: calls.append(("file", c.path)))
monkeypatch.setattr(commands, "show_all", lambda view, st, focus=None: calls.append(("all", st.change.path)))
st = state.ViewState("/repo")
st.change = change("kept.py")
st.files = [st.change]
kept = st.kept()
# the view switched to all-in-one after the snapshot; Esc still restores file-by-file
st.all_mode = True
window = sublime.Window()
commands.show_files_panel(window, sublime.View(), st, [change("a.py")], kept)
window.panel[1](-1)
assert calls[-1] == ("file", "kept.py") and not st.all_mode
def test_on_cancel_goes_back_to_commit_list(monkeypatch):
monkeypatch.setattr(commands, "show_file", lambda view, st, c: None)
st = state.ViewState("/repo")

View File

@@ -84,3 +84,66 @@ def test_a_hand_edited_override_is_never_clobbered(tmp_path):
assert "// mine" in path.read_text()
def colour_option(monkeypatch, name, value):
"""Set one of the colour settings for this test"""
from vdiff import utils
monkeypatch.setitem(sublime.load_settings(utils.SETTINGS_FILE), name, value)
def test_user_line_colours_get_rules_and_tint_the_token_layer(tmp_path, monkeypatch):
colour_option(monkeypatch, "deleted_line_background", "#ff000040")
colour_option(monkeypatch, "line_numbers_foreground", "#888888")
view = sublime.View()
palette.ensure(view)
rules = by_scope(tmp_path)
assert rules[palette.SCOPE_LINE_DEL] == {"scope": palette.SCOPE_LINE_DEL, "background": "#ff000040"}
assert palette.SCOPE_LINE_INS not in rules
# the token layer repaints deleted text, so it carries the same background
colour = view.styles["comment"]["foreground"]
assert rules[palette.scope_for("del", colour)]["background"] == "#ff000040"
# a foreground alone still gets a background, or sublime would fill the region with the foreground
numbers = rules[palette.SCOPE_LINE_NUMBERS]
assert numbers["foreground"] == "#888888" and numbers["background"]
def test_changing_a_colour_setting_rewrites_the_override(tmp_path, monkeypatch):
view = sublime.View()
palette.ensure(view)
assert palette.SCOPE_LINE_INS not in by_scope(tmp_path)
colour_option(monkeypatch, "inserted_line_background", "#00ff0040")
palette.ensure(view)
assert by_scope(tmp_path)[palette.SCOPE_LINE_INS]["background"] == "#00ff0040"
def test_render_uses_the_user_scopes_only_once_set_and_live(monkeypatch):
view = sublime.View()
# unset: the scheme's diff tint and the plain-text line numbers, as before
assert palette.line_scope(view, "del") == palette.SCOPE_DEL
assert palette.line_numbers_scope(view) == "text.plain"
colour_option(monkeypatch, "deleted_line_background", "#ff000040")
colour_option(monkeypatch, "line_numbers_background", "#222222")
# set, but the override is not loaded yet: an unknown scope would paint nothing
assert palette.line_scope(view, "del") == palette.SCOPE_DEL
view.override_loaded = True
assert palette.line_scope(view, "del") == palette.SCOPE_LINE_DEL
assert palette.line_scope(view, "ins") == palette.SCOPE_INS
assert palette.line_numbers_scope(view) == palette.SCOPE_LINE_NUMBERS
def test_file_header_colours_come_from_settings(tmp_path, monkeypatch):
palette.ensure(sublime.View())
rules = by_scope(tmp_path)
# unset: the built-in defaults
assert rules[palette.SCOPE_FILE_HEADER]["background"] == palette.FILE_HEADER_BACKGROUND
colour_option(monkeypatch, "file_header_background", "#202040")
colour_option(monkeypatch, "file_header_inserted_foreground", "#00ff00")
palette.ensure(sublime.View())
rules = by_scope(tmp_path)
assert rules[palette.SCOPE_FILE_HEADER]["background"] == "#202040"
# the counts sit on the heading, so they share its background
assert rules[palette.SCOPE_FILE_INS_COUNT]["background"] == "#202040"
assert rules[palette.SCOPE_FILE_INS_COUNT]["foreground"] == "#00ff00"
assert rules[palette.SCOPE_FILE_DEL_COUNT]["foreground"] == palette.FILE_DEL_FOREGROUND