A Neovim plugin enabling you to solve LeetCode problems.
| 1 | local template = require("leetcode.config.template") |
| 2 | local P = require("plenary.path") |
| 3 | |
| 4 | _Lc_state = { |
| 5 | menu = nil, ---@type lc.ui.Menu |
| 6 | questions = {}, ---@type lc.ui.Question[] |
| 7 | } |
| 8 | |
| 9 | local lazy_plugs = {} |
| 10 | |
| 11 | ---@class lc.Config |
| 12 | local config = { |
| 13 | default = template, |
| 14 | user = template, |
| 15 | |
| 16 | name = "leetcode.nvim", |
| 17 | domain = "com", |
| 18 | is_cn = false, |
| 19 | debug = false, |
| 20 | lang = "cpp", |
| 21 | version = "1.0.1", |
| 22 | storage = {}, ---@type table<string, Path> |
| 23 | theme = {}, ---@type lc.highlights |
| 24 | plugins = {}, |
| 25 | |
| 26 | translator = false, |
| 27 | |
| 28 | langs = require("leetcode.config.langs"), |
| 29 | icons = require("leetcode.config.icons"), |
| 30 | sessions = require("leetcode.config.sessions"), |
| 31 | stats = require("leetcode.config.stats"), |
| 32 | imports = require("leetcode.config.imports"), |
| 33 | hooks = require("leetcode.config.hooks"), |
| 34 | |
| 35 | ---@type lc.UserStatus |
| 36 | auth = {}, ---@diagnostic disable-line |
| 37 | } |
| 38 | |
| 39 | ---Merge configurations into default configurations and set it as user configurations. |
| 40 | --- |
| 41 | ---@param cfg lc.UserConfig Configurations to be merged. |
| 42 | |
| 43 | config.user = vim.tbl_deep_extend("force", config.default, cfg or {}) |
| 44 | config.load_plugins() |
| 45 | end |
| 46 | |
| 47 | |
| 48 | config.validate() |
| 49 | |
| 50 | -- deprecate `directory` config |
| 51 | if config.user.directory then |
| 52 | local log = require("leetcode.logger") |
| 53 | log.warn("leetcode.nvim config: `directory` is deprecated. Use `storage.home` instead.") |
| 54 | config.user.storage.home = config.user.directory |
| 55 | end |
| 56 | |
| 57 | config.user.storage = vim.tbl_map(vim.fn.expand, config.user.storage) |
| 58 | |
| 59 | config.debug = config.user.debug or false ---@diagnostic disable-line |
| 60 | config.lang = config.user.lang |
| 61 | |
| 62 | config.storage.home = P:new(config.user.storage.home) ---@diagnostic disable-line |
| 63 | config.storage.home:mkdir() |
| 64 | |
| 65 | config.storage.cache = P:new(config.user.storage.cache) ---@diagnostic disable-line |
| 66 | config.storage.cache:mkdir() |
| 67 | |
| 68 | for _, plug_load_fn in ipairs(lazy_plugs) do |
| 69 | plug_load_fn() |
| 70 | end |
| 71 | end |
| 72 | |
| 73 | |
| 74 | local utils = require("leetcode.utils") |
| 75 | |
| 76 | assert(vim.fn.has("nvim-0.9.0") == 1, "Neovim >= 0.9.0 required") |
| 77 | |
| 78 | if not utils.get_lang(config.lang) then |
| 79 | ---@type lc.lang[] |
| 80 | local lang_slugs = vim.tbl_map(function(lang) |
| 81 | return lang.slug |
| 82 | end, config.langs) |
| 83 | |
| 84 | local matches = {} |
| 85 | for _, slug in ipairs(lang_slugs) do |
| 86 | local percent = slug:match(config.lang) or config.lang:match(slug) |
| 87 | if percent then |
| 88 | table.insert(matches, slug) |
| 89 | end |
| 90 | end |
| 91 | |
| 92 | if not vim.tbl_isempty(matches) then |
| 93 | local log = require("leetcode.logger") |
| 94 | log.warn("Did you mean: { " .. table.concat(matches, ", ") .. " }?") |
| 95 | end |
| 96 | |
| 97 | error("Unsupported Language: " .. config.lang) |
| 98 | end |
| 99 | end |
| 100 | |
| 101 | |
| 102 | config.plugins = {} |
| 103 | local plugins = {} |
| 104 | |
| 105 | if config.user.cn.enabled then |
| 106 | table.insert(plugins, "cn") |
| 107 | end |
| 108 | |
| 109 | for plugin, enabled in pairs(config.user.plugins) do |
| 110 | if enabled then |
| 111 | table.insert(plugins, plugin) |
| 112 | end |
| 113 | end |
| 114 | |
| 115 | for _, plugin in ipairs(plugins) do |
| 116 | local ok, plug = pcall(require, "leetcode-plugins." .. plugin) |
| 117 | |
| 118 | if ok then |
| 119 | if not (plug.opts or {}).lazy then |
| 120 | plug.load() |
| 121 | else |
| 122 | table.insert(lazy_plugs, plug.load) |
| 123 | end |
| 124 | config.plugins[plugin] = true |
| 125 | else |
| 126 | table.insert(lazy_plugs, function() |
| 127 | local log = require("leetcode.logger") |
| 128 | log.error(plug) |
| 129 | end) |
| 130 | end |
| 131 | end |
| 132 | end |
| 133 | |
| 134 | return config |