Просмотр исходного кода

neovim: add treesitter and lsp config (#3129)

see utils/nvim/README.md
maan2003 2 лет назад
Родитель
Сommit
7f4cf794cf
6 измененных файлов с 78 добавлено и 16 удалено
  1. 5 0
      .pre-commit-config.yaml
  2. 18 0
      utils/nvim/README.md
  3. 28 0
      utils/nvim/carbon.lua
  4. 25 0
      utils/nvim/setup.sh
  5. 0 4
      utils/treesitter/README.md
  6. 2 12
      utils/vim/README.md

+ 5 - 0
.pre-commit-config.yaml

@@ -193,6 +193,11 @@ repos:
           - '; '
           - ''
           - --custom_format
+          - '\.lua$'
+          - ''
+          - '-- '
+          - ''
+          - --custom_format
           - '\.c$'
           - ''
           - '// '

+ 18 - 0
utils/nvim/README.md

@@ -0,0 +1,18 @@
+<!--
+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
+-->
+
+# 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`

+ 28 - 0
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 {}

+ 25 - 0
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

+ 0 - 4
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

+ 2 - 12
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/
-```