-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for the typst language
- Loading branch information
1 parent
c4e8260
commit e0e4dbe
Showing
2 changed files
with
49 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
local document = require("image/utils/document") | ||
return document.create_document_integration({ | ||
name = "typst", | ||
-- debug = true, | ||
default_options = { | ||
clear_in_insert_mode = false, | ||
download_remote_images = true, | ||
only_render_image_at_cursor = false, | ||
filetypes = { "typst" }, | ||
}, | ||
query_buffer_images = function(buffer) | ||
local buf = buffer or vim.api.nvim_get_current_buf() | ||
|
||
local parser = vim.treesitter.get_parser(buf, "typst") | ||
local root = parser:parse()[1]:root() | ||
local query = | ||
vim.treesitter.query.parse("typst", '(call item: (ident) @name (#eq? @name "image") (group (string) @url))') | ||
|
||
local images = {} | ||
local current_image = nil | ||
|
||
---@diagnostic disable-next-line: missing-parameter | ||
for id, node in query:iter_captures(root, 0) do | ||
local capture = query.captures[id] | ||
local value = vim.treesitter.get_node_text(node, buf) | ||
|
||
if capture == "name" then | ||
-- Get the parent since we want the "call" and not only the "ident" | ||
local start_row, start_col, end_row, end_col = node:parent():range() | ||
current_image = { | ||
node = node, | ||
range = { start_row = start_row, start_col = start_col, end_row = end_row, end_col = end_col }, | ||
} | ||
elseif current_image and capture == "url" then | ||
-- We need to remove the quotes from the string | ||
-- '"The URL"' -> "The URL" | ||
current_image.url = string.sub(value, 2, -2) | ||
|
||
table.insert(images, current_image) | ||
current_image = nil | ||
end | ||
end | ||
|
||
return images | ||
end, | ||
}) |