2024年12月21日 星期六

Neovim+LazyVIM

Neovim, 編譯時,相依滿多東西的,麻煩
a. sudo apt install npm lua-luv-dev lua-lpeg \
   libunibilium-dev libutf8proc-dev libluajit-5.1-dev -y

b. 其它的,缺什麼補什麼
   lua, tree-sitter
   fzf, luarocks(不裝,Checkhealth裡會有WARNING),但它不支援lua 5.5

c. 符號亂碼的話,裝Nerd Font字型:
       nerdfonts
   下載:
       JetBrainsMono Nerd Font
   安裝:
       font
   Terminal:
       要選有Nerd Font的字型,所有符號就能正常下載
       ex: JetBrainsMono Nerd Font
d. (用cmake build...有相依性問題)
   $ make distclean
   $ make CMAKE_BUILD_TYPE=Release
   $ sudo make install

LazyVim
安裝:How to Install

Lazy安裝插件: 剛進去nvim時,壓一下x -> LazyVim Extras, 壓x安裝

Commands for LazyVIM
:checkhealth
:checkhealth mason
:Mason  //初始化 Mason.nvim,通過 mason 可以自動裝各種LSP

Press "i" to install



Color theme ( for nvim -d A_file AA_file )
:colorscheme   ->  tokyonight-moon

$ vim ~/.config/nvim/lua/plugins/colorscheme.lua

return {
  {
    "folke/tokyonight.nvim",
    lazy = false,
    priority = 1000,
    opts = {
      style = "moon", -- 確保固定使用 moon 風格
      on_highlights = function(hl, c)
        hl.DiffChange = { bg = "#7636b0", fg = "NONE", reverse = false, nocombine = true }
        hl.DiffText   = { bg = "#e85050", fg = "#ffffff", bold = true, underline = true, reverse = false, nocombine = true } 
        hl.DiffAdd    = { bg = "#203a26", fg = "NONE", reverse = false, nocombine = true }
        hl.DiffDelete = { bg = "#4a2426", fg = "NONE", reverse = false, nocombine = true }
      end,
    },
  },
}
/home/ubuntu/.config/nvim/stylua.toml
indent_type = "Spaces"
indent_width = 4   //程式縮排設定這個有效
column_width = 120

/home/ubuntu/.config/nvim/lua/config/options.lua
Default options that are always set: 
https://github.com/LazyVim/LazyVim/blob/main/lua/lazyvim/config/options.lua


