Skip to content

Commit 36f2da3

Browse files
committed
Add simple range markers, fix Tab switching
Can now switch between tabs, and can add simple range markers over text and to the right of the numbers.
1 parent 9a6a935 commit 36f2da3

12 files changed

+260
-77
lines changed

demo_imgui/main.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ struct ZepContainer : public IZepComponent
115115
{
116116
ZepBuffer* pBuffer = spEditor->AddBuffer("shader.vert");
117117
pBuffer->SetText(shader.c_str());
118+
118119
}
119120
}
120121

src/buffer.cpp

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
#include <cctype>
22
#include <cstdint>
33
#include <cstdlib>
4+
#include <algorithm>
5+
#include <regex>
46

57
#include "buffer.h"
68
#include "mcommon/file/file.h"
79
#include "mcommon/string/stringutils.h"
810

9-
#include <algorithm>
10-
#include <regex>
11-
1211
#include "mcommon/logger.h"
1312

1413
namespace
@@ -856,4 +855,33 @@ void ZepBuffer::SetTheme(std::shared_ptr<ZepTheme> spTheme)
856855
m_spOverrideTheme = spTheme;
857856
}
858857

858+
BufferRange ZepBuffer::GetSelection() const
859+
{
860+
return m_selection;
861+
}
862+
863+
void ZepBuffer::SetSelection(const BufferRange& selection)
864+
{
865+
m_selection = selection;
866+
if (m_selection.first > m_selection.second)
867+
{
868+
std::swap(m_selection.first, m_selection.second);
869+
}
870+
}
871+
872+
void ZepBuffer::AddRangeMarker(const RangeMarker& marker)
873+
{
874+
m_rangeMarkers.emplace_back(marker);
875+
}
876+
877+
void ZepBuffer::ClearRangeMarkers()
878+
{
879+
m_rangeMarkers.clear();
880+
}
881+
882+
const tRangeMarkers& ZepBuffer::GetRangeMarkers() const
883+
{
884+
return m_rangeMarkers;
885+
}
886+
859887
} // namespace Zep

src/buffer.h

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#pragma once
22

3-
#include "editor.h"
43
#include <mcommon/file/file.h>
4+
#include "editor.h"
5+
#include "theme.h"
56

67
#include <set>
78
#include <shared_mutex>
@@ -15,6 +16,7 @@ namespace Zep
1516

1617
class ZepSyntax;
1718
class ZepTheme;
19+
enum class ThemeColor;
1820

1921
enum class SearchDirection
2022
{
@@ -57,8 +59,18 @@ enum class LineLocation
5759
using BufferLocation = long;
5860
using BufferRange = std::pair<BufferLocation, BufferLocation>;
5961

60-
const long InvalidOffset = -1;
62+
struct RangeMarker
63+
{
64+
long bufferLine = -1;
65+
BufferRange range;
66+
ThemeColor color;
67+
std::string name;
68+
std::string description;
69+
};
6170

71+
using tRangeMarkers = std::vector<RangeMarker>;
72+
73+
const long InvalidOffset = -1;
6274
extern const char* Msg_Buffer;
6375

6476
// A really big cursor move; which will likely clamp
@@ -165,6 +177,13 @@ class ZepBuffer : public ZepComponent
165177
ZepTheme& GetTheme() const;
166178
void SetTheme(std::shared_ptr<ZepTheme> spTheme);
167179

180+
void SetSelection(const BufferRange& sel);
181+
BufferRange GetSelection() const;
182+
183+
void AddRangeMarker(const RangeMarker& marker);
184+
void ClearRangeMarkers();
185+
const tRangeMarkers& GetRangeMarkers() const;
186+
168187
private:
169188
// Internal
170189
GapBuffer<utf8>::const_iterator SearchWord(uint32_t searchType, GapBuffer<utf8>::const_iterator itrBegin, GapBuffer<utf8>::const_iterator itrEnd, SearchDirection dir) const;
@@ -184,6 +203,9 @@ class ZepBuffer : public ZepComponent
184203
uint32_t m_fileFlags = 0;
185204
fs::path m_filePath;
186205
std::shared_ptr<ZepTheme> m_spOverrideTheme;
206+
207+
BufferRange m_selection;
208+
tRangeMarkers m_rangeMarkers;
187209
};
188210

189211
// Notification payload

src/editor.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,39 @@ void ZepEditor::ResetCursorTimer()
155155
timer_restart(m_cursorTimer);
156156
}
157157

