diff options
-rw-r--r-- | README.md | 10 | ||||
-rw-r--r-- | doc/formatter_options.md | 88 | ||||
-rw-r--r-- | lua/conform/formatters/init.lua | 7 | ||||
-rw-r--r-- | lua/conform/formatters/prettier.lua | 5 | ||||
-rwxr-xr-x | scripts/generate.py | 37 |
5 files changed, 143 insertions, 4 deletions
@@ -13,6 +13,7 @@ Lightweight yet powerful formatter plugin for Neovim - [Recipes](#recipes) - [Advanced topics](#advanced-topics) - [Options](#options) +- [Formatter options](#formatter-options) - [API](#api) - [format(opts, callback)](#formatopts-callback) - [list_formatters(bufnr)](#list_formattersbufnr) @@ -480,6 +481,15 @@ require("conform").formatters.my_formatter = { <!-- /OPTIONS --> +## Formatter options + +<!-- FORMATTER_OPTIONS --> + +- [injected](doc/formatter_options.md#injected) +- [prettier](doc/formatter_options.md#prettier) + +<!-- /FORMATTER_OPTIONS --> + ## API <!-- API --> diff --git a/doc/formatter_options.md b/doc/formatter_options.md new file mode 100644 index 0000000..e5c8493 --- /dev/null +++ b/doc/formatter_options.md @@ -0,0 +1,88 @@ +# Formatter Options + +<!-- TOC --> + +- [injected](#injected) +- [prettier](#prettier) + +<!-- /TOC --> + +All formatters can be customized by directly changing the command, args, or other values (see [customizing formatters](../README.md#customizing-formatters)). Some formatters have a bit more advanced logic built in to those functions and expose additional configuration options. You can pass these values in like so: + +```lua +-- Customize the "injected" formatter +require("conform").formatters.injected = { + -- Set the options field + options = { + -- Set individual option values + ignore_errors = true, + lang_to_formatters = { + json = { "jq" }, + }, + }, +} +``` + +<!-- OPTIONS --> + +## injected + +```lua +options = { + -- Set to true to ignore errors + ignore_errors = false, + -- Map of treesitter language to file extension + -- A temporary file name with this extension will be generated during formatting + -- because some formatters care about the filename. + lang_to_ext = { + bash = "sh", + c_sharp = "cs", + elixir = "exs", + javascript = "js", + julia = "jl", + latex = "tex", + markdown = "md", + python = "py", + ruby = "rb", + rust = "rs", + teal = "tl", + typescript = "ts", + }, + -- Map of treesitter language to formatters to use + -- (defaults to the value from formatters_by_ft) + lang_to_formatters = {}, +} +``` + +## prettier + +```lua +options = { + -- Use a specific prettier parser for a filetype + -- Otherwise, prettier will try to infer the parser from the file name + ft_parsers = { + -- javascript = "babel", + -- javascriptreact = "babel", + -- typescript = "typescript", + -- typescriptreact = "typescript", + -- vue = "vue", + -- css = "css", + -- scss = "scss", + -- less = "less", + -- html = "html", + -- json = "json", + -- jsonc = "json", + -- yaml = "yaml", + -- markdown = "markdown", + -- ["markdown.mdx"] = "mdx", + -- graphql = "graphql", + -- handlebars = "glimmer", + }, + -- Use a specific prettier parser for a file extension + ext_parsers = { + -- qmd = "markdown", + }, +} +``` + +<!-- /OPTIONS --> diff --git a/lua/conform/formatters/init.lua b/lua/conform/formatters/init.lua index c789e12..237d39c 100644 --- a/lua/conform/formatters/init.lua +++ b/lua/conform/formatters/init.lua @@ -1,7 +1,8 @@ local M = {} local uv = vim.uv or vim.loop --- This is used for documentation generation +---@private +---This is used for documentation generation M.list_all_formatters = function() local ret = {} for path in vim.gsplit(vim.o.runtimepath, ",", { plain = true }) do @@ -14,7 +15,9 @@ M.list_all_formatters = function() if entry.name ~= "init.lua" then local basename = string.match(entry.name, "^(.*)%.lua$") local module = require("conform.formatters." .. basename) - ret[basename] = module.meta + local module_data = vim.deepcopy(module.meta) + module_data.has_options = module.options ~= nil + ret[basename] = module_data end end entries = uv.fs_readdir(formatter_dir) diff --git a/lua/conform/formatters/prettier.lua b/lua/conform/formatters/prettier.lua index 0977748..9274577 100644 --- a/lua/conform/formatters/prettier.lua +++ b/lua/conform/formatters/prettier.lua @@ -26,7 +26,8 @@ return { description = [[Prettier is an opinionated code formatter. It enforces a consistent style by parsing your code and re-printing it with its own rules that take the maximum line length into account, wrapping code when necessary.]], }, options = { - -- add parsers for different filetypes + -- Use a specific prettier parser for a filetype + -- Otherwise, prettier will try to infer the parser from the file name ft_parsers = { -- javascript = "babel", -- javascriptreact = "babel", @@ -45,7 +46,7 @@ return { -- graphql = "graphql", -- handlebars = "glimmer", }, - -- add parsers for different extensions + -- Use a specific prettier parser for a file extension ext_parsers = { -- qmd = "markdown", }, diff --git a/scripts/generate.py b/scripts/generate.py index b4da7b4..032a86a 100755 --- a/scripts/generate.py +++ b/scripts/generate.py @@ -8,10 +8,12 @@ from typing import List from nvim_doc_tools import ( Vimdoc, VimdocSection, + dedent, generate_md_toc, indent, parse_functions, read_nvim_json, + read_section, render_md_api, render_vimdoc_api, replace_section, @@ -24,6 +26,7 @@ README = os.path.join(ROOT, "README.md") DOC = os.path.join(ROOT, "doc") RECIPES = os.path.join(DOC, "recipes.md") ADVANCED = os.path.join(DOC, "advanced_topics.md") +FORMATTER_OPTIONS = os.path.join(DOC, "formatter_options.md") VIMDOC = os.path.join(DOC, "conform.txt") OPTIONS = os.path.join(ROOT, "scripts", "options_doc.lua") AUTOFORMAT = os.path.join(ROOT, "scripts", "autoformat_doc.lua") @@ -34,6 +37,7 @@ class Formatter: name: str description: str url: str + has_options: bool deprecated: bool = False @@ -91,6 +95,25 @@ def update_autocmd_md(): ) +def update_formatter_options_md(): + lines = ["\n"] + for formatter in get_all_formatters(): + if formatter.has_options: + lines.extend([f"## {formatter.name}\n", "\n", "```lua\n", "options = {\n"]) + formatter_file = os.path.join( + ROOT, "lua", "conform", "formatters", f"{formatter.name}.lua" + ) + code = read_section(formatter_file, r"^ options = {$", r"^ },$") + lines.extend(dedent(code, 2)) + lines.extend(["}\n", "```\n", "\n"]) + replace_section( + FORMATTER_OPTIONS, + r"^<!-- OPTIONS -->$", + r"^<!-- /OPTIONS -->$", + lines, + ) + + def add_md_link_path(path: str, lines: List[str]) -> List[str]: ret = [] for line in lines: @@ -133,6 +156,18 @@ def update_advanced_toc(): replace_section(README, r"^<!-- ADVANCED -->$", r"^<!-- /ADVANCED -->$", subtoc) +def update_formatter_options_toc(): + toc = ["\n"] + generate_md_toc(FORMATTER_OPTIONS) + ["\n"] + replace_section(FORMATTER_OPTIONS, r"^<!-- TOC -->$", r"^<!-- /TOC -->$", toc) + subtoc = add_md_link_path("doc/formatter_options.md", toc) + replace_section( + README, + r"^<!-- FORMATTER_OPTIONS -->$", + r"^<!-- /FORMATTER_OPTIONS -->$", + subtoc, + ) + + def gen_options_vimdoc() -> VimdocSection: section = VimdocSection("Options", "conform-options", ["\n", ">lua\n"]) with open(OPTIONS, "r", encoding="utf-8") as f: @@ -208,8 +243,10 @@ def main() -> None: update_formatter_list() update_options() update_autocmd_md() + update_formatter_options_md() update_md_api() update_recipes_toc() update_advanced_toc() + update_formatter_options_toc() update_readme_toc() generate_vimdoc() |