Skip to content

Commit

Permalink
Merge pull request #538 from nvim-treesitter/ci2/stylua
Browse files Browse the repository at this point in the history
ci: lint with stylua and luals
  • Loading branch information
lewis6991 authored Dec 11, 2024
2 parents 3288c5a + eb06aa6 commit 01455ea
Show file tree
Hide file tree
Showing 8 changed files with 339 additions and 159 deletions.
32 changes: 31 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,36 @@ jobs:
# Check commit messages
- uses: webiny/[email protected]

stylua:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Download stylua
run: make stylua

- name: Lint
run: make stylua-check

luals:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4

- uses: leafo/gh-actions-lua@v9
with:
luaVersion: "5.1.5"

- uses: leafo/gh-actions-luarocks@v4

- name: Download nvim-test
run: make nvim-test

- name: LuaLS
run: make luals-check

test:
runs-on: ubuntu-latest

Expand All @@ -25,7 +55,7 @@ jobs:
# - 'v0.10.0'

env:
NEOVIM_VERSION: ${{ matrix.neovim_version }}
NVIM_TEST_VERSION: ${{ matrix.neovim_version }}

steps:
- name: Checkout
Expand Down
78 changes: 71 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.DEFAULT_GOAL := test

NEOVIM_VERSION ?= v0.9.5
NEOVIM_RUNNER_VERSION ?= v0.10.0
NVIM_TEST_VERSION ?= v0.10.2
NVIM_RUNNER_VERSION ?= v0.10.2

NVIM_TS_SHA ?= 894cb3c

Expand All @@ -18,22 +18,86 @@ nvim-treesitter:
nvim-test:
git clone https://github.com/lewis6991/nvim-test
nvim-test/bin/nvim-test --init \
--runner_version $(NEOVIM_RUNNER_VERSION) \
--target_version $(NEOVIM_VERSION)
--runner_version $(NVIM_RUNNER_VERSION) \
--target_version $(NVIM_TEST_VERSION)

.PHONY: test
test: nvim-test nvim-treesitter
nvim-test/bin/nvim-test test \
--runner_version $(NEOVIM_RUNNER_VERSION) \
--target_version $(NEOVIM_VERSION) \
--runner_version $(NVIM_RUNNER_VERSION) \
--target_version $(NVIM_TEST_VERSION) \
--lpath=$(PWD)/lua/?.lua \
--filter="$(FILTER)" \
--verbose

.PHONY: parsers
parsers: nvim-test nvim-treesitter
$(XDG_DATA_HOME)/nvim-test/nvim-runner-$(NEOVIM_RUNNER_VERSION)/bin/nvim \
$(XDG_DATA_HOME)/nvim-test/nvim-runner-$(NVIM_RUNNER_VERSION)/bin/nvim \
--clean -u NONE -c 'source install_parsers.lua'

lint:
luacheck lua

# ------------------------------------------------------------------------------
# LuaLS
# ------------------------------------------------------------------------------

ifeq ($(shell uname -m),arm64)
LUALS_ARCH ?= arm64
else
LUALS_ARCH ?= x64
endif

LUALS_VERSION := 3.13.2
LUALS_TARBALL := lua-language-server-$(LUALS_VERSION)-$(shell uname -s)-$(LUALS_ARCH).tar.gz
LUALS_URL := https://github.com/LuaLS/lua-language-server/releases/download/$(LUALS_VERSION)/$(LUALS_TARBALL)

.INTERMEDIATE: $(LUALS_TARBALL)
$(LUALS_TARBALL):
wget $(LUALS_URL)

luals: $(LUALS_TARBALL)
mkdir luals
tar -xf $< -C luals

export VIMRUNTIME=$(XDG_DATA_HOME)/nvim-test/nvim-test-$(NVIM_TEST_VERSION)/share/nvim/runtime
.PHONY: luals-check
luals-check: luals nvim-test
ls $(VIMRUNTIME)
VIMRUNTIME=$(XDG_DATA_HOME)/nvim-test/nvim-test-$(NVIM_TEST_VERSION)/share/nvim/runtime \
luals/bin/lua-language-server \
--logpath=luals_check \
--configpath=../.luarc.json \
--check=lua
@grep '^\[\]$$' luals_check/check.json

# ------------------------------------------------------------------------------
# Stylua
# ------------------------------------------------------------------------------
ifeq ($(shell uname -s),Darwin)
STYLUA_PLATFORM := macos-aarch64
else
STYLUA_PLATFORM := linux-x86_64
endif

