Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: do not use vim.treesitter.highlighter #392

Merged
merged 2 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 23 additions & 25 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,37 +8,35 @@ on:
workflow_dispatch:

jobs:
build:
commit_lint:
runs-on: ubuntu-latest
steps:
# Check commit messages
- uses: webiny/action-conventional-commits@v1.1.0

test:
runs-on: ubuntu-latest

strategy:
fail-fast: false
matrix:
neovim_branch: ['v0.9.5']
runs-on: ubuntu-latest
neovim_version: ['0.9.5']

env:
NEOVIM_BRANCH: ${{ matrix.neovim_branch }}
NEOVIM_VERSION: ${{ matrix.neovim_version }}

steps:
- uses: actions/checkout@v3

- name: Setup build dependencies
run: |
sudo apt-get update &&
sudo apt-get install -y \
cmake \
g++ \
gettext \
libtool-bin \
lua-bitop \
ninja-build \
unzip

- name: Cache neovim
uses: actions/cache@v3
- name: Checkout
uses: actions/checkout@v4

- uses: leafo/gh-actions-lua@v10
with:
path: neovim-${{env.NEOVIM_BRANCH}}
key: build-${{env.NEOVIM_BRANCH}}
luaVersion: "5.1.5"

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

- name: Build Neovim
run: make neovim NEOVIM_BRANCH=$NEOVIM_BRANCH
- name: Download nvim-test
run: make nvim-test

- name: Run Test
run: make test NEOVIM_BRANCH=$NEOVIM_BRANCH
run: make test NEOVIM_VERSION=$NEOVIM_VERSION
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
neovim-*
nvim-test
nvim-treesitter
3 changes: 2 additions & 1 deletion .luarc.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"workspace.library": [
"$VIMRUNTIME",
"${3rd}/busted/library",
"${3rd}/luv/library"
"${3rd}/luv/library",
"nvim-test"
],
"workspace.checkThirdParty": false,
"diagnostics": {
Expand Down
58 changes: 15 additions & 43 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,57 +1,29 @@
.DEFAULT_GOAL := test

NEOVIM_BRANCH := v0.9.5
NEOVIM_VERSION := 0.9.5

FILTER=.*

NEOVIM := neovim-$(NEOVIM_BRANCH)
NVIM_TS_SHA := 883c72cd

.PHONY: neovim
neovim: $(NEOVIM)

$(NEOVIM):
git clone --depth 1 https://github.com/neovim/neovim --branch $(NEOVIM_BRANCH) $@
make -C $@
FILTER=.*

nvim-treesitter:
git clone --depth 1 https://github.com/nvim-treesitter/nvim-treesitter
git clone \
--filter=blob:none \
https://github.com/nvim-treesitter/nvim-treesitter
cd nvim-treesitter && git checkout $(NVIM_TS_SHA)

nvim-treesitter/parser/%.so: nvim-treesitter $(NEOVIM)
$(RM) -r $@
VIMRUNTIME=$(NEOVIM)/runtime $(NEOVIM)/build/bin/nvim \
--headless \
--clean \
--cmd 'set rtp+=./nvim-treesitter' \
-c "TSInstallSync $*" \
-c "q"

export VIMRUNTIME=$(PWD)/$(NEOVIM)/runtime

BUSTED = $$( [ -f $(NEOVIM)/test/busted_runner.lua ] \
&& echo "$(NEOVIM)/build/bin/nvim -ll $(NEOVIM)/test/busted_runner.lua" \
|| echo "$(NEOVIM)/.deps/usr/bin/busted" )
nvim-test:
git clone https://github.com/lewis6991/nvim-test
nvim-test/bin/nvim-test --init

.PHONY: test
test: $(NEOVIM) nvim-treesitter \
nvim-treesitter/parser/cpp.so \
nvim-treesitter/parser/lua.so \
nvim-treesitter/parser/rust.so \
nvim-treesitter/parser/typescript.so
$(BUSTED) \
-v \
--lazy \
--helper=$(PWD)/test/preload.lua \
--output test.busted.outputHandlers.nvim \
--lpath=$(PWD)/$(NEOVIM)/?.lua \
--lpath=$(PWD)/$(NEOVIM)/build/?.lua \
--lpath=$(PWD)/$(NEOVIM)/runtime/lua/?.lua \
--lpath=$(PWD)/nvim-treesitter/lua/?.lua \
--lpath=$(PWD)/?.lua \
test: nvim-test nvim-treesitter
nvim-test/bin/nvim-test test \
--runner_version $(NEOVIM_VERSION) \
--target_version $(NEOVIM_VERSION) \
--lpath=$(PWD)/lua/?.lua \
--filter=$(FILTER) \
$(PWD)/test

-@stty sane
--verbose

lint:
luacheck lua
55 changes: 41 additions & 14 deletions lua/treesitter-context/render.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
local api, fn = vim.api, vim.fn
local highlighter = vim.treesitter.highlighter

local util = require('treesitter-context.util')
local config = require('treesitter-context.config')
local memoize = require('treesitter-context.cache').memoize

local ns = api.nvim_create_namespace('nvim-treesitter-context')

Expand Down Expand Up @@ -107,27 +107,54 @@ local function add_extmark(bufnr, row, col, opts)
end
end

--- @param query vim.treesitter.Query
--- @param capture integer
--- @return integer
local hl_from_capture = memoize(function(query, capture, lang)
local name = query.captures[capture]
local hl = 0
if not vim.startswith(name, '_') then
hl = api.nvim_get_hl_id_by_name('@' .. name .. '.' .. lang)
end
return hl
end, function(_, capture, lang)
return lang..tostring(capture)
end)

--- @param contexts Range4[]
--- @return integer start_row, integer end_row
local function get_contexts_range(contexts)
--- @type integer, integer
local srow, erow
for i, context in ipairs(contexts) do
local csrow, cerow = context[1], context[3]
if i == 1 or csrow < srow then
srow = csrow
end

if i == 1 or cerow > erow then
erow = cerow
end
end
return srow, erow
end

--- @param bufnr integer
--- @param ctx_bufnr integer
--- @param contexts Range4[]
local function highlight_contexts(bufnr, ctx_bufnr, contexts)
api.nvim_buf_clear_namespace(ctx_bufnr, ns, 0, -1)

local buf_highlighter = highlighter.active[bufnr]

copy_option('tabstop', bufnr, ctx_bufnr)

if not buf_highlighter then
-- Use standard highlighting when TS highlighting is not available
copy_option('filetype', bufnr, ctx_bufnr)
return
end

local parser = buf_highlighter.tree
local parser = vim.treesitter.get_parser(bufnr)
local srow, erow = get_contexts_range(contexts)
parser:parse({srow, erow})

parser:for_each_tree(function(tstree, ltree)
local buf_query = buf_highlighter:get_query(ltree:lang())
local query = buf_query:query()
local lang = ltree:lang()
local query = vim.treesitter.query.get(lang, 'highlights')

if not query then
return
end
Expand All @@ -151,13 +178,13 @@ local function highlight_contexts(bufnr, ctx_bufnr, contexts)
local msrow = offset + (nsrow - start_row)
local merow = offset + (nerow - start_row)

local hl = buf_query.hl_cache[capture]
local priority = tonumber(metadata.priority) or vim.highlight.priorities.treesitter
add_extmark(ctx_bufnr, msrow, nscol, {
end_row = merow,
end_col = necol,
priority = priority + p,
hl_group = hl
hl_group = hl_from_capture(query, capture, lang),
conceal = metadata.conceal,
})

-- TODO(lewis6991): Extmarks of equal priority appear to apply
Expand Down
Loading
Loading