├── lua └── cmp_go_pkgs │ ├── init.lua │ └── source.lua ├── plugin └── cmp_go_pkgs.lua ├── stylua.toml ├── LICENSE └── README.md /lua/cmp_go_pkgs/init.lua: -------------------------------------------------------------------------------- 1 | return require("cmp_go_pkgs.source").new() 2 | -------------------------------------------------------------------------------- /plugin/cmp_go_pkgs.lua: -------------------------------------------------------------------------------- 1 | require("cmp").register_source("go_pkgs", require("cmp_go_pkgs")) 2 | 3 | vim.api.nvim_create_user_command("CurNode", function(c) 4 | require("cmp_go_pkgs.source").kek(c) 5 | end, {}) 6 | -------------------------------------------------------------------------------- /stylua.toml: -------------------------------------------------------------------------------- 1 | syntax = "All" 2 | column_width = 120 3 | line_endings = "Unix" 4 | indent_type = "Tabs" 5 | indent_width = 4 6 | quote_style = "AutoPreferDouble" 7 | call_parentheses = "Always" 8 | collapse_simple_statement = "Never" 9 | space_after_function_names = "Never" 10 | 11 | [sort_requires] 12 | enabled = false 13 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2024 Snikimonkd 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # cmp-go-pkgs 2 | 3 | nvim-cmp source for golang packages path. 4 | 5 | Loads once on LspAttach event. 6 | 7 | Shows only inside import block. 8 | 9 | https://github.com/Snikimonkd/cmp-go-pkgs/assets/72211350/4bf7b149-4f72-4f24-a529-794a9201dcf5 10 | 11 | ## Setup 12 | 13 | Setup with [lazy.nvim](https://github.com/folke/lazy.nvim): 14 | 15 | ```lua 16 | return { 17 | "hrsh7th/nvim-cmp", 18 | dependencies = { 19 | "Snikimonkd/cmp-go-pkgs", 20 | }, 21 | config = function() 22 | local cmp = require("cmp") 23 | 24 | cmp.setup({ 25 | sources = { 26 | { name = "go_pkgs" }, 27 | }, 28 | matching = { disallow_symbol_nonprefix_matching = false }, -- to use . and / in urls 29 | }) 30 | end, 31 | }, 32 | ``` 33 | 34 | ## Lspkind 35 | 36 | You can use it with [lspkind](https://github.com/onsails/lspkind.nvim): 37 | 38 | ```lua 39 | return { 40 | "hrsh7th/nvim-cmp", 41 | dependencies = { 42 | "Snikimonkd/cmp-go-pkgs", 43 | }, 44 | config = function() 45 | local cmp = require("cmp") 46 | local lspkind = require("lspkind") 47 | 48 | cmp.setup({ 49 | sources = { 50 | { name = "go_pkgs" }, 51 | }, 52 | formatting = { 53 | format = lspkind.cmp_format({ 54 | with_text = true, 55 | menu = { 56 | go_pkgs = "[pkgs]", 57 | }, 58 | }), 59 | }, 60 | }) 61 | end, 62 | }, 63 | ``` 64 | 65 | ## Inspiration 66 | 67 | (where i stole some code) 68 | 69 | https://github.com/ray-x/go.nvim 70 | 71 | https://github.com/hrsh7th/cmp-buffer 72 | 73 | https://github.com/onsails/lspkind.nvim 74 | 75 | -------------------------------------------------------------------------------- /lua/cmp_go_pkgs/source.lua: -------------------------------------------------------------------------------- 1 | local source = {} 2 | 3 | local items = {} 4 | 5 | local list_pkgs_command = "gopls.list_known_packages" 6 | 7 | source.new = function() 8 | local self = setmetatable({}, { __index = source }) 9 | 10 | return self 11 | end 12 | 13 | local init_items = function(a) 14 | local client = vim.lsp.get_client_by_id(a.data.client_id) 15 | local bufnr = vim.api.nvim_get_current_buf() 16 | local uri = vim.uri_from_bufnr(bufnr) 17 | local arguments = { { uri = uri } } 18 | 19 | client.request("workspace/executeCommand", { 20 | command = list_pkgs_command, 21 | arguments = arguments, 22 | }, function(arg1, arg2, _) 23 | if arg2 == nil and arg1 ~= nil then 24 | vim.print("LSP error", arg1) 25 | return 26 | end 27 | 28 | if arg1 == nil and arg2 == nil then 29 | vim.print("both arg1 and arg2 are nil") 30 | return 31 | end 32 | 33 | local tmp = {} 34 | 35 | for _, v in ipairs(arg2.Packages) do 36 | table.insert(tmp, { 37 | label = string.format('"%s"', v), 38 | kind = 9, 39 | insertText = string.format('"%s"', v), 40 | }) 41 | end 42 | 43 | items[bufnr] = tmp 44 | end, bufnr) 45 | end 46 | 47 | vim.api.nvim_create_autocmd({ "LspAttach" }, { 48 | pattern = { "*.go" }, 49 | callback = init_items, 50 | }) 51 | 52 | source._check_if_inside_imports = function() 53 | local cur_node = require("nvim-treesitter.ts_utils").get_node_at_cursor() 54 | 55 | local func = cur_node 56 | local flag = false 57 | 58 | while func do 59 | if func:type() == "import_declaration" then 60 | flag = true 61 | break 62 | end 63 | 64 | func = func:parent() 65 | end 66 | 67 | return flag 68 | end 69 | 70 | source.complete = function(self, _, callback) 71 | local ok = self._check_if_inside_imports() 72 | if ok == false then 73 | vim.print("not inside imports") 74 | callback() 75 | return 76 | end 77 | 78 | local bufnr = vim.api.nvim_get_current_buf() 79 | 80 | if next(items) == nil or items[bufnr] == nil then 81 | callback() 82 | return 83 | end 84 | 85 | callback({ items = items[bufnr], isIncomplete = false }) 86 | end 87 | 88 | source.is_available = function() 89 | return vim.bo.filetype == "go" 90 | end 91 | 92 | return source 93 | --------------------------------------------------------------------------------