STYLUA_VERSION := v2.0.2
STYLUA_ZIP := stylua-$(STYLUA_PLATFORM).zip
STYLUA_URL := https://github.com/JohnnyMorganz/StyLua/releases/download/$(STYLUA_VERSION)/$(STYLUA_ZIP)

.INTERMEDIATE: $(STYLUA_ZIP)
$(STYLUA_ZIP):
wget $(STYLUA_URL)

stylua: $(STYLUA_ZIP)
unzip $<

.PHONY: stylua-check
stylua-check: stylua
./stylua --check lua/**/*.lua

.PHONY: stylua-run
stylua-run: stylua
./stylua \
lua/**/*.lua \
lua/*.lua \
test/*_spec.lua
30 changes: 18 additions & 12 deletions lua/treesitter-context.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ local function throttle_by_id(f, ms)
local waiting = {} --- @type table<any,boolean>

local schedule = function(id, wrapper)
state[id] = "scheduled"
state[id] = 'scheduled'
vim.schedule_wrap(wrapper)(id, wrapper)
end

local on_throttle_finish = function(id, wrapper)
assert(state[id] == "throttled")
assert(state[id] == 'throttled')
if waiting[id] == nil then
timers[id] = nil
state[id] = nil
Expand All @@ -38,16 +38,18 @@ local function throttle_by_id(f, ms)
end

local wrapper = function(id, wrapper)
assert(state[id] == "scheduled")
state[id] = "running"
assert(state[id] == 'scheduled')
state[id] = 'running'
f(id)
assert(state[id] == "running")
state[id] = "throttled"
assert(state[id] == 'running')
state[id] = 'throttled'

if timers[id] == nil then
timers[id] = assert(vim.loop.new_timer())
end
timers[id]:start(ms, 0, function() on_throttle_finish(id, wrapper) end)
timers[id]:start(ms, 0, function()
on_throttle_finish(id, wrapper)
end)
end

return function(id)
Expand All @@ -57,7 +59,7 @@ local function throttle_by_id(f, ms)
end
-- Don't set 'waiting' for 'scheduled' state since the callback is about to start.
-- Consequently, there is no need to run it again after throttling is completed.
if state[id] ~= "scheduled" then
if state[id] ~= 'scheduled' then
waiting[id] = true
end
end
Expand All @@ -67,7 +69,7 @@ local attached = {} --- @type table<integer,true>

local function close(args)
local render = require('treesitter-context.render')
if args.event == "WinClosed" then
if args.event == 'WinClosed' then
-- Closing current window instead of intended window may lead to context window flickering.
render.close(tonumber(args.match))
else
Expand Down Expand Up @@ -131,11 +133,11 @@ end)

---@param args table
local function update(args)
if args.event == "OptionSet" and args.match ~= 'number' and args.match ~= 'relativenumber' then
if args.event == 'OptionSet' and args.match ~= 'number' and args.match ~= 'relativenumber' then
return
end

local multiwindow_events = { "WinResized", "User" }
local multiwindow_events = { 'WinResized', 'User' }

if config.multiwindow and vim.tbl_contains(multiwindow_events, args.event) then
-- Resizing a single window may cause many resizes in different windows,
Expand Down Expand Up @@ -258,7 +260,11 @@ local function init()
api.nvim_set_hl(0, 'TreesitterContext', { link = 'NormalFloat', default = true })
api.nvim_set_hl(0, 'TreesitterContextLineNumber', { link = 'LineNr', default = true })
api.nvim_set_hl(0, 'TreesitterContextBottom', { link = 'NONE', default = true })
api.nvim_set_hl(0, 'TreesitterContextLineNumberBottom', { link = 'TreesitterContextBottom', default = true })
api.nvim_set_hl(
0,
'TreesitterContextLineNumberBottom',
{ link = 'TreesitterContextBottom', default = true }
)
api.nvim_set_hl(0, 'TreesitterContextSeparator', { link = 'FloatBorder', default = true })
end

Expand Down
3 changes: 1 addition & 2 deletions lua/treesitter-context/config.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

--- @class (exact) TSContext.Config
--- @field enable boolean
--- @field multiwindow boolean
Expand Down Expand Up @@ -72,7 +71,7 @@ end
setmetatable(M, {
__index = function(_, k)
return config[k]
end
end,
})

return M
7 changes: 5 additions & 2 deletions lua/treesitter-context/context.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,19 @@ local function get_parent_nodes(langtree, range)

local ret = {} --- @type TSNode[]

--- @diagnostic disable-next-line:undefined-field added in 0.11
if root.child_containing_descendant ~= nil then
local p = root --- @type TSNode?
while p do
ret[#ret + 1] = p
p = p:child_containing_descendant(n)
--- @diagnostic disable-next-line:undefined-field added in 0.11
p = p:child_containing_descendant(n) --- @type TSNode?
end
ret[#ret + 1] = n
else
while n do
table.insert(ret, 1, n)
n = n:parent()
n = n:parent() --- @type TSNode?
end
end

Expand Down Expand Up @@ -86,6 +88,7 @@ end
--- @param query vim.treesitter.Query
--- @return Range4?
local context_range = cache.memoize(function(node, bufnr, query)
--- @diagnostic disable-next-line:missing-fields
local range = { node:range() } --- @type Range4
range[3] = range[1] + 1
range[4] = 0
Expand Down
26 changes: 21 additions & 5 deletions lua/treesitter-context/render.lua
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,9 @@ local function highlight_contexts(bufnr, ctx_bufnr, contexts)
local parser = buf_highlighter.tree

parser:for_each_tree(function(tstree, ltree)
--- @diagnostic disable-next-line:invisible
local buf_query = buf_highlighter:get_query(ltree:lang())
--- @diagnostic disable-next-line:invisible
local query = buf_query:query()
if not query then
return
Expand All @@ -164,10 +166,13 @@ local function highlight_contexts(bufnr, ctx_bufnr, contexts)
if nsrow >= start_row then
local msrow = offset + (nsrow - start_row)
local merow = offset + (nerow - start_row)
local hl --- @type integer
local hl --- @type integer?
--- @diagnostic disable-next-line: invisible naughty
if buf_query.get_hl_from_capture then
--- @diagnostic disable-next-line: invisible naughty
hl = buf_query:get_hl_from_capture(capture)
else
--- @diagnostic disable-next-line: invisible naughty
hl = buf_query.hl_cache[capture]
end
local priority = tonumber(metadata.priority)
Expand Down Expand Up @@ -231,7 +236,7 @@ local function build_lno_str(win, lnum, width)
winid = win,
use_statuscol_lnum = lnum,
highlights = true,
fillchar = ' ', -- Fixed in Neovim 0.10 (#396)
fillchar = ' ', -- Fixed in Neovim 0.10 (#396)
})
if ok then
return data.str, data.highlights
Expand Down Expand Up @@ -341,6 +346,7 @@ end
--- @param context_winid integer
local function horizontal_scroll_contexts(winid, context_winid)
local active_win_view = api.nvim_win_call(winid, fn.winsaveview)
--- @type vim.fn.winsaveview.ret
local context_win_view = api.nvim_win_call(context_winid, fn.winsaveview)
if active_win_view.leftcol ~= context_win_view.leftcol then
context_win_view.leftcol = active_win_view.leftcol
Expand All @@ -357,11 +363,19 @@ local function copy_extmarks(bufnr, ctx_bufnr, contexts)
local offset = 0
for _, context in ipairs(contexts) do
local ctx_srow, ctx_scol, ctx_erow, ctx_ecol = context[1], context[2], context[3], context[4]
local extmarks = api.nvim_buf_get_extmarks(bufnr, -1, {ctx_srow, ctx_scol}, {ctx_erow, ctx_ecol}, { details = true })
local extmarks = api.nvim_buf_get_extmarks(
bufnr,
-1,
{ ctx_srow, ctx_scol },
{ ctx_erow, ctx_ecol },
{ details = true }
)

for _, m in ipairs(extmarks) do
--- @type integer, integer, integer, vim.api.keyset.extmark_details
local id, row, col, opts = m[1], m[2], m[3], m[4]
local id = m[1]
local row = m[2]
local col = m[3] --[[@as integer]]
local opts = m[4] --[[@as vim.api.keyset.extmark_details]]

local start_row = offset + (row - ctx_srow)

Expand All @@ -378,6 +392,7 @@ local function copy_extmarks(bufnr, ctx_bufnr, contexts)
end_col = opts.end_col,
priority = opts.priority,
hl_group = opts.hl_group,
--- @diagnostic disable-next-line:assign-type-mismatch bug in core
end_right_gravity = opts.end_right_gravity,
right_gravity = opts.right_gravity,
hl_eol = opts.hl_eol,
Expand All @@ -387,6 +402,7 @@ local function copy_extmarks(bufnr, ctx_bufnr, contexts)
hl_mode = opts.hl_mode,
line_hl_group = opts.line_hl_group,
spell = opts.spell,
--- @diagnostic disable-next-line:assign-type-mismatch fixed in 0.11
url = opts.url,
}, opts.ns_id)
end
Expand Down
1 change: 0 additions & 1 deletion lua/treesitter-context/util.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

local M = {}

--- @param r Range4
Expand Down
Loading

0 comments on commit 01455ea

Please sign in to comment.