Skip to content

Add LSP config generation for C/C++ native code - #143

Merged
khamilowicz merged 4 commits into
masterfrom
work-on-lsp
May 27, 2026
Merged

Add LSP config generation for C/C++ native code#143
khamilowicz merged 4 commits into
masterfrom
work-on-lsp

Conversation

@khamilowicz

Copy link
Copy Markdown
Contributor

Summary

  • Adds --generate-lsp-config flag to mix compile.bundlex that produces compile_commands.json and compile_flags.txt for clangd and other LSP tools
  • Parses build commands from the bundlex build script to extract compiler flags and source file paths
  • Normalizes Homebrew Erlang version-pinned paths to stable opt/ symlinks for portability
  • Writes compile_commands.json to the project root; compile_flags.txt is written to project root only (common flags intersection), avoiding pollution of VCS-tracked source directories
  • Adds jason as a runtime dependency for JSON serialization

Notes

  • compile_commands.json uses the actual compiler working directory (Path.dirname(source)) per entry so clangd resolves relative #include paths correctly
  • skip_command?/1 checks the basename of the first parsed token, so tools invoked via full path (e.g. /usr/bin/ar) are correctly filtered out
  • The shell argument tokenizer strips quotes and splits on whitespace — paths with spaces are a known limitation

Test plan

  • Run mix compile.bundlex --generate-lsp-config in a project with C natives and verify compile_commands.json and compile_flags.txt are generated at the project root
  • Open the project in an editor with clangd and confirm go-to-definition / autocompletion works on C/C++ source files
  • Verify no compile_flags.txt files are written inside source directories

🤖 Generated with Claude Code

@khamilowicz
khamilowicz requested a review from FelonEkonom as a code owner May 19, 2026 14:11
@khamilowicz
khamilowicz marked this pull request as draft May 19, 2026 14:15
@khamilowicz
khamilowicz marked this pull request as ready for review May 19, 2026 14:21
khamilowicz and others added 2 commits May 19, 2026 16:26
@khamilowicz khamilowicz linked an issue May 19, 2026 that may be closed by this pull request
@FelonEkonom
FelonEkonom requested a review from varsill May 21, 2026 09:01
@FelonEkonom FelonEkonom moved this to In Review in Smackore May 21, 2026
Comment thread lib/bundlex/lsp/config.ex
[first | _rest] -> Path.basename(first)
end

binary in ~w[mkdir rm ar] ||

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one will return false for /usr/bin/ar 🤔

@khamilowicz khamilowicz May 22, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how so? ok, if it was actually /usr/bin/ar string, then sure, but https://github.com/membraneframework/bundlex/blob/work-on-lsp/lib/bundlex/toolchain/common/unix.ex#L76-L76 returns bare ar.
We could use String.ends_with?, but, since we are controlling emitted commands, and they are within the same repo, IMO it is fine. The comment could be less confusing.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, I skipped this Path.basename part

Comment thread lib/bundlex/lsp/config.ex Outdated
Comment thread lib/bundlex/lsp/config.ex Outdated
Comment thread lib/bundlex/lsp/config.ex Outdated
@khamilowicz
khamilowicz requested a review from varsill May 22, 2026 09:51

@varsill varsill left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🥇

Comment thread lib/bundlex/lsp/config.ex
Comment on lines +1 to +25
defmodule Bundlex.LSP.Config do
@moduledoc """
Generates LSP configuration files (compile_commands.json, compile_flags.txt)
for C/C++ code analysis tools like clangd.
"""

alias Bundlex.Output

@type compile_command :: %{
required(:directory) => String.t(),
required(:command) => String.t(),
required(:file) => String.t(),
optional(:output) => String.t()
}

@doc """
Generates LSP configuration files from a list of build commands.

## Returns

`{:ok, [{:compile_commands_json, path} | {:compile_flags_txt, path}]}`
or `{:error, reason}` if all writes fail.
"""
@spec generate(commands :: [String.t()], project_dir :: String.t()) ::
{:ok, [{atom, String.t()}]} | {:error, String.t()}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this module is not a part of public API, so it should have @moduledoc false and no function @doc

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can add @moduledoc false and leave the current moduledoc content as a comment

@khamilowicz
khamilowicz requested a review from FelonEkonom May 27, 2026 09:03
@khamilowicz
khamilowicz merged commit 08aea65 into master May 27, 2026
4 checks passed
@khamilowicz
khamilowicz deleted the work-on-lsp branch May 27, 2026 09:32
@github-project-automation github-project-automation Bot moved this from In Review to Done in Smackore May 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

add a feature to generate compile_commands.json

3 participants