Sublime Forum

Clara: C++ semantic code completion

#1

Fast C++ auto completion plugin based on clang. The downside is that it has a compiled component. I made a pre-release on GitHub with binaries that you can download and try.

Download it here.

It’s very much experimental at this point, so I would be very grateful if you report any issues or crashes. Currently only Linux/OSX 64-bit builds are present.

The way this plugin works in practice is as follows. You’re working on a big CMake-enabled project and you have your build folder somewhere with your build artifacts. In your top-level CMakeLists.txt file, you must have the line

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

somewhere, or you could configure your build folder with the option

$ cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON ...

So that’s the first step. The second step is to let Clara know where that build folder is. You do that by having a .sublime-project file, and in that project file you add the following dictionary:

"cmake":
{
    "build_folder": "${project_path}/build"
}

Indeed, this supports any of the sublime-provided ${...} variables, so you can point to the build folder relative to your project source folder.

The above two steps need to be performed for every new project. There are also some settings that need to be set up for the Clara plugin itself. In particular, Clara needs to know where all of the system headers on your platform are located. You can set that up by running the command

Clara: Generate System Headers

from the command palette. This calls a subprocess and assumes that the clang binary is present on your system.
Another optional thing you can do for set up is to write default C+±syntax-specific completion settings to your Packages/User directory. You can do that by running the command

Clara: Create Auto-Complete Triggers

If such a file already exists, it will prompt you.

And that’s it, at that point you should get auto-completion results in implementation files (.cpp) of your project.

I use this plugin myself (of course), and I am aware that in some rare cases it might crash the plugin_host executable. Just restart sublime in that case and everything should be fine. These issues seem to stem from the clang library, so I’m not sure how to deal with that.

Some limitations:

  1. Doesn’t do auto-completion in header files.
  2. Must parse an implementation file before use.
  3. Doesn’t cache parsed files for later use.
  4. Might crash plugin_host in some cases.

Points (1) and (3) I’d like to fix sometime in the future. Point (2) is unavoidable, although with point (3) should not be a nuisance. Fixing point (4) would require me to analyze the clang library, which could be time-consuming.

Some pros:

  1. Very responsive.
  2. Easy to get up and running.
  3. Compilation-options-aware.

You can tweak the options by going to Preferences -> Packages Settings -> Clara -> Settings. Copy and paste some of the settings from the Default settings to your User settings and tweak them however you like. For instance, in the Default settings there is the line

"include_brief_comments": true

which will embed brief doxygen comments in the auto-completer widget. You can disable that by adding

"include_brief_comments": false

to your user settings.

3 Likes

#2

Hello! Your plugin looks interesting and I like the approach of using commands for generating system headers and such.

However as a developer of EasyClangComplete, I can’t help myself but ask if you tried my plugin? Along with a bunch of contributors, we have solved most of the issues you are referring here. For example, we don’t need to compile clang ourselves and it works on Linux, windows and osx. We also have caching of the translation units for opened files.

Anyway, I am interested in two things here. If you have tried my plugin, what was it that was so bad, that you opted for writing your own. And the second question is I would love to know how realistic it is to ask you to collaborate on my plugin instead of creating a new clang based completion plugin.

Again, by no means I want to discourage you from creating a new plugin as it is a lot of fun, I know that. I just wanted to point out that there is an actively developed plugin that follows similar philosophy to yours, is mostly unit tested, that would benefit from contributions.

Thanks for your attention! :slight_smile:

0 Likes

#3

I can’t help myself but ask if you tried my plugin?

I did! And thank you for that plugin!

we don’t need to compile clang ourselves and it works on Linux, windows and osx.

Sure, that’s the advantage of EasyClangComplete. I’d like to point out that not all of LLVM and Clang is compiled for this plugin, just parts of it that it needs. It then exposes some of the functionality with pybind11 to python and from there it hooks into Sublime’s plugin ecosystem.

If you have tried my plugin, what was it that was so bad, that you opted for writing your own.

The workflow of EasyClangComplete didn’t align with what I had in mind. That’s just a preference, not a bug. I didn’t find ECC bad, but I stumbled upon pybind11 and wanted to see how far I could go.

I would love to know how realistic it is to ask you to collaborate on my plugin

Maybe I will! :slightly_smiling:

I just wanted to point out that there is an actively developed plugin that follows similar philosophy to yours

Yeah I’m aware. But I disagree with the similar philosophy. Yes, they both try to solve the same problem, but I understand that ECC wants to be portable, falling back to calling into the clang binary if libclang cannot be found. My plugin doesn’t attempt that level of portability. It wants to work native or it’ll go home.

0 Likes

#4

Thanks! That clears up everything! :slightly_smiling:

Yeah, the clang binary part is more a historically motivated thing. That was the only way to have it work at all at first before the python ctypes bundled with sublime text were fixed.

I stumbled upon pybind11 and wanted to see how far I could go

This I totally can understand :slight_smile: A completely valid reason to me.

Anyway, we both seem to try to solve the autocompletion with clang for humans. Fast and with minimal configuration.
I do understand your motivation and wish you all the luck, but if at any point of time, you would like to contribute to ECC, I will be happy to see you there :slightly_smiling:

0 Likes