kawre / leetcode.nvim

A Neovim plugin enabling you to solve LeetCode problems.
MIT License
654 stars 34 forks source link
leetcode leetcode-neovim leetcode-nvim lua neovim neovim-plugin nvim nvim-plugin plugin
# leetcode.nvim đŸ”Ĩ Solve [LeetCode] problems within [Neovim] đŸ”Ĩ đŸ‡ē🇸 English, 🇨đŸ‡ŗ įŽ€äŊ“中文

https://github.com/kawre/leetcode.nvim/assets/69250723/aee6584c-e099-4409-b114-123cb32b7563

[!CAUTION] This plugin has been primarily tested with Java. If you encounter any errors while using other languages, please open an issue to report them.

✨ Features

đŸ“Ŧ Requirements

đŸ“Ļ Installation

{
    "kawre/leetcode.nvim",
    build = ":TSUpdate html",
    dependencies = {
        "nvim-telescope/telescope.nvim",
        "nvim-lua/plenary.nvim", -- required by telescope
        "MunifTanjim/nui.nvim",

        -- optional
        "nvim-treesitter/nvim-treesitter",
        "rcarriga/nvim-notify",
        "nvim-tree/nvim-web-devicons",
    },
    opts = {
        -- configuration goes here
    },
}

🛠ī¸ Configuration

To see full configuration types see template.lua

⚙ī¸ default configuration

{
    ---@type string
    arg = "leetcode.nvim",

    ---@type lc.lang
    lang = "cpp",

    cn = { -- leetcode.cn
        enabled = false, ---@type boolean
        translator = true, ---@type boolean
        translate_problems = true, ---@type boolean
    },

    ---@type lc.storage
    storage = {
        home = vim.fn.stdpath("data") .. "/leetcode",
        cache = vim.fn.stdpath("cache") .. "/leetcode",
    },

    ---@type table<string, boolean>
    plugins = {
        non_standalone = false,
    },

    ---@type boolean
    logging = true,

    injector = {}, ---@type table<lc.lang, lc.inject>

    cache = {
        update_interval = 60 * 60 * 24 * 7, ---@type integer 7 days
    },

    console = {
        open_on_runcode = true, ---@type boolean

        dir = "row", ---@type lc.direction

        size = { ---@type lc.size
            width = "90%",
            height = "75%",
        },

        result = {
            size = "60%", ---@type lc.size
        },

        testcase = {
            virt_text = true, ---@type boolean

            size = "40%", ---@type lc.size
        },
    },

    description = {
        position = "left", ---@type lc.position

        width = "40%", ---@type lc.size

        show_stats = true, ---@type boolean
    },

    hooks = {
        ---@type fun()[]
        ["enter"] = {},

        ---@type fun(question: lc.ui.Question)[]
        ["question_enter"] = {},

        ---@type fun()[]
        ["leave"] = {},
    },

    keys = {
        toggle = { "q" }, ---@type string|string[]
        confirm = { "<CR>" }, ---@type string|string[]

        reset_testcases = "r", ---@type string
        use_testcase = "U", ---@type string
        focus_testcases = "H", ---@type string
        focus_result = "L", ---@type string
    },

    ---@type lc.highlights
    theme = {},

    ---@type boolean
    image_support = false,
}

arg

Argument for Neovim

---@type string
arg = "leetcode.nvim"

See usage for more info

lang

Language to start your session with

---@type lc.lang
lang = "cpp"
available languages | Language | lang | | ---------- | ---------- | | C++ | cpp | | Java | java | | Python | python | | Python3 | python3 | | C | c | | C# | csharp | | JavaScript | javascript | | TypeScript | typescript | | PHP | php | | Swift | swift | | Kotlin | kotlin | | Dart | dart | | Go | golang | | Ruby | ruby | | Scala | scala | | Rust | rust | | Racket | racket | | Erlang | erlang | | Elixir | elixir | | Bash | bash |

cn

Use leetcode.cn instead of leetcode.com

cn = { -- leetcode.cn
    enabled = false, ---@type boolean
    translator = true, ---@type boolean
    translate_problems = true, ---@type boolean
},

storage

storage directories

---@type lc.storage
storage = {
    home = vim.fn.stdpath("data") .. "/leetcode",
    cache = vim.fn.stdpath("cache") .. "/leetcode",
},

plugins

plugins list

---@type table<string, boolean>
plugins = {
    non_standalone = false,
},

logging

Whether to log leetcode.nvim status notifications

---@type boolean
logging = true

injector

Inject code before or after your solution, injected code won't be submitted or run.

default imports

You can also pass before = true to inject default imports for the language. Supported languages are python, python3, java

Access default imports via require("leetcode.config.imports")

injector = { ---@type table<lc.lang, lc.inject>
    ["python3"] = {
        before = true
    },
    ["cpp"] = {
        before = { "#include <bits/stdc++.h>", "using namespace std;" },
        after = "int main() {}",
    },
    ["java"] = {
        before = "import java.util.*;",
    },
}

hooks

List of functions that get executed on specified event

hooks = {
    ---@type fun()[]
    ["enter"] = {},

    ---@type fun(question: lc.ui.Question)[]
    ["question_enter"] = {},

    ---@type fun()[]
    ["leave"] = {},
},

theme

Override the default theme.

Each value is the same type as val parameter in :help nvim_set_hl

---@type lc.highlights
theme = {
    ["alt"] = {
        bg = "#FFFFFF",
    },
    ["normal"] = {
        fg = "#EA4AAA",
    },
},

image support

Whether to render question description images using image.nvim

[!WARNING] Enabling this will disable question description wrap, because of https://github.com/3rd/image.nvim/issues/62#issuecomment-1778082534

---@type boolean
image_support = false,

📋 Commands

Leet opens menu dashboard

Some commands can take optional arguments. To stack argument values separate them by a ,

🚀 Usage

This plugin can be initiated in two ways:

Switching between questions

To switch between questions, use Leet tabs

Sign In

It is required to be signed-in to use leetcode.nvim

https://github.com/kawre/leetcode.nvim/assets/69250723/b7be8b95-5e2c-4153-8845-4ad3abeda5c3

🍴 Recipes

💤 lazy loading with lazy.nvim

[!WARNING] opting for either option makes the alternative launch method unavailable due to lazy loading

🧩 Plugins

Non-Standalone mode

To run leetcode.nvim in a non-standalone mode (i.e. not with argument or an empty Neovim session), enable the non_standalone plugin in your config:

plugins = {
    non_standalone = true,
}

You can then exit leetcode.nvim using :Leet exit command

🙌 Credits