A Neovim plugin enabling you to solve LeetCode problems.
| 1 | local config = require("leetcode.config") |
| 2 | |
| 3 | ---@class lc.LeetCode |
| 4 | local leetcode = {} |
| 5 | |
| 6 | ---@private |
| 7 | local |
| 8 | local log = require("leetcode.logger") |
| 9 | log.warn("Failed to initialize: `neovim` contains listed buffers") |
| 10 | end |
| 11 | |
| 12 | local |
| 13 | local log = require("leetcode.logger") |
| 14 | if bufname and bufname ~= "" then |
| 15 | log.warn(("Failed to initialize: `%s` is not an empty buffer"):format(bufname)) |
| 16 | else |
| 17 | log.warn("Failed to initialize: not an empty buffer") |
| 18 | end |
| 19 | end |
| 20 | |
| 21 | local |
| 22 | local lines = vim.api.nvim_buf_get_lines(buf, 0, -1, true) |
| 23 | return not (#lines > 1 or (#lines == 1 and lines[1]:len() > 0)) |
| 24 | end |
| 25 | |
| 26 | ---@param on_vimenter boolean |
| 27 | --- |
| 28 | ---@return boolean, boolean? (skip, standalone) |
| 29 | |
| 30 | if on_vimenter then |
| 31 | if vim.fn.argc(-1) ~= 1 then |
| 32 | return true |
| 33 | end |
| 34 | |
| 35 | local usr_arg, arg = config.user.arg, vim.fn.argv(0, -1) |
| 36 | if usr_arg ~= arg then |
| 37 | return true |
| 38 | end |
| 39 | |
| 40 | if not buf_is_empty(0) then |
| 41 | log_buf_not_empty(usr_arg) |
| 42 | return true |
| 43 | end |
| 44 | |
| 45 | return false, true |
| 46 | else |
| 47 | local listed_bufs = vim.tbl_filter(function(info) |
| 48 | return info.listed == 1 |
| 49 | end, vim.fn.getbufinfo()) |
| 50 | |
| 51 | if #listed_bufs == 0 then |
| 52 | return false, true |
| 53 | elseif vim.fn.argc(-1) == 0 and #listed_bufs == 1 then |
| 54 | local buf = listed_bufs[1] |
| 55 | |
| 56 | if vim.api.nvim_get_current_buf() ~= buf.bufnr then |
| 57 | if config.plugins.non_standalone then |
| 58 | return false, false |
| 59 | else |
| 60 | log_failed_to_init() |
| 61 | return true |
| 62 | end |
| 63 | end |
| 64 | |
| 65 | vim.schedule(function() |
| 66 | if buf.changed == 1 then |
| 67 | vim.api.nvim_buf_delete(buf.bufnr, { force = true }) |
| 68 | end |
| 69 | end) |
| 70 | |
| 71 | return false, true |
| 72 | elseif #listed_bufs >= 1 then |
| 73 | if config.plugins.non_standalone then |
| 74 | return false, false |
| 75 | else |
| 76 | log_failed_to_init() |
| 77 | return true |
| 78 | end |
| 79 | end |
| 80 | end |
| 81 | end |
| 82 | |
| 83 | |
| 84 | require("leetcode.command").setup() |
| 85 | end |
| 86 | |
| 87 | ---@param on_vimenter boolean |
| 88 | |
| 89 | local skip = leetcode.should_skip(on_vimenter) |
| 90 | if skip then |
| 91 | return false |
| 92 | end |
| 93 | |
| 94 | config.setup() |
| 95 | |
| 96 | vim.api.nvim_set_current_dir(config.storage.home:absolute()) |
| 97 | |
| 98 | leetcode.setup_cmds() |
| 99 | |
| 100 | local theme = require("leetcode.theme") |
| 101 | theme.setup() |
| 102 | |
| 103 | if not on_vimenter then |
| 104 | vim.cmd.enew() |
| 105 | end |
| 106 | |
| 107 | local Menu = require("leetcode-ui.renderer.menu") |
| 108 | Menu():mount() |
| 109 | |
| 110 | local utils = require("leetcode.utils") |
| 111 | utils.exec_hooks("enter") |
| 112 | |
| 113 | return true |
| 114 | end |
| 115 | |
| 116 | |
| 117 | vim.cmd("qa!") |
| 118 | end |
| 119 | |
| 120 | ---@param cfg? lc.UserConfig |
| 121 | |
| 122 | config.apply(cfg or {}) |
| 123 | |
| 124 | vim.api.nvim_create_user_command("Leet", require("leetcode.command").start_with_cmd, { |
| 125 | bar = true, |
| 126 | bang = true, |
| 127 | desc = "Open leetcode.nvim", |
| 128 | }) |
| 129 | |
| 130 | local group_id = vim.api.nvim_create_augroup("leetcode_start", { clear = true }) |
| 131 | vim.api.nvim_create_autocmd("VimEnter", { |
| 132 | group = group_id, |
| 133 | pattern = "*", |
| 134 | nested = true, |
| 135 | callback = function() |
| 136 | leetcode.start(true) |
| 137 | end, |
| 138 | }) |
| 139 | end |
| 140 | |
| 141 | return leetcode |