From a91b8a4b9663c7c18886a73f07d96b510bc8e97d Mon Sep 17 00:00:00 2001 From: Dan Sully Date: Sun, 15 Dec 2024 10:11:02 -0800 Subject: [PATCH 1/2] Move parser_installed() to a utils function. Update to handle nvim-treesitter's main branch --- .editorconfig | 13 +++++++++++++ lua/markview/health.lua | 21 ++++++++------------- lua/markview/parser.lua | 22 +++++++--------------- lua/markview/renderer.lua | 17 +++++------------ lua/markview/utils.lua | 8 ++++++++ 5 files changed, 41 insertions(+), 40 deletions(-) create mode 100644 .editorconfig diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..f202a68 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,13 @@ +# EditorConfig is awesome: http://EditorConfig.org + +# Unix-style newlines with a newline ending every file +[*] +charset = utf-8 +end_of_line = lf +insert_final_newline = true +trim_trailing_whitespace = true + +[*.lua] +max_line_length = 160 +indent_style = tab +quote_style = double diff --git a/lua/markview/health.lua b/lua/markview/health.lua index d8495fc..cdf7b58 100644 --- a/lua/markview/health.lua +++ b/lua/markview/health.lua @@ -1,19 +1,12 @@ local health = {}; -local ts_available, treesitter_parsers = pcall(require, "nvim-treesitter.parsers"); +local utils = require("markview.utils") -local devicons_loaded = pcall(require, "nvim-web-devicons"); -local mini_loaded = pcall(require, "mini.icons"); +health.check = function() + local ver = vim.version() ---- Checks if a parser is available or not ----@param parser_name string ----@return boolean -local function parser_installed(parser_name) - return (ts_available and treesitter_parsers.has_parser(parser_name)) or pcall(vim.treesitter.query.get, parser_name, "highlights") -end - -health.check = function () - local ver = vim.version(); + local devicons_loaded = pcall(require, "nvim-web-devicons") + local mini_loaded = pcall(require, "mini.icons") vim.health.start("Checking essentials:") @@ -25,6 +18,8 @@ health.check = function () vim.health.error("Unsupported neovim version. Minimum requirement `0.10.1`, found: " .. string.format("`%d.%d.%d`", ver.major, ver.minor, ver.patch)); end + local ts_available, _ = pcall(require, "nvim-treesitter.parsers") + if ts_available then vim.health.ok("`nvim-treesitter/nvim-treesitter` found!") else @@ -34,7 +29,7 @@ health.check = function () vim.health.start("Checking parsers:") for _, parser in ipairs({ "markdown", "markdown_inline", "html", "latex" }) do - if parser_installed(parser) then + if utils.parser_installed(parser) then vim.health.ok("`" .. parser .. "` " .. "was found!"); else vim.health.warn("`" .. parser .. "` " .. "wasn't found."); diff --git a/lua/markview/parser.lua b/lua/markview/parser.lua index e3b9ed7..01f5c90 100644 --- a/lua/markview/parser.lua +++ b/lua/markview/parser.lua @@ -1,14 +1,6 @@ local parser = {}; local lang = require("markview.languages") - -local ts_available, treesitter_parsers = pcall(require, "nvim-treesitter.parsers"); - ---- Checks if a parser is available or not ----@param parser_name string ----@return boolean -local function parser_installed(parser_name) - return (ts_available and treesitter_parsers.has_parser(parser_name)) or pcall(vim.treesitter.query.get, parser_name, "highlights") -end +local utils = require("markview.utils") ---@type markview.configuration | {} parser.cached_conf = {}; @@ -280,7 +272,7 @@ parser.parsed_content = {}; ---@param buffer number ---@param TStree any parser.md = function (buffer, TStree, from, to) - if not parser_installed("markdown") then + if not utils.parser_installed("markdown") then return; end @@ -726,7 +718,7 @@ end ---@param buffer number ---@param TStree any parser.md_inline = function (buffer, TStree, from, to) - if not parser_installed("markdown_inline") then + if not utils.parser_installed("markdown_inline") then return; end @@ -753,7 +745,7 @@ parser.md_inline = function (buffer, TStree, from, to) (inline_link) (full_reference_link) ] @hyperlink) - + ((email_autolink) @email) ((image) @image) @@ -965,7 +957,7 @@ parser.md_inline = function (buffer, TStree, from, to) end parser.html = function (buffer, TStree, from, to) - if not parser_installed("html") then + if not utils.parser_installed("html") then return; end @@ -983,7 +975,7 @@ parser.html = function (buffer, TStree, from, to) local root = TStree:root(); local root_r_start, _, _, _ = root:range(); - if root_r_start >= tbl.row_start and root_r_start <= tbl.row_end then + if root_r_start >= tbl.row_start and root_r_start <= tbl.row_end then return; end @@ -1035,7 +1027,7 @@ parser.html = function (buffer, TStree, from, to) end parser.latex = function (buffer, TStree, from, to) - if not parser_installed("latex") then + if not utils.parser_installed("latex") then return; end diff --git a/lua/markview/renderer.lua b/lua/markview/renderer.lua index 7a47640..be60ece 100644 --- a/lua/markview/renderer.lua +++ b/lua/markview/renderer.lua @@ -2,13 +2,6 @@ local renderer = {}; local devicons_loaded, devicons = pcall(require, "nvim-web-devicons"); local mini_loaded, MiniIcons = pcall(require, "mini.icons"); ---- Checks if a parser is available or not ----@param parser_name string ----@return boolean -local function parser_installed(parser_name) - return (ts_available and treesitter_parsers.has_parser(parser_name)) or pcall(vim.treesitter.query.get, parser_name, "highlights") -end - local utils = require("markview.utils"); local languages = require("markview.languages"); local latex_renderer = require("markview.latex_renderer"); @@ -211,7 +204,7 @@ local display_width = function (text, config) local html_conf = config.html; --- Without inline parser inline these syntaxes shouldn't occur - if not parser_installed("markdown_inline") then + if not utils.parser_installed("markdown_inline") then goto noMdInline; end @@ -475,7 +468,7 @@ local display_width = function (text, config) ::noMdInline:: --- Without HTML parser these syntaxes shouldn't occur - if not parser_installed("html") then + if not utils.parser_installed("html") then return d_width, vim.fn.strdisplaywidth(text), final_string; end @@ -657,7 +650,7 @@ local table_header = function (buffer, content, config_table) -- Extracted width of separator local tbl_col_width = math.max(content.col_widths[curr_tbl_col], tbl_conf.col_min_width or 0); - -- The column number of headers must match the + -- The column number of headers must match the -- column number of separators -- -- No need to add unnecessary condition @@ -1461,7 +1454,7 @@ renderer.render_code_blocks = function (buffer, content, config_table) end -- The text on the final line - -- We need to get the tail section to see if it contains ``` + -- We need to get the tail section to see if it contains ``` local block_end_line = vim.api.nvim_buf_get_lines(buffer, content.row_end - 1, content.row_end, false)[1]; vim.api.nvim_buf_set_extmark(buffer, renderer.namespace, content.row_end - 1, vim.fn.strchars(block_end_line or ""), { @@ -2155,7 +2148,7 @@ end --- Footnote renderer ---@param buffer integer ----@param content table +---@param content table ---@param user_config markview.conf.footnotes renderer.render_footnotes = function (buffer, content, user_config) if not user_config or user_config.enable == false then diff --git a/lua/markview/utils.lua b/lua/markview/utils.lua index 143069b..7562644 100644 --- a/lua/markview/utils.lua +++ b/lua/markview/utils.lua @@ -56,6 +56,14 @@ utils.get_cursor_range = function (buffer, window) local lines = vim.api.nvim_buf_line_count(buffer); return math.max(0, cursor[1] - 1), math.min(lines, cursor[1]); +--- Checks if a parser is available or not +---@param parser_name string +---@return boolean +utils.parser_installed = function(parser_name) + local ts_available, treesitter_parsers = pcall(require, "nvim-treesitter.parsers") + + -- Use pcall as in nvim-treesitter's main branch, '.parsers' is a table, and has no 'has_parser' function. + return (ts_available and pcall(treesitter_parsers, has_parser, parser_name)) or pcall(vim.treesitter.query.get, parser_name, "highlights") end return utils; From 93c2d89c63a511f2ab1ee6a53ebdca474240649a Mon Sep 17 00:00:00 2001 From: Dan Sully Date: Sun, 15 Dec 2024 10:50:24 -0800 Subject: [PATCH 2/2] Fix syntax error. --- lua/markview/utils.lua | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lua/markview/utils.lua b/lua/markview/utils.lua index 7562644..c21d820 100644 --- a/lua/markview/utils.lua +++ b/lua/markview/utils.lua @@ -56,6 +56,8 @@ utils.get_cursor_range = function (buffer, window) local lines = vim.api.nvim_buf_line_count(buffer); return math.max(0, cursor[1] - 1), math.min(lines, cursor[1]); +end + --- Checks if a parser is available or not ---@param parser_name string ---@return boolean