| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | #pragma once |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | #include "TextEditor.h" |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | class IMGUI_API TextDiff : public TextEditor { |
| 23 | public: |
| 24 | |
| 25 | TextDiff(); |
| 26 | |
| 27 | |
| 28 | inline void SetSideBySideMode(bool flag) { sideBySideMode = flag; } |
| 29 | inline bool GetSideBySideMode() const { return sideBySideMode; } |
| 30 | |
| 31 | |
| 32 | void SetText(const std::string_view& left, const std::string_view& right); |
| 33 | |
| 34 | |
| 35 | void SetLanguage(const Language* l); |
| 36 | |
| 37 | |
| 38 | inline void SetColors(ImU32 ac, ImU32 dc) { addedColor = ac; deletedColor = dc; } |
| 39 | |
| 40 | |
| 41 | void Render(const char* title, const ImVec2& size=ImVec2(), bool border=false); |
| 42 | |
| 43 | private: |
| 44 | |
| 45 | bool sideBySideMode = false; |
| 46 | |
| 47 | |
| 48 | TextEditor::Document leftDocument; |
| 49 | TextEditor::Document rightDocument; |
| 50 | |
| 51 | |
| 52 | int leftLineNumberDigits; |
| 53 | int rightLineNumberDigits; |
| 54 | |
| 55 | |
| 56 | enum class LineStatus { |
| 57 | common, |
| 58 | added, |
| 59 | deleted |
| 60 | }; |
| 61 | |
| 62 | |
| 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 | |
| 76 | ImU32 addedColor = IM_COL32(0, 150, 32, 128); |
| 77 | ImU32 deletedColor = IM_COL32(180, 0, 32, 128); |
| 78 | |
| 79 | |
| 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 | |
| 91 | void splitLines(std::vector<std::string_view>& result, const std::string_view& text); |
| 92 | |
| 93 | |
| 94 | void createCombinedView(); |
| 95 | |
| 96 | |
| 97 | void decorateLine(TextEditor::Decorator& decorator); |
| 98 | |
| 99 | |
| 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 | }; |