feat: Extend the server with the hover_range capability

This commit is contained in:
Alexander Gonzalez 2021-07-22 22:08:28 -04:00
parent 6a2a0b7abb
commit 20c64cc0e6
4 changed files with 44 additions and 6 deletions

View File

@ -241,6 +241,13 @@ fn try_hover_for_lint(attr: &ast::Attr, token: &SyntaxToken) -> Option<RangeInfo
))
}
pub(crate) fn hover_range(
db: &RootDatabase,
range: FileRange,
config: &HoverConfig,
) -> Option<RangeInfo<HoverResult>> {
}
fn show_implementations_action(db: &RootDatabase, def: Definition) -> Option<HoverAction> {
fn to_action(nav_target: NavigationTarget) -> HoverAction {
HoverAction::Implementation(FilePosition {

View File

@ -423,6 +423,15 @@ pub fn hover(
self.with_db(|db| hover::hover(db, position, config))
}
/// Returns a short text displaying the type for the expression.
pub fn hover_range(
&self,
config: &HoverConfig,
range: FileRange,
) -> Cancellable<Option<RangeInfo<HoverResult>>> {
self.with_db(|db| hover::hover_range(db, range, config))
}
/// Return URL(s) for the documentation of the symbol under the cursor.
pub fn external_docs(
&self,

View File

@ -867,14 +867,29 @@ pub(crate) fn handle_signature_help(
pub(crate) fn handle_hover(
snap: GlobalStateSnapshot,
params: lsp_types::HoverParams,
params: lsp_ext::HoverParams,
) -> Result<Option<lsp_ext::Hover>> {
let _p = profile::span("handle_hover");
let position = from_proto::file_position(&snap, params.text_document_position_params)?;
let info = match snap.analysis.hover(&snap.config.hover(), position)? {
let file_id = from_proto::file_id(&snap, &params.text_document.uri)?;
let range = from_proto::file_range(&snap, params.text_document, params.range)?;
let info = if range.end - range.start == 1 {
// It's a hover over a position
match snap
.analysis
.hover(&snap.config.hover(), FilePosition { file_id, offset: range.start })?
{
None => return Ok(None),
Some(info) => info,
}
} else {
// It's a hover over a range
match snap.analysis.hover_range(&snap.config.hover(), range)? {
None => return Ok(None),
Some(info) => info,
}
};
let line_index = snap.file_line_index(position.file_id)?;
let range = to_proto::range(&line_index, info.range);
let hover = lsp_ext::Hover {

View File

@ -376,11 +376,18 @@ pub struct SnippetTextEdit {
pub enum HoverRequest {}
impl Request for HoverRequest {
type Params = lsp_types::HoverParams;
type Params = HoverParams;
type Result = Option<Hover>;
const METHOD: &'static str = "textDocument/hover";
}
#[derive(Deserialize, Serialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct HoverParams {
pub text_document: TextDocumentIdentifier,
pub range: Range,
}
#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
pub struct Hover {
#[serde(flatten)]