|
| 1 | +/* |
| 2 | +MIT License |
| 3 | +
|
| 4 | +Copyright (c) 2024-2026 Johan A. Goossens. All rights reserved. |
| 5 | +
|
| 6 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | +of this software and associated documentation files (the "Software"), to deal |
| 8 | +in the Software without restriction, including without limitation the rights |
| 9 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | +copies of the Software, and to permit persons to whom the Software is |
| 11 | +furnished to do so, subject to the following conditions: |
| 12 | +
|
| 13 | +The above copyright notice and this permission notice shall be included in all |
| 14 | +copies or substantial portions of the Software. |
| 15 | +
|
| 16 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | +SOFTWARE. |
| 23 | +*/ |
| 24 | + |
| 25 | +// TextDiff - A syntax highlighting text diff widget for Dear ImGui. |
| 26 | +// Copyright (c) 2024-2026 Johan A. Goossens. All rights reserved. |
| 27 | +// |
| 28 | +// This work is licensed under the terms of the MIT license. |
| 29 | +// For a copy, see <https://opensource.org/licenses/MIT>. |
| 30 | + |
| 31 | + |
| 32 | +#pragma once |
| 33 | + |
| 34 | + |
| 35 | +// |
| 36 | +// Include files |
| 37 | +// |
| 38 | + |
| 39 | +#include "TextEditor.h" |
| 40 | + |
| 41 | +// |
| 42 | +// TextDiff |
| 43 | +// |
| 44 | + |
| 45 | +class TextDiff : public TextEditor { |
| 46 | +public: |
| 47 | + // constructor |
| 48 | + TextDiff(); |
| 49 | + |
| 50 | + // specify visual mode (combined is default) |
| 51 | + inline void SetSideBySideMode(bool flag) { sideBySideMode = flag; } |
| 52 | + inline bool GetSideBySideMode() const { return sideBySideMode; } |
| 53 | + |
| 54 | + // specify the text to be compared (using UTF-8 encoded strings) |
| 55 | + void SetText(const std::string_view& left, const std::string_view& right); |
| 56 | + |
| 57 | + // specify a new language |
| 58 | + void SetLanguage(const Language* l); |
| 59 | + |
| 60 | + // specify the background color for added/deleted lines |
| 61 | + inline void SetColors(ImU32 ac, ImU32 dc) { addedColor = ac; deletedColor = dc; } |
| 62 | + |
| 63 | + // render the text editor in a Dear ImGui context |
| 64 | + void Render(const char* title, const ImVec2& size=ImVec2(), bool border=false); |
| 65 | + |
| 66 | + // block/hide certain API calls |
| 67 | + inline void SetReadOnlyEnabled(bool /* value */) {} |
| 68 | + inline void SetShowLineNumbersEnabled(bool /* value */) {} |
| 69 | + inline void SetShowMatchingBrackets(bool /* value */) {} |
| 70 | + |
| 71 | + inline void AddMarker(int /* line */, ImU32 /* lineNumberColor */, ImU32 /* textColor */, const std::string_view& /* lineNumberTooltip */, const std::string_view& /* textTooltip */) {} |
| 72 | + inline void ClearMarkers() {} |
| 73 | + |
| 74 | + inline void SetLineDecorator(float /* width */, std::function<void(Decorator& decorator)> /* callback */) {} |
| 75 | + inline void ClearLineDecorator() {} |
| 76 | + |
| 77 | + inline void SetLineNumberContextMenuCallback(std::function<void(int line)> /* callback */) {} |
| 78 | + inline void ClearLineNumberContextMenuCallback() {} |
| 79 | + |
| 80 | + inline void SetTextContextMenuCallback(std::function<void(int line, int column)> /* callback */) {} |
| 81 | + inline void ClearTextContextMenuCallback() {} |
| 82 | + |
| 83 | +private: |
| 84 | + // rendering mode |
| 85 | + bool sideBySideMode = false; |
| 86 | + |
| 87 | + // the two documents being compared |
| 88 | + TextEditor::Document leftDocument; |
| 89 | + TextEditor::Document rightDocument; |
| 90 | + |
| 91 | + // number of digits in line numbers |
| 92 | + int leftLineNumberDigits; |
| 93 | + int rightLineNumberDigits; |
| 94 | + |
| 95 | + // status of text line |
| 96 | + enum class LineStatus { |
| 97 | + common, |
| 98 | + added, |
| 99 | + deleted |
| 100 | + }; |
| 101 | + |
| 102 | + // information about a single visible line |
| 103 | + class LineInfo { |
| 104 | + public: |
| 105 | + LineInfo(size_t l, size_t r, LineStatus s) : leftLine(l), rightLine(r), status(s) {} |
| 106 | + |
| 107 | + size_t leftLine; |
| 108 | + size_t rightLine; |
| 109 | + LineStatus status; |
| 110 | + }; |
| 111 | + |
| 112 | + std::vector<LineInfo> lineInfo; |
| 113 | + bool updated = false; |
| 114 | + |
| 115 | + // colors |
| 116 | + ImU32 addedColor = IM_COL32(0, 150, 32, 128); |
| 117 | + ImU32 deletedColor = IM_COL32(180, 0, 32, 128); |
| 118 | + |
| 119 | + // side-by-side rendering parameters |
| 120 | + float leftLineNumberWidth; |
| 121 | + float rightLineNumberWidth; |
| 122 | + float textColumnWidth; |
| 123 | + float leftLineNumberPos; |
| 124 | + float leftTextPos; |
| 125 | + float rightLineNumberPos; |
| 126 | + float rightTextPos; |
| 127 | + float rightTextEnd; |
| 128 | + float textScroll; |
| 129 | + |
| 130 | + // split string into lines |
| 131 | + void splitLines(std::vector<std::string_view>& result, const std::string_view& text); |
| 132 | + |
| 133 | + // create a combined view of the two documents and their differences |
| 134 | + void createCombinedView(); |
| 135 | + |
| 136 | + // line decorator |
| 137 | + void decorateLine(TextEditor::Decorator& decorator); |
| 138 | + |
| 139 | + // render the side-by-side mode and its parts |
| 140 | + void renderSideBySide(const char* title, const ImVec2& size, bool border); |
| 141 | + void renderSideBySideBackground(); |
| 142 | + void renderSideBySideText(); |
| 143 | + void renderSideBySideLine(float x, float y, TextEditor::Line& line); |
| 144 | + void renderSideBySideTextScrollbars(); |
| 145 | + void renderSideBySideMiniMap(); |
| 146 | +}; |
0 commit comments