Skip to content

Commit

Permalink
Merge pull request #46 from lukas-reineke/develop
Browse files Browse the repository at this point in the history
Version 2.4.0
  • Loading branch information
lukas-reineke authored May 21, 2022
2 parents 204e699 + fb27748 commit 210c4c5
Show file tree
Hide file tree
Showing 9 changed files with 305 additions and 12 deletions.
22 changes: 20 additions & 2 deletions .github/workflows/pr_check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,23 @@ jobs:
- name: test
run: |
luacheck lua
luacheck lua specs
tests:
strategy:
fail-fast: false
matrix:
version:
- stable
- nightly
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Install Neovim
uses: rhysd/action-setup-vim@v1
id: neovim
with:
neovim: true
version: ${{ matrix.version }}
- name: Run tests
run: make specs
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
vendor
2 changes: 1 addition & 1 deletion .luacheckrc
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
globals = { "vim", "_" }
globals = { "vim", "_", "assert", "describe", "before_each", "after_each", "it" }
max_line_length = false
20 changes: 20 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
ifndef VERBOSE
.SILENT:
endif

specs: dependencies
@echo "Running lsp-format specs..."
timeout 300 nvim -e \
--headless \
-u specs/minimal_init.vim \
-c "PlenaryBustedDirectory specs/features {minimal_init = 'specs/minimal_init.vim'}"

dependencies:
if [ ! -d vendor ]; then \
git clone --depth 1 \
https://github.com/nvim-lua/plenary.nvim \
vendor/pack/vendor/start/plenary.nvim; \
git clone --depth 1 \
https://github.com/neovim/nvim-lspconfig \
vendor/pack/vendor/start/nvim-lspconfig; \
fi
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ formatting.

`sync` turns on synchronous formatting. The editor will block until the formatting is done.

#### `force` format option

`force` will write the format result to the buffer, even if the buffer changed after the format request started.

## Notes

#### Make sure you remove any old format on save code
Expand Down
19 changes: 18 additions & 1 deletion doc/format.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


Author: Lukas Reineke <lukas@reineke.jp>
Version: 2.3.3
Version: 2.4.0

==============================================================================
CONTENTS *lsp-format*
Expand Down Expand Up @@ -87,6 +87,20 @@ sync *lsp-format-sync*
}
}
------------------------------------------------------------------------------
force *lsp-format-force*

`force` is a boolean flag. When on the format result will always be written
to the buffer, even if the buffer changed.

Example: >
require "lsp-format".setup {
go = {
force = true
}
}
==============================================================================
4. COMMANDS *lsp-format-commands*

Expand Down Expand Up @@ -121,6 +135,9 @@ sync *lsp-format-sync*
==============================================================================
5. CHANGELOG *lsp-format-changelog*

2.4.0
Add `force` option

2.3.3
Use "vim.lsp.log" for logging
Don't attach servers that don't support formatting
Expand Down
38 changes: 30 additions & 8 deletions lua/lsp-format/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,27 @@ M.setup = function(format_options)
)
end

M._parse_value = function(key, value)
if not value then
return true
end
if key == "order" or key == "exclude" then
return vim.split(value, ",")
end
local int_value = tonumber(value)
if int_value then
return int_value
end
if value == "false" then
return false
end
if value == "true" then
return true
end

return value
end

M.format = function(options)
if vim.b.format_saving or M.disabled or M.disabled_filetypes[vim.bo.filetype] then
return
Expand All @@ -38,10 +59,7 @@ M.format = function(options)
local format_options = vim.deepcopy(M.format_options[vim.bo.filetype] or {})
for _, option in ipairs(options.fargs or {}) do
local key, value = unpack(vim.split(option, "="))
if key == "order" or key == "exclude" then
value = vim.split(value, ",")
end
format_options[key] = value or true
format_options[key] = M._parse_value(key, value)
end

local clients = vim.tbl_values(vim.lsp.buf_get_clients())
Expand Down Expand Up @@ -144,10 +162,14 @@ M._handler = function(err, result, ctx)
if not vim.api.nvim_buf_is_loaded(ctx.bufnr) then
vim.fn.bufload(ctx.bufnr)
vim.api.nvim_buf_set_var(ctx.bufnr, "format_changedtick", vim.api.nvim_buf_get_var(ctx.bufnr, "changedtick"))
elseif
vim.api.nvim_buf_get_var(ctx.bufnr, "format_changedtick")
~= vim.api.nvim_buf_get_var(ctx.bufnr, "changedtick")
or vim.startswith(vim.api.nvim_get_mode().mode, "i")
end
if
not ctx.params.options.force
and (
vim.api.nvim_buf_get_var(ctx.bufnr, "format_changedtick")
~= vim.api.nvim_buf_get_var(ctx.bufnr, "changedtick")
or vim.startswith(vim.api.nvim_get_mode().mode, "i")
)
then
M._next()
return
Expand Down
198 changes: 198 additions & 0 deletions specs/features/main_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
local mock = require "luassert.mock"
local match = require "luassert.match"
local spy = require "luassert.spy"
local f = require "lsp-format"

