-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: use treesitter to determine filetype at cursor position
- Loading branch information
Showing
5 changed files
with
101 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
local M = {} | ||
|
||
-- Check if treesitter is available | ||
local ok_parsers, ts_parsers = pcall(require, "nvim-treesitter.parsers") | ||
if not ok_parsers then | ||
ts_parsers = nil | ||
end | ||
|
||
local ok_utils, ts_utils = pcall(require, "nvim-treesitter.ts_utils") | ||
if not ok_utils then | ||
ts_utils = nil | ||
end | ||
|
||
local function get_ft_at_cursor() | ||
if not ok_parsers or not ok_utils then | ||
error("[cmp_nvim_ultisnips.treesitter] Please install the nvim-treesitter plugin to use this feature.") | ||
end | ||
|
||
local parser = ts_parsers.get_parser() | ||
local cur_node = ts_utils.get_node_at_cursor() | ||
if cur_node then | ||
return parser:language_for_range({ cur_node:range() }):lang() | ||
end | ||
return nil | ||
end | ||
|
||
local cur_ft_at_cursor | ||
function M.update_ft_at_cursor() | ||
local new_lang = get_ft_at_cursor() | ||
if new_lang ~= cur_ft_at_cursor then | ||
vim.fn["cmp_nvim_ultisnips#update_filetypes"](cur_ft_at_cursor, new_lang) | ||
cur_ft_at_cursor = new_lang | ||
end | ||
end | ||
|
||
function M.clear_ft_at_cursor() | ||
if cur_ft_at_cursor ~= nil and cur_ft_at_cursor ~= vim.bo.filetype then | ||
vim.fn["cmp_nvim_ultisnips#remove_snippet_filetype"](cur_ft_at_cursor) | ||
cur_ft_at_cursor = nil | ||
end | ||
end | ||
|
||
return M |