Experimenting with execution

This commit is contained in:
Krzosa Karol
2024-08-05 08:25:33 +02:00
parent da1def101b
commit c9aabdec68
7 changed files with 103 additions and 66 deletions

View File

@@ -257,7 +257,7 @@ function ExtractLineAndColumn(s)
end
function BufferNameExists(name)
buffers = ListBuffers()
buffers = GetBufferList()
for i = 1,#buffers do
buff = buffers[i]
if buff == name then

View File

@@ -29,8 +29,8 @@ int64_t FuzzyRate(String16 string, String16 with) {
}
Array<FuzzyPair> FuzzySearchLines(Allocator allocator, Buffer *buffer, Int line_min, Int line_max, String16 needle) {
Assert(line_min >= 0 && line_min < buffer->line_starts.len);
Assert(line_max >= 0 && line_max <= buffer->line_starts.len);
if (line_min < 0 || line_min >= buffer->line_starts.len) return {};
if (line_max < 0 || line_min > buffer->line_starts.len) return {};
Array<FuzzyPair> ratings = {allocator};
for (Int i = line_min; i < line_max; i += 1) {
String16 s = GetLineStringWithoutNL(*buffer, i);

View File

@@ -281,6 +281,7 @@ bool GlobalCommand(Event event) {
if (event.ctrl && Mouse(RIGHT)) {
GoBackToLastCrumb();
} else if (event.alt && Mouse(RIGHT)) {
}
// @todo: maybe move some of this stuff to window command ???
@@ -288,6 +289,23 @@ bool GlobalCommand(Event event) {
// - maybe just do the check if active window is matching the DocumentSelected window
// - if scrollbar selected then don't invoke window command
if (event.alt && Mouse(LEFT)) {
Vec2I mouse = MouseVec2I();
Window *window = GetActiveWindow();
bool mouse_in_document = CheckCollisionPointRec(mouse, window->document_rect);
if (mouse_in_document) {
View *view = GetView(window->active_view);
Buffer *buffer = GetBuffer(view->active_buffer);
Int p = ScreenSpaceToBufferPosErrorOutOfBounds(window, view, buffer, mouse);
if (p != -1) {
Range enclose = EncloseExecWord(buffer, p);
String16 string = GetString(*buffer, enclose);
// Window *last_window = GetWindow(GetLastActiveWindow());
// View *last_view = GetView(last_window->active_view);
Command_EvalLua(view, string);
}
}
} else if (event.ctrl && Mouse(LEFT)) {
Vec2I mouse = MouseVec2I();
Window *window = GetActiveWindow();
@@ -298,10 +316,11 @@ bool GlobalCommand(Event event) {
Buffer *buffer = GetBuffer(view->active_buffer);
Int p = ScreenSpaceToBufferPosErrorOutOfBounds(window, view, buffer, mouse);
if (p != -1) {
view->carets.len = 1;
view->carets[0] = MakeCaret(p);
Range enclose = EncloseLoadWord(buffer, p);
String16 string = GetString(*buffer, enclose);
view->carets.len = 1;
view->carets[0] = MakeCaret(p);
Open(string);
}
}
@@ -408,8 +427,8 @@ bool GlobalCommand(Event event) {
SetActiveWindow(GetLastActiveWindow());
} else {
View *view = GetView(command_window->active_view);
Command_EvalLua(view, L"list_buffers()");
SetActiveWindow(command_window->id);
Command_EvalLua(view, L"ListBuffers()");
}
run_window_command = false;
}

View File

@@ -426,26 +426,6 @@ 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->active_buffer);
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 {
Range range = GetLineRangeWithoutNL(*buffer, 0);
Command_SelectRangeOneCursor(view, range);
Command_Replace(view, {});
}
}
// Merge carets that overlap, this needs to be handled before any edits to
// make sure overlapping edits won't happen.
//
@@ -816,6 +796,11 @@ void WindowCommand(Event event, Window *window, View *view) {
Range enclose = EncloseLoadWord(buffer, p);
String16 string = GetString(*buffer, enclose);
Open(string);
} else if (Alt(SDLK_Q)) {
Int p = GetFront(view->carets[0]);
Range enclose = EncloseExecWord(buffer, p);
String16 string = GetString(*buffer, enclose);
Command_EvalLua(view, string);
}
if (Ctrl(SDLK_W)) {

View File

@@ -127,7 +127,7 @@ function ExtractLineAndColumn(s)
end
function BufferNameExists(name)
buffers = ListBuffers()
buffers = GetBufferList()
for i = 1,#buffers do
buff = buffers[i]
if buff == name then

View File

@@ -84,19 +84,29 @@ int LuaPrint(lua_State *L) {
return 0;
}
int LuaListOpenBuffers(lua_State *L) {
int LuaListBuffers(lua_State *L) {
Window *window = GetWindow(ActiveWindow);
View *view = GetView(window->active_view);
Scratch scratch;
Array<String> strings = {scratch};
For(Buffers) {
String string = Format(scratch, "%.*s", FmtString(it.name));
Add(&strings, string);
}
String result = Merge(scratch, strings, "\n");
lua_pushlstring(LuaState, result.data, result.len);
return 1;
String result = Merge(scratch, strings, "\n");
String16 string16 = ToString16(scratch, result);
Command_SelectEntireBuffer(view);
Command_Replace(view, string16);
Command_SelectRangeOneCursor(view, {});
Command_Replace(view, L"\n");
Command_SelectRangeOneCursor(view, {});
return 0;
}
int LuaListOpenBuffersObject(lua_State *L) {
int LuaGetBufferList(lua_State *L) {
lua_createtable(L, 0, (int)Buffers.len);
int i = 1;
@@ -148,16 +158,16 @@ int LuaGetCurrentBufferDir(lua_State *L) {
}
luaL_Reg LuaFunctions[] = {
{ "Open", LuaOpen},
{ "list_buffers", LuaListOpenBuffers},
{ "ListBuffers", LuaListOpenBuffersObject},
{ "GetWorkingDir", LuaGetWorkingDir},
{ "FileExists", LuaFileExists},
{"GetCurrentBufferName", LuaGetCurrentBufferName},
{ "GetCurrentBufferDir", LuaGetCurrentBufferDir},
{ "print", LuaPrint},
{ "Print", LuaPrint},
{ NULL, NULL},
{ "Open", LuaOpen},
{ "ListBuffers", LuaListBuffers},
{ "GetBufferList", LuaGetBufferList},
{ "GetWorkingDir", LuaGetWorkingDir},
{ "FileExists", LuaFileExists},
{"GetCurrentBufferName", LuaGetCurrentBufferName},
{ "GetCurrentBufferDir", LuaGetCurrentBufferDir},
{ "print", LuaPrint},
{ "Print", LuaPrint},
{ NULL, NULL},
};
void InitLua() {
@@ -170,28 +180,6 @@ void InitLua() {
}
}
String16 EvalString(Allocator allocator, String16 string16) {
Scratch scratch((Arena *)allocator.object);
String string = ToString(scratch, string16);
string = Format(scratch, "return %.*s", FmtString(string));
if (luaL_dostring(LuaState, string.data) != LUA_OK) {
const char *error_message = lua_tostring(LuaState, -1);
ReportWarningf("Failed to load base lua config! %s", error_message);
lua_pop(LuaState, 1);
return {};
}
const char *text = lua_tostring(LuaState, -1);
if (text) {
String s = text;
String16 result = ToString16(allocator, s);
lua_pop(LuaState, 1);
return result;
} else {
return {};
}
}
Int GetStyle(String name, Int default_int) {
Int result = default_int;
lua_getglobal(LuaState, "Style");
@@ -284,4 +272,48 @@ void ReloadLuaConfig() {
}
LuaBufferChangeID = LuaBuffer->change_id;
}
}
}
//
String16 EvalString(Allocator allocator, String16 string16) {
Scratch scratch((Arena *)allocator.object);
String string = ToString(scratch, string16);
string = Format(scratch, "return %.*s", FmtString(string));
if (luaL_dostring(LuaState, string.data) != LUA_OK) {
const char *error_message = lua_tostring(LuaState, -1);
ReportWarningf("Execution error! %s", error_message);
lua_pop(LuaState, 1);
return {};
}
const char *text = lua_tostring(LuaState, -1);
if (text) {
String s = text;
String16 result = ToString16(allocator, s);
lua_pop(LuaState, 1);
return result;
} else {
return {};
}
}
void Command_EvalLua(View *view, String16 string) {
Scratch scratch;
Buffer *buffer = GetBuffer(view->active_buffer);
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 {
Range range = GetLineRangeWithoutNL(*buffer, 0);
Command_SelectRangeOneCursor(view, range);
Command_Replace(view, {});
}
}

View File

@@ -2,7 +2,7 @@
- I think the way sublime text and we display line highlights is confusing with multiple cursors (line highlight can be confused with selection)
- ctrl + delete maybe should stop on new line but it keeps on going, sublime is much more careful with deleting
- mouse execute
- mouse execute
- alt right click what to do ? (toggle console?)
- experiment with using multiple cursors to select command and it's input
- should be able click on title bar of windows which disappear on losing focus
@@ -13,6 +13,7 @@
- each buffer needs a directory even the special ones: C:\a\b\c\+errors?
- open directories - resulting in buffer with dir listing and proper buffer name
- alt/ctrl + double click should select the exec or load word
- clean \r\n into \n on trim and load
- baked font as fallback