Skip to content

Commit 601c32b

Browse files
authored
feat: QuartoPreviewNoWatch and QuartoUpdatePreview commands (#157)
1 parent aa6e0a9 commit 601c32b

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

lua/quarto/init.lua

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ local cfg = require 'quarto.config'
44
local tools = require 'quarto.tools'
55
local util = require 'lspconfig.util'
66

7+
---Quarto preview
8+
---@param opts table
9+
---@return nil|string url
710
function M.quartoPreview(opts)
811
opts = opts or {}
912
local args = opts.args or ''
@@ -52,6 +55,8 @@ function M.quartoPreview(opts)
5255

5356
-- Store the terminal buffer and return to previous tab
5457
local quartoOutputBuf = vim.api.nvim_get_current_buf()
58+
59+
-- go back to the previous tab
5560
vim.api.nvim_set_current_tabpage(current_tabpage)
5661
api.nvim_buf_set_var(0, 'quartoOutputBuf', quartoOutputBuf)
5762

@@ -69,6 +74,34 @@ function M.quartoPreview(opts)
6974
end
7075
end
7176

77+
function M.quartoPreviewNoWatch()
78+
M.quartoPreview({ args = '--no-watch-inputs' })
79+
end
80+
81+
function M.quartoUpdatePreview()
82+
local quartoOutputBuf = api.nvim_buf_get_var(0, 'quartoOutputBuf')
83+
local query_start = 'Browse at http'
84+
local lines = vim.api.nvim_buf_get_lines(quartoOutputBuf, 0, -1, false)
85+
local url = nil
86+
for _, line in ipairs(lines) do
87+
if line:find(query_start) then
88+
url = 'http' .. line:sub(#query_start+1)
89+
break
90+
end
91+
end
92+
if not url then
93+
vim.notify('Could not find the preview url in the terminal buffer. Maybe it is still warming up. Check the buffer and try again.', vim.log.levels.WARN)
94+
return
95+
end
96+
api.nvim_buf_set_var(0, 'quartoUrl', url)
97+
local request_url = url .. 'quarto-render/'
98+
local get_request = 'curl -s ' .. request_url
99+
local response = vim.fn.system(get_request)
100+
if response ~= 'rendered' then
101+
vim.notify_once('Failed to update preview with command: ' .. get_request, vim.log.levels.ERROR)
102+
end
103+
end
104+
72105
function M.quartoClosePreview()
73106
local success, quartoOutputBuf = pcall(api.nvim_buf_get_var, 0, 'quartoOutputBuf')
74107
if not success then

plugin/quarto.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ local quarto = require 'quarto'
1616
local api = vim.api
1717

1818
api.nvim_create_user_command('QuartoPreview', quarto.quartoPreview, { nargs = '*' })
19+
api.nvim_create_user_command('QuartoPreviewNoWatch', quarto.quartoPreviewNoWatch, { nargs = '*' })
20+
api.nvim_create_user_command('QuartoUpdatePreview', quarto.quartoUpdatePreview, { nargs = '*' })
1921
api.nvim_create_user_command('QuartoClosePreview', quarto.quartoClosePreview, {})
2022
api.nvim_create_user_command('QuartoActivate', quarto.activate, {})
2123
api.nvim_create_user_command('QuartoHelp', quarto.searchHelp, { nargs = 1 })

0 commit comments

Comments
 (0)