add file saving, fix paths on file loading

This commit is contained in:
Matthew 2025-10-02 04:52:23 +10:00
parent 72a903465b
commit fac809a663
4 changed files with 176 additions and 135 deletions

View File

@ -8,8 +8,9 @@ struct FlatBuffer
{ {
Tokenizer tk; Tokenizer tk;
Arena arena; Arena arena;
u8[] data;
Arena ls_arena; Arena ls_arena;
u8[] file_name;
u8[] data;
LineStart[] line_starts; LineStart[] line_starts;
u64 length; u64 length;
u64 lf_count; u64 lf_count;
@ -151,8 +152,6 @@ Fix(FlatBuffer* buffer)
line_starts[ls_idx].pos = i+1; line_starts[ls_idx].pos = i+1;
line_starts[ls_idx].level = level; line_starts[ls_idx].level = level;
Logf("line %s level %s", ls_idx, line_starts[ls_idx].level);
ls_idx += 1; ls_idx += 1;
} }
} }
@ -244,12 +243,9 @@ Insert(FlatBuffer* fb, u8[] insert, u64 length, u64 pos)
fb.data[pos .. pos+temp_len] = temp[0 .. temp_len]; fb.data[pos .. pos+temp_len] = temp[0 .. temp_len];
fb.length += length; fb.length += length;
Logf("out");
Fix(fb); Fix(fb);
Logf("out");
Reset(&fb.arena); Reset(&fb.arena);
AdjustOffset(fb); AdjustOffset(fb);
@ -517,7 +513,6 @@ Move(FlatBuffer* fb, Input key, Modifier md)
AdjustOffset(fb); AdjustOffset(fb);
U64Vec2 p = VecPos(fb); U64Vec2 p = VecPos(fb);
Logf("buf_pos %s pos %s", fb.buf_pos, p.v);
} }
void void

View File

