Skip to content

Commit

Permalink
chore: remove usage of deprecated functions
Browse files Browse the repository at this point in the history
  • Loading branch information
mochaaP committed Jul 17, 2024
1 parent 6648b03 commit 2fe7557
Show file tree
Hide file tree
Showing 24 changed files with 111 additions and 81 deletions.
2 changes: 1 addition & 1 deletion doc/BUILTINS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4055,4 +4055,4 @@ local sources = { null_ls.builtins.hover.printenv }

#### Notes

- This source is similar in function to `printenv` where it shows value of environment variable, however this source uses `vim.loop.os_getenv` instead of `printenv` thus making it cross-platform.
- This source is similar in function to `printenv` where it shows value of environment variable, however this source uses `vim.uv.os_getenv` instead of `printenv` thus making it cross-platform.
2 changes: 1 addition & 1 deletion lua/null-ls/builtins/_test/toggle_line_comment.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ return h.make_builtin({
generator = {
fn = function(params)
local bufnr = vim.api.nvim_get_current_buf()
local commentstring = vim.api.nvim_buf_get_option(bufnr, "commentstring")
local commentstring = vim.api.nvim_get_option_value("commentstring", { buf = bufnr })
local raw_commentstring = commentstring:gsub(vim.pesc("%s"), "")
local line = params.content[params.row]

Expand Down
4 changes: 2 additions & 2 deletions lua/null-ls/builtins/hover/printenv.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ return h.make_builtin({
-- Checks if cword is an environment variable
-- If cword is environment variable and value not nil, show in hover window value of environment variable
-- Else show in hover window "Error! `cword` is not an environment variable!"
local ok, value = pcall(vim.loop.os_getenv, cword)
local ok, value = pcall(vim.uv.os_getenv, cword)
if ok and (value ~= nil) then
done({ cword .. ": " .. value })
else
Expand All @@ -27,7 +27,7 @@ return h.make_builtin({
meta = {
description = "Shows the value for the current environment variable under the cursor.",
notes = {
"This source is similar in function to `printenv` where it shows value of environment variable, however this source uses `vim.loop.os_getenv` instead of `printenv` thus making it cross-platform.",
"This source is similar in function to `printenv` where it shows value of environment variable, however this source uses `vim.uv.os_getenv` instead of `printenv` thus making it cross-platform.",
},
},
})
17 changes: 10 additions & 7 deletions lua/null-ls/client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,17 @@ local lsp = vim.lsp

local client, id

---@param bufnr number
local should_attach = function(bufnr)
if api.nvim_buf_get_option(bufnr, "buftype") ~= "" or api.nvim_buf_get_name(bufnr) == "" then
if api.nvim_get_option_value("buftype", { buf = bufnr }) ~= "" or api.nvim_buf_get_name(bufnr) == "" then
return false
end

if c.get().should_attach and not c.get().should_attach(bufnr) then
return false
end

local ft = api.nvim_buf_get_option(bufnr, "filetype")
local ft = api.nvim_get_option_value("filetype", { buf = bufnr })
for _, source in ipairs(sources.get_all()) do
if sources.is_available(source, ft) then
return true
Expand All @@ -38,10 +39,10 @@ local get_root_dir = function(bufnr, cb)
local fname = api.nvim_buf_get_name(bufnr)
if config.root_dir_async then
config.root_dir_async(fname, function(found_root_dir)
cb(found_root_dir or vim.loop.cwd() or ".")
cb(found_root_dir or vim.uv.cwd() or ".")
end)
else
cb(config.root_dir(fname) or vim.loop.cwd() or ".")
cb(config.root_dir(fname) or vim.uv.cwd() or ".")
end
end

Expand Down Expand Up @@ -86,7 +87,7 @@ end

local M = {}

---@param root_dir string The root directory of the project.
---@param root_dir? string The root directory of the project.
M.start_client = function(root_dir)
local config = {
name = "null-ls",
Expand Down Expand Up @@ -123,7 +124,8 @@ end

--- This function can be asynchronous. Use cb to run code after the buffer has been retried.
---
---@param cb function(did_attach: bool)|nil
---@param bufnr? number
---@param cb? fun(did_attach: boolean)
M.try_add = function(bufnr, cb)
bufnr = bufnr or api.nvim_get_current_buf()
if not should_attach(bufnr) then
Expand Down Expand Up @@ -208,7 +210,7 @@ M.on_source_change = vim.schedule_wrap(function()
M.retry_add(bufnr, function()
-- if in named buffer, we can register conditional sources immediately
-- we need to check only for normal buffers, excluding nofile and terminals
local buftype = api.nvim_buf_get_option(bufnr, "buftype")
local buftype = api.nvim_get_option_value("buftype", { buf = bufnr })
local bufname = api.nvim_buf_get_name(bufnr)
if s.has_conditional_sources() and bufname ~= "" and buftype == "" then
s.register_conditional_sources()
Expand Down Expand Up @@ -277,6 +279,7 @@ M.send_progress_notification = function(token, opts)
},
}, {
client_id = client_id,
method = "$/progress",
})
end)
end
Expand Down
5 changes: 5 additions & 0 deletions lua/null-ls/diagnostics.lua
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ local convert_range = function(diagnostic)
end_col = 1
end

assert(row)
assert(col)
assert(end_row)
assert(end_col)

return u.range.to_lsp({ row = row, col = col, end_row = end_row, end_col = end_col })
end

Expand Down
16 changes: 10 additions & 6 deletions lua/null-ls/formatting.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,13 @@ M.handler = function(method, original_params, handler)

-- copy content and options to temp buffer
local temp_bufnr = api.nvim_create_buf(false, true)
api.nvim_buf_set_option(temp_bufnr, "eol", api.nvim_buf_get_option(bufnr, "eol"))
api.nvim_buf_set_option(temp_bufnr, "fixeol", api.nvim_buf_get_option(bufnr, "fixeol"))
api.nvim_buf_set_option(temp_bufnr, "fileformat", api.nvim_buf_get_option(bufnr, "fileformat"))
api.nvim_set_option_value("eol", api.nvim_get_option_value("eol", { buf = bufnr }), { buf = temp_bufnr })
api.nvim_set_option_value("fixeol", api.nvim_get_option_value("fixeol", { buf = bufnr }), { buf = temp_bufnr })
api.nvim_set_option_value(
"fileformat",
api.nvim_get_option_value("fileformat", { buf = bufnr }),
{ buf = temp_bufnr }
)
api.nvim_buf_set_lines(temp_bufnr, 0, -1, false, api.nvim_buf_get_lines(bufnr, 0, -1, false))

local called_handler = false
Expand Down Expand Up @@ -63,14 +67,14 @@ M.handler = function(method, original_params, handler)
local make_params = function()
local params = u.make_params(original_params, methods.map[method])
-- override actual content w/ temp buffer content
params.content = u.buf.content(temp_bufnr)
params.content = u.buf.content(temp_bufnr) --[[ @as string[] ]]
return params
end

local after_each = function(edits)
local ok, err =
pcall(lsp.util.apply_text_edits, edits, temp_bufnr, require("null-ls.client").get_offset_encoding())
api.nvim_buf_set_option(temp_bufnr, "buflisted", false) -- apply_text_edits sets buflisted to true
api.nvim_set_option_value("buflisted", false, { buf = temp_bufnr }) -- apply_text_edits sets buflisted to true
if not ok then
handle_err(err)
end
Expand Down Expand Up @@ -99,7 +103,7 @@ M.handler = function(method, original_params, handler)
end

require("null-ls.generators").run_registered_sequentially({
filetype = api.nvim_buf_get_option(bufnr, "filetype"),
filetype = api.nvim_get_option_value("filetype", { buf = bufnr }),
method = methods.map[method],
make_params = make_params,
postprocess = postprocess,
Expand Down
1 change: 1 addition & 0 deletions lua/null-ls/helpers/range_formatting_args_factory.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ local range_formatting_args_factory = function(base_args, start_arg, end_arg, op
end

local range = params.range
assert(range)
local row, col, end_row, end_col = range.row, range.col, range.end_row, range.end_col
if opts.row_offset then
row = row + opts.row_offset
Expand Down
10 changes: 5 additions & 5 deletions lua/null-ls/info.lua
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ M.show_window = function(opts)
opts = opts or {}
local client = require("null-ls.client").get_client()
local bufnr = api.nvim_get_current_buf()
local filetype = api.nvim_buf_get_option(bufnr, "filetype")
local filetype = api.nvim_get_option_value("filetype", { buf = bufnr })
local highlights = {}
local is_attached = true
if not client or not lsp.buf_is_attached(bufnr, client.id) then
Expand Down Expand Up @@ -160,12 +160,12 @@ M.show_window = function(opts)
end

local win_bufnr, win_id = make_window(0.8, 0.7, opts.border or c.get().border)
api.nvim_win_set_option(win_id, "winhl", "FloatBorder:NullLsInfoBorder")
api.nvim_set_option_value("winhl", "FloatBorder:NullLsInfoBorder", { win = win_id })

api.nvim_buf_set_lines(win_bufnr, 0, -1, true, lines)
api.nvim_buf_set_option(win_bufnr, "buftype", "nofile")
api.nvim_buf_set_option(win_bufnr, "filetype", "null-ls-info")
api.nvim_buf_set_option(win_bufnr, "modifiable", false)
api.nvim_set_option_value("buftype", "nofile", { buf = win_bufnr })
api.nvim_set_option_value("filetype", "null-ls-info", { buf = win_bufnr })
api.nvim_set_option_value("modifiable", false, { buf = win_bufnr })

for _, hi in ipairs(highlights) do
vim.fn.matchadd(hi[1], hi[2])
Expand Down
14 changes: 9 additions & 5 deletions lua/null-ls/logger.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ function log:add_entry(msg, level)
end

if self.__handle then
self.__handle[level](msg)
local fmt_msg = self.__handle[level]
---@cast fmt_msg fun(msg: string)
fmt_msg(msg)
return
end

Expand All @@ -44,14 +46,16 @@ function log:add_entry(msg, level)
end

local handle = plenary_log.new(default_opts)
handle[level](msg)
local fmt_msg = handle[level]
---@cast fmt_msg fun(msg: string)
fmt_msg(msg)
self.__handle = handle
end

---Retrieves the path of the logfile
---@return string path of the logfile
--- Retrieves the path of the logfile
---@return string path path of the logfile
function log:get_path()
return u.path.join(vim.fn.stdpath("cache"), "null-ls.log")
return u.path.join(vim.fn.stdpath("cache") --[[@as string]], "null-ls.log")
end

---Add a log entry at TRACE level
Expand Down
13 changes: 8 additions & 5 deletions lua/null-ls/loop.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
local log = require("null-ls.logger")
local u = require("null-ls.utils")

local uv = vim.loop
local uv = vim.uv
local wrap = vim.schedule_wrap --[[@as fun(cb: any): function]]

local close_handle = function(handle)
Expand Down Expand Up @@ -149,7 +149,7 @@ M.spawn = function(cmd, args, opts)
args = args,
env = parsed_env,
stdio = stdio,
cwd = opts.cwd or vim.loop.cwd(),
cwd = opts.cwd or vim.uv.cwd(),
}

handle, pid = uv.spawn(
Expand Down Expand Up @@ -224,15 +224,16 @@ end
---@param content string
---@param bufname string
---@param dirname string|nil
---@return string temp_path, fun() cleanup
---@return string? temp_path, fun() cleanup
M.temp_file = function(content, bufname, dirname)
dirname = dirname or vim.fn.fnamemodify(bufname, ":h")
local base_name = vim.fn.fnamemodify(bufname, ":t")

local filename = string.format(".null-ls_%d_%s", math.random(100000, 999999), base_name)
---@type string?
local temp_path = u.path.join(dirname, filename)

local fd, err = uv.fs_open(temp_path, "w", 384)
local fd, err = uv.fs_open(temp_path --[[@as string]], "w", 384)
if not fd then
error("failed to create temp file: " .. err)
end
Expand Down Expand Up @@ -271,7 +272,9 @@ M.read_file = function(path)
local content
local ok, err = pcall(function()
local fd = uv.fs_open(path, "r", 438)
assert(fd)
local stat = uv.fs_fstat(fd)
assert(stat)
content = uv.fs_read(fd, stat.size, 0)
uv.fs_close(fd)
end)
Expand All @@ -288,7 +291,7 @@ end
---@param flag string|number
M.write_file = function(path, txt, flag)
uv.fs_open(path, flag, 438, function(open_err, fd)
assert(not open_err, open_err)
assert(not open_err and fd, open_err)
uv.fs_write(fd, txt, -1, function(write_err)
assert(not write_err, write_err)
uv.fs_close(fd, function(close_err)
Expand Down
Loading

0 comments on commit 2fe7557

Please sign in to comment.