diff --git a/src/editor/buffer.d b/src/editor/buffer.d index 090e935..8c99919 100644 --- a/src/editor/buffer.d +++ b/src/editor/buffer.d @@ -8,8 +8,9 @@ struct FlatBuffer { Tokenizer tk; Arena arena; - u8[] data; Arena ls_arena; + u8[] file_name; + u8[] data; LineStart[] line_starts; u64 length; u64 lf_count; @@ -151,8 +152,6 @@ Fix(FlatBuffer* buffer) line_starts[ls_idx].pos = i+1; line_starts[ls_idx].level = level; - Logf("line %s level %s", ls_idx, line_starts[ls_idx].level); - 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.length += length; - Logf("out"); Fix(fb); - Logf("out"); - Reset(&fb.arena); AdjustOffset(fb); @@ -517,7 +513,6 @@ Move(FlatBuffer* fb, Input key, Modifier md) AdjustOffset(fb); U64Vec2 p = VecPos(fb); - Logf("buf_pos %s pos %s", fb.buf_pos, p.v); } void diff --git a/src/editor/editor.d b/src/editor/editor.d index aa9a39a..6d3f989 100644 --- a/src/editor/editor.d +++ b/src/editor/editor.d @@ -49,7 +49,6 @@ struct Editor { Arena arena; - u8[] filename; FlatBuffer buf; Tokenizer tk; LineBuffers linebufs; @@ -217,31 +216,130 @@ EditModeActive() 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 OpenFile(Editor* ed, u8[] file_name) { import core.stdc.stdio; + import std.file; + import std.conv; - auto f = fopen(cast(char*)file_name.ptr, "rb"); - if(f != null) + if(file_name.length > 0) { - fseek(f, 0, SEEK_END); - i64 len = ftell(f); - fseek(f, 0, SEEK_SET); - - if(len > 0) + char[] file_path = ToAbsolutePath(file_name); + auto f = fopen(file_path.ptr, "rb"); + if(f != null) { - u8[] buf = ScratchAlloc!(u8)(len); - fread(buf.ptr, u8.sizeof, len, f); - - Change(&ed.buf, buf); - } + fseek(f, 0, SEEK_END); + i64 len = ftell(f); + fseek(f, 0, SEEK_SET); - fclose(f); - } - else - { - Logf("[Error] Unable to open file %s", cast(char[])file_name); + if(len > 0) + { + u8[] buf = ScratchAlloc!(u8)(len); + fread(buf.ptr, u8.sizeof, len, f); + 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]); } } break; + case CT.SaveFile: + { + UIPanel* p = GetFocusedPanel(); + if(!Nil(p)) + { + SaveFile(p.ed, GetParam(cmd)); + } + } break; default: break; } @@ -712,7 +818,7 @@ HandleCmdMode(EditorCtx* ctx, InputEvent ev) } break; case Down: { - if(cmd.selected < cmd.opt_strs.length) + if(cmd.selected < cmd.opt_strs.length-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)) { GetCommands(cmd); - cmd.selected = 0; } else if(prev_count != cmd.icount) { @@ -750,11 +855,6 @@ HandleCmdMode(EditorCtx* ctx, InputEvent ev) CmdInputEnd: - if(cmd.selected >= cmd.opt_strs.length) - { - cmd.selected = 0; - } - return taken; } @@ -819,64 +919,10 @@ PopulateParams(CmdPalette* cmd, u8[][] strs) cmd.opt_strs = cmd.opt_strs[0 .. matches]; } -} -/* -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) + if(cmd.selected >= cmd.opt_strs.length) { - u8 ch = fb.data[i]; - - 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]]); - } - } - } + cmd.selected = Max(cmd.opt_strs.length, 0); } } -*/ + diff --git a/src/editor/widgets.d b/src/editor/widgets.d index 49ae274..7885579 100644 --- a/src/editor/widgets.d +++ b/src/editor/widgets.d @@ -676,7 +676,7 @@ SetFocusedPanel(UIPanel* panel) if(!CheckNil(g_UI_NIL_PANEL, panel)) { g_widget_ctx.focused_panel = panel; - SetFocus(panel.ed != null ? Get(panel.ed.filename) : Get(panel.id)); + SetFocus(Get(panel.id)); } } diff --git a/test/files/syntax_highlighting.d b/test/files/syntax_highlighting.d index 6326531..af68b64 100644 --- a/test/files/syntax_highlighting.d +++ b/test/files/syntax_highlighting.d @@ -3,71 +3,71 @@ import std.stdio; @nogc: bool Test() { - int x = 5; - int y = 10; + int x = 5; + 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; - x ~= x; + x = x & y; + x = x | y; + x = x ^ y; + x = ~x; + x ~= x; - // Commented here - if(x > 1L && y < 2u || x <= 3U && y >= 4Lu || x == 5LU && x != 6uL || x == 7UL) - { - return true; - } + // Commented here + if(x > 1L && y < 2u || x <= 3U && y >= 4Lu || x == 5LU && x != 6uL || x == 7UL) + { + return true; + } - float z = 10.5F; - float w = 11.25f; - real r = 200.55L; + float z = 10.5F; + float w = 11.25f; + real r = 200.55L; - z = e+102.5; - z = e-00.25; + z = e+102.5; + z = e-00.25; - int hex = 0x555; - int hex2 = 0X555; + int hex = 0x555; + int hex2 = 0X555; - int hexp = p+0X5555-; - hexp = p-0x555; + int hexp = p+0X5555-; + hexp = p-0x555; - string str_literal = r"Teststring"; - string str = "test string"; - char ch = 'c'; + string str_literal = r"Teststring"; + string str = "test string"; + char ch = 'c'; - u8[] array = [ - 1, 2, 3, 4, 5 - ]; + u8[] array = [ + 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 g = MacroFunc!int(x); + int h = MacroFunc!(int)(x); + int g = MacroFunc!int(x); - return false; + return false; } int WithParameters(float x, string y) { - assert(y.length > 0, "String must not be empty"); - return cast(int)(x) - y[0]; + assert(y.length > 0, "String must not be empty"); + return cast(int)(x) - y[0]; } T MacroFunc(T)(T x) { - return x; + return x; }