local mock_client = {
id = 1,
name = "lsp-client-test",
request = function(_, _, _, _) end,
request_sync = function(_, _, _, _) end,
supports_method = function(_) end,
setup = function() end,
}

vim.lsp.buf_get_clients = function()
local clients = {}
clients[mock_client.name] = mock_client
return clients
end

describe("lsp-format", function()
local c
local api

before_each(function()
c = mock(mock_client, true)
api = mock(vim.api)
c.supports_method = function(_)
return true
end
f.setup {}
f.on_attach(c)
end)

after_each(function()
mock.revert(c)
mock.revert(api)
end)

it("sends a valid format request", function()
f.format {}
assert.stub(c.request).was_called(1)
assert.stub(c.request).was_called_with("textDocument/formatting", {
options = {
insertSpaces = false,
tabSize = 8,
},
textDocument = {
uri = "file://",
},
}, match.is_ref(f._handler), 1)
end)

it("FormatToggle prevent/allow formatting", function()
f.toggle { args = "" }
f.format {}
assert.stub(c.request).was_called(0)

f.toggle { args = "" }
f.format {}
assert.stub(c.request).was_called(1)
end)

it("FormatDisable/Enable prevent/allow formatting", function()
f.disable { args = "" }
f.format {}
assert.stub(c.request).was_called(0)

f.enable { args = "" }
f.format {}
assert.stub(c.request).was_called(1)
end)

it("sends default format options", function()
f.setup {
lua = {
bool_test = true,
int_test = 1,
string_test = "string",
},
}
vim.bo.filetype = "lua"
f.format {}
assert.stub(c.request).was_called(1)
assert.stub(c.request).was_called_with("textDocument/formatting", {
options = {
insertSpaces = false,
tabSize = 8,
bool_test = true,
int_test = 1,
string_test = "string",
},
textDocument = {
uri = "file://",
},
}, match.is_ref(f._handler), 1)
end)

it("sends format options", function()
f.format {
fargs = { "bool_test", "int_test=1", "string_test=string" },
}
assert.stub(c.request).was_called(1)
assert.stub(c.request).was_called_with("textDocument/formatting", {
options = {
insertSpaces = false,
tabSize = 8,
bool_test = true,
int_test = 1,
string_test = "string",
},
textDocument = {
uri = "file://",
},
}, match.is_ref(f._handler), 1)
end)

it("overwrites default format options", function()
f.setup {
lua = {
bool_test = true,
int_test = 1,
string_test = "string",
},
}
vim.bo.filetype = "lua"
f.format {
fargs = { "bool_test=false", "int_test=2", "string_test=another_string" },
}
assert.stub(c.request).was_called(1)
assert.stub(c.request).was_called_with("textDocument/formatting", {
options = {
insertSpaces = false,
tabSize = 8,
bool_test = false,
int_test = 2,
string_test = "another_string",
},
textDocument = {
uri = "file://",
},
}, match.is_ref(f._handler), 1)
end)

it("does not overwrite changes", function()
local apply_text_edits = spy.on(vim.lsp.util, "apply_text_edits")
c.request = function(_, params, handler, bufnr)
api.nvim_buf_get_var = function(_, var)
if var == "format_changedtick" then
return 9999
end
return 1
end
handler(nil, {}, { bufnr = bufnr, params = params })
end
f.format {}
assert.spy(apply_text_edits).was.called(0)
end)

it("does overwrite changes with force", function()
local apply_text_edits = spy.on(vim.lsp.util, "apply_text_edits")
c.request = function(_, params, handler, bufnr)
api.nvim_buf_get_var = function(_, var)
if var == "format_changedtick" then
return 9999
end
return 1
end
handler(nil, {}, { bufnr = bufnr, params = params })
end
f.format { fargs = { "force=true" } }
assert.spy(apply_text_edits).was.called(1)
end)

it("does not overwrite when in insert mode", function()
local apply_text_edits = spy.on(vim.lsp.util, "apply_text_edits")
c.request = function(_, params, handler, bufnr)
api.nvim_get_mode = function()
return "insert"
end
handler(nil, {}, { bufnr = bufnr, params = params })
end
f.format {}
assert.spy(apply_text_edits).was.called(0)
end)

it("does overwrite when in insert mode with force", function()
local apply_text_edits = spy.on(vim.lsp.util, "apply_text_edits")
c.request = function(_, params, handler, bufnr)
api.nvim_get_mode = function()
return "insert"
end
handler(nil, {}, { bufnr = bufnr, params = params })
end
f.format { fargs = { "force=true" } }
assert.spy(apply_text_edits).was.called(1)
end)
end)
13 changes: 13 additions & 0 deletions specs/minimal_init.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
set rtp-=~/.config/nvim
set rtp-=~/.local/share/nvim/site
set rtp+=.
set noswapfile

let $lsp_format = getcwd()
let $specs = getcwd() .. "/specs"
let $vendor = getcwd() .. "/vendor"

set rtp+=$lsp_format,$specs
set packpath=$vendor

packloadall

0 comments on commit 210c4c5

Please sign in to comment.