Using file path rules
This commit is contained in:
@@ -187,8 +187,6 @@ char *ToColor(const char *value) {
|
|||||||
|
|
||||||
S8_String LuaScript = R"==(
|
S8_String LuaScript = R"==(
|
||||||
-- @todo: should we rewrite linux paths to windows on windows and vice-versa?
|
-- @todo: should we rewrite linux paths to windows on windows and vice-versa?
|
||||||
WorkingDir = "C:/text_editor"
|
|
||||||
OperatingSystem = "Windows"
|
|
||||||
|
|
||||||
function GenericTextFileRule(_s)
|
function GenericTextFileRule(_s)
|
||||||
function match_path(s)
|
function match_path(s)
|
||||||
@@ -197,7 +195,7 @@ function GenericTextFileRule(_s)
|
|||||||
local working_dir_i, working_dir_j = string.find(s, "^%./.+")
|
local working_dir_i, working_dir_j = string.find(s, "^%./.+")
|
||||||
if working_dir_j then
|
if working_dir_j then
|
||||||
result = s:sub(3)
|
result = s:sub(3)
|
||||||
result = WorkingDir.."/"..result
|
result = GetWorkingDir().."/"..result
|
||||||
return result
|
return result
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -225,8 +223,8 @@ function GenericTextFileRule(_s)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function match_line_col(s)
|
function match_line_col(s)
|
||||||
local line = 0
|
local line = 1
|
||||||
local col = 0
|
local col = 1
|
||||||
|
|
||||||
-- filename:line:column
|
-- filename:line:column
|
||||||
local lci, lcj = s:find(":%d+:%d+$")
|
local lci, lcj = s:find(":%d+:%d+$")
|
||||||
@@ -297,7 +295,7 @@ end
|
|||||||
Rules = {}
|
Rules = {}
|
||||||
Rules[#Rules + 1] = GenericTextFileRule
|
Rules[#Rules + 1] = GenericTextFileRule
|
||||||
|
|
||||||
function ResolvePath(s)
|
function ApplyRules(s)
|
||||||
for i = #Rules,1,-1 do
|
for i = #Rules,1,-1 do
|
||||||
rule = Rules[i]
|
rule = Rules[i]
|
||||||
result = rule(s)
|
result = rule(s)
|
||||||
|
|||||||
@@ -98,8 +98,6 @@ Color.Selection = GruvboxLight1
|
|||||||
Color.WhitespaceDuringSelection = GruvboxLight2
|
Color.WhitespaceDuringSelection = GruvboxLight2
|
||||||
|
|
||||||
-- @todo: should we rewrite linux paths to windows on windows and vice-versa?
|
-- @todo: should we rewrite linux paths to windows on windows and vice-versa?
|
||||||
WorkingDir = "C:/text_editor"
|
|
||||||
OperatingSystem = "Windows"
|
|
||||||
|
|
||||||
function GenericTextFileRule(_s)
|
function GenericTextFileRule(_s)
|
||||||
function match_path(s)
|
function match_path(s)
|
||||||
@@ -108,7 +106,7 @@ function GenericTextFileRule(_s)
|
|||||||
local working_dir_i, working_dir_j = string.find(s, "^%./.+")
|
local working_dir_i, working_dir_j = string.find(s, "^%./.+")
|
||||||
if working_dir_j then
|
if working_dir_j then
|
||||||
result = s:sub(3)
|
result = s:sub(3)
|
||||||
result = WorkingDir.."/"..result
|
result = GetWorkingDir().."/"..result
|
||||||
return result
|
return result
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -136,8 +134,8 @@ function GenericTextFileRule(_s)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function match_line_col(s)
|
function match_line_col(s)
|
||||||
local line = 0
|
local line = 1
|
||||||
local col = 0
|
local col = 1
|
||||||
|
|
||||||
-- filename:line:column
|
-- filename:line:column
|
||||||
local lci, lcj = s:find(":%d+:%d+$")
|
local lci, lcj = s:find(":%d+:%d+$")
|
||||||
@@ -208,7 +206,7 @@ end
|
|||||||
Rules = {}
|
Rules = {}
|
||||||
Rules[#Rules + 1] = GenericTextFileRule
|
Rules[#Rules + 1] = GenericTextFileRule
|
||||||
|
|
||||||
function ResolvePath(s)
|
function ApplyRules(s)
|
||||||
for i = #Rules,1,-1 do
|
for i = #Rules,1,-1 do
|
||||||
rule = Rules[i]
|
rule = Rules[i]
|
||||||
result = rule(s)
|
result = rule(s)
|
||||||
|
|||||||
@@ -15,29 +15,51 @@ String ListFiles(String path) {
|
|||||||
return result;
|
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) {
|
int LuaOpen(lua_State *L) {
|
||||||
Scratch scratch;
|
Scratch scratch;
|
||||||
String path = luaL_checkstring(L, 1);
|
String path = luaL_checkstring(L, 1);
|
||||||
if (StartsWith(path, "./")) {
|
lua_pop(L, 1);
|
||||||
path = Skip(path, 2);
|
|
||||||
path = Format(scratch, "%.*s/%.*s", FmtString(WorkingDir), FmtString(path));
|
lua_getglobal(L, "ApplyRules");
|
||||||
} else if (IsAbsolute(path)) {
|
lua_pushlstring(L, path.data, path.len);
|
||||||
// dont do anything
|
if (lua_pcall(L, 1, 1, 0) != 0) {
|
||||||
} else {
|
const char *error_message = lua_tostring(LuaState, -1);
|
||||||
path = Format(scratch, "%.*s/%.*s", FmtString(WorkingDir), FmtString(path));
|
ReportWarningf("Failed the call to ApplyRules! %s", error_message);
|
||||||
|
lua_pop(LuaState, 1);
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (IsDir(path)) {
|
if (FieldString(L, "kind") == "open_textfile") {
|
||||||
WorkingDir = GetAbsolutePath(GetSystemAllocator(), path);
|
String file_path = FieldString(L, "file_path");
|
||||||
String files = ListFiles(WorkingDir);
|
String line_string = FieldString(L, "line");
|
||||||
lua_pushlstring(LuaState, files.data, files.len);
|
Int line = strtoll(line_string.data, NULL, 10);
|
||||||
return 1;
|
String col_string = FieldString(L, "col");
|
||||||
} else {
|
Int col = strtoll(col_string.data, NULL, 10);
|
||||||
Window *window = GetWindow(GetLastActiveWindow());
|
|
||||||
View *view = ViewOpenFile(window, path);
|
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);
|
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) {
|
int LuaListOpenBuffers(lua_State *L) {
|
||||||
@@ -52,6 +74,11 @@ int LuaListOpenBuffers(lua_State *L) {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int LuaGetWorkingDir(lua_State *L) {
|
||||||
|
lua_pushlstring(LuaState, WorkingDir.data, WorkingDir.len);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
int LuaOpenBigBuffer(lua_State *L) {
|
int LuaOpenBigBuffer(lua_State *L) {
|
||||||
Window *window = GetWindow(GetLastActiveWindow());
|
Window *window = GetWindow(GetLastActiveWindow());
|
||||||
|
|
||||||
@@ -79,6 +106,9 @@ void InitLua() {
|
|||||||
|
|
||||||
lua_pushcfunction(LuaState, LuaOpenBigBuffer);
|
lua_pushcfunction(LuaState, LuaOpenBigBuffer);
|
||||||
lua_setglobal(LuaState, "open_big_buffer");
|
lua_setglobal(LuaState, "open_big_buffer");
|
||||||
|
|
||||||
|
lua_pushcfunction(LuaState, LuaGetWorkingDir);
|
||||||
|
lua_setglobal(LuaState, "GetWorkingDir");
|
||||||
}
|
}
|
||||||
|
|
||||||
String16 EvalString(Allocator allocator, String16 string16) {
|
String16 EvalString(Allocator allocator, String16 string16) {
|
||||||
|
|||||||
@@ -142,10 +142,15 @@ View *FindViewWithBufferName(String name) {
|
|||||||
return NULL;
|
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) {
|
Buffer *BufferOpenFile(String path) {
|
||||||
Assert(IsAbsolute(path));
|
|
||||||
|
|
||||||
Allocator sys_allocator = GetSystemAllocator();
|
Allocator sys_allocator = GetSystemAllocator();
|
||||||
Scratch scratch;
|
Scratch scratch;
|
||||||
|
|
||||||
@@ -155,13 +160,8 @@ Buffer *BufferOpenFile(String path) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!FileExists(path)) {
|
if (!FileExists(path)) {
|
||||||
String dir = ChopLastSlash(path);
|
path = Copy(sys_allocator, path);
|
||||||
if (!IsDir(dir)) {
|
buffer = CreateBuffer(sys_allocator, path);
|
||||||
buffer = GetBuffer(NullBufferID);
|
|
||||||
} else {
|
|
||||||
path = Copy(sys_allocator, path);
|
|
||||||
buffer = CreateBuffer(sys_allocator, path);
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
path = Copy(sys_allocator, path);
|
path = Copy(sys_allocator, path);
|
||||||
String string = ReadFile(scratch, path);
|
String string = ReadFile(scratch, path);
|
||||||
@@ -205,8 +205,7 @@ Buffer *BufferOpenFile(String path) {
|
|||||||
|
|
||||||
View *_WindowOpenViewAndBuffer(Window *window, String name, bool set_active = true) {
|
View *_WindowOpenViewAndBuffer(Window *window, String name, bool set_active = true) {
|
||||||
Buffer *buffer = BufferOpenFile(name);
|
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);
|
Add(&window->views, view->id);
|
||||||
if (set_active) window->active_view = view->id;
|
if (set_active) window->active_view = view->id;
|
||||||
return view;
|
return view;
|
||||||
|
|||||||
Reference in New Issue
Block a user