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

feat(treesitter): no longer depends on nvim-treesitter #151

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ So why not nvim-bqf?

### Requirements

- [Neovim](https://github.com/neovim/neovim) 0.6.1 or later
- [Neovim](https://github.com/neovim/neovim) 0.7.2 or later
- [fzf](https://github.com/junegunn/fzf) (optional, 0.42.0 later)
- [nvim-treesitter](https://github.com/nvim-treesitter/nvim-treesitter) (optional)

Expand All @@ -97,8 +97,9 @@ end
use {'nvim-treesitter/nvim-treesitter', run = ':TSUpdate'}
```

The nvim-bqf's preview builds upon the buffers. I highly recommended to use
[nvim-treesitter](https://github.com/nvim-treesitter/nvim-treesitter) to do syntax for the buffer,
The nvim-bqf's preview builds upon the buffers. I highly recommended to use queries
provided by [nvim-treesitter](https://github.com/nvim-treesitter/nvim-treesitter)
to get more accurate syntax highlighting,
because vim's syntax is very lagging and is extremely bad for the user experience in large files.

> nvim-bqf has optimized the preview performance for treesitter
Expand Down
37 changes: 22 additions & 15 deletions lua/bqf/preview/treesitter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
local M = {}

local api = vim.api
local treesitter = vim.treesitter

local parsers, configs
-- Shim function for compatibility
local get_lang
local parsersCache
local parsersLimit
local lru
Expand All @@ -14,7 +16,7 @@ local function injectParserForHighlight(parser, srcBufnr, dstBufnr, loaded)
parser._source = dstBufnr
end

vim.treesitter.highlighter.new(parser)
treesitter.highlighter.new(parser)

if loaded then
parser._source = srcBufnr
Expand All @@ -27,8 +29,8 @@ function M.disableActive(bufnr)
if not initialized then
return
end
if vim.treesitter.highlighter.active[bufnr] then
vim.treesitter.highlighter.active[bufnr] = nil
if treesitter.highlighter.active[bufnr] then
treesitter.highlighter.active[bufnr] = nil
end
end

Expand All @@ -44,15 +46,15 @@ function M.tryAttach(srcBufnr, dstBufnr, loaded)
end
local parser
if loaded then
parser = parsers.get_parser(srcBufnr)
parser = treesitter.get_parser(srcBufnr)
else
parser = parsersCache:get(srcBufnr)
if parser and not api.nvim_buf_is_valid(parser:source()) then
parser = nil
parsersCache:set(srcBufnr, nil)
end
end
if parser and configs.is_enabled('highlight', parser:lang(), srcBufnr) then
if parser and treesitter.highlighter.active[srcBufnr] then
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without nvim-treesitter, we can't decide whether the buffer of lang should render, so the condition may be meanless.

highlighter.active[bufnr] checks whether the target buffer is rendered by treesitter or not, it is incorrect to use it here.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only way to enable treesitter highlighting is vim.treesitter.start in the future, nvim-treesitter will no longer needed, the config module is removed in the experimental branch, see https://github.com/nvim-treesitter/nvim-treesitter/tree/main.

In the meantime, it is supported in neovim 0.10 now, now c, lua, and vim parsers and corresponding queries are bundled within neovim distribution, so everyone has treesitter bundled and highlight enabled for lua, see https://github.com/neovim/neovim/blob/5931f780e0282ad486fa070bb05b3877cc1d44f0/runtime/ftplugin/lua.lua#L2

nvim-treesitter will only provide extra parsers and queries in the experimental main branch and the future 1.0 release.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First, we need a prediction to decide whether the buffer of lang should render, highlighter.active[bufnr] is a state here, not a prediction. Without nvim-treesitter, passby this condition. If you have any ideas for prediction, write it to code directly.

Second, we must keep the original configs.is_enabled for nvim-treesitter.

injectParserForHighlight(parser, srcBufnr, dstBufnr, loaded)
ret = true
end
Expand All @@ -69,8 +71,8 @@ function M.attach(srcBufnr, dstBufnr, fileType)
if not initialized then
return ret
end
local lang = parsers.ft_to_lang(fileType)
if not configs.is_enabled('highlight', lang, srcBufnr) then
local lang = get_lang(fileType)
if not treesitter.highlighter.active[srcBufnr] then
return ret
end

Expand All @@ -80,9 +82,9 @@ function M.attach(srcBufnr, dstBufnr, fileType)
if loaded then
-- delete old cache if buffer has loaded
parsersCache:set(srcBufnr, nil)
parser = parsers.get_parser(srcBufnr, lang)
parser = treesitter.get_parser(srcBufnr, lang)
else
parser = parsers.get_parser(dstBufnr, lang)
parser = treesitter.get_parser(dstBufnr, lang)
-- no need to deepcopy the parser for the cache, upstream only dereference parser and
-- invalidate it to make self._tree up to date, so we can cache the parser and reuse it
-- to speed up rendering buffer.
Expand Down Expand Up @@ -112,12 +114,17 @@ function M.shrinkCache()
end

local function init()
initialized, parsers = pcall(require, 'nvim-treesitter.parsers')
if not initialized then
return
local language, parsers
initialized, language = pcall(require, 'vim.treesitter.language')
if initialized then
get_lang = language.get_lang
else
initialized, parsers = pcall(require, 'nvim-treesitter.parsers')
if not initialized then
return
end
get_lang = parsers.ft_to_lang
end
initialized = true
configs = require('nvim-treesitter.configs')
lru = require('bqf.struct.lru')

parsersLimit = 48
Expand Down