2.6 KB、106行 生 Blame 履歴
1// TextDiff - A syntax highlighting text diff widget for Dear ImGui.
2// Copyright (c) 2024-2026 Johan A. Goossens. All rights reserved.
3//
4// This work is licensed under the terms of the MIT license.
5// For a copy, see <https://opensource.org/licenses/MIT>.
6
7
8#pragma once
9
10
11//
12// Include files
13//
14
15#include "TextEditor.h"
16
17
18//
19// TextDiff
20//
21
22class IMGUI_API TextDiff : public TextEditor {
23public:
24 // constructor
25 TextDiff();
26
27 // specify visual mode (combined is default)
28 inline void SetSideBySideMode(bool flag) { sideBySideMode = flag; }
29 inline bool GetSideBySideMode() const { return sideBySideMode; }
30
31 // specify the text to be compared (using UTF-8 encoded strings)
32 void SetText(const std::string_view& left, const std::string_view& right);
33
34 // specify a new language
35 void SetLanguage(const Language* l);
36
37 // specify the background color for added/deleted lines
38 inline void SetColors(ImU32 ac, ImU32 dc) { addedColor = ac; deletedColor = dc; }
39
40 // render the text editor in a Dear ImGui context
41 void Render(const char* title, const ImVec2& size=ImVec2(), bool border=false);
42
43private:
44 // rendering mode
45 bool sideBySideMode = false;
46
47 // the two documents being compared
48 TextEditor::Document leftDocument;
49 TextEditor::Document rightDocument;
50
51 // number of digits in line numbers
52 int leftLineNumberDigits;
53 int rightLineNumberDigits;
54
55 // status of text line
56 enum class LineStatus {
57 common,
58 added,
59 deleted
60 };
61
62 // information about a single visible line
63 class LineInfo {
64 public:
65 LineInfo(size_t l, size_t r, LineStatus s) : leftLine(l), rightLine(r), status(s) {}
66
67 size_t leftLine;
68 size_t rightLine;
69 LineStatus status;
70 };
71
72 std::vector<LineInfo> lineInfo;
73 bool updated = false;
74
75 // colors
76 ImU32 addedColor = IM_COL32(0, 150, 32, 128);
77 ImU32 deletedColor = IM_COL32(180, 0, 32, 128);
78
79 // side-by-side rendering parameters
80 float leftLineNumberWidth;
81 float rightLineNumberWidth;
82 float textColumnWidth;
83 float leftLineNumberPos;
84 float leftTextPos;
85 float rightLineNumberPos;
86 float rightTextPos;
87 float rightTextEnd;
88 float textScroll;
89
90 // split string into lines
91 void splitLines(std::vector<std::string_view>& result, const std::string_view& text);
92
93 // create a combined view of the two documents and their differences
94 void createCombinedView();
95
96 // line decorator
97 void decorateLine(TextEditor::Decorator& decorator);
98
99 // render the side-by-side mode and its parts
100 void renderSideBySide(const char* title, const ImVec2& size, bool border);
101 void renderSideBySideBackground();
102 void renderSideBySideText();
103 void renderSideBySideLine(float x, float y, TextEditor::Line& line);
104 void renderSideBySideTextScrollbars();
105 void renderSideBySideMiniMap();
106};