Quick keyboard map, <leader>:space key
Switch buffer: ]b, [b
Searching for Keywords: <leader> + /

list all symbol: <leader> + c + s/S

keymap corresponds plug-ins
:verbose map <leader>cs

Manually loading clang( after updating to someone version)
:lua require('lspconfig').clangd.setup{} //try two or more times

  • Trace code
  • compile_commands.json, for clangd //A compilation database describes compile commands for a codebase
    放在根專案下
    a. Makefie:
        $ sudo apt install bear
        $ bear -- make
    
    b. CMake:
        $ cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=1 ..
        or
        set(CMAKE_EXPORT_COMPILE_COMMANDS True) at CMakeLists.txt
    
    c. Meson:
        $ meson setup builddir --buildtype debug  //it will create a compile_commands.json in builddir
    
    ex:
    .
    ├── compile_commands.json
    ├── include
    │   └── hello.h
    ├── Makefile
    └── src
        ├── hello.c
        └── main.c
    
    go to implementation(gI),它只會回應:[Fzf-lua] No implementations found

    for kernel
    $ make menuconfig
    
    $ make CC=clang
    
    $ python3 ./scripts/clang-tools/gen_compile_commands.py
    


  • codecompanion
  • Install and Sync
    ~/.config/nvim/lua/plugins/codecompanion.lua
    
    -- lua/plugins/codecompanion.lua
    
    return {
      "olimorris/codecompanion.nvim",
      dependencies = {
        "nvim-lua/plenary.nvim",
        "nvim-treesitter/nvim-treesitter",
        "hrsh7th/nvim-cmp",
        "nvim-telescope/telescope.nvim",
      },
      config = function()
        require("codecompanion").setup({
          strategies = {
            chat = {
              adapter = "ollama_orin",
            },
            inline = {
              adapter = "ollama_orin",
            },
            agent = {
              adapter = "ollama_orin",
            },
          },
          -- This block overrides the default chat behavior globally
          interactions = {
            chat = {
              opts = {
                system_prompt = [[You are an expert AI coding assistant. The user will provide file contents, code snippets, or terminal outputs directly within the chat history using markdown or context tags (like #file or #buffer).
    
    CRITICAL DIRECTION: Always treat this injected text as the literal content of the files. You have full visibility of this data. Never state that you cannot access, view, or read the files provided by the user. If the user asks about a file tagged with #file, look at the text block loaded into the chat context, as that is the file's exact content.
    
    Use Markdown formatting in your answers. When suggesting code changes or new content, use Markdown code blocks. To start a code block, use 4 backticks. After the backticks, add the programming language name as the language ID. To close a code block, use 4 backticks on a new line.]],
              },
            },
          },
          adapters = {
            -- Nesting under 'http' satisfies the internal layout validator
            http = {
              ollama_orin = function()
                -- Extending the native "ollama" preset inside the http block
                -- automatically configures roles, streaming, and correct endpoints
                return require("codecompanion.adapters").extend("ollama", {
                  env = {
                    url = "https://111.222.111.222",
                  },
                  schema = {
                    model = {
                      default = "codestral",
                    },
                    num_ctx = {
                      default = 32768, -- Utilizing the full context window for large log/source files
                    },
                  },
                  opts = {
                    extra_args = { "-k", "-s" }, -- Securely bypasses local self-signed HTTPS blocks
                  },
                })
              end,
            },
          },
        })
    
        -- Keymaps
        local map = vim.keymap.set
        map({ "n", "v" }, "<leader>ac", "<cmd>CodeCompanionChat Toggle<cr>", { desc = "Toggle AI Chat" })
        map({ "n", "v" }, "<leader>aa", "<cmd>CodeCompanionActions<cr>", { desc = "AI Actions Menu" })
        map("v", "<leader>ce", "<cmd>CodeCompanion /explain<cr>", { desc = "Explain Code" })
        map("v", "<leader>ai", ":<C-u>CodeCompanion ", { desc = "Inline AI Prompt" })
      end,
    }
    


  • LintaoAmons/bookmarks.nvim
  • -- with lazy.nvim
    return {
      "LintaoAmons/bookmarks.nvim",
      -- tag = "v4.0.0",
      dependencies = {
        { "kkharji/sqlite.lua" },
        { "nvim-telescope/telescope.nvim" },
        { "stevearc/dressing.nvim" }, -- optional: better UI
      },
      config = function()
        local opts = {} -- go to the following link to see all the options in the deafult config file
        require("bookmarks").setup(opts) -- you must call setup to init sqlite db
      end,
      keys = {
        { "mm", "<cmd>BookmarksMark<cr>", desc = "Mark current line into active BookmarkList", mode = { "n", "v" } },
        { "mo", "<cmd>BookmarksGoto<cr>", desc = "Go to bookmark at current active BookmarkList", mode = { "n", "v" } },
        { "ml", "<cmd>BookmarksTree<cr>", desc = "Browse bookmarks in tree view", mode = { "n", "v" } },
      },
    }
    
    -- run :BookmarksInfo to see the running status of the plugin
    
    ~/.local/share/nvim/bookmarks.backup/bookmarks_yyyymmdd.sqlite.db,一個約16kB
    ~/.local/share/nvim/bookmarks.sqlite.db

  • dap
  • GDB, at least version 14+
    ~/.local/share/nvim/lazy/LazyVim/lua/lazyvim/plugins/extras/dap/core.lua
    ...
    keys = {
          { "<leader>dB", function() require("dap").set_breakpoint(vim.fn.input('Breakpoint condition: ')) end, desc = "Breakpoint Condition" },
          { "<leader>db", function() require("dap").toggle_breakpoint() end, desc = "Toggle Breakpoint" },
          { "<leader>dc", function() require("dap").continue() end, desc = "Run/Continue" },
    ...
    
    Useage:
    $ gdb -g -Wall gdb_test.c -o gdb_test
    
    lazynvim:
    a. key: <leader>db, //toggle breakpoing
    b. start: <leader>da  //Lanuch file -> Run with args(enter) -> Patch to executable (enter gdb_test)
    

  • 內建bookmark
  • Delete: :delmarks a-z   or  :delmarks!
    

  • 莫名升到nvim 0.11後就有問題了, 再用最新版的build後就沒了, 14fb5b1c79f8d9fca5a8f2b341cf6fa2bb775540, NVIM v0.12.0-dev-213+g1670fbee0f
  • ~/.local/share/nvim/lazy/nui.nvim/lua/nui/utils/init.lua:379: Unknown option 'winborder'
    先把這段程式註記掉...
    -- if _.feature.v0_11 then
    --   function _.get_default_winborder()
    --     local style = vim.api.nvim_get_option_value("winborder", {})
    --     if style == "" then
    --       return "none"
    --     end
    --     return style
    --   end
    -- end
    
ref:
1. keymaps
2. 使用LazyVim将Neovim打造成强大IDE(常用快捷键)
3. gemini.nvim

沒有留言:

張貼留言

/*nice*/