Improve reopen buffer

This commit is contained in:
Krzosa Karol
2024-08-14 07:42:11 +02:00
parent 2b4a43ddc1
commit b60f846209
4 changed files with 22 additions and 19 deletions

View File

@@ -58,12 +58,8 @@ void Command_Paste(View *view) {
Buffer *buffer = GetBuffer(view->active_buffer);
const char *text = SDL_GetClipboardText();
defer { SDL_free((void *)text); };
String string_ = text;
Int cap = string_.len * 3;
wchar_t *string16_buffer = AllocArray(scratch, wchar_t, cap);
Int len = ConvertUTF8ToUTF16UnixLine(string_, string16_buffer, cap);
String16 string = {string16_buffer, len};
String16 string = ToUnixString16(scratch, text);
// Regular paste
if (string != SavedClipboardString || SavedClipboardCarets.len != view->carets.len) {

View File

@@ -118,7 +118,7 @@ int Lua_Open(lua_State *L) {
int Lua_Reopen(lua_State *L) {
BSet main = GetActiveMainSet();
ReopenBuffer(main.buffer);
ReopenBuffer(main.view, main.buffer);
return 0;
}

View File

@@ -280,6 +280,23 @@ Int ConvertUTF8ToUTF16UnixLine(String string, wchar_t *buffer, Int buffer_cap) {
return buffer_len;
}
String16 ToUnixString16(Allocator allocator, String string_) {
Int cap = string_.len * 3;
wchar_t *string16_buffer = AllocArray(allocator, wchar_t, cap);
Int len = ConvertUTF8ToUTF16UnixLine(string_, string16_buffer, cap);
String16 string = {string16_buffer, len};
return string;
}
void ReopenBuffer(View *view, Buffer *buffer) {
Scratch scratch;
String string = ReadFile(scratch, buffer->name);
if (string.len == 0) return;
String16 string16 = ToUnixString16(scratch, string);
Command_ReplaceWithoutMovingCarets(view, GetRange(buffer), string16);
buffer->file_mod_time = GetFileModTime(buffer->name);
}
// This function as name suggests tries to open a buffer,
// there is no name resolution here, path should already be resolved etc.
//
@@ -326,17 +343,6 @@ void ResetBuffer(Buffer *buffer) {
DeallocHistoryEntries(&buffer->undo_stack);
}
void ReopenBuffer(Buffer *buffer) {
Scratch scratch;
String string = ReadFile(scratch, buffer->name);
if (string.len) {
ResetBuffer(buffer);
buffer->len = ConvertUTF8ToUTF16UnixLine(string, buffer->str, buffer->cap);
buffer->file_mod_time = GetFileModTime(buffer->name);
UpdateLines(buffer, {}, String16{(wchar_t *)buffer->data, buffer->len});
}
}
View *OpenBufferView(String name) {
Buffer *buffer = BufferOpenFile(name);
View *view = CreateView(buffer->id);

View File

@@ -3,14 +3,15 @@
- ask user if he really wants to quit even though he has an unsaved buffer - popup window
- add plumb rules for some web stuff
- test the code editor: try writing in it, try browsing in it, create test tooling
- event serialization to lua object format
- Execute enclosure which is going to execute on every keypress, modification
- Store editor metadata in user accessible buffers? (read only)
- Find matches using grep, change things in that buffer then apply those changes to all items
- group history entries so the you can rollback through multiple ones at once and not waste time on skipping whitespace trimming or deleting every character
- Fuzzy search buffer which uses titlebar as query!
- Check if file exists in ApplyRules otherwise return null
- Store editor metadata in user accessible buffers? (read only)
- Add the event buffer with serialized events
- event serialization to lua object format
- Gotos, jumping between views should preserve cursors
- OnWindowCommand allow config user to overwrite the WindowCommand keybinding, introduce his own