refactor: prefer orelse to if(pred) |x| x else y
This commit is contained in:
parent
1caf2aa0f6
commit
c01576412a
15 changed files with 43 additions and 43 deletions
|
@ -136,6 +136,6 @@ pub const List = struct {
|
|||
if (consume)
|
||||
return true;
|
||||
}
|
||||
return if (e) |e_| e_ else false;
|
||||
return e orelse false;
|
||||
}
|
||||
};
|
||||
|
|
|
@ -356,7 +356,7 @@ pub const Editor = struct {
|
|||
fn buf_for_update(self: *Self) !*const Buffer {
|
||||
self.cursels_saved.clearAndFree();
|
||||
self.cursels_saved = try self.cursels.clone();
|
||||
return if (self.buffer) |p| p else error.Stop;
|
||||
return self.buffer orelse error.Stop;
|
||||
}
|
||||
|
||||
fn buf_root(self: *const Self) !Buffer.Root {
|
||||
|
@ -379,7 +379,7 @@ pub const Editor = struct {
|
|||
}
|
||||
|
||||
pub fn is_dirty(self: *Self) bool {
|
||||
const b = if (self.buffer) |p| p else return false;
|
||||
const b = self.buffer orelse return false;
|
||||
return b.is_dirty();
|
||||
}
|
||||
|
||||
|
@ -437,7 +437,7 @@ pub const Editor = struct {
|
|||
}
|
||||
|
||||
fn close_internal(self: *Self, allow_dirty_close: bool) !void {
|
||||
const b = if (self.buffer) |p| p else return error.Stop;
|
||||
const b = self.buffer orelse return error.Stop;
|
||||
if (!allow_dirty_close and b.is_dirty()) return tp.exit("unsaved changes");
|
||||
if (self.buffer) |b_mut| b_mut.deinit();
|
||||
self.buffer = null;
|
||||
|
@ -450,7 +450,7 @@ pub const Editor = struct {
|
|||
}
|
||||
|
||||
fn save(self: *Self) !void {
|
||||
const b = if (self.buffer) |p| p else return error.Stop;
|
||||
const b = self.buffer orelse return error.Stop;
|
||||
if (!b.is_dirty()) return self.logger.print("no changes to save", .{});
|
||||
if (self.file_path) |file_path| {
|
||||
if (self.buffer) |b_mut| try b_mut.store_to_file_and_clean(file_path);
|
||||
|
@ -460,7 +460,7 @@ pub const Editor = struct {
|
|||
}
|
||||
|
||||
pub fn push_cursor(self: *Self) !void {
|
||||
const primary = if (self.cursels.getLastOrNull()) |c| c orelse CurSel{} else CurSel{};
|
||||
const primary = self.cursels.getLastOrNull() orelse CurSel{} orelse CurSel{};
|
||||
(try self.cursels.addOne()).* = primary;
|
||||
}
|
||||
|
||||
|
@ -505,7 +505,7 @@ pub const Editor = struct {
|
|||
}
|
||||
|
||||
fn update_buf(self: *Self, root: Buffer.Root) !void {
|
||||
const b = if (self.buffer) |p| p else return error.Stop;
|
||||
const b = self.buffer orelse return error.Stop;
|
||||
var sfa = std.heap.stackFallback(512, self.a);
|
||||
const a = sfa.get();
|
||||
const meta = try self.store_undo_meta(a);
|
||||
|
@ -999,7 +999,7 @@ pub const Editor = struct {
|
|||
fn render_syntax(self: *Self, theme: *const Widget.Theme, cache: *StyleCache, root: Buffer.Root) !void {
|
||||
const frame = tracy.initZone(@src(), .{ .name = "editor render syntax" });
|
||||
defer frame.deinit();
|
||||
const syn = if (self.syntax) |syn| syn else return;
|
||||
const syn = self.syntax orelse return;
|
||||
const Ctx = struct {
|
||||
self: *Self,
|
||||
theme: *const Widget.Theme,
|
||||
|
@ -1275,7 +1275,7 @@ pub const Editor = struct {
|
|||
}
|
||||
|
||||
fn cancel_all_selections(self: *Self) void {
|
||||
var primary = if (self.cursels.getLast()) |p| p else CurSel{};
|
||||
var primary = self.cursels.getLast() orelse CurSel{};
|
||||
primary.selection = null;
|
||||
self.cursels.clearRetainingCapacity();
|
||||
self.cursels.addOneAssumeCapacity().* = primary;
|
||||
|
@ -1490,7 +1490,7 @@ pub const Editor = struct {
|
|||
}
|
||||
|
||||
fn delete_selection(self: *Self, root: Buffer.Root, cursel: *CurSel, a: Allocator) error{Stop}!Buffer.Root {
|
||||
var sel: Selection = if (cursel.selection) |sel| sel else return error.Stop;
|
||||
var sel: Selection = cursel.selection orelse return error.Stop;
|
||||
sel.normalize();
|
||||
cursel.cursor = sel.begin;
|
||||
cursel.selection = null;
|
||||
|
|
|
@ -305,8 +305,8 @@ fn diff_update(self: *Self) !void {
|
|||
return;
|
||||
}
|
||||
const editor = self.editor;
|
||||
const new = if (editor.get_current_root()) |new| new else return;
|
||||
const old = if (editor.buffer) |buffer| if (buffer.last_save) |old| old else return else return;
|
||||
const new = editor.get_current_root() orelse return;
|
||||
const old = if (editor.buffer) |buffer| buffer.last_save orelse return else return;
|
||||
return self.diff.diff(diff_result, new, old);
|
||||
}
|
||||
|
||||
|
|
|
@ -75,7 +75,7 @@ fn clear(self: *Self) void {
|
|||
}
|
||||
|
||||
fn inspect_location(self: *Self, row: usize, col: usize) void {
|
||||
const syn = if (self.editor.syntax) |p| p else return;
|
||||
const syn = self.editor.syntax orelse return;
|
||||
syn.highlights_at_point(self, dump_highlight, .{ .row = @intCast(row), .column = @intCast(col) });
|
||||
}
|
||||
|
||||
|
|
|
@ -422,7 +422,7 @@ const cmds = struct {
|
|||
tui_.config.gutter_line_numbers_relative = lnr;
|
||||
try tui_.save_config();
|
||||
if (self.widgets.get("editor_gutter")) |gutter_widget| {
|
||||
const gutter = if (gutter_widget.dynamic_cast(@import("editor_gutter.zig"))) |p| p else return;
|
||||
const gutter = gutter_widget.dynamic_cast(@import("editor_gutter.zig")) orelse return;
|
||||
gutter.linenum = ln;
|
||||
gutter.relative = lnr;
|
||||
}
|
||||
|
|
|
@ -37,7 +37,7 @@ longest: usize = 0,
|
|||
commands: Commands = undefined,
|
||||
|
||||
pub fn create(a: std.mem.Allocator) !tui.Mode {
|
||||
const mv = if (tui.current().mainview.dynamic_cast(mainview)) |mv_| mv_ else return error.NotFound;
|
||||
const mv = tui.current().mainview.dynamic_cast(mainview) orelse return error.NotFound;
|
||||
const self: *Self = try a.create(Self);
|
||||
self.* = .{
|
||||
.a = a,
|
||||
|
|
|
@ -48,7 +48,7 @@ pub fn Create(options: type) type {
|
|||
pub const ButtonState = Button.State(*Menu.State(*Self));
|
||||
|
||||
pub fn create(a: std.mem.Allocator) !tui.Mode {
|
||||
const mv = if (tui.current().mainview.dynamic_cast(mainview)) |mv_| mv_ else return error.NotFound;
|
||||
const mv = tui.current().mainview.dynamic_cast(mainview) orelse return error.NotFound;
|
||||
const self: *Self = try a.create(Self);
|
||||
self.* = .{
|
||||
.a = a,
|
||||
|
|
|
@ -91,7 +91,7 @@ pub fn render(self: *Self, btn: *Button.State(Self), theme: *const Widget.Theme)
|
|||
|
||||
fn render_mini_mode(plane: *Plane, theme: *const Widget.Theme) void {
|
||||
plane.off_styles(style.italic);
|
||||
const mini_mode = if (tui.current().mini_mode) |m| m else return;
|
||||
const mini_mode = tui.current().mini_mode orelse return;
|
||||
_ = plane.print(" {s}", .{mini_mode.text}) catch {};
|
||||
if (mini_mode.cursor) |cursor| {
|
||||
const pos: c_int = @intCast(cursor);
|
||||
|
|
|
@ -747,7 +747,7 @@ pub const KeybindHints = std.static_string_map.StaticStringMap([]const u8);
|
|||
threadlocal var instance_: ?*Self = null;
|
||||
|
||||
pub fn current() *Self {
|
||||
return if (instance_) |p| p else @panic("tui call out of context");
|
||||
return instance_ orelse @panic("tui call out of context");
|
||||
}
|
||||
|
||||
pub fn get_mode() []const u8 {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue