A Neovim plugin enabling you to solve LeetCode problems.
| 1 | local devicons_ok, devicons = pcall(require, "nvim-web-devicons") |
| 2 | local log = require("leetcode.logger") |
| 3 | local config = require("leetcode.config") |
| 4 | local default = require("leetcode.theme.default") |
| 5 | |
| 6 | ---@class lc.Theme |
| 7 | local Theme = {} |
| 8 | |
| 9 | ---@type table<string, string[]> |
| 10 | local dynamic_hls = {} |
| 11 | |
| 12 | -- by default tags use `normal` theme |
| 13 | local highlights = { |
| 14 | strong = "bold", |
| 15 | b = "bold", |
| 16 | |
| 17 | em = "italic", |
| 18 | i = "italic", |
| 19 | |
| 20 | u = "underline", |
| 21 | |
| 22 | a = "link", |
| 23 | example = "example", |
| 24 | constraints = "constraints", |
| 25 | code = "code", |
| 26 | input = "header", |
| 27 | output = "header", |
| 28 | explanation = "header", |
| 29 | followup = "followup", |
| 30 | |
| 31 | sup = "su", |
| 32 | sub = "su", |
| 33 | |
| 34 | -- pre = "", |
| 35 | -- span = "", |
| 36 | -- p = "", |
| 37 | -- ul = "", |
| 38 | -- ol = "", |
| 39 | -- li = "", |
| 40 | -- font = "", |
| 41 | -- small = "", |
| 42 | -- div = "", |
| 43 | } |
| 44 | |
| 45 | |
| 46 | ---@param l lc.language |
| 47 | vim.tbl_map(function(l) |
| 48 | local icon, color = devicons.get_icon_color(l.ft) |
| 49 | |
| 50 | if icon then |
| 51 | l.icon = icon |
| 52 | end |
| 53 | if color then |
| 54 | l.color = color |
| 55 | end |
| 56 | |
| 57 | return l |
| 58 | end, config.langs) |
| 59 | end |
| 60 | |
| 61 | |
| 62 | config.theme = vim.tbl_extend("force", default.get(), config.user.theme) |
| 63 | |
| 64 | for key, t in pairs(config.theme) do |
| 65 | key = "leetcode_" .. key |
| 66 | vim.api.nvim_set_hl(0, key, t) |
| 67 | end |
| 68 | |
| 69 | if devicons_ok then |
| 70 | Theme.load_devicons() |
| 71 | end |
| 72 | |
| 73 | ---@param lang lc.language |
| 74 | vim.tbl_map(function(lang) |
| 75 | local name = "leetcode_lang_" .. lang.slug |
| 76 | vim.api.nvim_set_hl(0, name, { fg = lang.color }) |
| 77 | lang.hl = name |
| 78 | |
| 79 | return lang |
| 80 | end, config.langs) |
| 81 | |
| 82 | Theme.load_dynamic() |
| 83 | end |
| 84 | |
| 85 | |
| 86 | for name, tags in pairs(dynamic_hls) do |
| 87 | Theme.create_dynamic(name, tags) |
| 88 | end |
| 89 | end |
| 90 | |
| 91 | ---@param tags string[] |
| 92 | |
| 93 | if vim.tbl_isempty(tags) then |
| 94 | return "leetcode_normal" |
| 95 | end |
| 96 | |
| 97 | local name = "leetcode_dyn_" .. table.concat(tags, "_") |
| 98 | if dynamic_hls[name] then |
| 99 | return name |
| 100 | end |
| 101 | |
| 102 | return Theme.create_dynamic(name, tags) |
| 103 | end |
| 104 | |
| 105 | ---@param name string |
| 106 | ---@param tags string[] |
| 107 | |
| 108 | local theme = config.theme |
| 109 | |
| 110 | local tbl = theme["normal"] |
| 111 | for _, tag in ipairs(tags) do |
| 112 | local hl = highlights[tag] |
| 113 | if hl then |
| 114 | tbl = vim.tbl_extend("force", tbl, theme[hl]) |
| 115 | end |
| 116 | end |
| 117 | |
| 118 | if tbl.italic or tbl.bold then |
| 119 | if tbl.fg == theme["normal"].fg then |
| 120 | tbl.fg = theme[""].fg |
| 121 | end |
| 122 | end |
| 123 | |
| 124 | if pcall(vim.api.nvim_set_hl, 0, name, tbl) then |
| 125 | dynamic_hls[name] = tags |
| 126 | return name |
| 127 | else |
| 128 | return "leetcode_normal" |
| 129 | end |
| 130 | end |
| 131 | |
| 132 | |
| 133 | vim.api.nvim_create_autocmd("ColorScheme", { |
| 134 | group = vim.api.nvim_create_augroup("lc.colorscheme_sync", {}), |
| 135 | desc = "Colorscheme Synchronizer", |
| 136 | callback = Theme.load, |
| 137 | }) |
| 138 | |
| 139 | Theme.load() |
| 140 | end |
| 141 | |
| 142 | return Theme |