aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas Vigouroux <tomvig38@gmail.com>2020-04-20 16:18:02 +0200
committerThomas Vigouroux <tomvig38@gmail.com>2020-04-22 22:02:42 +0200
commitd05728e155ced9a815bb3f6fc9e8004093beb3d3 (patch)
treed97aa90a327d54e1099694dcf01b847b033f3b27
parentMerge pull request #20 from kyazdani42/fix-readme (diff)
downloadnvim-treesitter-d05728e155ced9a815bb3f6fc9e8004093beb3d3.tar
nvim-treesitter-d05728e155ced9a815bb3f6fc9e8004093beb3d3.tar.gz
nvim-treesitter-d05728e155ced9a815bb3f6fc9e8004093beb3d3.tar.bz2
nvim-treesitter-d05728e155ced9a815bb3f6fc9e8004093beb3d3.tar.lz
nvim-treesitter-d05728e155ced9a815bb3f6fc9e8004093beb3d3.tar.xz
nvim-treesitter-d05728e155ced9a815bb3f6fc9e8004093beb3d3.tar.zst
nvim-treesitter-d05728e155ced9a815bb3f6fc9e8004093beb3d3.zip
feat: syntax highlighting
-rw-r--r--lua/nvim-treesitter.lua3
-rw-r--r--lua/nvim-treesitter/highlight.lua17
-rw-r--r--lua/nvim-treesitter/query.lua2
-rw-r--r--queries/lua/highlights.scm36
4 files changed, 57 insertions, 1 deletions
diff --git a/lua/nvim-treesitter.lua b/lua/nvim-treesitter.lua
index 8f2fa05ed..b053f39b0 100644
--- a/lua/nvim-treesitter.lua
+++ b/lua/nvim-treesitter.lua
@@ -3,6 +3,7 @@ local parsers = require'nvim-treesitter.parsers'
local configs = require 'nvim-treesitter.configs'
local install = require'nvim-treesitter.install'
local locals = require'nvim-treesitter.locals'
+local highlight = require'nvim-treesitter.highlight'
local M = {}
@@ -14,6 +15,8 @@ end
-- this is the main interface through the plugin
function M.setup(lang)
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
diff --git a/lua/nvim-treesitter/highlight.lua b/lua/nvim-treesitter/highlight.lua
new file mode 100644
index 000000000..4acb202b5
--- /dev/null
+++ b/lua/nvim-treesitter/highlight.lua
@@ -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
diff --git a/lua/nvim-treesitter/query.lua b/lua/nvim-treesitter/query.lua
index e9f671f93..277c29697 100644
--- a/lua/nvim-treesitter/query.lua
+++ b/lua/nvim-treesitter/query.lua
@@ -62,7 +62,7 @@ function M.iter_prepared_matches(query, qnode, bufnr, start_row, end_row)
local preds = query.info.patterns[pattern]
if preds then
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])
end
end
diff --git a/queries/lua/highlights.scm b/queries/lua/highlights.scm
new file mode 100644
index 000000000..6efdece7a
--- /dev/null
+++ b/queries/lua/highlights.scm
@@ -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