Skip to content
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
5 changes: 4 additions & 1 deletion handlers.v
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,12 @@ fn (mut app App) operation_at_pos(method Method, request Request) Response {
}

line_info := match method {
.completion, .hover {
.completion {
'${line_nr}:${col}'
}
.hover {
'${line_nr}:hv^${col}'
}
.signature_help {
'${line_nr}:fn^${col}'
}
Expand Down
37 changes: 11 additions & 26 deletions interop.v
Original file line number Diff line number Diff line change
Expand Up @@ -353,18 +353,19 @@ fn (mut app App) run_v_line_info(method Method, path string, line_info string) R
result = json.decode(SignatureHelp, x.output) or { SignatureHelp{} }
}
.hover {
result_tmp := json.decode(JsonVarAC, x.output) or { JsonVarAC{} }
// Decode the Hover JSON emitted by the compiler's hv^ mode.
hover_result := json.decode(Hover, x.output) or { Hover{} }
// Extract vdoc comment via cross-file search as a fallback when the
// compiler does not provide documentation.
mut doc := ''
file_content := app.open_files[path] or { app.text }
file_lines := file_content.split_into_lines()
// line_info format for hover is "line:col" (1-based line, 0-based col)
// line_info format for hover is "${line_nr}:hv^${col}"
info_parts := line_info.split(':')
mut cursor_symbol := ''
if info_parts.len >= 2 {
cursor_line := info_parts[0].int() - 1
cursor_col := info_parts[1].int()
cursor_col := info_parts[1].all_after('hv^').int()
if cursor_line >= 0 && cursor_line < file_lines.len {
cursor_symbol = get_word_at_col(file_lines[cursor_line], cursor_col)
if cursor_symbol != '' {
Expand All @@ -373,36 +374,20 @@ fn (mut app App) run_v_line_info(method Method, path string, line_info string) R
}
}
}
if result_tmp.details.len > 0 {
detail := result_tmp.details[0]
// When the compiler returned a named symbol (e.g. hovering over the `fn`
// keyword resolves to the function's label), re-run the doc search using
// the compiler's label so we pick up the correct vdoc comment.
if doc == '' && detail.label != '' && detail.label != cursor_symbol {
doc = app.find_doc_comment_for_symbol(detail.label, file_lines, path)
}
// Prefer compiler-provided documentation over our extracted vdoc
if detail.documentation != '' {
doc = detail.documentation
}
// Use the full declaration from the compiler when available;
// fall back to detail (return type) if declaration is absent.
sig := if detail.declaration != '' { detail.declaration } else { detail.detail }
mut content := if sig != '' { '```v\n${sig}\n```' } else { '' }
if doc != '' {
if content != '' {
content += '\n\n'
}
content += doc
if hover_result.contents.value != '' {
mut value := hover_result.contents.value
// Augment with doc comment if the compiler didn't include one
if doc != '' && !value.contains(doc) {
value += '\n\n' + doc
}
result = Hover{
contents: MarkupContent{
kind: 'markdown'
value: content
value: value
}
}
} else if doc != '' {
// Compiler returned no type info but we found a vdoc comment
// Compiler returned no info but we found a vdoc comment
result = Hover{
contents: MarkupContent{
kind: 'markdown'
Expand Down