diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 25e218f3a7a7..1be29cedcf49 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -193,6 +193,11 @@ repos: - '; ' - '' - --custom_format + - '\.lua$' + - '' + - '-- ' + - '' + - --custom_format - '\.c$' - '' - '// ' diff --git a/utils/nvim/README.md b/utils/nvim/README.md new file mode 100644 index 000000000000..d860bc134a73 --- /dev/null +++ b/utils/nvim/README.md @@ -0,0 +1,18 @@ + + +# Neovim plugin for Carbon + +Treesitter based syntax highlighting and language server client for Neovim. + +This requires neovim >= 0.9 and +[nvim-lspconfig](https://github.com/neovim/nvim-lspconfig) to be installed. + +1. Run `bazel build language_server` in project root. +2. Run `utils/nvim/setup.sh`. +3. Start nvim in carbon-lang root folder and open a carbon file. +4. View document symbols. If you have telescope.nvim installed, you can use + `:Telescope lsp_document_symbols` diff --git a/utils/nvim/carbon.lua b/utils/nvim/carbon.lua new file mode 100644 index 000000000000..188c78df7ce8 --- /dev/null +++ b/utils/nvim/carbon.lua @@ -0,0 +1,28 @@ +-- 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") + +-- LSP +local lsp = require 'lspconfig' +local configs = require 'lspconfig.configs' +local util = require 'lspconfig.util' + +-- Check if the config is already defined (useful when reloading this file) +if not configs.carbon then + configs.carbon = { + default_config = { + cmd = { "./bazel-bin/language_server/language_server" }, + filetypes = { "carbon" }, + root_dir = util.find_git_ancestor, + } + } +end +lsp.carbon.setup {} diff --git a/utils/nvim/setup.sh b/utils/nvim/setup.sh new file mode 100755 index 000000000000..d151577bdd86 --- /dev/null +++ b/utils/nvim/setup.sh @@ -0,0 +1,25 @@ +#!/bin/sh + +# 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 + +set -xeuo pipefail + +ROOT="$(git rev-parse --show-toplevel)" + +mkdir -p ~/.config/nvim/{lua,parser,queries} + +# add highlight queries +ln -sTf "$PWD/utils/treesitter/queries" ~/.config/nvim/queries/carbon + +# add carbon.lua +ln -sf "$PWD/utils/nvim/carbon.lua" ~/.config/nvim/lua/carbon.lua + +# load carbon.lua on startup +grep 'require "carbon"' ~/.config/nvim/init.lua || echo 'require "carbon"' >> ~/.config/nvim/init.lua + +# build treesitter +cd utils/treesitter +tree-sitter generate +clang -o ~/.config/nvim/parser/carbon.so -shared src/parser.c src/scanner.c -I ./src -Os -fPIC diff --git a/utils/treesitter/README.md b/utils/treesitter/README.md index 276e799c2e05..fca990fab57e 100644 --- a/utils/treesitter/README.md +++ b/utils/treesitter/README.md @@ -18,10 +18,6 @@ Tree-sitter is currently used for syntax highlighting in supported editors. 2. Install [Helix](https://docs.helix-editor.com/install.html). 3. Run `./helix.sh` -### Neovim - -TODO - ### Emacs TODO diff --git a/utils/vim/README.md b/utils/vim/README.md index e023ead0987c..c47e8bc0c7a3 100644 --- a/utils/vim/README.md +++ b/utils/vim/README.md @@ -6,8 +6,8 @@ Exceptions. See /LICENSE for license information. SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception --> -For Carbon developers using Vim or Neovim, this plugin provides syntax -highlighting for .carbon files found throughout `explorer/testdata` +For Carbon developers using Vim, this plugin provides syntax highlighting for +.carbon files found throughout `explorer/testdata` ## Repository and contributing @@ -32,13 +32,3 @@ install the syntax file. mkdir -p ~/.vim/syntax && cp syntax/carbon.vim ~/.vim/syntax/ mkdir -p ~/.vim/ftdetect && cp ftdetect/carbon.vim ~/.vim/ftdetect/ ``` - -### Neovim Users - -Instead of copying to the `~/.vim` directory, please use the `~/.config/nvim` -directory, or your custom Neovim root directory. - -``` -mkdir -p ~/.config/nvim/syntax && cp syntax/carbon.vim ~/.config/nvim/syntax/ -mkdir -p ~/.config/nvim/ftdetect && cp ftdetect/carbon.vim ~/.config/nvim/ftdetect/ -```