~gjabell/vis-fix

52e76861638b9caa653266285a4bf085f487d07d — Galen Abell 2 years ago master
Initial commit
3 files changed, 82 insertions(+), 0 deletions(-)

A LICENSE
A README.md
A init.lua
A  => LICENSE +18 -0
@@ 1,18 @@
Copyright 2021 Galen Abell

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

A  => README.md +37 -0
@@ 1,37 @@
# vis-fix

[Vis](https://sr.ht/~martanne/vis/) plugin for configuring file fixers on a per-filetype basis.

`vis-fix` adds a new on-save hook for running an arbitrary shell script based on the type of file being edited. The script to be run can be configured by setting the filetype and command in the plugin's `fixer` table (see below).

## Usage

```lua
local fix = require('plugins/vis-fix')
check.fixers['go'] = 'goimports'
check.fixers['python'] = 'black -q -'
```

## License

MIT License

## Attribution

This plugin was inspired by and modified from code at [https://gitlab.com/timoha/vis-go](https://gitlab.com/timoha/vis-go), which is also licensed under the MIT license:

```
MIT License

Copyright (c) 2018 Andrey Elenskiy

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
```

A  => init.lua +27 -0
@@ 1,27 @@
local M = {}
M.fixers = {}

local function fix(file, path)
  local win = vis.win

  local fix = M.fixers[win.syntax]
  if not fix then
    return true
  end

  local pos = win.selection.pos
  local status, out, err = vis:pipe(file, { start = 0, finish = file.size }, fix)
  if status ~= 0 or not out then
    if err then vis:info(err) end
    return false
  end

  file:delete(0, file.size)
  file:insert(0, out)
  win.selection.pos = pos
  return true
end

vis.events.subscribe(vis.events.FILE_SAVE_PRE, fix)

return M