dotfiles

Personal dotfiles
git clone https://git.bracken.jp/dotfiles.git
Log | Files | Refs | LICENSE

languages.lua (6021B)


      1 local function get_sdk_path(sdk)
      2   return vim.fn.system('xcrun --show-sdk-path --sdk ' .. sdk):gsub('\n', '')
      3 end
      4 local function get_platform_path(sdk)
      5   return vim.fn.system('xcrun --show-sdk-platform-path --sdk ' .. sdk):gsub('\n', '')
      6 end
      7 
      8 local sdk_map = {
      9   iOS = {
     10     platform = get_platform_path('iphoneos'),
     11     path = get_sdk_path('iphoneos'),
     12     target = 'arm64-apple-ios13.0'
     13   },
     14   macOS = {
     15     platform = get_platform_path('macosx'),
     16     path = get_sdk_path('macosx'),
     17     target = 'arm64-apple-macosx10.15'
     18   }
     19 }
     20 -- Returns the sourcekit config for the specified SDK.
     21 local function create_sourcekit_cfg(sdk)
     22   local sourcekit_lsp_config = {
     23     cmd = {
     24       'sourcekit-lsp',
     25       '-Xswiftc',
     26       '-sdk',
     27       '-Xswiftc',
     28       sdk.path,
     29       '-Xswiftc',
     30       '-target',
     31       '-Xswiftc',
     32       sdk.target,
     33       '-Xswiftc',
     34       '-I' .. sdk.platform .. '/Developer/usr/lib',
     35       '-Xswiftc',
     36       '-F' .. sdk.platform .. '/Developer/Library/Frameworks',
     37     },
     38     filetypes = { 'swift', 'c', 'cpp', 'objective-c', 'objective-cpp' },
     39     root_markers = { '.sourcekit-lsp', 'Package.swift', '.git' },
     40     capabilities = {
     41       workspace = {
     42         didChangeWatchedFiles = {
     43           dynamicRegistration = true,
     44         },
     45       },
     46     },
     47   }
     48   return sourcekit_lsp_config
     49 end
     50 
     51 -- UseIosSdk: configures sourcekit for iOS development.
     52 vim.api.nvim_create_user_command('UseIosSdk', function()
     53   local cfg = create_sourcekit_cfg(sdk_map.iOS)
     54   vim.lsp.config.sourcekit = cfg
     55   vim.lsp.enable('sourcekit')
     56 end, {})
     57 
     58 -- UseMacosSdk: configures sourcekit for macOS development.
     59 vim.api.nvim_create_user_command('UseMacosSdk', function()
     60   local cfg = create_sourcekit_cfg(sdk_map.macOS)
     61   vim.lsp.config.sourcekit = cfg
     62   vim.lsp.enable('sourcekit')
     63 end, {})
     64 
     65 return {
     66   {
     67     "https://gn.googlesource.com/gn",
     68     opts = {},
     69     config = function(plugin)
     70       vim.opt.rtp:append(plugin.dir .. "/misc/vim")
     71     end
     72   },
     73   {
     74     "neovim/nvim-lspconfig",
     75     event = { "BufReadPre", "BufNewFile" },
     76     config = function()
     77       if vim.fn.executable('clangd') == 1 then
     78         vim.lsp.enable('clangd')
     79       end
     80       if vim.fn.executable('lua-language-server') == 1 then
     81         vim.lsp.enable('lua_ls')
     82       end
     83       if vim.fn.executable('zls') == 1 then
     84         vim.lsp.enable('zls')
     85       end
     86       if vim.fn.executable('rust-analyzer') == 1 then
     87         vim.lsp.enable('rust_analyzer')
     88       end
     89       if vim.fn.executable('beancount-language-server') == 1 then
     90         vim.lsp.enable('beancount')
     91       end
     92       if vim.fn.executable('sourcekit-lsp') == 1 then
     93         local cfg = create_sourcekit_cfg(sdk_map.iOS)
     94         vim.lsp.config.sourcekit = cfg
     95         vim.lsp.enable('sourcekit')
     96       end
     97       if vim.fn.executable('basedpyright-langserver') == 1 then
     98         vim.lsp.enable('basedpyright')
     99       end
    100       if vim.fn.executable('markdown-oxide') == 1 then
    101         vim.lsp.config('markdown_oxide', {
    102           capabilities = vim.lsp.protocol.make_client_capabilities()
    103         })
    104         vim.lsp.enable('markdown_oxide')
    105       end
    106 
    107       vim.api.nvim_create_autocmd("LspAttach", {
    108         group = vim.api.nvim_create_augroup("UserLspConfig", {}),
    109         callback = function(ev)
    110           local opts = { buffer = ev.buf, silent = true }
    111 
    112           -- Enable auto-completion if supported/enabled by the client.
    113           local client = vim.lsp.get_client_by_id(ev.data.client_id)
    114           if client and client:supports_method("textDocument/completion") then
    115             vim.lsp.completion.enable(true, ev.data.client_id, ev.buf, {
    116               autotrigger = false,
    117             })
    118           end
    119 
    120           opts.desc = "See available code actions"
    121           vim.keymap.set({"n", "v"}, "<leader>ca", vim.lsp.buf.code_action, opts)
    122           opts.desc = "Smart rename"
    123           vim.keymap.set("n", "<leader>cr", vim.lsp.buf.rename, opts)
    124           opts.desc = "Show documentation for what is under cursor"
    125           vim.keymap.set("n", "K", vim.lsp.buf.hover, opts)
    126           opts.desc = "Format buffer"
    127           vim.keymap.set("n", "<leader>F", vim.lsp.buf.format, opts)
    128 
    129           -- Setup markdown-oxide daily note command if attached.
    130           if client and client.name == "markdown_oxide" then
    131             vim.api.nvim_buf_create_user_command(ev.buf, "Daily", function(args)
    132               local input = args.args
    133               local arguments = {}
    134               if input ~= "" then
    135                 arguments = { input }
    136               end
    137               client:exec_cmd({ command = "jump", arguments = arguments }, { bufnr = ev.buf })
    138             end, { desc = "Open daily note", nargs = "*" })
    139           end
    140         end,
    141       })
    142     end,
    143   },
    144   {
    145     "nvim-treesitter/nvim-treesitter",
    146     build = ":TSUpdate",
    147     init = function()
    148       vim.api.nvim_create_autocmd("FileType", {
    149         desc = "Automatically start Tree-sitter highlighting if a parser is available",
    150         callback = function(args)
    151           local lang = vim.treesitter.language.get_lang(args.match)
    152           if not lang then return end
    153       
    154           local ok, parser = pcall(vim.treesitter.get_parser, args.buf, lang)
    155           if not ok or not parser then return end
    156       
    157           vim.treesitter.start(args.buf, lang)
    158         end,
    159       })
    160     end,
    161     config = function()
    162       require("nvim-treesitter").install({
    163         -- Bash, C, C++, Markdown, Lua, Markdown, Python supported by default.
    164         "rust",
    165         "swift",
    166         "zig",
    167       })
    168     end,
    169   },
    170   {
    171     'MeanderingProgrammer/render-markdown.nvim',
    172     -- dependencies = { 'nvim-treesitter/nvim-treesitter', 'nvim-mini/mini.nvim' },            -- if you use the mini.nvim suite
    173     -- dependencies = { 'nvim-treesitter/nvim-treesitter', 'nvim-mini/mini.icons' },        -- if you use standalone mini plugins
    174     -- dependencies = { 'nvim-treesitter/nvim-treesitter', 'nvim-tree/nvim-web-devicons' }, -- if you prefer nvim-web-devicons
    175     ---@module 'render-markdown'
    176     ---@type render.md.UserConfig
    177     opts = {},
    178   }
    179 }
    180