@ -49,7 +49,6 @@ struct Editor
{ {
Arena arena; Arena arena;
u8[] filename;
FlatBuffer buf; FlatBuffer buf;
Tokenizer tk; Tokenizer tk;
LineBuffers linebufs; LineBuffers linebufs;
@ -217,31 +216,130 @@ EditModeActive()
return g_input_mode; return g_input_mode;
} }
char[]
ToAbsolutePath(u8[] file_name)
{
import core.stdc.string : strlen;
char[1024] name_buf = '\0';
char[1024] wd_buf = '\0';
version(linux)
{
import core.sys.posix.unistd;
getcwd(wd_buf.ptr, wd_buf.length);
}
version(Windows)
{
import core.sys.windows.direct;
_getcwd(wd_buf.ptr, wd_buf.length);
}
char[] wd = wd_buf[0 .. strlen(wd_buf.ptr)];
version(linux)
{
if(file_name[0] != '/')
{
name_buf.sformat("%s/%s", wd, cast(char[])file_name);
}
else
{
name_buf.sformat("%s", cast(char[])file_name);
}
}
version(Windows)
{
name_buf.sformat("%s/%s", wd, cast(char[])file_name);
}
char[] path_buf = ScratchAlloc!(char)(strlen(name_buf.ptr)+1);
path_buf[0 .. $] = name_buf[0 .. path_buf.length];
return path_buf;
}
void
SaveFile(Editor* ed, u8[] file_name)
{
import core.stdc.stdio;
file_name = file_name.length == 0 ? ed.buf.file_name : file_name;
if(file_name.length > 0)
{
char[] file_path = ToAbsolutePath(file_name);
auto f = fopen(cast(char*)file_path.ptr, "wb");
if(f != null)
{
u64 tab_count;
for(u64 i = 0; i < ed.buf.length; i += 1)
{
if(ed.buf.data[i] == '\t')
{
tab_count += 1;
}
}
u64 tab_width = GetCtx().tab_width;
u64 buf_size = ed.buf.length + ((tab_width-1) * tab_count);
u8[] temp_buf = ScratchAlloc!(u8)(buf_size);
u64 buf_pos;
for(u64 i = 0; i < ed.buf.length; i += 1)
{
if(ed.buf.data[i] == '\t')
{
for(u64 j = 0; j < tab_width; j += 1)
{
temp_buf[buf_pos++] = ' ';
}
}
else
{
temp_buf[buf_pos++] = ed.buf.data[i];
}
}
fwrite(temp_buf.ptr, 1, buf_size, f);
fflush(f);
fclose(f);
}
}
}
void void
OpenFile(Editor* ed, u8[] file_name) OpenFile(Editor* ed, u8[] file_name)
{ {
import core.stdc.stdio; import core.stdc.stdio;
import std.file;
import std.conv;
auto f = fopen(cast(char*)file_name.ptr, "rb"); if(file_name.length > 0)
if(f != null)
{ {
fseek(f, 0, SEEK_END); char[] file_path = ToAbsolutePath(file_name);
i64 len = ftell(f); auto f = fopen(file_path.ptr, "rb");
fseek(f, 0, SEEK_SET); if(f != null)
if(len > 0)
{ {
u8[] buf = ScratchAlloc!(u8)(len); fseek(f, 0, SEEK_END);
fread(buf.ptr, u8.sizeof, len, f); i64 len = ftell(f);
fseek(f, 0, SEEK_SET);
Change(&ed.buf, buf);
}
fclose(f); if(len > 0)
} {
else u8[] buf = ScratchAlloc!(u8)(len);
{ fread(buf.ptr, u8.sizeof, len, f);
Logf("[Error] Unable to open file %s", cast(char[])file_name); Change(&ed.buf, buf);
ed.buf.file_name = file_name;
}
fclose(f);
}
else
{
perror("[Error] Unable to open file");
}
} }
} }
@ -671,6 +769,14 @@ HandleCmdMode(EditorCtx* ctx, InputEvent ev)
OpenFile(p.ed, cmd.opt_strs[cmd.selected]); OpenFile(p.ed, cmd.opt_strs[cmd.selected]);
} }
} break; } break;
case CT.SaveFile:
{
UIPanel* p = GetFocusedPanel();
if(!Nil(p))
{
SaveFile(p.ed, GetParam(cmd));
}
} break;
default: break; default: break;
} }
@ -712,7 +818,7 @@ HandleCmdMode(EditorCtx* ctx, InputEvent ev)
} break; } break;
case Down: case Down:
{ {
if(cmd.selected < cmd.opt_strs.length) if(cmd.selected < cmd.opt_strs.length-1)
{ {
cmd.selected += 1; cmd.selected += 1;
} }
@ -730,7 +836,6 @@ HandleCmdMode(EditorCtx* ctx, InputEvent ev)
if(cmd.current.type == CT.None && (cmd.commands.length == 0 || prev_count != cmd.icount)) if(cmd.current.type == CT.None && (cmd.commands.length == 0 || prev_count != cmd.icount))
{ {
GetCommands(cmd); GetCommands(cmd);
cmd.selected = 0;
} }
else if(prev_count != cmd.icount) else if(prev_count != cmd.icount)
{ {
@ -750,11 +855,6 @@ HandleCmdMode(EditorCtx* ctx, InputEvent ev)
CmdInputEnd: CmdInputEnd:
if(cmd.selected >= cmd.opt_strs.length)
{
cmd.selected = 0;
}
return taken; return taken;
} }
@ -819,64 +919,10 @@ PopulateParams(CmdPalette* cmd, u8[][] strs)
cmd.opt_strs = cmd.opt_strs[0 .. matches]; cmd.opt_strs = cmd.opt_strs[0 .. matches];
} }
}
/* if(cmd.selected >= cmd.opt_strs.length)
void
DrawBuffer(Editor* ed, f32 x, f32 y, f32 px, FlatBuffer* fb)
{
const Vec4[TS.max] cols = [
TS.None: Vec4(1.0, 1.0, 1.0, 1.0),
TS.ImportTarget: Vec4(0.4, 0.7, 0.2, 1.0),
TS.Keyword: Vec4(1.0, 0.0, 0.0, 1.0),
TS.Bracket: Vec4(0.3, 0.3, 0.3, 1.0),
TS.String: Vec4(0.2, 0.6, 0.2, 1.0),
TS.Char: Vec4(0.3, 0.5, 0.5, 1.0),
TS.Number: Vec4(0.2, 0.3, 0.8, 1.0),
TS.Function: Vec4(0.4, 0.3, 0.7, 1.0),
TS.Identifier: Vec4(0.8, 0.4, 0.5, 1.0),
TS.Macro: Vec4(0.1, 0.3, 0.3, 1.0),
TS.Op: Vec4(0.5, 0.8, 0.2, 1.0),
TS.Comment: Vec4(0.3, 0.3, 0.3, 1.0),
TS.Type: Vec4(1.0, 1.0, 0.5, 1.0),
];
u32 tab_count = 2;
f32 x_pos = x;
f32 y_pos = y + px;
f32 scale = px / ed.atlas_buf.atlas.size;
foreach(i; 0 .. fb.length)
{ {
u8 ch = fb.data[i]; cmd.selected = Max(cmd.opt_strs.length, 0);
if(ch > 0 && ch < ed.atlas_buf.atlas.glyphs.length)
{
Glyph* g = ed.atlas_buf.atlas.glyphs.ptr + ch;
if(g.advance > 0.0)
{
if(ch == '\t')
{
g.atlas_left = g.atlas_right = 0.0;
foreach(j; 0 .. tab_count)
{
DrawGlyph(g, scale, &x_pos, y_pos);
}
}
else if(ch == '\n')
{
g.atlas_left = g.atlas_right = 0.0;
DrawGlyph(g, scale, &x_pos, y_pos);
y_pos += px;
x_pos = 0.0;
}
else
{
DrawGlyph(g, scale, &x_pos, y_pos, false, cols[fb.tk.buffer[i]]);
}
}
}
} }
} }
*/

