|
1 | | --- ---@diagnostic disable: undefined-field, undefined-global |
2 | | --- require('plugin.guard') |
3 | | --- local api = vim.api |
4 | | --- local same = assert.are.same |
5 | | --- local ft = require('guard.filetype') |
6 | | --- local gapi = require('guard.api') |
7 | | --- |
8 | | --- describe('format module', function() |
9 | | --- local bufnr |
10 | | --- local ill_c = { |
11 | | --- 'int main( void ) {return 0;}', |
12 | | --- } |
13 | | --- |
14 | | --- before_each(function() |
15 | | --- for k, _ in pairs(ft) do |
16 | | --- ft[k] = nil |
17 | | --- end |
18 | | --- vim.g.guard_config = { |
19 | | --- lsp_as_default_formatter = true, |
20 | | --- } |
21 | | --- |
22 | | --- vim.lsp.config('clangd', { |
23 | | --- cmd = { 'clangd' }, |
24 | | --- filetypes = { 'c' }, |
25 | | --- capabilities = { |
26 | | --- textDocument = { |
27 | | --- formatting = { |
28 | | --- dynamicRegistration = true, |
29 | | --- }, |
30 | | --- }, |
31 | | --- }, |
32 | | --- }) |
33 | | --- vim.lsp.enable('clangd') |
34 | | --- |
35 | | --- bufnr = api.nvim_create_buf(true, false) |
36 | | --- |
37 | | --- vim.bo[bufnr].filetype = 'c' |
38 | | --- api.nvim_set_current_buf(bufnr) |
39 | | --- vim.cmd('silent! write! /tmp/lsp_spec_test.c') |
40 | | --- end) |
41 | | --- |
42 | | --- after_each(function() |
43 | | --- vim.lsp.enable('clangd', false) |
44 | | --- end) |
45 | | --- |
46 | | --- local function getlines() |
47 | | --- return api.nvim_buf_get_lines(bufnr, 0, -1, false) |
48 | | --- end |
49 | | --- |
50 | | --- local function setlines(lines) |
51 | | --- api.nvim_buf_set_lines(bufnr, 0, -1, false, lines) |
52 | | --- end |
53 | | --- |
54 | | --- it('lsp default formatter works', function() |
55 | | --- vim.wait(500) |
56 | | --- setlines(ill_c) |
57 | | --- gapi.fmt(bufnr) |
58 | | --- vim.wait(500) |
59 | | --- same({ 'int main(void) { return 0; }' }, getlines()) |
60 | | --- end) |
61 | | --- end) |
| 1 | +---@diagnostic disable: undefined-field, undefined-global |
| 2 | +require('plugin.guard') |
| 3 | +local api = vim.api |
| 4 | +local same = assert.are.same |
| 5 | +local ft = require('guard.filetype') |
| 6 | +local gapi = require('guard.api') |
| 7 | + |
| 8 | +describe('format module', function() |
| 9 | + local bufnr |
| 10 | + local ill_c = { |
| 11 | + 'int main( void ) {return 0;}', |
| 12 | + } |
| 13 | + |
| 14 | + before_each(function() |
| 15 | + for k, _ in pairs(ft) do |
| 16 | + ft[k] = nil |
| 17 | + end |
| 18 | + vim.g.guard_config = { |
| 19 | + lsp_as_default_formatter = true, |
| 20 | + } |
| 21 | + |
| 22 | + vim.lsp.config('clangd', { |
| 23 | + cmd = { 'clangd' }, |
| 24 | + filetypes = { 'c' }, |
| 25 | + capabilities = { |
| 26 | + textDocument = { |
| 27 | + formatting = { |
| 28 | + dynamicRegistration = true, |
| 29 | + }, |
| 30 | + }, |
| 31 | + }, |
| 32 | + }) |
| 33 | + vim.lsp.enable('clangd') |
| 34 | + |
| 35 | + bufnr = api.nvim_create_buf(true, false) |
| 36 | + |
| 37 | + vim.bo[bufnr].filetype = 'c' |
| 38 | + api.nvim_set_current_buf(bufnr) |
| 39 | + vim.cmd('silent! write! /tmp/lsp_spec_test.c') |
| 40 | + end) |
| 41 | + |
| 42 | + after_each(function() |
| 43 | + vim.lsp.enable('clangd', false) |
| 44 | + end) |
| 45 | + |
| 46 | + local function getlines() |
| 47 | + return api.nvim_buf_get_lines(bufnr, 0, -1, false) |
| 48 | + end |
| 49 | + |
| 50 | + local function setlines(lines) |
| 51 | + api.nvim_buf_set_lines(bufnr, 0, -1, false, lines) |
| 52 | + end |
| 53 | + |
| 54 | + it('lsp default formatter works', function() |
| 55 | + setlines(ill_c) |
| 56 | + vim.api.nvim_create_autocmd('LspAttach', { |
| 57 | + callback = function(args) |
| 58 | + same(args.buf, bufnr) |
| 59 | + same(args.file, '/tmp/lsp_spec_test.c') |
| 60 | + same(args.match, '/tmp/lsp_spec_test.c') |
| 61 | + gapi.fmt(bufnr) |
| 62 | + vim.wait(500) |
| 63 | + same({ 'int main(void) { return 0; }' }, getlines()) |
| 64 | + end, |
| 65 | + }) |
| 66 | + vim.wait(1500) |
| 67 | + end) |
| 68 | +end) |
0 commit comments