Skip to content

Commit 9331913

Browse files
authored
refactor(nes): add calc helper and type annotations (#1)
* refactor(nes): add calc helper and type annotations * ci: add format, lint, test, typecheck workflows Introduce GitHub workflows for Lua formatting, linting, testing, and type checking. Also update luacheck globals, add a minimal init, and provide a Makefile test target to streamline local test execution. * fixup
1 parent 4d2a948 commit 9331913

File tree

12 files changed

+388
-39
lines changed

12 files changed

+388
-39
lines changed

.github/workflows/format.yml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
---
2+
name: Format
3+
4+
on:
5+
push:
6+
branches:
7+
- "main"
8+
pull_request:
9+
branches:
10+
- "main"
11+
12+
jobs:
13+
format:
14+
name: Stylua
15+
runs-on: ubuntu-latest
16+
steps:
17+
- uses: actions/checkout@v4
18+
- run: date +%W > weekly
19+
20+
- name: Restore cache
21+
id: cache
22+
uses: actions/cache@v4
23+
with:
24+
path: |
25+
~/.cargo/bin
26+
key: ${{ runner.os }}-cargo-${{ hashFiles('weekly') }}
27+
28+
- name: Install
29+
if: steps.cache.outputs.cache-hit != 'true'
30+
run: cargo install stylua
31+
32+
- name: Format
33+
run: stylua --check lua/ --config-path=.stylua.toml

.github/workflows/lint.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
name: Lint
3+
4+
on:
5+
push:
6+
branches:
7+
- "main"
8+
pull_request:
9+
branches:
10+
- "main"
11+
12+
jobs:
13+
lint:
14+
name: Luacheck
15+
runs-on: ubuntu-latest
16+
steps:
17+
- uses: actions/checkout@v4
18+
- name: Setup
19+
run: |
20+
sudo apt-get update
21+
sudo apt-get install luarocks -y
22+
sudo luarocks install luacheck
23+
24+
- name: Lint
25+
run: luacheck lua/ --globals vim

.github/workflows/tests.yml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: Test
2+
3+
on:
4+
push:
5+
branches:
6+
- "main"
7+
pull_request:
8+
branches:
9+
- "main"
10+
11+
jobs:
12+
test:
13+
runs-on: ${{ matrix.os }}
14+
strategy:
15+
matrix:
16+
nvim-versions: ['stable', 'nightly']
17+
os: [ubuntu-latest, windows-latest, macos-latest]
18+
fail-fast: false
19+
name: Plenary Tests
20+
steps:
21+
- name: checkout
22+
uses: actions/checkout@v4
23+
24+
- uses: rhysd/action-setup-vim@v1
25+
with:
26+
neovim: true
27+
version: ${{ matrix.nvim-versions }}
28+
29+
- name: run tests
30+
run: make test

.github/workflows/typecheck.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
name: lua_ls-typecheck
3+
4+
on:
5+
push:
6+
branches:
7+
- "main"
8+
pull_request:
9+
branches:
10+
- "main"
11+
12+
jobs:
13+
build:
14+
name: Type Check Code Base
15+
runs-on: ubuntu-latest
16+
17+
steps:
18+
- uses: actions/checkout@v4
19+
- uses: stevearc/nvim-typecheck-action@v2
20+
with:
21+
level: Warning
22+
configpath: ".luarc.json"

.luacheckrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
globals = { "vim", "describe", "it", "before_each", "after_each", "assert", "async" }
1+
globals = { "vim", "describe", "it", "before_each", "after_each", "assert", "async", "MiniTest" }

.luarc.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"describe",
1111
"it",
1212
"before_each",
13+
"MiniTest",
1314
"after_each"
1415
]
1516
}

Makefile

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
TESTS_INIT=scripts/minimal_init.lua
2+
TESTS_DIR=tests/
3+
4+
.PHONY: test
5+
6+
test:
7+
@nvim \
8+
--headless \
9+
--noplugin \
10+
-u ${TESTS_INIT} \
11+
-c "lua MiniTest.run()" \

