diff --git a/src/client.ts b/src/client.ts index 5df479d4..eede1dba 100644 --- a/src/client.ts +++ b/src/client.ts @@ -1,9 +1,10 @@ -import { CodeActionKind, Command, Executable, LanguageClient, LanguageClientOptions, ServerOptions, StaticFeature, Uri, workspace } from 'coc.nvim'; +import { CodeActionKind, Command, Executable, LanguageClient, LanguageClientOptions, Position, Range, ServerOptions, StaticFeature, Uri, workspace } from 'coc.nvim'; import { existsSync } from 'fs'; import { join } from 'path'; import { CodeAction, CodeActionParams, CodeActionRequest } from 'vscode-languageserver-protocol'; import { Env } from './config'; import { isRustDocument } from './ctx'; +import * as ra from './lsp_ext'; class ExperimentalFeatures implements StaticFeature { fillClientCapabilities(capabilities: any): void { @@ -62,6 +63,21 @@ export function createClient(bin: string, extra: Env): LanguageClient { documentSelector: [{ language: 'rust' }], initializationOptions, middleware: { + async provideHover(document, position, token) { + let positionOrRange: Range | Position | null = null; + const mode = (await workspace.nvim.call('mode')) as string; + if (mode === 'v' || mode === 'V') { + const doc = workspace.getDocument(document.uri); + await workspace.nvim.call('eval', 'feedkeys("\\", "in")'); + positionOrRange = await workspace.getSelectedRange(mode, doc); + } + if (!positionOrRange) positionOrRange = position; + const param: ra.HoverParams = { + position: positionOrRange || position, + textDocument: { uri: document.uri }, + }; + return await client.sendRequest(ra.hover, param, token); + }, async resolveCompletionItem(item, token, next) { if (item.data && !item.data.position) { // TODO: remove this if coc undefined item.data diff --git a/src/lsp_ext.ts b/src/lsp_ext.ts index 2905f32e..9bff239c 100644 --- a/src/lsp_ext.ts +++ b/src/lsp_ext.ts @@ -19,6 +19,14 @@ export const serverStatus = new lc.NotificationType('experim export const reloadWorkspace = new lc.RequestType0('rust-analyzer/reloadWorkspace'); +export const hover = new lc.RequestType('textDocument/hover'); + +export interface HoverParams { + workDoneToken?: lc.ProgressToken; + textDocument: lc.TextDocumentIdentifier; + position: lc.Range | lc.Position; +} + export interface SyntaxTreeParams { textDocument: lc.TextDocumentIdentifier; range: lc.Range | null; @@ -55,7 +63,7 @@ export interface MatchingBraceParams { } export const matchingBrace = new lc.RequestType('experimental/matchingBrace'); -export const parentModule = new lc.RequestType('experimental/parentModule'); +export const parentModule = new lc.RequestType('experimental/parentModule'); export interface JoinLinesParams { textDocument: lc.TextDocumentIdentifier;