mirror of
https://github.com/nvim-treesitter/nvim-treesitter.git
synced 2026-07-13 17:00:09 -04:00
feat: syntax highlighting
This commit is contained in:
parent
8bb53c93d0
commit
d05728e155
4 changed files with 57 additions and 1 deletions
|
|
@ -3,6 +3,7 @@ local parsers = require'nvim-treesitter.parsers'
|
||||||
local configs = require 'nvim-treesitter.configs'
|
local configs = require 'nvim-treesitter.configs'
|
||||||
local install = require'nvim-treesitter.install'
|
local install = require'nvim-treesitter.install'
|
||||||
local locals = require'nvim-treesitter.locals'
|
local locals = require'nvim-treesitter.locals'
|
||||||
|
local highlight = require'nvim-treesitter.highlight'
|
||||||
|
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
|
|
@ -14,6 +15,8 @@ end
|
||||||
-- this is the main interface through the plugin
|
-- this is the main interface through the plugin
|
||||||
function M.setup(lang)
|
function M.setup(lang)
|
||||||
if parsers.has_parser(lang) then
|
if parsers.has_parser(lang) then
|
||||||
|
local autocmd = "autocmd NvimTreesitter FileType %s lua require'nvim-treesitter.highlight'.setup()"
|
||||||
|
api.nvim_command(string.format(autocmd, lang))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
17
lua/nvim-treesitter/highlight.lua
Normal file
17
lua/nvim-treesitter/highlight.lua
Normal file
|
|
@ -0,0 +1,17 @@
|
||||||
|
local api = vim.api
|
||||||
|
local queries = require'nvim-treesitter.query'
|
||||||
|
local ts = vim.treesitter
|
||||||
|
|
||||||
|
local M = {}
|
||||||
|
|
||||||
|
function M.setup(bufnr, ft)
|
||||||
|
local buf = bufnr or api.nvim_get_current_buf()
|
||||||
|
local ft = ft or api.nvim_buf_get_option(buf, 'ft')
|
||||||
|
|
||||||
|
local query = queries.get_query(ft, "highlights")
|
||||||
|
if not query then return end
|
||||||
|
|
||||||
|
ts.TSHighlighter.new(query, buf, ft)
|
||||||
|
end
|
||||||
|
|
||||||
|
return M
|
||||||
|
|
@ -62,7 +62,7 @@ function M.iter_prepared_matches(query, qnode, bufnr, start_row, end_row)
|
||||||
local preds = query.info.patterns[pattern]
|
local preds = query.info.patterns[pattern]
|
||||||
if preds then
|
if preds then
|
||||||
for _, pred in pairs(preds) do
|
for _, pred in pairs(preds) do
|
||||||
if pred[1] == "set!" and pred[2] ~= nil then
|
if pred[1] == "set!" and type(pred[2]) == "string" then
|
||||||
insert_to_path(prepared_match, split(pred[2]), pred[3])
|
insert_to_path(prepared_match, split(pred[2]), pred[3])
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
36
queries/lua/highlights.scm
Normal file
36
queries/lua/highlights.scm
Normal file
|
|
@ -0,0 +1,36 @@
|
||||||
|
;;; Highlighting for lua
|
||||||
|
|
||||||
|
;;; Builtins
|
||||||
|
;; Keywords
|
||||||
|
"local" @keyword
|
||||||
|
"if" @keyword
|
||||||
|
"then" @keyword
|
||||||
|
"else" @keyword
|
||||||
|
"elseif" @keyword
|
||||||
|
"end" @keyword
|
||||||
|
"return" @keyword
|
||||||
|
"do" @keyword
|
||||||
|
"while" @keyword
|
||||||
|
"repeat" @keyword
|
||||||
|
"for" @keyword
|
||||||
|
|
||||||
|
;; Operators
|
||||||
|
"~=" @operator
|
||||||
|
"==" @operator
|
||||||
|
"not" @operator
|
||||||
|
"and" @operator
|
||||||
|
"or" @operator
|
||||||
|
|
||||||
|
;; Constants
|
||||||
|
(false) @constant
|
||||||
|
(true) @constant
|
||||||
|
(nil) @constant
|
||||||
|
|
||||||
|
;; Nodes
|
||||||
|
(function "function" @function "end" @function)
|
||||||
|
(comment) @comment
|
||||||
|
(string) @string
|
||||||
|
(number) @constant
|
||||||
|
|
||||||
|
;; Error
|
||||||
|
(ERROR) @Error
|
||||||
Loading…
Add table
Add a link
Reference in a new issue