Skip to content

Commit

Permalink
Add support for neovim 0.11 (#593)
Browse files Browse the repository at this point in the history
* fix: 0.11 compatibility

* chore: style

* fix: array like table

* docs: changelog
  • Loading branch information
tris203 committed May 23, 2024
1 parent 7b59d90 commit 0890a3f
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fixed regression where frontmatter is updated in template files.
- Fixed finding backlinks with URL-encoded path references.
- Fixed using templates with frontmatter when `disable_frontmatter` is set to true. Previously the frontmatter would be removed when the template was inserted, now it will be kept unchanged.
- Add compatibility for NVIM 0.11

## [v3.7.12](https://github.com/epwalsh/obsidian.nvim/releases/tag/v3.7.12) - 2024-05-02

Expand Down
7 changes: 4 additions & 3 deletions lua/obsidian/commands/rename.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ local search = require "obsidian.search"
local util = require "obsidian.util"
local enumerate = require("obsidian.itertools").enumerate
local zip = require("obsidian.itertools").zip
local compat = require "obsidian.compat"

---@param client obsidian.Client
return function(client, data)
Expand Down Expand Up @@ -91,7 +92,7 @@ return function(client, data)
local new_note_path
if #parts > 1 then
parts[#parts] = nil
new_note_path = client.dir:joinpath(unpack(vim.tbl_flatten { parts, new_note_id })):with_suffix ".md"
new_note_path = client.dir:joinpath(unpack(compat.flatten { parts, new_note_id })):with_suffix ".md"
else
new_note_path = (dirname / new_note_id):with_suffix ".md"
end
Expand Down Expand Up @@ -213,12 +214,12 @@ return function(client, data)
}
end

local reference_forms = vim.tbl_flatten {
local reference_forms = compat.flatten {
get_ref_forms(cur_note_id),
get_ref_forms(cur_note_rel_path),
get_ref_forms(string.sub(cur_note_rel_path, 1, -4)),
}
local replace_with = vim.tbl_flatten {
local replace_with = compat.flatten {
get_ref_forms(new_note_id),
get_ref_forms(new_note_rel_path),
get_ref_forms(string.sub(new_note_rel_path, 1, -4)),
Expand Down
13 changes: 13 additions & 0 deletions lua/obsidian/compat.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
local compat = {}

compat.is_list = vim.islist or vim.tbl_islist

compat.flatten = function(t)
if vim.fn.has "nvim-0.11" == 1 then
return vim.iter(t):flatten():totable()
else
return vim.tbl_flatten(t)
end
end

return compat
4 changes: 3 additions & 1 deletion lua/obsidian/itertools.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
local compat = require "obsidian.compat"

local M = {}

---Create an iterator from an iterable type such as a table/array, or string.
Expand Down Expand Up @@ -26,7 +28,7 @@ M.iter = function(iterable)
return function()
return nil
end
elseif vim.tbl_islist(iterable) then
elseif compat.is_list(iterable) then
local i = 1
local n = #iterable

Expand Down
5 changes: 3 additions & 2 deletions lua/obsidian/note.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ local util = require "obsidian.util"
local search = require "obsidian.search"
local iter = require("obsidian.itertools").iter
local enumerate = require("obsidian.itertools").enumerate
local compat = require "obsidian.compat"

local SKIP_UPDATING_FRONTMATTER = { "README.md", "CONTRIBUTING.md", "CHANGELOG.md" }

Expand Down Expand Up @@ -724,10 +725,10 @@ Note.save = function(self, opts)
local new_lines
if opts.insert_frontmatter ~= false then
-- Replace frontmatter.
new_lines = vim.tbl_flatten { self:frontmatter_lines(false, opts.frontmatter), content }
new_lines = compat.flatten { self:frontmatter_lines(false, opts.frontmatter), content }
else
-- Use existing frontmatter.
new_lines = vim.tbl_flatten { existing_frontmatter, content }
new_lines = compat.flatten { existing_frontmatter, content }
end

-- Write new lines.
Expand Down
13 changes: 7 additions & 6 deletions lua/obsidian/search.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ local abc = require "obsidian.abc"
local util = require "obsidian.util"
local iter = require("obsidian.itertools").iter
local run_job_async = require("obsidian.async").run_job_async
local compat = require "obsidian.compat"

local M = {}

M._BASE_CMD = { "rg", "--no-config", "--type=md" }
M._SEARCH_CMD = vim.tbl_flatten { M._BASE_CMD, "--json" }
M._FIND_CMD = vim.tbl_flatten { M._BASE_CMD, "--files" }
M._SEARCH_CMD = compat.flatten { M._BASE_CMD, "--json" }
M._FIND_CMD = compat.flatten { M._BASE_CMD, "--files" }

---@enum obsidian.search.RefTypes
M.RefTypes = {
Expand Down Expand Up @@ -372,7 +373,7 @@ M.build_search_cmd = function(dir, term, opts)
path = assert(vim.fn.fnameescape(path))
end

return vim.tbl_flatten {
return compat.flatten {
M._SEARCH_CMD,
opts:to_ripgrep_opts(),
search_terms,
Expand Down Expand Up @@ -411,7 +412,7 @@ M.build_find_cmd = function(path, term, opts)
additional_opts[#additional_opts + 1] = path
end

return vim.tbl_flatten { M._FIND_CMD, opts:to_ripgrep_opts(), additional_opts }
return compat.flatten { M._FIND_CMD, opts:to_ripgrep_opts(), additional_opts }
end

--- Build the 'rg' grep command for pickers.
Expand All @@ -422,7 +423,7 @@ end
M.build_grep_cmd = function(opts)
opts = SearchOpts.from_tbl(opts and opts or {})

return vim.tbl_flatten {
return compat.flatten {
M._BASE_CMD,
opts:to_ripgrep_opts(),
"--column",
Expand Down Expand Up @@ -590,7 +591,7 @@ M.find_notes_async = function(dir, note_file_name, callback)
-- skip it, but Obsidian does allow root-level notes.
visit_dir(root_dir)

scan.scan_dir_async(root_dir, {
scan.scan_dir_async(root_dir.filename, {
hidden = false,
add_dirs = false,
only_dirs = true,
Expand Down
3 changes: 2 additions & 1 deletion lua/obsidian/util.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
local iter = require("obsidian.itertools").iter
local enumerate = require("obsidian.itertools").enumerate
local log = require "obsidian.log"
local compat = require "obsidian.compat"

local util = {}

Expand Down Expand Up @@ -44,7 +45,7 @@ util.tbl_is_array = function(t)
return false
end

return vim.tbl_islist(t)
return compat.is_list(t)
end

---Check if an object is an non-array table.
Expand Down

0 comments on commit 0890a3f

Please sign in to comment.