View File

@ -676,7 +676,7 @@ SetFocusedPanel(UIPanel* panel)
if(!CheckNil(g_UI_NIL_PANEL, panel)) if(!CheckNil(g_UI_NIL_PANEL, panel))
{ {
g_widget_ctx.focused_panel = panel; g_widget_ctx.focused_panel = panel;
SetFocus(panel.ed != null ? Get(panel.ed.filename) : Get(panel.id)); SetFocus(Get(panel.id));
} }
} }

View File

@ -3,71 +3,71 @@ import std.stdio;
@nogc: bool @nogc: bool
Test() Test()
{ {
int x = 5; int x = 5;
int y = 10; int y = 10;
x = x + y; x = x + y;
x = x - y; x = x - y;
x = x / y; x = x / y;
x = x * y; x = x * y;
x = x % y; x = x % y;
x += y; x += y;
x -= y; x -= y;
x /= y; x /= y;
x *= y; x *= y;
x %= y; x %= y;
x = x & y; x = x & y;
x = x | y; x = x | y;
x = x ^ y; x = x ^ y;
x = ~x; x = ~x;
x ~= x; x ~= x;
// Commented here // Commented here
if(x > 1L && y < 2u || x <= 3U && y >= 4Lu || x == 5LU && x != 6uL || x == 7UL) if(x > 1L && y < 2u || x <= 3U && y >= 4Lu || x == 5LU && x != 6uL || x == 7UL)
{ {
return true; return true;
} }
float z = 10.5F; float z = 10.5F;
float w = 11.25f; float w = 11.25f;
real r = 200.55L; real r = 200.55L;
z = e+102.5; z = e+102.5;
z = e-00.25; z = e-00.25;
int hex = 0x555; int hex = 0x555;
int hex2 = 0X555; int hex2 = 0X555;
int hexp = p+0X5555-; int hexp = p+0X5555-;
hexp = p-0x555; hexp = p-0x555;
string str_literal = r"Teststring"; string str_literal = r"Teststring";
string str = "test string"; string str = "test string";
char ch = 'c'; char ch = 'c';
u8[] array = [ u8[] array = [
1, 2, 3, 4, 5 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
]; ];
array[5] = 22; array[5] = 22;
int value = WithParameters(z, str); int value = WithParameters(z, str);
int h = MacroFunc!(int)(x); int h = MacroFunc!(int)(x);
int g = MacroFunc!int(x); int g = MacroFunc!int(x);
return false; return false;
} }
int WithParameters(float x, string y) int WithParameters(float x, string y)
{ {
assert(y.length > 0, "String must not be empty"); assert(y.length > 0, "String must not be empty");
return cast(int)(x) - y[0]; return cast(int)(x) - y[0];
} }
T MacroFunc(T)(T x) T MacroFunc(T)(T x)
{ {
return x; return x;
} }