Language Server Protocol - Helix and Emacs
Jan Walter August 12, 2023 [EDIT] #helix #emacs #pbrt-v4I wrote already about the Language Server Protocol and Text Editors a while ago, but this time I would like to focus on a larger code base and how to use your text editor to navigate through the source code after compilation.
As an example I compiled pbrt-v4 on
my MacBook Pro and used the CMAKE_EXPORT_COMPILE_COMMANDS
option of
CMake to create a compile_commands.json
file
(see CMake
Tutorial):
[
{
}
The resulting compile_commands.json
file contains basically for each
C++ file a description how the file was compiled, including the full
path to the compiler and all compiler options and defines.
Helix
So let's try to find an interesting C++ file to start with and use the Helix editor to view it:
;
)
)
)
)
The syntax highlighting works already, but on my computer the path to
the language server
is not found by default:
Let's fix that:
|
PATH='...:/usr/local/bin:...'
|
Now we are looking for some warnings during compilation:
After loading the header file in question (which created the first warning) you can see an orange dot in the lower right part:
You can jump to that line by pressing ]
followed by d
. The warning
message which was shown during compilation is now visible in orange in
the upper right corner.
The second file mentioned above (PtexUtils.cpp
) has 4 warnings in
it. Try to load the file into Helix and navigate to the lines
responsible for those warnings.
Emacs
Alternatively you can load one of the files into Emacs and use one of the packages provided there:
After loading the file you can replace the default cc-mode
and
activate syntax highlighting using
Tree-sitter via M-x tree-sitter-hl-mode
(for older Emacs versions you might have to
install the Emacs binding
first).
For using the language server you can use e.g. the Emacs LSP client
Emacs Polyglot via M-x eglot
and navigate via M-x flymake-goto-next-error
to the next warning
message.
Or you use M-x flymake-show-buffer-diagnostics
to see all warnings
in an extra buffer where you can select the warning line and jump
directly to C++ line which caused it (see above).
Others
The Language Server
Protocol (LSP)
can be used with many other text editors or IDEs. I just wanted to
mention two of my favourite text editors, Emacs
which is around for
ages, and Helix
, which is pretty new, written in
Rust, and looks pretty promising
(regarding LSP and Tree-sitter).