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