lua improvements

This commit is contained in:
Krzosa Karol
2024-07-24 10:30:33 +02:00
parent 8dace27f56
commit bb3044d168
3 changed files with 35 additions and 26 deletions

View File

@@ -378,10 +378,11 @@ void HandleActiveWindowBindings(Window *window) {
if (window->execute_line) { if (window->execute_line) {
if (Press(KEY_ENTER)) { if (Press(KEY_ENTER)) {
Scratch scratch;
Int line = PosToLine(*buffer, GetFront(view.carets[0])); Int line = PosToLine(*buffer, GetFront(view.carets[0]));
Range range = GetLineRange(*buffer, line); Range range = GetLineRange(*buffer, line);
String16 string = GetString(*buffer, range); String16 string = GetString(*buffer, range);
String16 eval_result = EvalString(string); String16 eval_result = EvalString(scratch, string);
if (Ctrl()) { if (Ctrl()) {
Command_SelectEntireBuffer(&view); Command_SelectEntireBuffer(&view);

View File

@@ -10,45 +10,48 @@ int LuaOpenFile(lua_State *L) {
} }
int LuaListOpenBuffers(lua_State *L) { int LuaListOpenBuffers(lua_State *L) {
Scratch scratch; Scratch scratch;
Array<String16> strings = {scratch}; Array<String> strings = {scratch};
For(Buffers) { For(Buffers) {
String string = Format(scratch, "open \"%.*s\"", FmtString(it.name)); String string = Format(scratch, "open \"%.*s\"", FmtString(it.name));
Add(&strings, ToString16(scratch, string)); Add(&strings, string);
} }
LuaCommandResult = Merge(scratch, strings, L"\n"); String result = Merge(scratch, strings, "\n");
return 0; lua_pushlstring(LuaState, result.data, result.len);
return 1;
} }
int LuaListViews(lua_State *L) { int LuaListViews(lua_State *L) {
Scratch scratch; Scratch scratch;
Array<String16> strings = {scratch}; Array<String> strings = {scratch};
For(Views) { For(Views) {
Buffer *buffer = GetBuffer(it.buffer_id); Buffer *buffer = GetBuffer(it.buffer_id);
String string = Format(scratch, "view = %lld buffer = %lld name = %.*s", (long long)it.id.id, (long long)buffer->id.id, FmtString(buffer->name)); String string = Format(scratch, "view = %lld buffer = %lld name = %.*s", (long long)it.id.id, (long long)buffer->id.id, FmtString(buffer->name));
Add(&strings, ToString16(scratch, string)); Add(&strings, string);
} }
LuaCommandResult = Merge(scratch, strings, L"\n"); String result = Merge(scratch, strings, "\n");
return 0; lua_pushlstring(LuaState, result.data, result.len);
return 1;
} }
int LuaListWindows(lua_State *L) { int LuaListWindows(lua_State *L) {
Scratch scratch; Scratch scratch;
Array<String16> strings = {scratch}; Array<String> strings = {scratch};
For(Windows) { For(Windows) {
View *view = GetActiveView(&it); View *view = GetActiveView(&it);
Buffer *buffer = GetBuffer(view->buffer_id); Buffer *buffer = GetBuffer(view->buffer_id);
String string = Format(scratch, "window = %lld active_view = %lld buffer_name = %.*s", (long long)it.id.id, (long long)it.active_view.id, FmtString(buffer->name)); String string = Format(scratch, "window = %lld active_view = %lld buffer_name = %.*s", (long long)it.id.id, (long long)it.active_view.id, FmtString(buffer->name));
Add(&strings, ToString16(scratch, string)); Add(&strings, string);
ForItem(child_view_id, it.views) { ForItem(child_view_id, it.views) {
View *child_view = GetView(child_view_id); View *child_view = GetView(child_view_id);
Buffer *child_buffer = GetBuffer(child_view->buffer_id); Buffer *child_buffer = GetBuffer(child_view->buffer_id);
String child_string = Format(scratch, " view = %lld buffer = %lld name = %.*s", (long long)child_view->id.id, (long long)child_buffer->id.id, FmtString(child_buffer->name)); String child_string = Format(scratch, " view = %lld buffer = %lld name = %.*s", (long long)child_view->id.id, (long long)child_buffer->id.id, FmtString(child_buffer->name));
Add(&strings, ToString16(scratch, child_string)); Add(&strings, child_string);
} }
} }
LuaCommandResult = Merge(scratch, strings, L"\n"); String result = Merge(scratch, strings, "\n");
return 0; lua_pushlstring(LuaState, result.data, result.len);
return 1;
} }
void InitLua() { void InitLua() {
@@ -75,14 +78,19 @@ void SetInfoBarErrorMessage(String string) {
if (string.len) InfoBarErrorMessage = Copy(sys, string); if (string.len) InfoBarErrorMessage = Copy(sys, string);
} }
// @todo: get result from lua? String16 EvalString(Allocator allocator, String16 string16) {
String16 EvalString(String16 string16) { Scratch scratch((Arena *)allocator.object);
Scratch scratch; String string = ToString(scratch, string16);
LuaCommandResult = {}; string = Format(scratch, "return %.*s", FmtString(string));
String string = ToString(scratch, string16);
if (luaL_dostring(LuaState, string.data) != LUA_OK) { if (luaL_dostring(LuaState, string.data) != LUA_OK) {
const char *text = lua_tostring(LuaState, -1);
SetInfoBarErrorMessage(text);
} }
return LuaCommandResult; 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 {};
}
} }

View File

@@ -109,7 +109,7 @@ WindowID LastActiveWindow;
Int LastFrameIDWhenSwitchedActiveWindow; Int LastFrameIDWhenSwitchedActiveWindow;
String InfoBarErrorMessage; String InfoBarErrorMessage;
String16 EvalString(String16 string16); 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);