158+
void ZepEditor::NextTabWindow()
159+
{
160+
auto itr = std::find(m_tabWindows.begin(), m_tabWindows.end(), m_pActiveTabWindow);
161+
if (itr != m_tabWindows.end())
162+
itr++;
163+
164+
if (itr == m_tabWindows.end())
165+
{
166+
itr = m_tabWindows.begin();
167+
}
168+
m_pActiveTabWindow = *itr;
169+
}
170+
171+
void ZepEditor::PreviousTabWindow()
172+
{
173+
auto itr = std::find(m_tabWindows.begin(), m_tabWindows.end(), m_pActiveTabWindow);
174+
if (itr == m_tabWindows.end())
175+
{
176+
return;
177+
}
178+
179+
if (itr == m_tabWindows.begin())
180+
{
181+
itr = m_tabWindows.end() - 1;
182+
}
183+
else
184+
{
185+
itr--;
186+
}
187+
188+
m_pActiveTabWindow = *itr;
189+
}
190+
158191
void ZepEditor::SetCurrentTabWindow(ZepTabWindow* pTabWindow)
159192
{
160193
m_pActiveTabWindow = pTabWindow;

src/editor.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,8 @@ class ZepEditor
186186

187187
// Tab windows
188188
using tTabWindows = std::vector<ZepTabWindow*>;
189+
void NextTabWindow();
190+
void PreviousTabWindow();
189191
void SetCurrentTabWindow(ZepTabWindow* pTabWindow);
190192
ZepTabWindow* GetActiveTabWindow() const;
191193
ZepTabWindow* AddTabWindow();

src/mode_standard.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ void ZepMode_Standard::AddKeyPress(uint32_t key, uint32_t modifierKeys)
390390

391391
if (m_currentMode == EditorMode::Visual)
392392
{
393-
GetCurrentWindow()->SetSelectionRange(m_visualBegin, m_visualEnd);
393+
buffer.SetSelection(BufferRange{m_visualBegin, m_visualEnd});
394394
}
395395
}
396396

src/mode_vim.cpp

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,7 @@ bool ZepMode_Vim::GetCommand(CommandContext& context)
405405
{
406406
auto bufferCursor = GetCurrentWindow()->GetBufferCursor();
407407
auto pWindow = GetCurrentWindow();
408+
auto& buffer = GetCurrentWindow()->GetBuffer();
408409

409410
// Motion
410411
if (context.command == "$")
@@ -422,6 +423,17 @@ bool ZepMode_Vim::GetCommand(CommandContext& context)
422423
GetCurrentWindow()->SetBufferCursor(context.buffer.GetLinePos(bufferCursor, LineLocation::LineFirstGraphChar));
423424
return true;
424425
}
426+
// Moving between tabs
427+
else if (context.command == "H" && (context.modifierKeys & ModifierKey::Shift))
428+
{
429+
GetEditor().PreviousTabWindow();
430+
return true;
431+
}
432+
else if (context.command == "L" && (context.modifierKeys & ModifierKey::Shift))
433+
{
434+
GetEditor().NextTabWindow();
435+
return true;
436+
}
425437
// Moving between splits
426438
else if (context.command == "j" && (context.modifierKeys & ModifierKey::Ctrl))
427439
{
@@ -999,10 +1011,29 @@ bool ZepMode_Vim::GetCommand(CommandContext& context)
9991011
GetEditor().SetCommandText(str.str());
10001012
return true;
10011013
}
1002-
else if (context.command == ":tabedit")
1014+
else if (context.command.find(":tabedit") == 0)
10031015
{
10041016
auto pTab = GetEditor().AddTabWindow();
1005-
pTab->AddWindow(&GetEditor().GetActiveTabWindow()->GetActiveWindow()->GetBuffer(), nullptr, true);
1017+
auto strTok = string_split(context.command, " ");
1018+
if (strTok.size() > 1)
1019+
{
1020+
if (strTok[1] == "%")
1021+
{
1022+
pTab->AddWindow(&GetEditor().GetActiveTabWindow()->GetActiveWindow()->GetBuffer(), nullptr, true);
1023+
}
1024+
else
1025+
{
1026+
auto fname = strTok[1];
1027+
auto pBuffer = GetEditor().AddBuffer(fname);
1028+
pBuffer->Load(fname);
1029+
pTab->AddWindow(pBuffer, nullptr, true);
1030+
}
1031+
}
1032+
else
1033+
{
1034+
pTab->AddWindow(GetEditor().AddBuffer("Empty"), nullptr, true);
1035+
}
1036+
GetEditor().SetCurrentTabWindow(pTab);
10061037
}
10071038
else if (context.command == ":vsplit")
10081039
{
@@ -1051,6 +1082,34 @@ bool ZepMode_Vim::GetCommand(CommandContext& context)
10511082
{
10521083
pWindow->ToggleFlag(WindowFlags::ShowCR);
10531084
}
1085+
else if (context.command.find(":ZTestMarkers") == 0)
1086+
{
1087+
int markerType = 0;
1088+
auto strTok = string_split(context.command, " ");
1089+
if (strTok.size() > 1)
1090+
{
1091+
markerType = std::stoi(strTok[1]);
1092+
}
1093+
RangeMarker marker;
1094+
long start, end;
1095+
start = buffer.GetLinePos(bufferCursor, LineLocation::LineFirstGraphChar);
1096+
end = buffer.GetLinePos(bufferCursor, LineLocation::LineLastGraphChar) + 1;
1097+
marker.range = BufferRange{start, end};
1098+
switch (markerType)
1099+
{
1100+
case 1:
1101+
marker.color = ThemeColor::Warning;
1102+
marker.name = "Warning";
1103+
marker.name = "This is an example warning mark";
1104+
break;
1105+
case 0:
1106+
default:
1107+
marker.color = ThemeColor::Error;
1108+
marker.name = "Error";
1109+
marker.name = "This is an example error mark";
1110+
}
1111+
buffer.AddRangeMarker(marker);
1112+
}
10541113
else if (context.command == ":ZThemeToggle")
10551114
{
10561115
// An easy test command to check per-buffer themeing
@@ -1206,7 +1265,7 @@ void ZepMode_Vim::UpdateVisualSelection()
12061265
{
12071266
m_visualEnd = GetCurrentWindow()->GetBuffer().LocationFromOffsetByChars(GetCurrentWindow()->GetBufferCursor(), 1);
12081267
}
1209-
GetCurrentWindow()->SetSelectionRange(m_visualBegin, m_visualEnd);
1268+
GetCurrentWindow()->GetBuffer().SetSelection(BufferRange{m_visualBegin, m_visualEnd});
12101269
}
12111270
}
12121271
void ZepMode_Vim::AddKeyPress(uint32_t key, uint32_t modifierKeys)

