Files
carbon-lang/utils/nvim/carbon.lua
nicole mazzuca 3056e7e318 nvim: Only enable the integration in the carbon repo (#7995)
This fixes an error that would happen when opening neovim in a
non-git-controlled directory, where `root` is `nil` and therefore one
cannot `..` it as a string.
2026-10-19 06:07:22 +00:00

39 lines
1.1 KiB
Lua

-- Part of the Carbon Language project, under the Apache License v2.0 with LLVM
-- Exceptions. See /LICENSE for license information.
-- SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
vim.filetype.add({
extension = {
carbon = 'carbon',
},
})
vim.treesitter.language.add("carbon")
vim.api.nvim_create_autocmd('FileType', {
pattern = { 'carbon' },
callback = function() vim.treesitter.start() end,
})
-- LSP
function find_git_ancestor(startpath)
return vim.fs.dirname(vim.fs.find('.git', { path = startpath, upward = true })[1])
end
local root = find_git_ancestor(vim.env.PWD);
toolchain_path = nil
if root and vim.uv.fs_stat(root .. "/bazel/carbon_rules") then
-- when we're in the carbon repo, use the toolchain from there
-- (note that this won't update if one `cd`s into the carbon repo)
toolchain_path = root .. "/bazel-bin/toolchain/carbon"
elseif vim.fn.executable("carbon") then
-- otherwise, check if it's on the path
toolchain_path = "carbon"
end
if toolchain_path then
vim.lsp.config("carbon", {
cmd = { toolchain_path, "language-server" },
filetypes = { "carbon" },
})
vim.lsp.enable("carbon")
end