Skip to content

Commit

Permalink
fix: compatibility with neovim-stable + type checker for neovim-stable (
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcjkb authored Oct 28, 2023
1 parent 5ae04f9 commit a0e8b51
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 36 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [3.1.1] - 2023-10-28
### Fixed
- Remove accidental use of Neovim nightly API (`dap`, `crateGraph`, `explainError`) [[#26](https://github.com/mrcjkb/rustaceanvim/issues/26)].
- Add static type checking for Neovim stable API.

## [3.1.0] - 2023-10-28
### Added
- `:RustLsp explainError` command, uses `rustc --explain` on error diagnostics with
Expand Down
83 changes: 55 additions & 28 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -65,38 +65,62 @@
];
};

type-check = pre-commit-hooks.lib.${system}.run {
src = self;
hooks = {
lua-ls.enable = true;
};
settings = {
lua-ls = {
config = {
runtime.version = "LuaJIT";
Lua = {
workspace = {
library = [
"${pkgs.neovim-nightly}/share/nvim/runtime/lua"
"${pkgs.neodev-plugin}/types/nightly"
# "${pkgs.luajitPackages.busted}"
];
checkThirdParty = false;
ignoreDir = [
".git"
".github"
".direnv"
"result"
"nix"
"doc"
"spec" # FIXME: Add busted library
];
mkTypeCheck = {
rtp ? [],
disabled-diagnostics ? [],
}:
pre-commit-hooks.lib.${system}.run {
src = self;
hooks = {
lua-ls.enable = true;
};
settings = {
lua-ls = {
config = {
runtime.version = "LuaJIT";
Lua = {
workspace = {
library = rtp;
checkThirdParty = false;
ignoreDir = [
".git"
".github"
".direnv"
"result"
"nix"
"doc"
"spec" # FIXME: Add busted library
];
};
diagnostics = {
libraryFiles = "Disable";
disable = disabled-diagnostics;
};
};
diagnostics. libraryFiles = "Disable";
};
};
};
};

type-check-stable = mkTypeCheck {
rtp = [
"${pkgs.neovim}/share/nvim/runtime/lua"
"${pkgs.neodev-plugin}/types/stable"
# "${pkgs.luajitPackages.busted}"
];
disabled-diagnostics = [
"undefined-doc-name"
"redundant-parameter"
"invisible"
];
};

type-check-nightly = mkTypeCheck {
rtp = [
"${pkgs.neovim-nightly}/share/nvim/runtime/lua"
"${pkgs.neodev-plugin}/types/nightly"
# "${pkgs.luajitPackages.busted}"
];
};

pre-commit-check = pre-commit-hooks.lib.${system}.run {
Expand Down Expand Up @@ -143,7 +167,10 @@

checks = {
formatting = pre-commit-check;
inherit type-check;
inherit
type-check-stable
type-check-nightly
;
inherit
(pkgs)
nvim-stable-tests
Expand Down
3 changes: 2 additions & 1 deletion lua/rustaceanvim/commands/crate_graph.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local config = require('rustaceanvim.config.internal')
local compat = require('rustaceanvim.compat')

local M = {}

Expand Down Expand Up @@ -32,7 +33,7 @@ local function handler_factory(backend, output, pipe)
vim.notify('no crate graph backend specified.', vim.log.levels.ERROR)
return
end
if not vim.list_contains(config.tools.crate_graph.enabled_graphviz_backends, backend) then
if not compat.list_contains(config.tools.crate_graph.enabled_graphviz_backends, backend) then
vim.notify('crate graph backend not recognized as valid: ' .. vim.inspect(backend), vim.log.levels.ERROR)
return
end
Expand Down
4 changes: 3 additions & 1 deletion lua/rustaceanvim/commands/explain_error.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
local M = {}

local compat = require('rustaceanvim.compat')

local rustc = 'rustc'

function M.explain_error()
Expand Down Expand Up @@ -64,7 +66,7 @@ function M.explain_error()
vim.api.nvim_win_set_cursor(win_id, { diagnostic.lnum + 1, diagnostic.col })
-- Open folds under the cursor
vim.cmd('normal! zv')
vim.system({ rustc, '--explain', diagnostic.code }, nil, vim.schedule_wrap(handler))
compat.system({ rustc, '--explain', diagnostic.code }, nil, vim.schedule_wrap(handler))
end

return M.explain_error
35 changes: 34 additions & 1 deletion lua/rustaceanvim/compat.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
---@diagnostic disable: deprecated
---@diagnostic disable: deprecated, duplicate-doc-field
---@mod rustaceanvim.compat Functions for backward compatibility with older Neovim versions

local M = {}
Expand All @@ -11,4 +11,37 @@ M.get_clients = vim.lsp.get_clients or vim.lsp.get_active_clients

M.uv = vim.uv or vim.loop

--- @class vim.SystemCompleted
--- @field code integer
--- @field signal integer
--- @field stdout? string
--- @field stderr? string

M.system = vim.system
-- wrapper around vim.fn.system to give it a similar API to vim.system
or function(cmd, _, on_exit)
local output = vim.fn.system(cmd)
local ok = vim.v.shell_error
---@type vim.SystemCompleted
local systemObj = {
signal = 0,
stdout = ok and (output or '') or nil,
stderr = not ok and (output or '') or nil,
code = vim.v.shell_error,
}
on_exit(systemObj)
return systemObj
end

M.list_contains = vim.list_contains
or function(t, value)
vim.validate { t = { t, 't' } }
for _, v in ipairs(t) do
if v == value then
return true
end
end
return false
end

return M
11 changes: 6 additions & 5 deletions lua/rustaceanvim/dap.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local config = require('rustaceanvim.config.internal')
local compat = require('rustaceanvim.compat')

local function scheduled_error(err)
vim.schedule(function()
Expand Down Expand Up @@ -43,12 +44,12 @@ local function get_cargo_args_from_runnables_args(runnable_args)
local cargo_args = runnable_args.cargoArgs

local message_json = '--message-format=json'
if not vim.list_contains(cargo_args, message_json) then
if not compat.list_contains(cargo_args, message_json) then
table.insert(cargo_args, message_json)
end

for _, value in ipairs(runnable_args.cargoExtraArgs) do
if not vim.list_contains(cargo_args, value) then
if not compat.list_contains(cargo_args, value) then
table.insert(cargo_args, value)
end
end
Expand Down Expand Up @@ -91,10 +92,10 @@ function M.start(args)
goto loop_end
end

local is_binary = vim.list_contains(artifact.target.crate_types, 'bin')
local is_build_script = vim.list_contains(artifact.target.kind, 'custom-build')
local is_binary = compat.list_contains(artifact.target.crate_types, 'bin')
local is_build_script = compat.list_contains(artifact.target.kind, 'custom-build')
local is_test = ((artifact.profile.test == true) and (artifact.executable ~= nil))
or vim.list_contains(artifact.target.kind, 'test')
or compat.list_contains(artifact.target.kind, 'test')
-- only add executable to the list if we want a binary debug and it is a binary
-- or if we want a test debug and it is a test
if
Expand Down

0 comments on commit a0e8b51

Please sign in to comment.