2022-04-02 22:28:21 +00:00
# Emacs Project Configuration
Emacs can be configured with `lsp-mode` and `clangd` to work well.
### clangd
The official clangd extension can be used for C++ comprehension. You
can use the following `.clangd` file placed in the project root:
```yaml
CompileFlags:
2022-10-04 00:32:37 +00:00
CompilationDatabase: Build/x86_64
2022-04-02 22:28:21 +00:00
Add:
- "-D__serenity__"
- "-UNO_TLS"
2024-05-30 21:06:21 +00:00
- "-I/path/to/ladybird/Toolchain/Local/x86_64/x86_64-pc-serenity/include/c++/13.1.0"
- "-I/path/to/ladybird/Toolchain/Local/x86_64/x86_64-pc-serenity/include/c++/13.1.0/x86_64-pc-serenity"
2024-02-05 11:28:11 +00:00
Diagnostics:
UnusedIncludes: None
MissingIncludes: None
2022-04-02 22:28:21 +00:00
```
2024-05-30 21:06:21 +00:00
You will need to change `/path/to/ladybird` and change `13.1.0` to
2022-04-02 22:28:21 +00:00
whatever your GCC toolchain version at the time is.
Run cmake (`Meta/serenity.sh run` or similar) at least once for this
2022-10-04 00:32:37 +00:00
to work, as it will generate the `Build/x86_64/compile_commands.json`
2022-04-02 22:28:21 +00:00
that is needed by `clangd` .
### lsp-mode
```lisp
(use-package lsp-mode
:hook ((c++-mode) . lsp-deferred)
:commands lsp
:config
(setq lsp-clients-clangd-args '("-j=4" "-background-index" "--log=error" "--clang-tidy" "--enable-config"))
;; Optionally, set the location of clangd -- See notes below for options.
(setq lsp-clangd-binary-path "/usr/bin/clangd"))
```
### clangd
There are a few different ways to specify which clangd to use:
- By default, without configuration `lsp-mode` will try to find and use your system `clangd` . This is the easiest solution, but your system clangd might be out of date.
- You can manually specify any `clangd` binary with `lsp-clangd-binary-path` , as shown in the use-package example above.
- You can have `lsp-mode` manage your `clangd` installation with emacs' `lsp-install-server` . This will install a `clangd` binary for you.
2024-05-30 21:06:21 +00:00
- You can build the LLVM toolchain, including `clangd` , from Ladybird's repository. This is an advanced option that is not currently documented.
2022-04-02 22:28:21 +00:00
### clang-format
There are multiple packages to handle auto formatting with
`clang-format` , within emacs. Choose what works best for your setup:
- [format-all-mode ](https://github.com/lassik/emacs-format-all-the-code )
- [clang-format-plus ](https://github.com/SavchenkoValeriy/emacs-clang-format-plus )
2022-08-06 10:35:29 +00:00
Alternatively, this can be done without additional packages, using `lsp-mode` .
You can use the following `.dir-locals.el` file placed in the project root:
```lisp
((c++-mode
(eval add-hook 'before-save-hook #'lsp-format-buffer nil t)))
2022-04-02 22:28:21 +00:00
```