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

refactor!: remove deprecated syntax #121

Merged
merged 1 commit into from
Jun 7, 2024
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
5 changes: 0 additions & 5 deletions after/queries/python/highlights.scm
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
; extends

(expression_statement
((string) @_var @variable)
(#match? @_var "^[\"']{3}[%]{2}.*[%]{2}[\"']{3}$")
)

; it can be # %% [markdown] or # %% [md]
((
(comment) @_mdcomment
Expand Down
8 changes: 0 additions & 8 deletions after/queries/python/injections.scm
Original file line number Diff line number Diff line change
@@ -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
Expand Down
8 changes: 3 additions & 5 deletions lua/jupynium/cells.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
local utils = require "jupynium.utils"

local M = {}

--- Get the line type (cell separator, magic commands, empty, others)
Expand All @@ -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"
Expand Down Expand Up @@ -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

Expand Down
49 changes: 8 additions & 41 deletions lua/jupynium/highlighter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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, {
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions lua/jupynium/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lua/jupynium/server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 .. [[']]
Expand Down
18 changes: 2 additions & 16 deletions lua/jupynium/utils.lua
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Loading