Skip to content
This repository was archived by the owner on Apr 24, 2020. It is now read-only.
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
5 changes: 1 addition & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,16 @@ It uses neovim's treesitter api to provide color definition for languages that h
The main goal is to provide a solid example on how to use this library. \
It can be used by those who need better syntax coloring in their neovim environment.

### Development of this plugin will continue when the treesitter api becomes more stable

### Notice

- Requires neovim v0.5
- Is usable in the current state only to test and experiment
- Only js filetypes are supported ATM
- The plugin is under heavy development, it might change a lot or break things.
- Even though its only to experiment while the neovim team is working on a better api, i still wish people to see what can be done with the treesitter API

### Installing

With plug: `Plug 'kyazdani42/highlight.lua'`
With plug: `Plug 'nvim-treesitter/highlight.lua'`

You can install parsers running `:InstallTSParser LANGUAGE`. Only `javascript` is available at the moment. \
The command does not work on windows and might not work on every OS. It depends on `git` and `gcc`.
Expand Down
35 changes: 13 additions & 22 deletions lua/highlight.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,15 @@ local api = vim.api

local filetypes = require'lib/init'
if not filetypes then
return {
attach = function() end,
detach = function() end
}
return { setup = function() end }
end

local parser_cmd = require 'lib/parser'
local has_parser = parser_cmd.has_parser
local get_query = parser_cmd.get_query

local install_parser = require 'lib/install'.install_parser

local init_ts_parser = require 'lib/runner'.init_ts_parser
local BUF_HANDLES = {}

local function on_detach(buf)
Expand All @@ -25,27 +22,13 @@ local function on_detach(buf)
end
end

local function attach_to_buf(buf, attributes)
api.nvim_command('setlocal syntax=off') -- reset the syntax and use only our api
api.nvim_buf_clear_namespace(buf, -1, 0, -1)

local update_highlight = init_ts_parser(buf, attributes)

local has_attached = vim.api.nvim_buf_attach(buf, 0, {
on_detach = on_detach,
on_lines = update_highlight
})

if has_attached then table.insert(BUF_HANDLES, buf) end
end

local function get_attributes(name)
local ext = string.match(name or '', '%.[^%./]*$') or ''
ext = string.sub(ext, 2, -1)
return filetypes[ext]
end

local function attach()
local function setup()
local buf = api.nvim_win_get_buf(0)

for i, handle in pairs(BUF_HANDLES) do
Expand All @@ -54,11 +37,19 @@ local function attach()

local attributes = get_attributes(api.nvim_buf_get_name(buf))
if attributes ~= nil and has_parser(attributes.parser_lang) then
attach_to_buf(buf, attributes)
api.nvim_command('setlocal syntax=off') -- reset the syntax and use only our api
api.nvim_buf_clear_namespace(buf, -1, 0, -1)

local query = get_query(attributes.parser_lang)
if not query then return end

vim.treesitter.TSHighlighter.new(query, buf, attributes.parser_lang)
local has_attached = vim.api.nvim_buf_attach(buf, 0, { on_detach = on_detach })
if has_attached then table.insert(BUF_HANDLES, buf) end
end
end

return {
attach = attach;
setup = setup;
install_parser = install_parser;
}
2 changes: 1 addition & 1 deletion lua/lib/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ local colors = {
}

local filetypes = {
js = require 'syntax/javascript'.get_attributes(colors)
js = require 'syntax/javascript'(colors)
}

local function define_colors(group_colors, color_lang)
Expand Down
22 changes: 21 additions & 1 deletion lua/lib/parser.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,25 @@ local nvim = require 'lib/nvim'
local err_msg = nvim.err_msg
local get_runtime_file = nvim.get_runtime_file

local function read_scm(path)
local f = io.open(path, "rb")
if not f then return nil end
local content = f:read("*all")
f:close()
return content
end

local function get_query(lang)
local file = get_runtime_file('lua/syntax/'..lang..'.scm', false)[1]

if not file or not vim.loop.fs_access(file, 'R') then
err_msg('Did not find query for language `' .. lang ..'`')
return nil
end

return read_scm(file)
end

local function has_parser(lang)
local parser = get_runtime_file('parser/'..lang..'.so', false)[1]

Expand All @@ -14,5 +33,6 @@ local function has_parser(lang)
end

return {
has_parser = has_parser
has_parser = has_parser;
get_query = get_query;
}
110 changes: 0 additions & 110 deletions lua/lib/runner.lua

This file was deleted.

Loading