-
Notifications
You must be signed in to change notification settings - Fork 11
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
feat: inlay hints #38
Merged
Merged
Changes from 13 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
dad87d9
feat: inlay hints
tris203 607cd62
tests: fix tests
tris203 fd1a352
chore: format
tris203 ce77dc4
fix: 0.9 compatibility
tris203 db5e9b2
chore: format
tris203 5424e07
tests: refactor create test utils
tris203 307ff71
wip: works fine apart from tab spaced lines
tris203 a50454f
chore: style
tris203 d96eced
feat: padding table and tab compatibility
tris203 554a470
fix: tweaks to positioning
tris203 e34b3b7
chore: unused parameter
tris203 6758524
tests: LSP tests
tris203 d946daf
tests: more tests
tris203 50185f0
chore: remove debug print
willothy ad44762
Merge branch 'main' into inlay_hints
tris203 96c658a
chore: end
tris203 c6cb9d6
Merge branch 'main' into inlay_hints
tris203 b927107
Merge branch 'main' into inlay_hints
tris203 fbcab75
Merge branch 'main' into inlay_hints
tris203 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,5 +3,6 @@ | |
"describe", | ||
"it", | ||
"before_each", | ||
"after_each", | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
local M = {} | ||
|
||
function M.inlay_hints_enabled(t) | ||
if vim.lsp and vim.lsp.inlay_hint and vim.lsp.inlay_hint.is_enabled then | ||
return vim.lsp.inlay_hint.is_enabled(t) | ||
end | ||
return false | ||
end | ||
|
||
return M |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
local server = require("tests.precognition.utils.lsp").server | ||
local compat = require("tests.precognition.utils.compat") | ||
local precognition = require("precognition") | ||
---@diagnostic disable-next-line: undefined-field | ||
local eq = assert.are.same | ||
---@diagnostic disable-next-line: undefined-field | ||
local neq = assert.are_not.same | ||
local buf | ||
|
||
local function wait(condition, msg) | ||
vim.wait(100, condition) | ||
local result = condition() | ||
neq(false, result, msg) | ||
neq(nil, result, msg) | ||
end | ||
|
||
describe("lsp based tests", function() | ||
before_each(function() | ||
require("tests.precognition.utils.lsp").Reset() | ||
buf = vim.api.nvim_create_buf(true, false) | ||
local srv = vim.lsp.start_client({ cmd = server }) | ||
if srv then | ||
vim.lsp.buf_attach_client(buf, srv) | ||
end | ||
end) | ||
|
||
it("initialize lsp", function() | ||
eq(2, #require("tests.precognition.utils.lsp").messages) | ||
eq("initialize", require("tests.precognition.utils.lsp").messages[1].method) | ||
eq("initialized", require("tests.precognition.utils.lsp").messages[2].method) | ||
end) | ||
|
||
it("can enable inlay hints", function() | ||
vim.lsp.inlay_hint.enable(true, { bufnr = buf }) | ||
|
||
eq(3, #require("tests.precognition.utils.lsp").messages) | ||
eq("textDocument/inlayHint", require("tests.precognition.utils.lsp").messages[3].method) | ||
end) | ||
|
||
it("inlay hint shifts the line", function() | ||
vim.api.nvim_buf_set_lines(buf, 0, -1, false, { "here is a string" }) | ||
vim.api.nvim_set_current_buf(buf) | ||
vim.api.nvim_win_set_cursor(0, { 1, 1 }) | ||
|
||
precognition.on_cursor_moved() | ||
|
||
local extmarks = vim.api.nvim_buf_get_extmark_by_id(buf, precognition.ns, precognition.extmark, { | ||
details = true, | ||
}) | ||
|
||
eq("b e w $", extmarks[3].virt_lines[1][1][1]) | ||
|
||
vim.lsp.inlay_hint.enable(true, { bufnr = buf }) | ||
-- NOTE:The test LSP replies with an inlay hint, that suggest "foo" as line 1, position 4 | ||
-- This means that the inlay hint is shifted by 3 chars | ||
|
||
precognition.on_cursor_moved() | ||
|
||
extmarks = vim.api.nvim_buf_get_extmark_by_id(buf, precognition.ns, precognition.extmark, { | ||
details = true, | ||
}) | ||
|
||
eq("b e w $", extmarks[3].virt_lines[1][1][1]) | ||
end) | ||
|
||
after_each(function() | ||
vim.lsp.inlay_hint.enable(false, { bufnr = buf }) | ||
vim.api.nvim_buf_delete(buf, { force = true }) | ||
vim.lsp.stop_client(compat.get_active_lsp_clients()) | ||
wait(function() | ||
return vim.tbl_count(compat.get_active_lsp_clients()) == 0 | ||
end, "clients must stop") | ||
end) | ||
end) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
local M = {} | ||
|
||
M.get_active_lsp_clients = vim.lsp.get_clients() or vim.lsp.get_active_clients() | ||
|
||
return M |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
local M = {} | ||
|
||
M.messages = {} | ||
|
||
function M.server() | ||
local closing = false | ||
local srv = {} | ||
|
||
function srv.request(method, params, handler) | ||
table.insert(M.messages, { method = method, params = params }) | ||
if method == "initialize" then | ||
handler(nil, { | ||
capabilities = { | ||
inlayHintProvider = true, | ||
}, | ||
}) | ||
elseif method == "shutdown" then | ||
handler(nil, nil) | ||
elseif method == "textDocument/inlayHint" then | ||
handler(nil, { | ||
{ | ||
position = { line = 0, character = 3 }, | ||
label = { { value = "foo" } }, | ||
}, | ||
}) | ||
else | ||
assert(false, "Unhandled method: " .. method) | ||
end | ||
end | ||
|
||
function srv.notify(method, params) | ||
table.insert(M.messages, { method = method, params = params }) | ||
if method == "exit" then | ||
closing = true | ||
end | ||
end | ||
|
||
function srv.is_closing() | ||
return closing | ||
end | ||
|
||
function srv.terminate() | ||
closing = true | ||
end | ||
|
||
return srv | ||
end | ||
|
||
function M.Reset() | ||
M.messages = {} | ||
end | ||
|
||
return M |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
local precognition = require("precognition") | ||
|
||
local M = {} | ||
|
||
function M.get_gutter_extmarks(buffer) | ||
local gutter_extmarks = {} | ||
for _, extmark in | ||
pairs(vim.api.nvim_buf_get_extmarks(buffer, -1, 0, -1, { | ||
details = true, | ||
})) | ||
do | ||
if extmark[4] and extmark[4].sign_name and extmark[4].sign_name:match(precognition.gutter_group) then | ||
table.insert(gutter_extmarks, extmark) | ||
end | ||
end | ||
return gutter_extmarks | ||
end | ||
|
||
function M.hex2dec(hex) | ||
hex = hex:gsub("#", "") | ||
local r = tonumber("0x" .. hex:sub(1, 2)) | ||
local g = tonumber("0x" .. hex:sub(3, 4)) | ||
local b = tonumber("0x" .. hex:sub(5, 6)) | ||
|
||
local dec = (r * 256 ^ 2) + (g * 256) + b | ||
|
||
return dec | ||
end | ||
|
||
return M |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Couldn't you use
has("nvim-0.10")
here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this isn't a big deal then this PR is good to merge once conflicts are handled imo :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I need to rebase and will check the field name. I know the lsp API changed a few times in 0.10. I can't remember which released
I will check docs tomorrow