Skip to content

Commit

Permalink
feat: QuartoPreviewNoWatch and QuartoUpdatePreview commands (#157)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmbuhr authored Dec 22, 2024
1 parent aa6e0a9 commit 601c32b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
33 changes: 33 additions & 0 deletions lua/quarto/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ local cfg = require 'quarto.config'
local tools = require 'quarto.tools'
local util = require 'lspconfig.util'

---Quarto preview
---@param opts table
---@return nil|string url
function M.quartoPreview(opts)
opts = opts or {}
local args = opts.args or ''
Expand Down Expand Up @@ -52,6 +55,8 @@ function M.quartoPreview(opts)

-- Store the terminal buffer and return to previous tab
local quartoOutputBuf = vim.api.nvim_get_current_buf()

-- go back to the previous tab
vim.api.nvim_set_current_tabpage(current_tabpage)
api.nvim_buf_set_var(0, 'quartoOutputBuf', quartoOutputBuf)

Expand All @@ -69,6 +74,34 @@ function M.quartoPreview(opts)
end
end

function M.quartoPreviewNoWatch()
M.quartoPreview({ args = '--no-watch-inputs' })
end

function M.quartoUpdatePreview()
local quartoOutputBuf = api.nvim_buf_get_var(0, 'quartoOutputBuf')
local query_start = 'Browse at http'
local lines = vim.api.nvim_buf_get_lines(quartoOutputBuf, 0, -1, false)
local url = nil
for _, line in ipairs(lines) do
if line:find(query_start) then
url = 'http' .. line:sub(#query_start+1)
break
end
end
if not url then
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)
return
end
api.nvim_buf_set_var(0, 'quartoUrl', url)
local request_url = url .. 'quarto-render/'
local get_request = 'curl -s ' .. request_url
local response = vim.fn.system(get_request)
if response ~= 'rendered' then
vim.notify_once('Failed to update preview with command: ' .. get_request, vim.log.levels.ERROR)
end
end

function M.quartoClosePreview()
local success, quartoOutputBuf = pcall(api.nvim_buf_get_var, 0, 'quartoOutputBuf')
if not success then
Expand Down
2 changes: 2 additions & 0 deletions plugin/quarto.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ local quarto = require 'quarto'
local api = vim.api

api.nvim_create_user_command('QuartoPreview', quarto.quartoPreview, { nargs = '*' })
api.nvim_create_user_command('QuartoPreviewNoWatch', quarto.quartoPreviewNoWatch, { nargs = '*' })
api.nvim_create_user_command('QuartoUpdatePreview', quarto.quartoUpdatePreview, { nargs = '*' })
api.nvim_create_user_command('QuartoClosePreview', quarto.quartoClosePreview, {})
api.nvim_create_user_command('QuartoActivate', quarto.activate, {})
api.nvim_create_user_command('QuartoHelp', quarto.searchHelp, { nargs = 1 })

0 comments on commit 601c32b

Please sign in to comment.