Skip to content

Commit 9e7def7

Browse files
committed
Better Buffer management
Saturday morning; my favourite coding session ;) Better management of buffers by tracking file names and matching them to buffers. Fixed delete in command mode too.
1 parent 9a6a935 commit 9e7def7

File tree

11 files changed

+223
-163
lines changed

11 files changed

+223
-163
lines changed

demo_imgui/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ struct ZepContainer : public IZepComponent
113113
// Add a shader, as a default when no file - for the demo
114114
if (spEditor->GetBuffers().size() == 0)
115115
{
116-
ZepBuffer* pBuffer = spEditor->AddBuffer("shader.vert");
116+
ZepBuffer* pBuffer = spEditor->GetBuffer("shader.vert");
117117
pBuffer->SetText(shader.c_str());
118118
}
119119
}

demo_qt/mainwindow.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ MainWindow::MainWindow()
5656
}
5757
else
5858
{
59-
ZepBuffer* pBuffer = pWidget->GetEditor().AddBuffer("shader.vert");
59+
ZepBuffer* pBuffer = pWidget->GetEditor().GetBuffer("shader.vert");
6060
pBuffer->SetText(shader.c_str());
6161
}
6262

src/buffer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,7 @@ void ZepBuffer::LockRead()
536536
// the file path in case you want to write later
537537
void ZepBuffer::Load(const fs::path& path)
538538
{
539-
m_filePath = path;
539+
m_filePath = fs::canonical(path);
540540

541541
auto read = file_read(path);
542542
if (!read.empty())

src/editor.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,26 @@ void ZepEditor::SaveBuffer(ZepBuffer& buffer)
9191
SetCommandText(strText.str());
9292
}
9393

94+
ZepBuffer* ZepEditor::GetBuffer(const fs::path& filePath)
95+
{
96+
auto path = fs::canonical(filePath);
97+
for (auto& pBuffer : m_buffers)
98+
{
99+
if (fs::equivalent(pBuffer->GetFilePath(), path))
100+
{
101+
LOG(DEBUG) << "Found equivalent buffer for file: " << path.string();
102+
return pBuffer.get();
103+
}
104+
}
105+
106+
auto pBuffer = AddBuffer(path.filename().string());
107+
if (fs::exists(path))
108+
{
109+
pBuffer->Load(path);
110+
}
111+
return pBuffer;
112+
}
113+
94114
void ZepEditor::InitWithFileOrDir(const std::string& str)
95115
{
96116
fs::path startPath(str);
@@ -298,6 +318,7 @@ ZepBuffer* ZepEditor::AddBuffer(const std::string& str)
298318
// Adding a buffer immediately updates the window state, in case this is the first one
299319
UpdateWindowState();
300320

321+
LOG(DEBUG) << "Added buffer: " << str;
301322
return pBuffer.get();
302323
}
303324

src/editor.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <vector>
1313
#include "mcommon/math/math.h"
1414
#include "mcommon/animation/timer.h"
15+
#include "mcommon/file/file.h"
1516
#include "splits.h"
1617

1718
// Basic Architecture
@@ -166,9 +167,9 @@ class ZepEditor
166167
}
167168

168169
const tBuffers& GetBuffers() const;
169-
ZepBuffer* AddBuffer(const std::string& str);
170170
ZepBuffer* GetMRUBuffer() const;
171171
void SaveBuffer(ZepBuffer& buffer);
172+
ZepBuffer* GetBuffer(const fs::path& filePath);
172173

173174
void SetRegister(const std::string& reg, const Register& val);
174175
void SetRegister(const char reg, const Register& val);
@@ -218,6 +219,10 @@ class ZepEditor
218219

219220
ZepTheme& GetTheme() const;
220221

222+
private:
223+
// Call GetBuffer publicly, to stop creation of duplicate buffers refering to the same file
224+
ZepBuffer* AddBuffer(const std::string& bufferName);
225+
221226
private:
222227
IZepDisplay* m_pDisplay;
223228
std::set<IZepComponent*> m_notifyClients;

src/mcommon/file/mfilesystem.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,11 @@ inline bool exists(const path& path)
175175
return stat(path.c_str(), &buffer) == 0;
176176
}
177177

178+
inline bool equivalent(const path& a, const path& b)
179+
{
180+
return canonical(a).string() == canonical(b).string();
181+
}
182+
178183
inline path canonical(const path& input)
179184
{
180185
return path(ReplaceString(input.string(), "\\", "/"));

src/mode.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ enum class EditorMode
4747
None,
4848
Normal,
4949
Insert,
50-
Visual,
51-
Command
50+
Visual
5251
};
5352

5453
class ZepMode : public ZepComponent

0 commit comments

Comments
 (0)