Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RDY] Notification provider #13843

Merged
merged 3 commits into from
Feb 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions runtime/lua/vim/lsp/log.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,7 @@ local log = {}
-- Can be used to lookup the number from the name or the name from the number.
-- Levels by name: 'trace', 'debug', 'info', 'warn', 'error'
-- Level numbers begin with 'trace' at 0
log.levels = {
TRACE = 0;
DEBUG = 1;
INFO = 2;
WARN = 3;
ERROR = 4;
}
log.levels = vim.log.levels

-- Default log level is warn.
local current_log_level = log.levels.WARN
Expand Down
20 changes: 20 additions & 0 deletions src/nvim/api/vim.c
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,26 @@ Object nvim_exec_lua(String code, Array args, Error *err)
return nlua_exec(code, args, err);
}

/// Notify the user with a message
///
/// Relays the call to vim.notify . By default forwards your message in the
/// echo area but can be overriden to trigger desktop notifications.
///
/// @param msg Message to display to the user
/// @param log_level The log level
/// @param opts Reserved for future use.
/// @param[out] err Error details, if any
Object nvim_notify(String msg, Integer log_level, Dictionary opts, Error *err)
FUNC_API_SINCE(7)
{
FIXED_TEMP_ARRAY(args, 3);
args.items[0] = STRING_OBJ(msg);
args.items[1] = INTEGER_OBJ(log_level);
args.items[2] = DICTIONARY_OBJ(opts);

return nlua_exec(STATIC_CSTR_AS_STRING("return vim.notify(...)"), args, err);
}

/// Calls a VimL function.
///
/// @param fn Function name
Expand Down
9 changes: 5 additions & 4 deletions src/nvim/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@
#endif


#define DEBUG_LOG_LEVEL 0
#define INFO_LOG_LEVEL 1
#define WARN_LOG_LEVEL 2
#define ERROR_LOG_LEVEL 3
#define TRACE_LOG_LEVEL 0
#define DEBUG_LOG_LEVEL 1
#define INFO_LOG_LEVEL 2
#define WARN_LOG_LEVEL 3
#define ERROR_LOG_LEVEL 4
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This changes behavior, where CMAKE_EXTRA_FLAGS += -DMIN_LOG_LEVEL=1 now enables debug messages, whereas it was INFO only before..! (e.g. via

#if MIN_LOG_LEVEL <= INFO_LOG_LEVEL
)

Since it is released in 0.5 already it should probably stay like this, but it seems like documentation (and CI config) needs to be adjusted also (e.g.

BUILD_FLAGS="CMAKE_FLAGS=-DCI_BUILD=ON -DCMAKE_BUILD_TYPE=Debug -DCMAKE_INSTALL_PREFIX:PATH=$HOME/nvim-install -DBUSTED_OUTPUT_TYPE=nvim -DDEPS_PREFIX=$HOME/nvim-deps/usr -DMIN_LOG_LEVEL=3"
).


#define DLOG(...)
#define DLOGN(...)
Expand Down
27 changes: 27 additions & 0 deletions src/nvim/lua/vim.lua
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ assert(vim)
vim.inspect = package.loaded['vim.inspect']
assert(vim.inspect)

vim.log = {
levels = {
TRACE = 0;
DEBUG = 1;
INFO = 2;
WARN = 3;
ERROR = 4;
}
}

-- Internal-only until comments in #8107 are addressed.
-- Returns:
-- {errcode}, {output}
Expand Down Expand Up @@ -478,6 +488,23 @@ function vim.defer_fn(fn, timeout)
return timer
end


--- Notification provider
--- without a runtime, writes to :Messages
-- see :help nvim_notify
--@param msg Content of the notification to show to the user
--@param log_level Optional log level
--@param opts Dictionary with optional options (timeout, etc)
function vim.notify(msg, log_level, _opts)

if log_level == vim.log.levels.ERROR then
vim.api.nvim_err_writeln(msg)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

which reminds me, nvim_echo should take optional 'error' arg :)

else
vim.api.nvim_echo(msg)
end
end


local on_keystroke_callbacks = {}

--- Register a lua {fn} with an {id} to be run after every keystroke.
Expand Down
9 changes: 9 additions & 0 deletions test/functional/api/vim_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,15 @@ describe('API', function()
end)
end)

describe('nvim_notify', function()
it('can be overriden', function()
command("lua vim.notify = function(...) return 42 end")
eq(42, meths.exec_lua("return vim.notify('Hello world')", {}))
nvim("notify", "hello world", 4, {})
end)
end)


describe('nvim_input', function()
it("VimL error: does NOT fail, updates v:errmsg", function()
local status, _ = pcall(nvim, "input", ":call bogus_fn()<CR>")
Expand Down