You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I was working with C++ and OpenMP, and I stumbled upon an annoying non-optional behavior of clang-format regarding pragmas. I looked into the documentation for clang-format, but it was impossible to change it. So I tried to fix it using comform.nvim instead. Essentially, I want to indent the #pragma omp ... lines so it matches the rest of the code.
Problem: The problem is that there is almost no documentation for custom formatters as functions. One can look at the injected.lua code, but it is cluttered with code specific to that formatter.
Suggestion: Adding a recipe for a simple function-based formatter. This hack solution for the clang-format issue is inspired by the GitHub repo: MedicineYeh/p-clang-format.
require('conform').setup({
formatters= {
['clang-fmt-pre'] = {
---Replace all `#pragma omp ...` with `//#pragma omp ...`format=function(self, ctx, lines, callback)
-- Use this variable if options should be possiblelocal_=self.optionslocalformat_erros=nillocalformatted_lines=vim.deepcopy(lines)
localpattern='^%s*#pragma omp'fori, lineinipairs(lines) doifline:match(pattern) thenlocalfmt_line=line:gsub(pattern, '//#pragma omp')
formatted_lines[i] =fmt_lineendendcallback(format_erros, formatted_lines)
end
},
['clang-fmt-post'] = {
---Replace all `//#pragma omp ...` with `#pragma omp ...`format=function(self, ctx, lines, callback)
-- Use this variable if options should be possiblelocal_=self.optionslocalformat_erros=nillocalformatted_lines=vim.deepcopy(lines)
localpattern='//%s#pragma omp'fori, lineinipairs(formatted_lines) doifline:match(pattern) thenformatted_lines[i] =line:gsub(pattern, '#pragma omp')
endendcallback(format_erros, formatted_lines)
end,
},
},
formatters_by_ft= {
cpp= { 'clang-fmt-pre', 'clang-format', 'clang-fmt-post' },
},
)}
The text was updated successfully, but these errors were encountered:
caleskog
changed the title
Documentation: Simple example for custom formatter as a function
Documentation: Simple recipe/example for custom formatter as a function
Jul 23, 2024
Yes, they are perfect. Might be good to reference those two in the README.md, too, so it's easy to find. Like including it under the Options section. As the format option is not mentioned.
I was working with C++ and OpenMP, and I stumbled upon an annoying non-optional behavior of clang-format regarding pragmas. I looked into the documentation for clang-format, but it was impossible to change it. So I tried to fix it using comform.nvim instead. Essentially, I want to indent the
#pragma omp ...
lines so it matches the rest of the code.Problem: The problem is that there is almost no documentation for custom formatters as functions. One can look at the injected.lua code, but it is cluttered with code specific to that formatter.
Suggestion: Adding a recipe for a simple function-based formatter. This hack solution for the clang-format issue is inspired by the GitHub repo: MedicineYeh/p-clang-format.
The text was updated successfully, but these errors were encountered: