|
| 1 | +local leetcode = require("leetcode") |
| 2 | +local config = require("leetcode.config") |
| 3 | + |
| 4 | +local standalone = true |
| 5 | +local prev_cwd = nil |
| 6 | + |
| 7 | +---@param on_vimenter boolean |
| 8 | +leetcode.should_skip = function(on_vimenter) |
| 9 | + if on_vimenter then |
| 10 | + if vim.fn.argc() ~= 1 then return true end |
| 11 | + |
| 12 | + local usr_arg, arg = vim.fn.argv()[1], config.user.arg |
| 13 | + if usr_arg ~= arg then return true end |
| 14 | + |
| 15 | + local lines = vim.api.nvim_buf_get_lines(0, 0, -1, true) |
| 16 | + if #lines > 1 or (#lines == 1 and lines[1]:len() > 0) then |
| 17 | + local log = require("leetcode.logger") |
| 18 | + log.warn(("Failed to initialize: `%s` is not an empty buffer"):format(usr_arg)) |
| 19 | + return true |
| 20 | + end |
| 21 | + else |
| 22 | + for _, buf_id in pairs(vim.api.nvim_list_bufs()) do |
| 23 | + local bufinfo = vim.fn.getbufinfo(buf_id)[1] |
| 24 | + if bufinfo and (bufinfo.listed == 1 and #bufinfo.windows > 0) then -- |
| 25 | + return false, true |
| 26 | + end |
| 27 | + end |
| 28 | + end |
| 29 | + |
| 30 | + return false |
| 31 | +end |
| 32 | + |
| 33 | +---@param on_vimenter boolean |
| 34 | +leetcode.start = function(on_vimenter) |
| 35 | + local skip, buflisted = leetcode.should_skip(on_vimenter) |
| 36 | + if skip then -- |
| 37 | + return false |
| 38 | + end |
| 39 | + |
| 40 | + config.setup() |
| 41 | + |
| 42 | + leetcode.setup_cmds() |
| 43 | + |
| 44 | + local theme = require("leetcode.theme") |
| 45 | + theme.setup() |
| 46 | + |
| 47 | + if not on_vimenter then -- |
| 48 | + if buflisted then |
| 49 | + prev_cwd = vim.fn.getcwd() |
| 50 | + vim.cmd.tabe() |
| 51 | + else |
| 52 | + vim.cmd.enew() |
| 53 | + end |
| 54 | + |
| 55 | + standalone = not buflisted |
| 56 | + end |
| 57 | + |
| 58 | + vim.api.nvim_set_current_dir(config.storage.home:absolute()) |
| 59 | + |
| 60 | + local Menu = require("leetcode-ui.renderer.menu") |
| 61 | + Menu():mount() |
| 62 | + |
| 63 | + local utils = require("leetcode.utils") |
| 64 | + utils.exec_hooks("enter") |
| 65 | + |
| 66 | + return true |
| 67 | +end |
| 68 | + |
| 69 | +leetcode.stop = vim.schedule_wrap(function() |
| 70 | + if standalone then return vim.cmd("qa!") end |
| 71 | + |
| 72 | + _Lc_state.menu:unmount() |
| 73 | + |
| 74 | + vim.api.nvim_create_user_command("Leet", require("leetcode.command").start_with_cmd, { |
| 75 | + bar = true, |
| 76 | + bang = true, |
| 77 | + desc = "Open leetcode.nvim", |
| 78 | + }) |
| 79 | + |
| 80 | + local utils = require("leetcode.utils") |
| 81 | + utils.exec_hooks("leave") |
| 82 | + |
| 83 | + if prev_cwd then |
| 84 | + vim.api.nvim_set_current_dir(prev_cwd) |
| 85 | + prev_cwd = nil |
| 86 | + end |
| 87 | +end) |
0 commit comments