clang.lua 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. local M = {}
  2. M.config = function()
  3. O.lang.clang = {
  4. diagnostics = {
  5. virtual_text = { spacing = 0, prefix = "" },
  6. signs = true,
  7. underline = true,
  8. },
  9. cross_file_rename = true,
  10. header_insertion = "never",
  11. filetypes = { "c", "cpp", "objc" },
  12. formatter = {
  13. exe = "clang-format",
  14. args = {},
  15. },
  16. debug = {
  17. adapter = {
  18. command = "/usr/bin/lldb-vscode",
  19. },
  20. stop_on_entry = false,
  21. },
  22. }
  23. end
  24. M.format = function()
  25. local shared_config = {
  26. function()
  27. return {
  28. exe = O.lang.clang.formatter.exe,
  29. args = O.lang.clang.formatter.args,
  30. stdin = not (O.lang.clang.formatter.stdin ~= nil),
  31. }
  32. end,
  33. }
  34. O.formatters.filetype["c"] = shared_config
  35. O.formatters.filetype["cpp"] = shared_config
  36. O.formatters.filetype["objc"] = shared_config
  37. require("formatter.config").set_defaults {
  38. logging = false,
  39. filetype = O.formatters.filetype,
  40. }
  41. end
  42. M.lint = function()
  43. -- TODO: implement linters (if applicable)
  44. return "No linters configured!"
  45. end
  46. M.lsp = function()
  47. if require("lv-utils").check_lsp_client_active "clangd" then
  48. return
  49. end
  50. local clangd_flags = { "--background-index" }
  51. if O.lang.clang.cross_file_rename then
  52. table.insert(clangd_flags, "--cross-file-rename")
  53. end
  54. table.insert(clangd_flags, "--header-insertion=" .. O.lang.clang.header_insertion)
  55. require("lspconfig").clangd.setup {
  56. cmd = { DATA_PATH .. "/lspinstall/cpp/clangd/bin/clangd", unpack(clangd_flags) },
  57. on_attach = require("lsp").common_on_attach,
  58. handlers = {
  59. ["textDocument/publishDiagnostics"] = vim.lsp.with(vim.lsp.diagnostic.on_publish_diagnostics, {
  60. virtual_text = O.lang.clang.diagnostics.virtual_text,
  61. signs = O.lang.clang.diagnostics.signs,
  62. underline = O.lang.clang.diagnostics.underline,
  63. update_in_insert = true,
  64. }),
  65. },
  66. }
  67. end
  68. M.dap = function()
  69. if O.plugin.dap.active then
  70. local dap_install = require "dap-install"
  71. local dap = require "dap"
  72. dap_install.config("ccppr_vsc_dbg", {})
  73. dap.adapters.lldb = {
  74. type = "executable",
  75. command = O.lang.clang.debug.adapter.command,
  76. name = "lldb",
  77. }
  78. local shared_dap_config = {
  79. {
  80. name = "Launch",
  81. type = "lldb",
  82. request = "launch",
  83. program = function()
  84. return vim.fn.input("Path to executable: ", vim.fn.getcwd() .. "/", "file")
  85. end,
  86. cwd = "${workspaceFolder}",
  87. stopOnEntry = O.lang.clang.debug.stop_on_entry,
  88. args = {},
  89. env = function()
  90. local variables = {}
  91. for k, v in pairs(vim.fn.environ()) do
  92. table.insert(variables, string.format("%s=%s", k, v))
  93. end
  94. return variables
  95. end,
  96. runInTerminal = false,
  97. },
  98. {
  99. -- If you get an "Operation not permitted" error using this, try disabling YAMA:
  100. -- echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope
  101. name = "Attach to process",
  102. type = "lldb", -- Adjust this to match your adapter name (`dap.adapters.<name>`)
  103. request = "attach",
  104. pid = function()
  105. local output = vim.fn.system { "ps", "a" }
  106. local lines = vim.split(output, "\n")
  107. local procs = {}
  108. for _, line in pairs(lines) do
  109. -- output format
  110. -- " 107021 pts/4 Ss 0:00 /bin/zsh <args>"
  111. local parts = vim.fn.split(vim.fn.trim(line), " \\+")
  112. local pid = parts[1]
  113. local name = table.concat({ unpack(parts, 5) }, " ")
  114. if pid and pid ~= "PID" then
  115. pid = tonumber(pid)
  116. if pid ~= vim.fn.getpid() then
  117. table.insert(procs, { pid = tonumber(pid), name = name })
  118. end
  119. end
  120. end
  121. local choices = { "Select process" }
  122. for i, proc in ipairs(procs) do
  123. table.insert(choices, string.format("%d: pid=%d name=%s", i, proc.pid, proc.name))
  124. end
  125. local choice = vim.fn.inputlist(choices)
  126. if choice < 1 or choice > #procs then
  127. return nil
  128. end
  129. return procs[choice].pid
  130. end,
  131. args = {},
  132. },
  133. }
  134. dap.configurations.c = shared_dap_config
  135. dap.configurations.cpp = shared_dap_config
  136. end
  137. end
  138. return M