~tmpod/black-lite

7f31cc366df65e413aeb29d329701df854f3f8d3 — Tmpod 4 years ago 616dcef
Simplified the config

Instead of using a table on a seperate module,
configs are now loaded from the main config table
(`core.config`) and have a flat structure, having
each field's name prefixed with `black_`
2 files changed, 11 insertions(+), 20 deletions(-)

M README.md
M black.lua
M README.md => README.md +6 -7
@@ 28,22 28,21 @@ A message in the status bar will appear letting you know if the `black` ran succ
## Configurations

This plugin has two configuration fields.
You can set these configurations by creating a `black.lua` file in your `user` lite directory.
This file must return a table containing some or all the following fields:
You can set these configurations by editing your `init.lua` file in the `data/user` lite directory and modifying the `core.config` table.
Each field name should be prefixed with `black_`, for example, `black_args`.
Here's a description of each valid configuration field:

| Name | Type | Description |
| :--- | ---- | ----------- |
| `black_args` | `string[]` | A table containing extra arguments to pass to the `black` command. It must only contain strings which will be joined by spaces and inserted onto the command. |
| `args` | `string[]` | A table containing extra arguments to pass to the `black` command. It must only contain strings which will be joined by spaces and inserted onto the command. |
| `keybind` | `string` | The key-combo to register for the `Black: Format` command shortcut. |

Example:
```lua
local config = {}
local config = require "core.config"

-- Lines can be 120 chars long
config.black_args = {"-l 120"}

return config

```



M black.lua => black.lua +5 -13
@@ 7,24 7,16 @@
local core = require "core"
local command = require "core.command"
local keymap = require "core.keymap"
local config = require "core.config"

-- TODO: Think about something to solve the issue of slow auto-reloading that doesn't involve copying the whole plugin here.

-- Configurations --
local default_config = {}
-- Defaults
-- Extra arguments to pass. The table must contain strings which will be "joined" with spaces.
default_config.black_args = {}
config.black_args = {}
-- The keybind to register for the "black: format" command.
default_config.keybind = "alt+l"

local ok, config = pcall(require("user.black"))
if not ok then
  config = default_config
else
  -- Make sure the config is populated
  config.black_args = config.black_args or default_config.black_args
  config.keybind =    config.keybind    or default_config.keybind
end
config.black_keybind = "alt+l"


-- Logic --


@@ 58,6 50,6 @@ command.add("core.docview", {["black:format"] = format})

-- And a shotcut
keymap.add {
    [config.keybind] = "black:format"
    [config.black_keybind] = "black:format"
}