Skip to content

Commit

Permalink
chore(format): stylua
Browse files Browse the repository at this point in the history
  • Loading branch information
jmbuhr committed Mar 6, 2024
1 parent a6e7452 commit be95823
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 88 deletions.
12 changes: 6 additions & 6 deletions ftplugin/quarto.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
vim.b.slime_cell_delimiter = "```"
vim.b.slime_cell_delimiter = '```'

local config = require("quarto.config").config
local quarto = require("quarto")
local config = require('quarto.config').config
local quarto = require 'quarto'

local function set_keymaps()
if not config.keymap then
Expand All @@ -10,7 +10,7 @@ local function set_keymaps()
local b = vim.api.nvim_get_current_buf()
local function set(lhs, rhs)
if lhs then
vim.api.nvim_buf_set_keymap(b, "n", lhs, rhs, { silent = true, noremap = true })
vim.api.nvim_buf_set_keymap(b, 'n', lhs, rhs, { silent = true, noremap = true })
end
end
set(config.keymap.definition, ":lua require'otter'.ask_definition()<cr>")
Expand All @@ -35,9 +35,9 @@ if config.lspFeatures.enabled then
-- is used in the lspconfig setup
-- because this gets executed after the `LspAttach` autocommand
-- <https://github.com/neovim/neovim/blob/d0d132fbd055834cbecb3d4e3a123a6ea8f099ec/runtime/lua/vim/lsp.lua#L1702-L1711>
vim.api.nvim_create_autocmd("LspAttach", {
vim.api.nvim_create_autocmd('LspAttach', {
buffer = vim.api.nvim_get_current_buf(),
group = vim.api.nvim_create_augroup("QuartoKeymapSetup", {}),
group = vim.api.nvim_create_augroup('QuartoKeymapSetup', {}),
callback = set_keymaps,
})
end
24 changes: 12 additions & 12 deletions lua/quarto/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ M.defaultConfig = {
closePreviewOnExit = true,
lspFeatures = {
enabled = true,
chunks = "curly",
languages = { "r", "python", "julia", "bash", "html" },
chunks = 'curly',
languages = { 'r', 'python', 'julia', 'bash', 'html' },
diagnostics = {
enabled = true,
triggers = { "BufWritePost" },
triggers = { 'BufWritePost' },
},
completion = {
enabled = true,
Expand All @@ -19,17 +19,17 @@ M.defaultConfig = {
enabled = false,
default_method = nil, -- "molten" or "slime"
ft_runners = {}, -- filetype to runner, ie. `{ python = "molten" }`.
-- Takes precedence over `default_method`
never_run = { "yaml" }, -- filetypes which are never sent to a code runner
-- Takes precedence over `default_method`
never_run = { 'yaml' }, -- filetypes which are never sent to a code runner
},
keymap = {
hover = "K",
definition = "gd",
type_definition = "gD",
rename = "<leader>lR",
format = "<leader>lf",
references = "gr",
document_symbols = "gS",
hover = 'K',
definition = 'gd',
type_definition = 'gD',
rename = '<leader>lR',
format = '<leader>lf',
references = 'gr',
document_symbols = 'gS',
},
}

Expand Down
94 changes: 49 additions & 45 deletions lua/quarto/init.lua
Original file line number Diff line number Diff line change
@@ -1,57 +1,57 @@
local M = {}
local api = vim.api
local cfg = require("quarto.config")
local tools = require("quarto.tools")
local util = require("lspconfig.util")
local cfg = require 'quarto.config'
local tools = require 'quarto.tools'
local util = require 'lspconfig.util'

function M.quartoPreview(opts)
opts = opts or {}
local args = opts.args or ""
local args = opts.args or ''

-- find root directory / check if it is a project
local buffer_path = api.nvim_buf_get_name(0)
local root_dir = util.root_pattern("_quarto.yml")(buffer_path)
local root_dir = util.root_pattern '_quarto.yml'(buffer_path)
local cmd
local mode
if root_dir then
mode = "project"
cmd = "quarto preview" .. " " .. args
mode = 'project'
cmd = 'quarto preview' .. ' ' .. args
else
mode = "file"
if vim.loop.os_uname().sysname == "Windows_NT" then
cmd = 'quarto preview \\"' .. buffer_path .. '\\"' .. " " .. args
mode = 'file'
if vim.loop.os_uname().sysname == 'Windows_NT' then
cmd = 'quarto preview \\"' .. buffer_path .. '\\"' .. ' ' .. args
else
cmd = "quarto preview '" .. buffer_path .. "'" .. " " .. args
cmd = "quarto preview '" .. buffer_path .. "'" .. ' ' .. args
end
end

local quarto_extensions = { ".qmd", ".Rmd", ".ipynb", ".md" }
local file_extension = buffer_path:match("^.+(%..+)$")
if mode == "file" and not file_extension then
vim.notify("Not in a file. exiting.")
local quarto_extensions = { '.qmd', '.Rmd', '.ipynb', '.md' }
local file_extension = buffer_path:match '^.+(%..+)$'
if mode == 'file' and not file_extension then
vim.notify 'Not in a file. exiting.'
return
end
if mode == "file" and not tools.contains(quarto_extensions, file_extension) then
vim.notify("Not a quarto file, ends in " .. file_extension .. " exiting.")
if mode == 'file' and not tools.contains(quarto_extensions, file_extension) then
vim.notify('Not a quarto file, ends in ' .. file_extension .. ' exiting.')
return
end

-- run command in embedded terminal
-- in a new tab and go back to the buffer
vim.cmd("tabedit term://" .. cmd)
vim.cmd('tabedit term://' .. cmd)
local quartoOutputBuf = vim.api.nvim_get_current_buf()
vim.cmd("tabprevious")
api.nvim_buf_set_var(0, "quartoOutputBuf", quartoOutputBuf)
vim.cmd 'tabprevious'
api.nvim_buf_set_var(0, 'quartoOutputBuf', quartoOutputBuf)

if not cfg.config then
return
end

-- close preview terminal on exit of the quarto buffer
if cfg.config.closePreviewOnExit then
api.nvim_create_autocmd({ "QuitPre", "WinClosed" }, {
api.nvim_create_autocmd({ 'QuitPre', 'WinClosed' }, {
buffer = api.nvim_get_current_buf(),
group = api.nvim_create_augroup("quartoPreview", {}),
group = api.nvim_create_augroup('quartoPreview', {}),
callback = function(_, _)
if api.nvim_buf_is_loaded(quartoOutputBuf) then
api.nvim_buf_delete(quartoOutputBuf, { force = true })
Expand All @@ -62,7 +62,7 @@ function M.quartoPreview(opts)
end

function M.quartoClosePreview()
local success, quartoOutputBuf = pcall(api.nvim_buf_get_var, 0, "quartoOutputBuf")
local success, quartoOutputBuf = pcall(api.nvim_buf_get_var, 0, 'quartoOutputBuf')
if not success then
return
end
Expand All @@ -73,25 +73,23 @@ end

M.searchHelp = function(cmd_input)
local topic = cmd_input.args
local url = "https://quarto.org/?q=" .. topic .. "&show-results=1"
local url = 'https://quarto.org/?q=' .. topic .. '&show-results=1'
local sysname = vim.loop.os_uname().sysname
local cmd
if sysname == "Linux" then
if sysname == 'Linux' then
cmd = 'xdg-open "' .. url .. '"'
elseif sysname == "Darwin" then
elseif sysname == 'Darwin' then
cmd = 'open "' .. url .. '"'
else
print(
"sorry, I do not know how to make Windows open a url with the default browser. This feature currently only works on linux and mac."
)
print 'sorry, I do not know how to make Windows open a url with the default browser. This feature currently only works on linux and mac.'
return
end
vim.fn.jobstart(cmd)
end

M.activate = function()
local tsquery = nil
if cfg.config.lspFeatures.chunks == "curly" then
if cfg.config.lspFeatures.chunks == 'curly' then
tsquery = [[
(fenced_code_block
(info_string
Expand All @@ -107,21 +105,16 @@ M.activate = function()
]]
end
require'otter'.activate(
cfg.config.lspFeatures.languages,
cfg.config.lspFeatures.completion.enabled,
cfg.config.lspFeatures.diagnostics.enabled,
tsquery
)
require('otter').activate(cfg.config.lspFeatures.languages, cfg.config.lspFeatures.completion.enabled, cfg.config.lspFeatures.diagnostics.enabled, tsquery)
end

-- setup
M.setup = function(opt)
cfg.config = vim.tbl_deep_extend("force", cfg.defaultConfig, opt or {})
cfg.config = vim.tbl_deep_extend('force', cfg.defaultConfig, opt or {})

if cfg.config.codeRunner.enabled then
-- setup top level run functions
local runner = require("quarto.runner")
local runner = require 'quarto.runner'
M.quartoSend = runner.run_cell
M.quartoSendAbove = runner.run_above
M.quartoSendBelow = runner.run_below
Expand All @@ -130,14 +123,25 @@ M.setup = function(opt)
M.quartoSendLine = runner.run_line

-- setup run user commands
api.nvim_create_user_command("QuartoSend", function(_) runner.run_cell() end, {})
api.nvim_create_user_command("QuartoSendAbove", function(_) runner.run_above() end, {})
api.nvim_create_user_command("QuartoSendBelow", function(_) runner.run_below() end, {})
api.nvim_create_user_command("QuartoSendAll", function(_) runner.run_all() end, {})
api.nvim_create_user_command("QuartoSendRange", function(_) runner.run_range() end, { range = 2 })
api.nvim_create_user_command("QuartoSendLine", function(_) runner.run_line() end, {})
api.nvim_create_user_command('QuartoSend', function(_)
runner.run_cell()
end, {})
api.nvim_create_user_command('QuartoSendAbove', function(_)
runner.run_above()
end, {})
api.nvim_create_user_command('QuartoSendBelow', function(_)
runner.run_below()
end, {})
api.nvim_create_user_command('QuartoSendAll', function(_)
runner.run_all()
end, {})
api.nvim_create_user_command('QuartoSendRange', function(_)
runner.run_range()
end, { range = 2 })
api.nvim_create_user_command('QuartoSendLine', function(_)
runner.run_line()
end, {})
end
end


return M
17 changes: 8 additions & 9 deletions lua/quarto/runner/init.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
--- Code runner, configurable to use different engines.
local Runner = {}

local otterkeeper = require("otter.keeper")
local config = require("quarto.config").config
local otterkeeper = require 'otter.keeper'
local config = require('quarto.config').config

local no_code_found =
"No code chunks found for the current language, which is detected based on the current code block. Is your cursor in a code block?"
local no_code_found = 'No code chunks found for the current language, which is detected based on the current code block. Is your cursor in a code block?'

local function overlaps_range(range, other)
return range.from[1] <= other.to[1] and other.from[1] <= range.to[1]
Expand Down Expand Up @@ -68,7 +67,7 @@ local function send(cell, opts)
end

if runner ~= nil then
require("quarto.runner." .. runner).run(cell, opts.ignore_cols)
require('quarto.runner.' .. runner).run(cell, opts.ignore_cols)
else
vim.notify("[Quarto] couldn't find appropriate code runner for language: " .. cell.lang, vim.log.levels.ERROR)
end
Expand Down Expand Up @@ -161,14 +160,14 @@ Runner.run_range = function()
return
end

local vstart = vim.fn.getpos("'<")
local vend = vim.fn.getpos("'>")
local vstart = vim.fn.getpos "'<"
local vend = vim.fn.getpos "'>"

if vstart and vend then
local range = { from = { vstart[2] - 1, vstart[1] }, to = { vend[2], vend[1] } }
send({ lang = otterkeeper.get_current_language_context(), range = range, text = lines })
send { lang = otterkeeper.get_current_language_context(), range = range, text = lines }
else
print("No visual selection")
print 'No visual selection'
end
end

Expand Down
2 changes: 1 addition & 1 deletion lua/quarto/runner/slime.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ local concat = require('quarto.tools').concat
---@param _ boolean
local function run(cell, _)
local text_lines = concat(cell.text)
vim.fn["slime#send"](text_lines)
vim.fn['slime#send'](text_lines)
end

---@class CodeRunner
Expand Down
14 changes: 7 additions & 7 deletions lua/quarto/tools.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@ M.contains = function(list, x)
end

M.replace_header_div = function(response)
response.contents = response.contents:gsub('<div class="container">', "")
response.contents = response.contents:gsub('<div class="container">', '')
return response
end

M.concat = function(ls)
if type(ls) ~= "table" then
return ls .. "\n\n"
if type(ls) ~= 'table' then
return ls .. '\n\n'
end
local s = ""
local s = ''
for _, l in ipairs(ls) do
if l ~= "" then
s = s .. "\n" .. l
if l ~= '' then
s = s .. '\n' .. l
end
end
return s .. "\n"
return s .. '\n'
end

return M
14 changes: 7 additions & 7 deletions plugin/quarto.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
if vim.fn.has("nvim-0.9.0") ~= 1 then
if vim.fn.has 'nvim-0.9.0' ~= 1 then
local msg = [[
quarto-dev/quarto-nvim and jmbuhr/otter.nvim require Neovim version >= 0.9.0 (https://github.com/neovim/neovim/releases/tag/stable).
If you are unable to update Neovim, you can specify a specific version of the plugins involved instead of the latest stable version.
Expand All @@ -12,11 +12,11 @@ if vim.fn.has("nvim-0.9.0") ~= 1 then
return
end

local quarto = require("quarto")
local quarto = require 'quarto'
local api = vim.api

api.nvim_create_user_command("QuartoPreview", quarto.quartoPreview, { 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 })
api.nvim_create_user_command("QuartoHover", ':lua require"otter".ask_hover()<cr>', {})
api.nvim_create_user_command('QuartoPreview', quarto.quartoPreview, { 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 })
api.nvim_create_user_command('QuartoHover', ':lua require"otter".ask_hover()<cr>', {})
5 changes: 4 additions & 1 deletion stylua.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
column_width = 160
line_endings = "Unix"
indent_type = "Spaces"
indent_width = 2
column_width = 120
quote_style = "AutoPreferSingle"
call_parentheses = "None"
[sort_requires]
enabled = true

0 comments on commit be95823

Please sign in to comment.