languages.lua (4581B)
1 -- Sourcekit SDK map. 2 local sdk_map = { 3 iOS = { 4 platform = '/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform', 5 path = '/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk', 6 target = 'arm64-apple-ios13.0' 7 }, 8 macOS = { 9 platform = '/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform', 10 path = '/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk', 11 target = 'arm64-apple-macosx10.15' 12 } 13 } 14 -- Returns the sourcekit config for the specified SDK. 15 local function create_sourcekit_cfg(sdk) 16 local sourcekit_lsp_config = { 17 cmd = { 18 'sourcekit-lsp', 19 '-Xswiftc', 20 '-sdk', 21 '-Xswiftc', 22 sdk.path, 23 '-Xswiftc', 24 '-target', 25 '-Xswiftc', 26 sdk.target, 27 '-Xswiftc', 28 '-I' .. sdk.platform .. '/Developer/usr/lib', 29 '-Xswiftc', 30 '-F' .. sdk.platform .. '/Developer/Library/Frameworks', 31 }, 32 capabilities = { 33 workspace = { 34 didChangeWatchedFiles = { 35 dynamicRegistration = true, 36 }, 37 }, 38 }, 39 } 40 return sourcekit_lsp_config 41 end 42 43 -- UseIosSdk: configures sourcekit for iOS development. 44 vim.api.nvim_create_user_command('UseIosSdk', function() 45 local cfg = create_sourcekit_cfg(sdk_map.iOS) 46 vim.lsp.config.sourcekit = cfg 47 vim.lsp.enable('sourcekit') 48 end, {}) 49 50 -- UseMacosSdk: configures sourcekit for macOS development. 51 vim.api.nvim_create_user_command('UseMacosSdk', function() 52 local cfg = create_sourcekit_cfg(sdk_map.macOS) 53 vim.lsp.config.sourcekit = cfg 54 vim.lsp.enable('sourcekit') 55 end, {}) 56 57 return { 58 { 59 "https://gn.googlesource.com/gn", 60 opts = {}, 61 config = function(plugin) 62 vim.opt.rtp:append(plugin.dir .. "/misc/vim") 63 end 64 }, 65 { 66 "neovim/nvim-lspconfig", 67 event = { "BufReadPre", "BufNewFile" }, 68 config = function() 69 if vim.fn.executable('clangd') == 1 then 70 vim.lsp.enable('clangd') 71 end 72 if vim.fn.executable('lua-language-server') == 1 then 73 vim.lsp.enable('lua_ls') 74 end 75 if vim.fn.executable('zls') == 1 then 76 vim.lsp.enable('zls') 77 end 78 if vim.fn.executable('rust-analyzer') == 1 then 79 vim.lsp.enable('rust_analyzer') 80 end 81 if vim.fn.executable('beancount-language-server') == 1 then 82 vim.lsp.enable('beancount') 83 end 84 if vim.fn.executable('sourcekit-lsp') == 1 then 85 local cfg = create_sourcekit_cfg(sdk_map.iOS) 86 vim.lsp.config.sourcekit = cfg 87 vim.lsp.enable('sourcekit') 88 end 89 if vim.fn.executable('basedpyright-langserver') == 1 then 90 vim.lsp.enable('basedpyright') 91 end 92 93 vim.api.nvim_create_autocmd("LspAttach", { 94 group = vim.api.nvim_create_augroup("UserLspConfig", {}), 95 callback = function(ev) 96 local opts = { buffer = ev.buf, silent = true } 97 98 -- Enable auto-completion if supported/enabled by the client. 99 local client = vim.lsp.get_client_by_id(ev.data.client_id) 100 if client and client:supports_method("textDocument/completion") then 101 vim.lsp.completion.enable(true, ev.data.client_id, ev.buf, { 102 autotrigger = false, 103 }) 104 end 105 106 opts.desc = "See available code actions" 107 vim.keymap.set({"n", "v"}, "<leader>ca", vim.lsp.buf.code_action, opts) 108 opts.desc = "Smart rename" 109 vim.keymap.set("n", "<leader>cr", vim.lsp.buf.rename, opts) 110 opts.desc = "Show documentation for what is under cursor" 111 vim.keymap.set("n", "K", vim.lsp.buf.hover, opts) 112 opts.desc = "Format buffer" 113 vim.keymap.set("n", "<leader>F", vim.lsp.buf.format, opts) 114 end, 115 }) 116 end, 117 }, 118 { 119 "nvim-treesitter/nvim-treesitter", 120 build = ":TSUpdate", 121 init = function() 122 vim.api.nvim_create_autocmd("FileType", { 123 desc = "Automatically start Tree-sitter highlighting if a parser is available", 124 callback = function(args) 125 local lang = vim.treesitter.language.get_lang(args.match) 126 if not lang then return end 127 128 local parser = vim.treesitter.get_parser(args.buf, lang) 129 if not parser then return end 130 131 vim.treesitter.start(args.buf, lang) 132 end, 133 }) 134 end, 135 config = function() 136 require("nvim-treesitter").install({ 137 -- Bash, C, C++, Markdown, Lua, Markdown, Python supported by default. 138 "rust", 139 "swift", 140 "zig", 141 }) 142 end, 143 }, 144 } 145