Using file path rules
This commit is contained in:
@@ -187,8 +187,6 @@ char *ToColor(const char *value) {
|
||||
|
||||
S8_String LuaScript = R"==(
|
||||
-- @todo: should we rewrite linux paths to windows on windows and vice-versa?
|
||||
WorkingDir = "C:/text_editor"
|
||||
OperatingSystem = "Windows"
|
||||
|
||||
function GenericTextFileRule(_s)
|
||||
function match_path(s)
|
||||
@@ -197,7 +195,7 @@ function GenericTextFileRule(_s)
|
||||
local working_dir_i, working_dir_j = string.find(s, "^%./.+")
|
||||
if working_dir_j then
|
||||
result = s:sub(3)
|
||||
result = WorkingDir.."/"..result
|
||||
result = GetWorkingDir().."/"..result
|
||||
return result
|
||||
end
|
||||
|
||||
@@ -225,8 +223,8 @@ function GenericTextFileRule(_s)
|
||||
end
|
||||
|
||||
function match_line_col(s)
|
||||
local line = 0
|
||||
local col = 0
|
||||
local line = 1
|
||||
local col = 1
|
||||
|
||||
-- filename:line:column
|
||||
local lci, lcj = s:find(":%d+:%d+$")
|
||||
@@ -297,7 +295,7 @@ end
|
||||
Rules = {}
|
||||
Rules[#Rules + 1] = GenericTextFileRule
|
||||
|
||||
function ResolvePath(s)
|
||||
function ApplyRules(s)
|
||||
for i = #Rules,1,-1 do
|
||||
rule = Rules[i]
|
||||
result = rule(s)
|
||||
|
||||
@@ -98,8 +98,6 @@ Color.Selection = GruvboxLight1
|
||||
Color.WhitespaceDuringSelection = GruvboxLight2
|
||||
|
||||
-- @todo: should we rewrite linux paths to windows on windows and vice-versa?
|
||||
WorkingDir = "C:/text_editor"
|
||||
OperatingSystem = "Windows"
|
||||
|
||||
function GenericTextFileRule(_s)
|
||||
function match_path(s)
|
||||
@@ -108,7 +106,7 @@ function GenericTextFileRule(_s)
|
||||
local working_dir_i, working_dir_j = string.find(s, "^%./.+")
|
||||
if working_dir_j then
|
||||
result = s:sub(3)
|
||||
result = WorkingDir.."/"..result
|
||||
result = GetWorkingDir().."/"..result
|
||||
return result
|
||||
end
|
||||
|
||||
@@ -136,8 +134,8 @@ function GenericTextFileRule(_s)
|
||||
end
|
||||
|
||||
function match_line_col(s)
|
||||
local line = 0
|
||||
local col = 0
|
||||
local line = 1
|
||||
local col = 1
|
||||
|
||||
-- filename:line:column
|
||||
local lci, lcj = s:find(":%d+:%d+$")
|
||||
@@ -208,7 +206,7 @@ end
|
||||
Rules = {}
|
||||
Rules[#Rules + 1] = GenericTextFileRule
|
||||
|
||||
function ResolvePath(s)
|
||||
function ApplyRules(s)
|
||||
for i = #Rules,1,-1 do
|
||||
rule = Rules[i]
|
||||
result = rule(s)
|
||||
|
||||
@@ -15,29 +15,51 @@ String ListFiles(String path) {
|
||||
return result;
|
||||
}
|
||||
|
||||
String FieldString(lua_State *L, String name) {
|
||||
String result = {};
|
||||
if (lua_istable(L, -1)) {
|
||||
lua_pushlstring(L, name.data, name.len);
|
||||
lua_gettable(L, -2);
|
||||
defer { lua_pop(L, 1); };
|
||||
if (lua_isstring(L, -1)) {
|
||||
result = lua_tostring(L, -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));
|
||||
lua_pop(L, 1);
|
||||
|
||||
lua_getglobal(L, "ApplyRules");
|
||||
lua_pushlstring(L, path.data, path.len);
|
||||
if (lua_pcall(L, 1, 1, 0) != 0) {
|
||||
const char *error_message = lua_tostring(LuaState, -1);
|
||||
ReportWarningf("Failed the call to ApplyRules! %s", error_message);
|
||||
lua_pop(LuaState, 1);
|
||||
return 0;
|
||||
}
|
||||
|
||||
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);
|
||||
if (FieldString(L, "kind") == "open_textfile") {
|
||||
String file_path = FieldString(L, "file_path");
|
||||
String line_string = FieldString(L, "line");
|
||||
Int line = strtoll(line_string.data, NULL, 10);
|
||||
String col_string = FieldString(L, "col");
|
||||
Int col = strtoll(col_string.data, NULL, 10);
|
||||
|
||||
Window *window = GetWindow(GetLastActiveWindow());
|
||||
View *view = ViewOpenFile(window, file_path);
|
||||
Buffer *buffer = GetBuffer(view->active_buffer);
|
||||
view->carets[0] = MakeCaret(XYToPos(*buffer, {line - 1, col - 1}));
|
||||
SetActiveWindow(window->id);
|
||||
return 0; // number of results
|
||||
} else {
|
||||
// Window *window = GetWindow(GetLastActiveWindow());
|
||||
// View *view = ViewOpenFile(window, path);
|
||||
// SetActiveWindow(window->id);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int LuaListOpenBuffers(lua_State *L) {
|
||||
@@ -52,6 +74,11 @@ int LuaListOpenBuffers(lua_State *L) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
int LuaGetWorkingDir(lua_State *L) {
|
||||
lua_pushlstring(LuaState, WorkingDir.data, WorkingDir.len);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int LuaOpenBigBuffer(lua_State *L) {
|
||||
Window *window = GetWindow(GetLastActiveWindow());
|
||||
|
||||
@@ -79,6 +106,9 @@ void InitLua() {
|
||||
|
||||
lua_pushcfunction(LuaState, LuaOpenBigBuffer);
|
||||
lua_setglobal(LuaState, "open_big_buffer");
|
||||
|
||||
lua_pushcfunction(LuaState, LuaGetWorkingDir);
|
||||
lua_setglobal(LuaState, "GetWorkingDir");
|
||||
}
|
||||
|
||||
String16 EvalString(Allocator allocator, String16 string16) {
|
||||
@@ -120,4 +150,4 @@ Color GetColor(String name, Color default_color) {
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -142,10 +142,15 @@ View *FindViewWithBufferName(String name) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// path should be already resolved to absolute
|
||||
// This function as name suggests tries to open a buffer,
|
||||
// there is no name resolution here, path should already be resolved etc.
|
||||
//
|
||||
// 1. It tries to find an already open buffer
|
||||
// 2. Returns a buffer if the file doesn't exist (even if a directory doesn't exist)
|
||||
// - This is a worry for later time, also we want to handle weird names and so on
|
||||
// 3. If file exists we read it, convert to utf16, tabs to spaces etc.
|
||||
//
|
||||
Buffer *BufferOpenFile(String path) {
|
||||
Assert(IsAbsolute(path));
|
||||
|
||||
Allocator sys_allocator = GetSystemAllocator();
|
||||
Scratch scratch;
|
||||
|
||||
@@ -155,13 +160,8 @@ Buffer *BufferOpenFile(String path) {
|
||||
}
|
||||
|
||||
if (!FileExists(path)) {
|
||||
String dir = ChopLastSlash(path);
|
||||
if (!IsDir(dir)) {
|
||||
buffer = GetBuffer(NullBufferID);
|
||||
} else {
|
||||
path = Copy(sys_allocator, path);
|
||||
buffer = CreateBuffer(sys_allocator, path);
|
||||
}
|
||||
path = Copy(sys_allocator, path);
|
||||
buffer = CreateBuffer(sys_allocator, path);
|
||||
} else {
|
||||
path = Copy(sys_allocator, path);
|
||||
String string = ReadFile(scratch, path);
|
||||
@@ -205,8 +205,7 @@ Buffer *BufferOpenFile(String path) {
|
||||
|
||||
View *_WindowOpenViewAndBuffer(Window *window, String name, bool set_active = true) {
|
||||
Buffer *buffer = BufferOpenFile(name);
|
||||
if (IsNull(buffer)) Assert(0); // @todo
|
||||
View *view = CreateView(buffer->id);
|
||||
View *view = CreateView(buffer->id);
|
||||
Add(&window->views, view->id);
|
||||
if (set_active) window->active_view = view->id;
|
||||
return view;
|
||||
|
||||
Reference in New Issue
Block a user