lua/copilot-lsp/completion/init.lua

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
local M = {}
22

3-
---@param results table<integer, { err: lsp.ResponseError, result: lsp.InlineCompletionList}>
3+
---@param _results table<integer, { err: lsp.ResponseError, result: lsp.InlineCompletionList}>
44
---@param _ctx lsp.HandlerContext
55
---@param _config table
6-
local function handle_inlineCompletion_response(results, _ctx, _config)
7-
-- Filter errors from results
8-
local results1 = {} --- @type table<integer,lsp.InlineCompletionList>
9-
10-
for client_id, resp in pairs(results) do
11-
local err, result = resp.err, resp.result
12-
if err then
13-
vim.lsp.log.error(err.code, err.message)
14-
elseif result then
15-
results1[client_id] = result
16-
end
17-
end
18-
19-
for _, result in pairs(results1) do
20-
--TODO: Ghost text for completions
21-
-- This is where we show the completion results
22-
-- However, the LSP being named "copilot" is enough for blink-cmp to show the completion
23-
end
6+
local function handle_inlineCompletion_response(_results, _ctx, _config)
7+
-- -- Filter errors from results
8+
-- local results1 = {} --- @type table<integer,lsp.InlineCompletionList>
9+
--
10+
-- for client_id, resp in pairs(results) do
11+
-- local err, result = resp.err, resp.result
12+
-- if err then
13+
-- vim.lsp.log.error(err.code, err.message)
14+
-- elseif result then
15+
-- results1[client_id] = result
16+
-- end
17+
-- end
18+
--
19+
-- for _, result in pairs(results1) do
20+
-- --TODO: Ghost text for completions
21+
-- -- This is where we show the completion results
22+
-- -- However, the LSP being named "copilot" is enough for blink-cmp to show the completion
23+
-- end
2424
end
2525

2626
---@param type lsp.InlineCompletionTriggerKind

lua/copilot-lsp/nes/ui.lua

Lines changed: 57 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,53 @@ function M.clear_suggestion(bufnr, ns_id)
2323
vim.b[bufnr].nes_state = nil
2424
end
2525

26+
---@private
27+
---@param suggestion copilotlsp.InlineEdit
28+
---@return nes.LineCalculationResult
29+
function M._calculate_lines(suggestion)
30+
local deleted_lines_count = suggestion.range["end"].line - suggestion.range.start.line
31+
local added_lines = vim.split(suggestion.newText, "\n")
32+
local added_lines_count = suggestion.newText == "" and 0 or #added_lines
33+
local same_line = 0
34+
35+
if deleted_lines_count == 0 and added_lines_count == 1 then
36+
---changing within line
37+
deleted_lines_count = 1
38+
same_line = 1
39+
end
40+
41+
-- Calculate positions for delete highlight extmark
42+
---@type nes.DeleteExtmark
43+
local delete_extmark = {
44+
row = suggestion.range.start.line,
45+
end_row = suggestion.range["end"].line + 1,
46+
}
47+
48+
-- Calculate positions for virtual lines extmark
49+
---@type nes.VirtLinesExtmark
50+
local virt_lines_extmark = {
51+
row = suggestion.range["end"].line,
52+
virt_lines_count = added_lines_count,
53+
}
54+
55+
-- Calculate positions for floating window
56+
---@type nes.FloatWin
57+
local float_win = {
58+
height = #added_lines,
59+
row = suggestion.range["end"].line + deleted_lines_count + 1,
60+
}
61+
62+
return {
63+
deleted_lines_count = deleted_lines_count,
64+
added_lines = added_lines,
65+
added_lines_count = added_lines_count,
66+
same_line = same_line,
67+
delete_extmark = delete_extmark,
68+
virt_lines_extmark = virt_lines_extmark,
69+
float_win = float_win,
70+
}
71+
end
72+
2673
---@private
2774
---@param edits copilotlsp.InlineEdit[]
2875
---@param ns_id integer
@@ -36,38 +83,29 @@ function M._display_next_suggestion(edits, ns_id)
3683
local suggestion = edits[1]
3784

