From f4dd30b1c26e63fd57c35c5eb20c800976ef70a3 Mon Sep 17 00:00:00 2001 From: CJ van den Berg Date: Sat, 12 Oct 2024 19:56:51 +0200 Subject: [PATCH] feat: make --no-syntax just disable syntax highlighting and not language server support --- src/syntax/src/syntax.zig | 9 ++++----- src/tui/editor.zig | 20 +++++++++++--------- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/syntax/src/syntax.zig b/src/syntax/src/syntax.zig index 6737389..e85d859 100644 --- a/src/syntax/src/syntax.zig +++ b/src/syntax/src/syntax.zig @@ -26,7 +26,7 @@ query: *Query, injections: *Query, tree: ?*treez.Tree = null, -pub fn create(file_type: *const FileType, allocator: std.mem.Allocator, content: []const u8) !*Self { +pub fn create(file_type: *const FileType, allocator: std.mem.Allocator) !*Self { const self = try allocator.create(Self); self.* = .{ .allocator = allocator, @@ -38,18 +38,17 @@ pub fn create(file_type: *const FileType, allocator: std.mem.Allocator, content: }; errdefer self.destroy(); try self.parser.setLanguage(self.lang); - try self.refresh_full(content); return self; } -pub fn create_file_type(allocator: std.mem.Allocator, content: []const u8, lang_name: []const u8) !*Self { +pub fn create_file_type(allocator: std.mem.Allocator, lang_name: []const u8) !*Self { const file_type = FileType.get_by_name(lang_name) orelse return error.NotFound; - return create(file_type, allocator, content); + return create(file_type, allocator); } pub fn create_guess_file_type(allocator: std.mem.Allocator, content: []const u8, file_path: ?[]const u8) !*Self { const file_type = FileType.guess(file_path, content) orelse return error.NotFound; - return create(file_type, allocator, content); + return create(file_type, allocator); } pub fn destroy(self: *Self) void { diff --git a/src/tui/editor.zig b/src/tui/editor.zig index 061fe79..c85773c 100644 --- a/src/tui/editor.zig +++ b/src/tui/editor.zig @@ -248,6 +248,7 @@ pub const Editor = struct { } = .{}, syntax: ?*syntax = null, + syntax_no_render: bool = false, syntax_refresh_full: bool = false, syntax_token: usize = 0, syntax_refresh_update: bool = false, @@ -427,7 +428,7 @@ pub const Editor = struct { if (self.buffer) |_| try self.close(); self.buffer = new_buf; - self.syntax = if (tp.env.get().is("no-syntax")) null else syntax: { + self.syntax = syntax: { if (new_buf.root.lines() > root_mod.max_syntax_lines) break :syntax null; const lang_override = tp.env.get().str("language"); @@ -435,13 +436,14 @@ pub const Editor = struct { defer content.deinit(); try new_buf.root.store(content.writer(), new_buf.file_eol_mode); const syn = if (lang_override.len > 0) - syntax.create_file_type(self.allocator, content.items, lang_override) catch null + syntax.create_file_type(self.allocator, lang_override) catch null else syntax.create_guess_file_type(self.allocator, content.items, self.file_path) catch null; if (syn) |syn_| project_manager.did_open(file_path, syn_.file_type, self.lsp_version, try content.toOwnedSlice()) catch {}; break :syntax syn; }; + self.syntax_no_render = tp.env.get().is("no-syntax"); const ftn = if (self.syntax) |syn| syn.file_type.name else "text"; const fti = if (self.syntax) |syn| syn.file_type.icon else "🖹"; @@ -3063,6 +3065,7 @@ pub const Editor = struct { if (self.syntax_token == token) return; if (self.syntax) |syn| { + if (self.syntax_no_render) return; if (!self.syntax_refresh_update) self.syntax_refresh_full = true; if (self.syntax_refresh_full) { @@ -3083,13 +3086,12 @@ pub const Editor = struct { var content = std.ArrayList(u8).init(self.allocator); defer content.deinit(); try root.store(content.writer(), eol_mode); - self.syntax = if (tp.env.get().is("no-syntax")) - null - else - syntax.create_guess_file_type(self.allocator, content.items, self.file_path) catch |e| switch (e) { - error.NotFound => null, - else => return e, - }; + self.syntax = syntax.create_guess_file_type(self.allocator, content.items, self.file_path) catch |e| switch (e) { + error.NotFound => null, + else => return e, + }; + if (self.syntax_no_render) return; + if (self.syntax) |syn| try syn.refresh_full(content.items); } }