diff --git a/after/queries/python/highlights.scm b/after/queries/python/highlights.scm index 74d494e..cf412d2 100644 --- a/after/queries/python/highlights.scm +++ b/after/queries/python/highlights.scm @@ -1,10 +1,5 @@ ; extends -(expression_statement - ((string) @_var @variable) - (#match? @_var "^[\"']{3}[%]{2}.*[%]{2}[\"']{3}$") -) - ; it can be # %% [markdown] or # %% [md] (( (comment) @_mdcomment diff --git a/after/queries/python/injections.scm b/after/queries/python/injections.scm index f6b077c..95a481a 100644 --- a/after/queries/python/injections.scm +++ b/after/queries/python/injections.scm @@ -1,13 +1,5 @@ ; extends -; match a string that starts with """%% and ends with %%""" -; or starts with '''%% and ends with %%''' -; and highlight it as markdown -(expression_statement - ((string) @markdown @markdown_inline) - (#match? @markdown_inline "^[\"']{3}[%]{2}.*[%]{2}[\"']{3}$") -) - ; it can be # %% [markdown] or # %% [md] (( (comment) @_mdcomment diff --git a/lua/jupynium/cells.lua b/lua/jupynium/cells.lua index 047e042..2dae8a8 100644 --- a/lua/jupynium/cells.lua +++ b/lua/jupynium/cells.lua @@ -1,5 +1,3 @@ -local utils = require "jupynium.utils" - local M = {} --- Get the line type (cell separator, magic commands, empty, others) @@ -14,9 +12,9 @@ function M.line_type(line) return "cell separator: markdown" elseif vim.fn.trim(line) == "# %%" then return "cell separator: code" - elseif utils.string_begins_with(line, "# ---") then + elseif vim.startswith(line, "# ---") then return "metadata" - elseif utils.string_begins_with(line, "# %") then + elseif vim.startswith(line, "# %") then return "magic command" elseif vim.fn.trim(line) == "" then return "empty" @@ -72,7 +70,7 @@ end ---@return boolean function M.is_line_separator(line) local line_type = M.line_type(line) - if utils.string_begins_with(line_type, "cell separator:") then + if vim.startswith(line_type, "cell separator:") then return true end diff --git a/lua/jupynium/highlighter.lua b/lua/jupynium/highlighter.lua index ae655c4..fae8ef5 100644 --- a/lua/jupynium/highlighter.lua +++ b/lua/jupynium/highlighter.lua @@ -22,44 +22,18 @@ local function set_hlgroup_if_not_exists(hlgroup, hlgroup_default) end function M.setup(opts) - if opts.syntax_highlight.highlight_groups then - -- deprecated, use vim.cmd [[hi! link Jupynium... ...]] - vim.notify_once( - "Jupynium: Setting highlight groups via opts is deprecated. Set directly using e.g. vim.cmd[[hi! link JupyniumCodeCellSeparator Folded]]", - vim.log.levels.WARN - ) - if opts.syntax_highlight.highlight_groups.code_cell_separator then - vim.cmd("hi! link JupyniumCodeCellSeparator " .. opts.syntax_highlight.highlight_groups.code_cell_separator) - end - if opts.syntax_highlight.highlight_groups.markdown_cell_separator then - vim.cmd( - "hi! link JupyniumMarkdownCellSeparator " .. opts.syntax_highlight.highlight_groups.markdown_cell_separator - ) - end - if opts.syntax_highlight.highlight_groups.code_cell_content then - vim.cmd("hi! link JupyniumCodeCellContent " .. opts.syntax_highlight.highlight_groups.code_cell_content) - end - if opts.syntax_highlight.highlight_groups.markdown_cell_content then - vim.cmd("hi! link JupyniumMarkdownCellContent " .. opts.syntax_highlight.highlight_groups.markdown_cell_content) - end - if opts.syntax_highlight.highlight_groups.magic_command then - vim.cmd("hi! link JupyniumMagicCommand " .. opts.syntax_highlight.highlight_groups.magic_command) - end - end - -- If the colourscheme doesn't support Jupynium yet, link to some default highlight groups -- Here we can define some default settings per colourscheme. local colorscheme = vim.g.colors_name if colorscheme == nil then colorscheme = "" end - if utils.string_begins_with(colorscheme, "tokyonight") then + if vim.startswith(colorscheme, "tokyonight") then colorscheme = "tokyonight" end local hlgroup if colorscheme == "tokyonight" then hlgroup = "Pmenu" - set_hlgroup_if_not_exists("JupyniumCodeCellSeparatorString", hlgroup) set_hlgroup_if_not_exists("JupyniumCodeCellSeparator", "Folded") set_hlgroup_if_not_exists("JupyniumMarkdownCellSeparator", hlgroup) set_hlgroup_if_not_exists("JupyniumMarkdownCellContent", hlgroup) @@ -108,8 +82,10 @@ local ns_shortsighted = vim.api.nvim_create_namespace "jupynium-shortsighted" --- Set highlight group for a line ---@param buffer number +---@param namespace number ---@param line_number number 0-indexed ---@param hl_group string +---@param priority? number function M.set_line_hlgroup(buffer, namespace, line_number, hl_group, priority) priority = priority or 99 -- Treesitter uses 100 pcall(vim.api.nvim_buf_set_extmark, buffer, namespace, line_number, 0, { @@ -121,6 +97,7 @@ function M.set_line_hlgroup(buffer, namespace, line_number, hl_group, priority) }) end +---@param namespace number function M.clear_namespace(namespace) vim.api.nvim_buf_clear_namespace(0, namespace, 0, -1) end @@ -192,23 +169,13 @@ function M.update() for i, line_type in ipairs(line_types) do -- priority 9000: above treesitter, below shortsighted -- priority 99: below treesitter (default) - if utils.string_begins_with(line_type, "cell separator: code") then + if line_type == "cell separator: code" then M.set_line_hlgroup(0, ns_highlight, i - 1, "JupyniumCodeCellSeparator", 9000) - if line_type == "cell separator: code (string)" then - -- For closing markdown cell - -- %%""" or %%''' - M.set_line_hlgroup(0, ns_shortsighted, i - 1, "JupyniumCodeCellSeparatorString", 9001) - end - elseif utils.string_begins_with(line_type, "cell separator: markdown") then + elseif line_type == "cell separator: markdown" then M.set_line_hlgroup(0, ns_highlight, i - 1, "JupyniumMarkdownCellSeparator", 9000) - if line_type == "cell separator: markdown (string)" then - -- For opening markdown cell with string - -- """%% or '''%% - M.set_line_hlgroup(0, ns_shortsighted, i - 1, "JupyniumMarkdownCellSeparatorString", 9001) - end - elseif utils.string_begins_with(line_type, "cell content: code") then + elseif line_type == "cell content: code" then M.set_line_hlgroup(0, ns_highlight, i - 1, "JupyniumCodeCellContent") - elseif utils.string_begins_with(line_type, "cell content: markdown") then + elseif line_type == "cell content: markdown" then M.set_line_hlgroup(0, ns_highlight, i - 1, "JupyniumMarkdownCellContent") elseif line_type == "magic command" then M.set_line_hlgroup(0, ns_highlight, i - 1, "JupyniumMagicCommand", 9000) diff --git a/lua/jupynium/init.lua b/lua/jupynium/init.lua index ab9bb43..e12b3dc 100644 --- a/lua/jupynium/init.lua +++ b/lua/jupynium/init.lua @@ -18,7 +18,7 @@ function M.get_folds() local line_types = cells.line_types_entire_buf() for i, line_type in ipairs(line_types) do - if utils.string_begins_with(line_type, "metadata") then + if vim.startswith(line_type, "metadata") then -- make sure metadata is before cells if vim.tbl_isempty(fold) then if vim.tbl_isempty(metadata) then @@ -29,7 +29,7 @@ function M.get_folds() end end end - if utils.string_begins_with(line_type, "cell separator") then + if vim.startswith(line_type, "cell separator") then if vim.tbl_isempty(fold) then fold.startLine = i - 1 -- close metadata diff --git a/lua/jupynium/server.lua b/lua/jupynium/server.lua index c4fd7da..fc6f96d 100644 --- a/lua/jupynium/server.lua +++ b/lua/jupynium/server.lua @@ -40,7 +40,7 @@ local function get_system_cmd(cmd, args) if vim.fn.has "win32" == 1 then -- powershell.exe for powershell <= 5 -- pwsh.exe for powershell >= 6 - if utils.string_begins_with(vim.o.shell, "powershell") or utils.string_begins_with(vim.o.shell, "pwsh") then + if vim.startswith(vim.o.shell, "powershell") or vim.startswith(vim.o.shell, "pwsh") then cmd_str = [[& ']] .. vim.fn.expand(cmd) .. [[']] for _, v in ipairs(args) do cmd_str = cmd_str .. [[ ']] .. v .. [[']] diff --git a/lua/jupynium/utils.lua b/lua/jupynium/utils.lua index 8f929b5..a3f1bb3 100644 --- a/lua/jupynium/utils.lua +++ b/lua/jupynium/utils.lua @@ -1,25 +1,11 @@ local M = {} -function M.string_begins_with(str, start) - if str == nil then - return false - end - return start == "" or str:sub(1, #start) == start -end - -function M.string_ends_with(str, ending) - if str == nil then - return false - end - return ending == "" or str:sub(-#ending) == ending -end - function M.wildcard_to_regex(pattern) local reg = pattern:gsub("([^%w])", "%%%1"):gsub("%%%*", ".*") - if not M.string_begins_with(reg, ".*") then + if not vim.startswith(reg, ".*") then reg = "^" .. reg end - if not M.string_ends_with(reg, ".*") then + if not vim.endswith(reg, ".*") then reg = reg .. "$" end return reg