src/tab_window.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,11 @@ void ZepTabWindow::CloseActiveWindow()
186186
{
187187
RemoveWindow(m_pActiveWindow);
188188
}
189-
SetDisplayRegion(m_lastRegionRect, true);
189+
190+
if (!m_windows.empty())
191+
{
192+
SetDisplayRegion(m_lastRegionRect, true);
193+
}
190194
}
191195

192196
void ZepTabWindow::RemoveWindow(ZepWindow* pWindow)

src/theme.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#include "theme.h"
21
#include "editor.h"
32
#include "syntax.h"
3+
#include "theme.h"
44

55
namespace Zep
66
{
@@ -64,6 +64,10 @@ void ZepTheme::SetDarkTheme()
6464
m_colors[ThemeColor::Keyword] = NVec4f(0.1f, 1.0f, 1.0f, 1.0f);
6565
m_colors[ThemeColor::Integer] = NVec4f(0.1f, 1.0f, 1.0f, 1.0f);
6666
m_colors[ThemeColor::Whitespace] = NVec4f(0.15f, .2f, .15f, 1.0f);
67+
68+
m_colors[ThemeColor::Error] = NVec4f(0.65f, .2f, .15f, 1.0f);
69+
m_colors[ThemeColor::Warning] = NVec4f(0.15f, .2f, .65f, 1.0f);
70+
m_colors[ThemeColor::Info] = NVec4f(0.15f, .6f, .15f, 1.0f);
6771
}
6872

6973
void ZepTheme::SetLightTheme()
@@ -92,6 +96,10 @@ void ZepTheme::SetLightTheme()
9296
m_colors[ThemeColor::Keyword] = NVec4f(0.1f, .2f, .3f, 1.0f);
9397
m_colors[ThemeColor::Integer] = NVec4f(0.1f, .3f, .2f, 1.0f);
9498
m_colors[ThemeColor::Whitespace] = NVec4f(0.15f, .2f, .15f, 1.0f);
99+
100+
m_colors[ThemeColor::Error] = NVec4f(0.89f, .2f, .15f, 1.0f);
101+
m_colors[ThemeColor::Warning] = NVec4f(0.15f, .2f, .89f, 1.0f);
102+
m_colors[ThemeColor::Info] = NVec4f(0.15f, .85f, .15f, 1.0f);
95103
}
96104
NVec4f ZepTheme::GetUniqueColor(uint32_t index) const
97105
{

src/theme.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#pragma once
22

3-
#include "editor.h"
4-
#include "syntax.h"
3+
#include "math/math.h"
54

65
namespace Zep
76
{
@@ -31,7 +30,10 @@ enum class ThemeColor
3130
Comment,
3231
Whitespace,
3332
HiddenChar,
34-
Parenthesis
33+
Parenthesis,
34+
Error,
35+
Warning,
36+
Info
3537
};
3638

3739
enum class ThemeType

0 commit comments

Comments
 (0)