Language Server Protocol - Helix and Emacs

Jan Walter August 12, 2023 [EDIT] #helix #emacs #pbrt-v4

I 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):

% cd ~/git/github/pbrt-v4
% mkdir build
% cd build
% cmake .. -DCMAKE_EXPORT_COMPILE_COMMANDS=1
% cmake --build .
...
% head compile_commands.json
[
{
  "directory": "/Users/jan/git/github/pbrt-v4/build",
  "command": "/Library/Developer/CommandLineTools/usr/bin/c++ -DPBRT_HAVE_MMAP ... -std=gnu++17 ...",
  "file": "/Users/jan/git/github/pbrt-v4/src/pbrt/cmd/soac.cpp",
  "output": "CMakeFiles/soac.dir/src/pbrt/cmd/soac.cpp.o"
},
...

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:

% ./pbrt                     
pbrt version 4 (built Aug 12 2023 at 09:56:20)
Copyright (c)1998-2021 Matt Pharr, Wenzel Jakob, and Greg Humphreys.
The source code to pbrt (but *not* the book contents) is covered by the Apache 2.0 License.
See the file LICENSE.txt for the conditions of the license.
^C
% rg "pbrt version 4" ..
../src/pbrt/cmd/pbrt.cpp
217:        printf("pbrt version 4 (built %s at %s)\n", __DATE__, __TIME__);
% hx ../src/pbrt/cmd/pbrt.cpp

Syntax highlighting with Helix

The syntax highlighting works already, but on my computer the path to the language server is not found by default:

% hx --health cpp            
Configured language server: clangd
Binary for language server: 'clangd' not found in $PATH
Configured debug adapter: lldb-vscode
Binary for debug adapter: 'lldb-vscode' not found in $PATH
Highlight queries: ✓
Textobject queries: ✓
Indent queries: ✓

Let's fix that:

% set | grep "^PATH"
PATH='...:/usr/local/bin:...'
% locate clangd | head -n 1
/Library/Developer/CommandLineTools/usr/bin/clangd
% export PATH=/Library/Developer/CommandLineTools/usr/bin:$PATH
% hx --health cpp
Configured language server: clangd
Binary for language server: /Library/Developer/CommandLineTools/usr/bin/clangd
Configured debug adapter: lldb-vscode
Binary for debug adapter: 'lldb-vscode' not found in $PATH
Highlight queries: ✓
Textobject queries: ✓
Indent queries: ✓

Now we are looking for some warnings during compilation:

% cmake --build .
...
/Users/jan/git/github/pbrt-v4/src/ext/ptex/src/ptex/PtexCache.h:289:21: warning: ...
...
/Users/jan/git/github/pbrt-v4/src/ext/ptex/src/ptex/PtexUtils.cpp:51:22: warning: ...
% hx /Users/jan/git/github/pbrt-v4/src/ext/ptex/src/ptex/PtexCache.h

After loading the header file in question (which created the first warning) you can see an orange dot in the lower right part:

Diagnostic message from language server within Helix

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:

% emacs -nw /Users/jan/git/github/pbrt-v4/src/ext/ptex/src/ptex/PtexUtils.cpp

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.

Diagnostic messages from language server within Emacs

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).

Back to top