3885
local ui = {}
39-
local deleted_lines_count = suggestion.range["end"].line - suggestion.range.start.line
40-
local added_lines = vim.split(suggestion.newText, "\n")
41-
local added_lines_count = suggestion.newText == "" and 0 or #added_lines
42-
local same_line = 0
43-
44-
if deleted_lines_count == 0 and added_lines_count == 1 then
45-
---changing within line
46-
deleted_lines_count = 1
47-
same_line = 1
48-
end
86+
local lines = M._calculate_lines(suggestion)
4987

50-
if deleted_lines_count > 0 then
88+
if lines.deleted_lines_count > 0 then
5189
-- Deleted range red highlight
52-
vim.api.nvim_buf_set_extmark(bufnr, ns_id, suggestion.range.start.line, 0, {
90+
vim.api.nvim_buf_set_extmark(bufnr, ns_id, lines.delete_extmark.row, 0, {
5391
hl_group = "NesDelete",
54-
end_row = suggestion.range["end"].line + 1,
92+
end_row = lines.delete_extmark.end_row,
5593
})
5694
end
57-
if added_lines_count > 0 then
95+
if lines.added_lines_count > 0 then
5896
-- Create space for float
5997
local virt_lines = {}
60-
for _ = 1, added_lines_count do
98+
for _ = 1, lines.virt_lines_extmark.virt_lines_count do
6199
table.insert(virt_lines, {
62100
{ "", "Normal" },
63101
})
64102
end
65-
vim.api.nvim_buf_set_extmark(bufnr, ns_id, suggestion.range["end"].line, 0, {
103+
vim.api.nvim_buf_set_extmark(bufnr, ns_id, lines.virt_lines_extmark.row, 0, {
66104
virt_lines = virt_lines,
67105
})
68106

69107
local preview_bufnr = vim.api.nvim_create_buf(false, true)
70-
vim.api.nvim_buf_set_lines(preview_bufnr, 0, -1, false, added_lines)
108+
vim.api.nvim_buf_set_lines(preview_bufnr, 0, -1, false, lines.added_lines)
71109
vim.bo[preview_bufnr].modifiable = false
72110
vim.bo[preview_bufnr].buflisted = false
73111
vim.bo[preview_bufnr].bufhidden = "wipe"
@@ -79,8 +117,8 @@ function M._display_next_suggestion(edits, ns_id)
79117
local preview_winnr = vim.api.nvim_open_win(preview_bufnr, false, {
80118
relative = "cursor",
81119
width = win_width - offset,
82-
height = #added_lines,
83-
row = (suggestion.range["end"].line + deleted_lines_count + 1) - cursor[1],
120+
height = lines.float_win.height,
121+
row = lines.float_win.row - cursor[1],
84122
col = 0,
85123
style = "minimal",
86124
border = "none",

lua/copilot-lsp/types.lua

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,28 @@
1515
---@class nes.Apply.Opts
1616
---@field jump? boolean | { hl_timeout: integer? } auto jump to the end of the new edit
1717
---@field trigger? boolean auto trigger the next edit suggestion
18+
19+
---@class nes.DeleteExtmark
20+
--- Holds row information for delete highlight extmark.
21+
---@field row number
22+
---@field end_row number
23+
24+
---@class nes.VirtLinesExtmark
25+
-- Holds row and virtual lines count for virtual lines extmark.
26+
---@field row number
27+
---@field virt_lines_count number
28+
29+
---@class nes.FloatWin
30+
--- Defines dimensions and position for the floating window.
31+
---@field height number
32+
---@field row number
33+
34+
---@class nes.LineCalculationResult
35+
--- The result of calculating lines for inline suggestion UI.
36+
---@field deleted_lines_count number
37+
---@field added_lines string[]
38+
---@field added_lines_count number
39+
---@field same_line number
40+
---@field delete_extmark nes.DeleteExtmark
41+
---@field virt_lines_extmark nes.VirtLinesExtmark
42+
---@field float_win nes.FloatWin

0 commit comments

Comments
 (0)