Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migration to ts-org parser 0.2.0. #215

Merged
merged 3 commits into from
Feb 19, 2022
Merged
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
1 change: 1 addition & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ jobs:
- v0.5.0
- v0.5.1
- v0.6.0
- v0.6.1
- nightly
runs-on: ubuntu-latest
steps:
Expand Down
3 changes: 3 additions & 0 deletions DOCS.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ Available options:
* underline - `:underline on`
* italic - `:slant italic`

---

Full configuration example with additional todo keywords and their colors:
```lua
require('orgmode').setup({
Expand Down Expand Up @@ -1183,6 +1185,7 @@ set statusline=%{v:lua.orgmode.statusline()}
```

## Changelog
To track breaking changes, subscribe to [Notice of breaking changes](https://github.com/nvim-orgmode/orgmode/issues/217) issue where those are announced.

#### 24 October 2021
* Help mapping was changed from `?` to `g?` to avoid conflict with built in backward search. See issue [#106](https://github.com/nvim-orgmode/orgmode/issues/106).
Expand Down
25 changes: 7 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,10 @@ call dein#add('nvim-orgmode/orgmode')
```lua
-- init.lua

local parser_config = require "nvim-treesitter.parsers".get_parser_configs()
parser_config.org = {
install_info = {
url = 'https://github.com/milisims/tree-sitter-org',
revision = 'f110024d539e676f25b72b7c80b0fd43c34264ef',
files = {'src/parser.c', 'src/scanner.cc'},
},
filetype = 'org',
}
-- Load custom tree-sitter grammar for org filetype
require('orgmode').setup_ts_grammar()

-- Tree-sitter configuration
require'nvim-treesitter.configs'.setup {
-- If TS highlights are not enabled at all, or disabled via `disable` prop, highlighting will fallback to default Vim syntax highlighting
highlight = {
Expand All @@ -101,16 +95,11 @@ Or if you are using `init.vim`:
```vim
" init.vim
lua << EOF
local parser_config = require "nvim-treesitter.parsers".get_parser_configs()
parser_config.org = {
install_info = {
url = 'https://github.com/milisims/tree-sitter-org',
revision = 'f110024d539e676f25b72b7c80b0fd43c34264ef',
files = {'src/parser.c', 'src/scanner.cc'},
},
filetype = 'org',
}

-- Load custom tree-sitter grammar for org filetype
require('orgmode').setup_ts_grammar()

-- Tree-sitter configuration
require'nvim-treesitter.configs'.setup {
-- If TS highlights are not enabled at all, or disabled via `disable` prop, highlighting will fallback to default Vim syntax highlighting
highlight = {
Expand Down
3 changes: 3 additions & 0 deletions doc/orgmode.txt
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ Available options:
* underline - `:underline on`
* italic - `:slant italic`

--------------------------------------------------------------------------------
Full configuration example with additional todo keywords and their colors:
>
require('orgmode').setup({
Expand Down Expand Up @@ -1572,6 +1573,8 @@ Show the currently clocked in headline (if any), with total clocked time / effor
--------------------------------------------------------------------------------
CHANGELOG *orgmode-changelog*

To track breaking changes, subscribe to Notice of breaking changes (https://github.com/nvim-orgmode/orgmode/issues/217) issue where those are announced.

24 OCTOBER 2021 *orgmode-24_october_2021*

* Help mapping was changed from `?` to `g?` to avoid conflict with built in backward search. See issue #106 (https://github.com/nvim-orgmode/orgmode/issues/106).
Expand Down
6 changes: 3 additions & 3 deletions lua/orgmode/colors/todo_highlighter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ local function add_todo_keyword_highlights()
end, todo_keywords.DONE),
' '
)
table.insert(lines, string.format([[(item "keyword?" @OrgTODO (#any-of? @OrgTODO %s))]], todo_type))
table.insert(lines, string.format([[(item "keyword?" @OrgDONE (#any-of? @OrgDONE %s))]], done_type))
table.insert(lines, string.format([[(item . (expr) @OrgTODO (#any-of? @OrgTODO %s))]], todo_type))
table.insert(lines, string.format([[(item . (expr) @OrgDONE (#any-of? @OrgDONE %s))]], done_type))
for face_name, face_hl in pairs(faces) do
table.insert(lines, string.format([[(item "keyword?" @%s (#eq? @%s %s))]], face_hl, face_hl, face_name))
table.insert(lines, string.format([[(item . (expr) @%s (#eq? @%s %s))]], face_hl, face_hl, face_name))
end
vim.treesitter.set_query('org', 'highlights', table.concat(lines, '\n'))
if vim.bo.filetype == 'org' then
Expand Down
38 changes: 37 additions & 1 deletion lua/orgmode/init.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
_G.orgmode = _G.orgmode or {}
_G.orgmode = _G.orgmode or {
ts_revision = '1c3eb533a9cf6800067357b59e03ac3f91fc3a54',
}
local setup_ts_grammar_used = false
local instance = nil

---@class Org
Expand Down Expand Up @@ -58,10 +61,42 @@ function Org:setup_autocmds()
vim.cmd([[augroup END]])
end

--- @param revision string?
local function setup_ts_grammar(revision)
setup_ts_grammar_used = true
local parser_config = require('nvim-treesitter.parsers').get_parser_configs()
parser_config.org = {
install_info = {
url = 'https://github.com/milisims/tree-sitter-org',
revision = revision or _G.orgmode.ts_revision,
files = { 'src/parser.c', 'src/scanner.cc' },
},
filetype = 'org',
}
end

local function check_ts_grammar()
if setup_ts_grammar_used then
return
end
vim.defer_fn(function()
local parser_config = require('nvim-treesitter.parsers').get_parser_configs()
if parser_config and parser_config.org and parser_config.org.install_info.revision ~= _G.orgmode.ts_revision then
require('orgmode.utils').echo_error({
'You are using outdated version of tree-sitter grammar for Orgmode.',
'To use latest version, replace current grammar installation with "require(\'orgmode\').setup_ts_grammar()" and run :TSUpdate org.',
'More info in setup section of readme: https://github.com/nvim-orgmode/orgmode#setup',
})
end
end, 200)
end

---@param opts? table
---@return Org
local function setup(opts)
opts = opts or {}
instance = Org:new()
check_ts_grammar()
local config = require('orgmode.config'):extend(opts)
vim.defer_fn(function()
if config.notifications.enabled and #vim.api.nvim_list_uis() > 0 then
Expand Down Expand Up @@ -140,6 +175,7 @@ function _G.orgmode.statusline()
end

return {
setup_ts_grammar = setup_ts_grammar,
setup = setup,
reload = reload,
action = action,
Expand Down
30 changes: 30 additions & 0 deletions lua/orgmode/objects/date.lua
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,35 @@ local function from_string(datestr, opts)
return parse_date(date, dayname, adjustments, opts)
end

--- @param datestr string
--- @return table[]
local function parse_parts(datestr)
local result = {}
local counter = 1
local patterns = {
{ type = 'date', rgx = '^%d%d%d%d%-%d%d%-%d%d$' },
{ type = 'dayname', rgx = '^%a%a%a$' },
{ type = 'time', rgx = '^%d?%d:%d%d$' },
{ type = 'time_range', rgx = '^%d?%d:%d%d%-%d?%d:%d%d$' },
{ type = 'adjustment', rgx = '^[%.%+%-]+%d+[hdwmy]?$' },
}
for space, item in string.gmatch(datestr, '(%s*)(%S+)') do
local from = counter + space:len()
for _, dt_pattern in ipairs(patterns) do
if item:match(dt_pattern.rgx) then
table.insert(result, {
type = dt_pattern.type,
value = item,
from = from,
to = from + item:len() - 1,
})
counter = counter + item:len() + space:len()
end
end
end
return result
end

local function from_org_date(datestr, opts)
local from_open, from, from_close, delimiter, to_open, to, to_close = datestr:match(pattern .. '(%-%-)' .. pattern)
if not delimiter then
Expand Down Expand Up @@ -858,6 +887,7 @@ local function parse_all_from_line(line, lnum)
end

return {
parse_parts = parse_parts,
from_org_date = from_org_date,
from_string = from_string,
now = now,
Expand Down
7 changes: 1 addition & 6 deletions lua/orgmode/objects/edit_special/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,7 @@ function EditSpecial:new()
end

function EditSpecial:_parse_position()
local nearest_block_node_info = utils.get_nearest_block_node(
self.file,
{ 'name', 'contents', 'parameters' },
self.org_pos,
true
)
local nearest_block_node_info = utils.get_nearest_block_node(self.file, self.org_pos, true)

if not nearest_block_node_info then
utils.echo_warning('No block node found near cursor')
Expand Down
63 changes: 49 additions & 14 deletions lua/orgmode/org/mappings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -183,38 +183,70 @@ function OrgMappings:_adjust_date_part(direction, amount, fallback)
local do_replacement = function(date)
local col = vim.fn.col('.')
local char = vim.fn.getline('.'):sub(col, col)
local raw_date_value = vim.fn.getline('.'):sub(date.range.start_col + 1, date.range.end_col - 1)
if col == date.range.start_col or col == date.range.end_col then
date.active = not date.active
return self:_replace_date(date)
end
local node = Files.get_node_at_cursor()
local col_from_start = col - date.range.start_col
local modify_end_time = false
local parts = Date.parse_parts(raw_date_value)
local adj = nil
if node:type() == 'date' then
if col_from_start <= 5 then
local modify_end_time = false
local part = nil
for _, p in ipairs(parts) do
if col_from_start >= p.from and col_from_start <= p.to then
part = p
break
end
end

if not part then
return
end

local offset = col_from_start - part.from

if part.type == 'date' then
if offset <= 4 then
adj = get_adj('y')
elseif col_from_start <= 8 then
elseif offset <= 7 then
adj = get_adj('m')
elseif col_from_start <= 15 then
else
adj = get_adj('d')
end
elseif node:type() == 'time' then
local has_end_time = node:parent() and node:parent():type() == 'timerange'
if col_from_start <= 17 then
end

if part.type == 'dayname' then
adj = get_adj('d')
end

if part.type == 'time' then
if offset <= 2 then
adj = get_adj('h')
else
adj = minute_adj
end
end

if part.type == 'time_range' then
if offset <= 2 then
adj = get_adj('h')
elseif col_from_start <= 20 then
elseif offset <= 5 then
adj = minute_adj
elseif has_end_time and col_from_start <= 23 then
elseif offset <= 8 then
adj = get_adj('h')
modify_end_time = true
elseif has_end_time and col_from_start <= 26 then
else
adj = minute_adj
modify_end_time = true
end
elseif (node:type() == 'repeater' or node:type() == 'delay') and char:match('[hdwmy]') ~= nil then
end

if part.type == 'adjustment' then
local map = { h = 'd', d = 'w', w = 'm', m = 'y', y = 'h' }
vim.cmd(string.format('norm!r%s', map[char]))
if map[char] then
vim.cmd(string.format('norm!r%s', map[char]))
end
return true
end

Expand Down Expand Up @@ -397,6 +429,9 @@ function OrgMappings:handle_return(suffix)
suffix = suffix or ''
local current_file = Files.get_current_file()
local item = current_file:get_current_node()
if item.type == 'expr' then
item = current_file:convert_to_file_node(item.node:parent())
end

if item.node:parent() and item.node:parent():type() == 'headline' then
item = current_file:convert_to_file_node(item.node:parent())
Expand Down
42 changes: 20 additions & 22 deletions lua/orgmode/parser/file.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ local utils = require('orgmode.utils')
---@field sections_by_line table<number, Section>
---@field source_code_filetypes string[]
---@field is_archive_file boolean
---@field archive_location string
---@field clocked_headline Section
---@field tags string[]
local File = {}
Expand Down Expand Up @@ -43,7 +44,8 @@ end

function File:_parse()
self:_parse_source_code_filetypes()
self:_parse_sections_and_root_directives()
self:_parse_directives()
self:_parse_sections()
end

function File:get_errors()
Expand Down Expand Up @@ -321,9 +323,8 @@ end

---@return string
function File:get_archive_file_location()
local matches = self:get_ts_matches('(document (directive (name) @name (value) @value (#eq? @name "ARCHIVE")))')
if #matches > 0 then
return config:parse_archive_location(self.filename, matches[1].value.text)
if self.archive_location then
return self.archive_location
end
return config:parse_archive_location(self.filename)
end
Expand All @@ -335,11 +336,8 @@ function File:get_section(index)
end

---@private
function File:_parse_sections_and_root_directives()
function File:_parse_sections()
for child in self.tree:root():iter_children() do
if child:type() == 'directive' then
self:_parse_directive(child)
end
if child:type() == 'section' then
local section = Section.from_node(child, self)
table.insert(self.sections, section)
Expand All @@ -366,11 +364,10 @@ end

---@private
function File:_parse_source_code_filetypes()
local blocks = self:get_ts_matches('(block (name) @name (parameters) @parameters (#eq? @name "SRC"))')
local blocks = self:get_ts_matches('(block name: (expr) @name parameter: (expr) @parameters (#eq? @name "SRC"))')
local source_code_filetypes = {}
for _, item in ipairs(blocks) do
local params = vim.split(item.parameters.text, '%s+')
local ft = params[1]
local ft = item.parameters and item.parameters.text
if
ft
and ft ~= ''
Expand All @@ -383,18 +380,19 @@ function File:_parse_source_code_filetypes()
self.source_code_filetypes = source_code_filetypes
end

function File:_parse_directive(node)
local name = node:named_child(0)
local value = node:named_child(1)
if not name or not value then
return
end

local name_text = self:get_node_text(name)
if name_text:upper() == 'FILETAGS' then
local value_text = self:get_node_text(value)
self.tags = utils.parse_tags_string(value_text)
function File:_parse_directives()
local directives = self:get_ts_matches([[(directive name: (expr) @name value: (value) @value)]])
local tags = {}
for _, directive in ipairs(directives) do
local directive_name = directive.name.text:lower()
if directive_name == 'filetags' then
utils.concat(tags, utils.parse_tags_string(directive.value.text), true)
end
if directive_name == 'archive' then
self.archive_location = config:parse_archive_location(self.filename, directive.value.text)
end
end
self.tags = tags
end

return File
Loading