Improve open command
This commit is contained in:
@@ -6,6 +6,7 @@ struct FuzzyPair {
|
||||
int64_t FuzzyCloserWordBegin = 5;
|
||||
int64_t FuzzyConsecutiveMultiplier = 3;
|
||||
int64_t FuzzyRate(String16 string, String16 with) {
|
||||
if (with.len == 0) return 0;
|
||||
int64_t points = 0;
|
||||
int64_t consecutive = 0;
|
||||
int64_t with_i = 0;
|
||||
|
||||
@@ -123,6 +123,38 @@ void Command_SelectEntireBuffer(View *view) {
|
||||
view->carets[0] = MakeCaret(0, buffer->len);
|
||||
}
|
||||
|
||||
void Command_EvalLua(View *view, String16 string) {
|
||||
Scratch scratch;
|
||||
Buffer *buffer = GetBuffer(view->buffer_id);
|
||||
String16 eval_result = EvalString(scratch, string);
|
||||
|
||||
if (eval_result.len) {
|
||||
Command_SelectEntireBuffer(view);
|
||||
Command_Replace(view, eval_result);
|
||||
Command_SelectRangeOneCursor(view, {});
|
||||
Command_Replace(view, L"\n");
|
||||
Command_SelectRangeOneCursor(view, {});
|
||||
} else {
|
||||
{
|
||||
Window *window = GetWindow(GetLastActiveWindow());
|
||||
View *view = GetView(window->active_view);
|
||||
SetActiveWindow(window->id);
|
||||
Command_Replace(view, eval_result);
|
||||
}
|
||||
|
||||
Range range = GetLineRangeWithoutNL(*buffer, 0);
|
||||
Command_SelectRangeOneCursor(view, range);
|
||||
Command_Replace(view, {});
|
||||
}
|
||||
}
|
||||
|
||||
void Command_EvalLua(View *view) {
|
||||
Buffer *buffer = GetBuffer(view->buffer_id);
|
||||
Int line = PosToLine(*buffer, GetFront(view->carets[0]));
|
||||
String16 string = GetLineStringWithoutNL(*buffer, line);
|
||||
Command_EvalLua(view, string);
|
||||
}
|
||||
|
||||
void HandleActiveWindowBindings(Window *window) {
|
||||
View &view = *GetActiveView(window);
|
||||
Buffer *buffer = GetBuffer(view.buffer_id);
|
||||
@@ -310,14 +342,17 @@ void HandleActiveWindowBindings(Window *window) {
|
||||
|
||||
if (window->fuzzy_search && search) {
|
||||
Scratch scratch;
|
||||
String16 first_line_string = GetLineString(*buffer, 0);
|
||||
String16 first_line_string = GetLineStringWithoutNL(*buffer, 0);
|
||||
Array<FuzzyPair> ratings = FuzzySearchLines(scratch, buffer, 1, buffer->line_starts.len, first_line_string);
|
||||
|
||||
Buffer *temp_buffer = CreateTempBuffer(scratch, buffer->cap);
|
||||
ReplaceText(temp_buffer, GetEndAsRange(*temp_buffer), first_line_string);
|
||||
ReplaceText(temp_buffer, GetEndAsRange(*temp_buffer), L"\n");
|
||||
For(ratings) {
|
||||
String16 s = GetLineString(*buffer, it.index);
|
||||
String16 s = GetLineStringWithoutNL(*buffer, it.index);
|
||||
if (s.len == 0) continue;
|
||||
ReplaceText(temp_buffer, GetEndAsRange(*temp_buffer), s);
|
||||
ReplaceText(temp_buffer, GetEndAsRange(*temp_buffer), L"\n");
|
||||
}
|
||||
|
||||
Caret caret = view.carets[0];
|
||||
@@ -328,29 +363,7 @@ void HandleActiveWindowBindings(Window *window) {
|
||||
|
||||
if (window->execute_line) {
|
||||
if (Press(KEY_ENTER)) {
|
||||
Scratch scratch;
|
||||
Int line = PosToLine(*buffer, GetFront(view.carets[0]));
|
||||
String16 string = GetLineStringWithoutNL(*buffer, line);
|
||||
String16 eval_result = EvalString(scratch, string);
|
||||
|
||||
if (Ctrl() && eval_result.len) {
|
||||
Command_SelectEntireBuffer(&view);
|
||||
Command_Replace(&view, eval_result);
|
||||
Command_SelectRangeOneCursor(&view, {});
|
||||
Command_Replace(&view, L"\n");
|
||||
Command_SelectRangeOneCursor(&view, {});
|
||||
} else {
|
||||
{
|
||||
Window *window = GetWindow(GetLastActiveWindow());
|
||||
View *view = GetView(window->active_view);
|
||||
SetActiveWindow(window->id);
|
||||
Command_Replace(view, eval_result);
|
||||
}
|
||||
|
||||
Range range = GetLineRangeWithoutNL(*buffer, 0);
|
||||
Command_SelectRangeOneCursor(&view, range);
|
||||
Command_Replace(&view, {});
|
||||
}
|
||||
Command_EvalLua(&view);
|
||||
}
|
||||
} else {
|
||||
if (CtrlPress(KEY_ENTER)) {
|
||||
|
||||
@@ -1,29 +1,43 @@
|
||||
lua_State *LuaState = NULL;
|
||||
String16 LuaCommandResult = {};
|
||||
|
||||
int LuaOpenFile(lua_State *L) {
|
||||
const char *text = luaL_checkstring(L, 1);
|
||||
Window *window = GetWindow(GetLastActiveWindow());
|
||||
View *view = ViewOpenFile(window, text);
|
||||
SetActiveWindow(window->id);
|
||||
return 0; // number of results
|
||||
}
|
||||
|
||||
int LuaListFiles(lua_State *L) {
|
||||
const char *text = luaL_checkstring(L, 1);
|
||||
String path = text;
|
||||
|
||||
String ListFiles(String path) {
|
||||
Scratch scratch;
|
||||
Array<String> strings = {scratch};
|
||||
Add(&strings, String{"open \"..\""});
|
||||
for (FileIter iter = IterateFiles(scratch, path); IsValid(iter); Advance(&iter)) {
|
||||
if (iter.is_directory) continue;
|
||||
String string = Format(scratch, "open \"%.*s\"", FmtString(iter.absolute_path));
|
||||
Add(&strings, string);
|
||||
}
|
||||
Add(&strings, Format(scratch, "working dir = %.*s", FmtString(WorkingDir)));
|
||||
|
||||
String result = Merge(scratch, strings, "\n");
|
||||
lua_pushlstring(LuaState, result.data, result.len);
|
||||
return 1;
|
||||
return result;
|
||||
}
|
||||
|
||||
int LuaOpen(lua_State *L) {
|
||||
Scratch scratch;
|
||||
String path = luaL_checkstring(L, 1);
|
||||
if (StartsWith(path, "./")) {
|
||||
path = Skip(path, 2);
|
||||
path = Format(scratch, "%.*s/%.*s", FmtString(WorkingDir), FmtString(path));
|
||||
} else if (IsAbsolute(path)) {
|
||||
// dont do anything
|
||||
} else {
|
||||
path = Format(scratch, "%.*s/%.*s", FmtString(WorkingDir), FmtString(path));
|
||||
}
|
||||
|
||||
if (IsDir(path)) {
|
||||
WorkingDir = GetAbsolutePath(GetSystemAllocator(), path);
|
||||
String files = ListFiles(WorkingDir);
|
||||
lua_pushlstring(LuaState, files.data, files.len);
|
||||
return 1;
|
||||
} else {
|
||||
Window *window = GetWindow(GetLastActiveWindow());
|
||||
View *view = ViewOpenFile(window, path);
|
||||
SetActiveWindow(window->id);
|
||||
return 0; // number of results
|
||||
}
|
||||
}
|
||||
|
||||
int LuaListOpenBuffers(lua_State *L) {
|
||||
@@ -75,7 +89,7 @@ void InitLua() {
|
||||
LuaState = luaL_newstate();
|
||||
luaL_openlibs(LuaState);
|
||||
|
||||
lua_pushcfunction(LuaState, LuaOpenFile);
|
||||
lua_pushcfunction(LuaState, LuaOpen);
|
||||
lua_setglobal(LuaState, "open");
|
||||
|
||||
lua_pushcfunction(LuaState, LuaListOpenBuffers);
|
||||
@@ -86,9 +100,6 @@ void InitLua() {
|
||||
|
||||
lua_pushcfunction(LuaState, LuaListWindows);
|
||||
lua_setglobal(LuaState, "list_windows");
|
||||
|
||||
lua_pushcfunction(LuaState, LuaListFiles);
|
||||
lua_setglobal(LuaState, "list_files");
|
||||
}
|
||||
|
||||
String16 EvalString(Allocator allocator, String16 string16) {
|
||||
|
||||
@@ -92,27 +92,30 @@ View *FindViewWithBufferName(String name) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// path should be already resolved to absolute
|
||||
Buffer *BufferOpenFile(String path) {
|
||||
Scratch scratch;
|
||||
String abs_path = GetAbsolutePath(scratch, path);
|
||||
|
||||
Buffer *possible_buffer = GetBuffer(abs_path);
|
||||
if (!IsNull(possible_buffer)) return possible_buffer;
|
||||
Assert(IsAbsolute(path));
|
||||
|
||||
Allocator sys_allocator = GetSystemAllocator();
|
||||
Scratch scratch;
|
||||
|
||||
Buffer *buffer = GetBuffer(path);
|
||||
if (!IsNull(buffer)) {
|
||||
return buffer;
|
||||
}
|
||||
|
||||
if (!FileExists(path)) {
|
||||
String dir = ChopLastSlash(abs_path);
|
||||
String dir = ChopLastSlash(path);
|
||||
if (!IsDir(dir)) {
|
||||
return GetBuffer(NullBufferID);
|
||||
buffer = GetBuffer(NullBufferID);
|
||||
} else {
|
||||
path = Copy(sys_allocator, abs_path);
|
||||
Buffer *buffer = CreateBuffer(sys_allocator, path);
|
||||
return buffer;
|
||||
path = Copy(sys_allocator, path);
|
||||
buffer = CreateBuffer(sys_allocator, path);
|
||||
}
|
||||
} else {
|
||||
path = Copy(sys_allocator, abs_path);
|
||||
String string = ReadFile(scratch, path);
|
||||
Buffer *buffer = CreateBuffer(sys_allocator, path, string.len * 4);
|
||||
path = Copy(sys_allocator, path);
|
||||
String string = ReadFile(scratch, path);
|
||||
buffer = CreateBuffer(sys_allocator, path, string.len * 4);
|
||||
|
||||
for (Int i = 0; i < string.len;) {
|
||||
if (string.data[i] == '\r') {
|
||||
@@ -146,8 +149,8 @@ Buffer *BufferOpenFile(String path) {
|
||||
}
|
||||
}
|
||||
UpdateLines(buffer, {}, String16{(wchar_t *)buffer->data, buffer->len});
|
||||
return buffer;
|
||||
}
|
||||
return buffer;
|
||||
}
|
||||
|
||||
View *_WindowOpenViewAndBuffer(Window *window, String name, bool set_active = true) {
|
||||
|
||||
@@ -32,6 +32,7 @@
|
||||
|
||||
/*
|
||||
- fix pathing in open, make it predictable
|
||||
- update info bar until '|' if tghere is no sign delete whole
|
||||
|
||||
- Save file (utf16->utf8)
|
||||
- resize windows
|
||||
@@ -59,6 +60,7 @@ int main(void) {
|
||||
Arena Perm = {};
|
||||
InitArena(&Perm);
|
||||
|
||||
WorkingDir = GetWorkingDir(Perm);
|
||||
SetWindowState(FLAG_WINDOW_RESIZABLE | FLAG_WINDOW_HIGHDPI | FLAG_MSAA_4X_HINT);
|
||||
InitWindow(1280, 720, "hello :)");
|
||||
SetExitKey(KEY_F5);
|
||||
@@ -131,13 +133,10 @@ int main(void) {
|
||||
w->dont_save_in_active_window_history = true;
|
||||
Buffer *b = CreateBuffer(sys_allocator, "*commands*");
|
||||
View *v = CreateView(b->id);
|
||||
ReplaceText(b, GetEndAsRange(*b), L"\n");
|
||||
ReplaceText(b, GetEndAsRange(*b), L"open \"C:/Work/text_editor/src/text_editor/text_editor.cpp\"\n");
|
||||
ReplaceText(b, GetEndAsRange(*b), L"open \"C:/Work/text_editor/src/text_editor/text_editor.h\"\n");
|
||||
ReplaceText(b, GetEndAsRange(*b), L"open \"C:/Work/text_editor/src/text_editor/commands.cpp\"\n");
|
||||
ReplaceText(b, GetEndAsRange(*b), L"open \"C:/Work/text_editor/src/text_editor/commands_clipboard.cpp\"\n");
|
||||
AddView(w, v->id);
|
||||
CommandWindowID = w->id;
|
||||
|
||||
Command_EvalLua(v, L"open \"./\"");
|
||||
}
|
||||
|
||||
while (!WindowShouldClose()) {
|
||||
|
||||
@@ -106,6 +106,7 @@ Int FontCharSpacing;
|
||||
Int FrameID;
|
||||
Int LastFrameIDWhenScrolled = -1;
|
||||
|
||||
String WorkingDir;
|
||||
Int LastFrameIDWhenSwitchedActiveWindow;
|
||||
Array<WindowID> WindowSwitchHistory; // @todo: probably better as a circular buffer
|
||||
WindowID ActiveWindow;
|
||||
@@ -114,6 +115,7 @@ String16 EvalString(Allocator allocator, String16 string16);
|
||||
Rect2I GetVisibleCells(Window &window);
|
||||
void AfterEdit(View *view, Array<Edit> edits);
|
||||
Scroller ComputeScrollerRect(Window &window);
|
||||
void Command_EvalLua(View *view, String16 string);
|
||||
|
||||
inline ViewID AllocViewID() { return {ViewIDs.id++}; }
|
||||
inline WindowID AllocWindowID() { return {WindowIDs.id++}; }
|
||||
|
||||
Reference in New Issue
Block a user