mirror of
https://github.com/nvim-treesitter/nvim-treesitter
synced 2024-09-16 14:34:05 +02:00
commit
ac3c2ec2ec
5 changed files with 168 additions and 0 deletions
13
.editorconfig
Normal file
13
.editorconfig
Normal file
|
@ -0,0 +1,13 @@
|
|||
root = true
|
||||
|
||||
[*]
|
||||
indent_style = space
|
||||
indent_size = 2
|
||||
tab_width = 8
|
||||
end_of_line = lf
|
||||
insert_final_newline = true
|
||||
charset = utf-8
|
||||
|
||||
[{Makefile,**/Makefile,runtime/doc/*.txt}]
|
||||
indent_style = tab
|
||||
indent_size = 8
|
13
lua/nvim-treesitter.lua
Normal file
13
lua/nvim-treesitter.lua
Normal file
|
@ -0,0 +1,13 @@
|
|||
local api = vim.api
|
||||
local parsers = require'nvim-treesitter.parsers'
|
||||
|
||||
local M = {}
|
||||
|
||||
-- This function sets up everythin needed for a given language
|
||||
-- this is the main interface through the plugin
|
||||
function M.setup(lang)
|
||||
if parsers.has_parser(lang) then
|
||||
end
|
||||
end
|
||||
|
||||
return M
|
21
lua/nvim-treesitter/parsers.lua
Normal file
21
lua/nvim-treesitter/parsers.lua
Normal file
|
@ -0,0 +1,21 @@
|
|||
local api = vim.api
|
||||
local ts = vim.treesitter
|
||||
|
||||
local M = {}
|
||||
|
||||
function M.has_parser(lang)
|
||||
local lang = lang or api.nvim_buf_get_option(0, 'filetype')
|
||||
return #api.nvim_get_runtime_file('parser/' .. lang .. '.*', false) > 0
|
||||
end
|
||||
|
||||
function M.get_parser(bufnr)
|
||||
if M.has_parser() then
|
||||
local buf = bufnr or api.nvim_get_current_buf()
|
||||
if not M[buf] then
|
||||
M[buf] = ts.get_parser(buf)
|
||||
end
|
||||
return M[buf]
|
||||
end
|
||||
end
|
||||
|
||||
return M
|
76
lua/nvim-treesitter/query.lua
Normal file
76
lua/nvim-treesitter/query.lua
Normal file
|
@ -0,0 +1,76 @@
|
|||
-- Treesitter utils
|
||||
|
||||
local api = vim.api
|
||||
local ts = vim.treesitter
|
||||
|
||||
local M = {}
|
||||
|
||||
local function read_query_file(fname)
|
||||
return table.concat(vim.fn.readfile(fname), '\n')
|
||||
end
|
||||
|
||||
function M.get_query(ft, query_name)
|
||||
local query_files = api.nvim_get_runtime_file(string.format('queries/%s/%s.scm', ft, query_name), false)
|
||||
if #query_files > 0 then
|
||||
return ts.parse_query(ft, read_query_file(query_files[1]))
|
||||
end
|
||||
end
|
||||
|
||||
function M.iter_prepared_matches(query, qnode, bufnr, start_row, end_row)
|
||||
-- A function that splits a string on '.'
|
||||
local function split(string)
|
||||
local t = {}
|
||||
for str in string.gmatch(string, "([^.]+)") do
|
||||
table.insert(t, str)
|
||||
end
|
||||
|
||||
return t
|
||||
end
|
||||
|
||||
-- Given a path (i.e. a List(String)) this functions inserts value at path
|
||||
local function insert_to_path(object, path, value)
|
||||
local curr_obj = object
|
||||
|
||||
for index=1,(#path -1) do
|
||||
if curr_obj[path[index]] == nil then
|
||||
curr_obj[path[index]] = {}
|
||||
end
|
||||
|
||||
curr_obj = curr_obj[path[index]]
|
||||
end
|
||||
|
||||
curr_obj[path[#path]] = value
|
||||
end
|
||||
|
||||
local matches = query:iter_matches(qnode, bufnr, start_row, end_row)
|
||||
|
||||
return function()
|
||||
local pattern, match = matches()
|
||||
if pattern ~= nil then
|
||||
local prepared_match = {}
|
||||
|
||||
-- Extract capture names from each match
|
||||
for id, node in pairs(match) do
|
||||
local name = query.captures[id] -- name of the capture in the query
|
||||
if name ~= nil then
|
||||
local path = split(name)
|
||||
insert_to_path(prepared_match, path, node)
|
||||
end
|
||||
end
|
||||
|
||||
-- Add some predicates for testing
|
||||
local preds = query.info.patterns[pattern]
|
||||
if preds then
|
||||
for _, pred in pairs(preds) do
|
||||
if pred[1] == "set!" and pred[2] ~= nil then
|
||||
insert_to_path(prepared_match, split(pred[2]), pred[3])
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return prepared_match
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return M
|
45
queries/lua/locals.scm
Normal file
45
queries/lua/locals.scm
Normal file
|
@ -0,0 +1,45 @@
|
|||
;;; DECLARATIONS AND SCOPES
|
||||
|
||||
;; Variable and field declarations
|
||||
((variable_declarator
|
||||
(identifier) @definition)
|
||||
(set! kind "v"))
|
||||
|
||||
((variable_declarator
|
||||
(field_expression object:(*) @definition.associated (property_identifier) @definition))
|
||||
(set! kind "v"))
|
||||
|
||||
;; Parameters
|
||||
((local_function
|
||||
(parameters (identifier) @definition))
|
||||
(set! kind "v"))
|
||||
((function
|
||||
(parameters (identifier) @definition))
|
||||
(set! kind "v"))
|
||||
|
||||
;; Function definitions
|
||||
;; Functions definitions creates both a definition and a new scope
|
||||
((function
|
||||
(function_name_field
|
||||
object: (identifier) @definition.associated
|
||||
(property_identifier) @definition)) @scope
|
||||
(set! kind "m"))
|
||||
|
||||
((function
|
||||
(function_name (identifier) @definition)) @scope
|
||||
(set! kind "f"))
|
||||
|
||||
((local_function
|
||||
(identifier) @definition) @scope
|
||||
(set! kind "f"))
|
||||
|
||||
((if_statement) @scope)
|
||||
((for_in_statement) @scope)
|
||||
((repeat_statement) @scope)
|
||||
;; Loops
|
||||
((loop_expression
|
||||
(identifier) @definition)
|
||||
(set! kind "v"))
|
||||
|
||||
;;; REFERENCES
|
||||
((identifier) @reference)
|
Loading…
Reference in a new issue