//! Tab completion of field aliases in a rule's condition. //! //! Not a milestone-one requirement: M1-UI-022 only says a guided builder is //! not required. The interface asks with the condition's text up to the //! cursor, never with a cursor offset, so nothing here has to agree with the //! browser about how offsets are counted. //! //! M1-RULE-003 admits a field only as the left side of a comparison, and a //! comparison begins an expression or follows `(`, `and`, `or` or `not`. That //! is the whole of "a field may go here". const Completion = @This(); const std = @import("std"); const lexer = @import("lexer.zig"); const Lexer = lexer.Lexer; const order = @import("../util/order.zig"); /// The aliases that start with what has been typed, in byte order. Matching /// is case-sensitive, as identifiers are (M1-RULE-014). Empty when the cursor /// is not where a field may go. candidates: []const []const u8, /// What Tab appends at the cursor: the candidates' longest common prefix, /// less the part already typed. Empty when that part is all they share. insert: []const u8, /// Offers nothing, so Tab keeps its usual meaning. const none: Completion = .{ .candidates = &.{}, .insert = "" }; pub fn init( arena: std.mem.Allocator, before: []const u8, aliases: []const []const u8, ) error{OutOfMemory}!Completion { const partial = try fieldPartial(arena, before) orelse return none; var candidates: std.ArrayList([]const u8) = .empty; for (aliases) |alias| { if (std.mem.startsWith(u8, alias, partial)) try candidates.append(arena, alias); } if (candidates.items.len == 0) return none; order.sortBytewise(candidates.items); var common = candidates.items[0]; for (candidates.items[1..]) |candidate| { if (std.mem.findDiff(u8, common, candidate)) |diff| common = common[0..diff]; } return .{ .candidates = candidates.items, .insert = common[partial.len..] }; } /// The identifier being typed at the cursor, or null when the cursor is not /// where a field may go. The partial is taken by characters, not by token, so /// `not` directly before the cursor is the start of an alias like `notes`. /// Only `OutOfMemory` escapes: text that does not lex offers no completion. fn fieldPartial(arena: std.mem.Allocator, before: []const u8) error{OutOfMemory}!?[]const u8 { var start = before.len; while (start > 0 and Lexer.isIdentifierChar(before[start - 1])) start -= 1; const partial = before[start..]; // A run like `12` is the end of a decimal, not the start of a field. if (partial.len > 0 and !Lexer.isIdentifier(partial)) return null; var tokens = Lexer.init(arena, before[0..start]); var last: ?lexer.Token = null; while (tokens.next() catch |err| switch (err) { error.InvalidExpression => return null, error.OutOfMemory => return error.OutOfMemory, }) |token| last = token; const token = last orelse return partial; return switch (token) { .keyword_and, .keyword_not, .keyword_or, .lparen => partial, .decimal, .eq, .ge, .gt, .identifier, .le, .lt, .matches, .ne, .regex, .rparen, .string => null, }; } // -- tests ------------------------------------------------------------------- const expectEqual = std.testing.expectEqual; const expectEqualDeep = std.testing.expectEqualDeep; const expectEqualStrings = std.testing.expectEqualStrings; const expectError = std.testing.expectError; fn expectPartial(expected: []const u8, before: []const u8) !void { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const partial = try fieldPartial(arena.allocator(), before) orelse return error.TestExpectedPartial; try expectEqualStrings(expected, partial); } fn expectNoPartial(before: []const u8) !void { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); try expectEqual(null, try fieldPartial(arena.allocator(), before)); } test "a field may start the condition" { try expectPartial("", ""); try expectPartial("", " \n"); try expectPartial("pay", "pay"); } test "a field may follow an opening parenthesis" { try expectPartial("", "("); try expectPartial("pa", "not (pa"); } test "a field may follow and, or and not" { try expectPartial("", "a == 1 and "); try expectPartial("pa", "a == 1 or pa"); try expectPartial("", "not "); try expectPartial("pa", "a == 1 and not pa"); } test "a keyword touching the cursor is a partial, judged by what precedes it" { try expectPartial("not", "not"); try expectPartial("and", "a == 1 or and"); try expectNoPartial("a == 1 and"); } test "no field follows a field, an operator or a literal" { try expectNoPartial("payee "); try expectNoPartial("payee == "); try expectNoPartial("payee != "); try expectNoPartial("payee < "); try expectNoPartial("payee <= "); try expectNoPartial("payee > "); try expectNoPartial("payee >= "); try expectNoPartial("payee ~= "); try expectNoPartial("a ~= /x/ "); try expectNoPartial("a == \"x\" "); try expectNoPartial("a == 1 "); try expectNoPartial("(a == 1) "); } test "the text typed at the cursor must be able to start an identifier" { try expectNoPartial("(12"); try expectNoPartial("not 1"); try expectNoPartial("a == 1 and 1x"); } test "nothing completes inside an unterminated literal" { try expectNoPartial("a == \"x and "); try expectNoPartial("a == \"x and pa"); try expectNoPartial("a ~= /x or "); } test "nothing completes after text that does not lex" { try expectNoPartial("a = 1 and "); try expectNoPartial("# "); } const test_aliases = [_][]const u8{ "payee_iban", "amount", "Payer", "payee", "notes" }; fn expectCompletion(expected: Completion, before: []const u8) !void { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const completion = try Completion.init(arena.allocator(), before, &test_aliases); try expectEqualDeep(expected, completion); } test "an empty partial offers every alias, in byte order" { try expectCompletion(.{ .candidates = &.{ "Payer", "amount", "notes", "payee", "payee_iban" }, .insert = "", }, ""); } test "the partial narrows the candidates, case-sensitively" { try expectCompletion(.{ .candidates = &.{"Payer"}, .insert = "yer" }, "a == 1 or Pa"); try expectCompletion(.{ .candidates = &.{"notes"}, .insert = "es" }, "not"); try expectCompletion(none, "x"); } test "the insert is the candidates' common prefix, less what was typed" { try expectCompletion(.{ .candidates = &.{ "payee", "payee_iban" }, .insert = "yee" }, "(pa"); try expectCompletion(.{ .candidates = &.{ "payee", "payee_iban" }, .insert = "" }, "(payee"); try expectCompletion(.{ .candidates = &.{"payee_iban"}, .insert = "iban" }, "(payee_"); } test "nothing is offered where no field may go" { try expectCompletion(none, "payee == pa"); } test "running out of memory while collecting candidates is reported" { var failing = std.testing.FailingAllocator.init(std.testing.allocator, .{ .fail_index = 0 }); try expectError(error.OutOfMemory, Completion.init(failing.allocator(), "", &test_aliases)); } test "running out of memory while lexing is reported, not taken for invalid text" { var failing = std.testing.FailingAllocator.init(std.testing.allocator, .{ .fail_index = 0 }); try expectError(error.OutOfMemory, fieldPartial(failing.allocator(), "a == \"x\" and ")); }