-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(lsp): provide hover information
Generate hover information for any element that is hovered by the user. This currently only replies the documentation for the element (if present), and calculates the next available ID for struct fields, enum variants and enum variant fields.
- Loading branch information
Showing
4 changed files
with
241 additions
and
16 deletions.
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 |
---|---|---|
@@ -0,0 +1,184 @@ | ||
use std::{fmt::Write, ops::Range}; | ||
|
||
use anyhow::{Context, Result}; | ||
use line_index::{LineIndex, TextSize, WideLineCol}; | ||
use lsp_types::{Position, Range as LspRange}; | ||
use stef_parser::{ | ||
Comment, Const, Definition, Enum, Fields, Module, NamedField, Schema, Span, Spanned, Struct, | ||
TypeAlias, Variant, | ||
}; | ||
|
||
pub fn visit_schema( | ||
index: &LineIndex, | ||
item: &Schema<'_>, | ||
position: Position, | ||
) -> Result<Option<(String, LspRange)>> { | ||
let position = index | ||
.offset( | ||
index | ||
.to_utf8( | ||
line_index::WideEncoding::Utf16, | ||
WideLineCol { | ||
line: position.line, | ||
col: position.character, | ||
}, | ||
) | ||
.context("missing utf-16 position")?, | ||
) | ||
.context("missing offset position")? | ||
.into(); | ||
|
||
item.definitions | ||
.iter() | ||
.find_map(|def| visit_definition(def, position)) | ||
.map(|(text, span)| Ok((text, get_range(index, span)?))) | ||
.transpose() | ||
} | ||
|
||
fn visit_definition(item: &Definition<'_>, position: usize) -> Option<(String, Span)> { | ||
match item { | ||
Definition::Module(m) => visit_module(m, position), | ||
Definition::Struct(s) => visit_struct(s, position), | ||
Definition::Enum(e) => visit_enum(e, position), | ||
Definition::TypeAlias(a) => visit_alias(a, position), | ||
Definition::Const(c) => visit_const(c, position), | ||
Definition::Import(_) => None, | ||
} | ||
} | ||
|
||
fn visit_module(item: &Module<'_>, position: usize) -> Option<(String, Span)> { | ||
(Range::from(item.name.span()).contains(&position)) | ||
.then(|| (fold_comment(&item.comment), item.name.span())) | ||
.or_else(|| { | ||
item.definitions | ||
.iter() | ||
.find_map(|def| visit_definition(def, position)) | ||
}) | ||
} | ||
|
||
fn visit_struct(item: &Struct<'_>, position: usize) -> Option<(String, Span)> { | ||
(Range::from(item.name.span()).contains(&position)) | ||
.then(|| { | ||
let mut text = fold_comment(&item.comment); | ||
|
||
if let Some(next_id) = next_field_id(&item.fields) { | ||
let _ = writeln!(&mut text, "- next ID: `{next_id}`"); | ||
} | ||
|
||
(text, item.name.span()) | ||
}) | ||
.or_else(|| visit_fields(&item.fields, position)) | ||
} | ||
|
||
fn visit_enum(item: &Enum<'_>, position: usize) -> Option<(String, Span)> { | ||
(Range::from(item.name.span()).contains(&position)) | ||
.then(|| { | ||
let mut text = fold_comment(&item.comment); | ||
|
||
let _ = writeln!( | ||
&mut text, | ||
"- next ID: `{}`", | ||
next_variant_id(&item.variants) | ||
); | ||
|
||
(text, item.name.span()) | ||
}) | ||
.or_else(|| { | ||
item.variants | ||
.iter() | ||
.find_map(|variant| visit_variant(variant, position)) | ||
}) | ||
} | ||
|
||
fn visit_variant(item: &Variant<'_>, position: usize) -> Option<(String, Span)> { | ||
(Range::from(item.name.span()).contains(&position)) | ||
.then(|| { | ||
let mut text = fold_comment(&item.comment); | ||
|
||
if let Some(next_id) = next_field_id(&item.fields) { | ||
let _ = writeln!(&mut text, "- next ID: `{next_id}`"); | ||
} | ||
|
||
(text, item.name.span()) | ||
}) | ||
.or_else(|| visit_fields(&item.fields, position)) | ||
} | ||
|
||
fn visit_fields(item: &Fields<'_>, position: usize) -> Option<(String, Span)> { | ||
if let Fields::Named(named) = item { | ||
named | ||
.iter() | ||
.find_map(|field| visit_named_field(field, position)) | ||
} else { | ||
None | ||
} | ||
} | ||
|
||
fn visit_named_field(item: &NamedField<'_>, position: usize) -> Option<(String, Span)> { | ||
(Range::from(item.name.span()).contains(&position)) | ||
.then(|| (fold_comment(&item.comment), item.name.span())) | ||
} | ||
|
||
fn visit_alias(item: &TypeAlias<'_>, position: usize) -> Option<(String, Span)> { | ||
(Range::from(item.name.span()).contains(&position)) | ||
.then(|| (fold_comment(&item.comment), item.name.span())) | ||
} | ||
|
||
fn visit_const(item: &Const<'_>, position: usize) -> Option<(String, Span)> { | ||
(Range::from(item.name.span()).contains(&position)) | ||
.then(|| (fold_comment(&item.comment), item.name.span())) | ||
} | ||
|
||
fn fold_comment(comment: &Comment<'_>) -> String { | ||
comment.0.iter().fold(String::new(), |mut acc, line| { | ||
acc.push_str(line.value); | ||
acc.push('\n'); | ||
acc | ||
}) | ||
} | ||
|
||
fn next_variant_id(variants: &[Variant<'_>]) -> u32 { | ||
variants | ||
.iter() | ||
.map(|variant| variant.id.get()) | ||
.max() | ||
.unwrap_or(0) | ||
+ 1 | ||
} | ||
|
||
fn next_field_id(fields: &Fields<'_>) -> Option<u32> { | ||
match fields { | ||
Fields::Named(named) => { | ||
Some(named.iter().map(|field| field.id.get()).max().unwrap_or(0) + 1) | ||
} | ||
Fields::Unnamed(unnamed) => Some( | ||
unnamed | ||
.iter() | ||
.map(|field| field.id.get()) | ||
.max() | ||
.unwrap_or(0) | ||
+ 1, | ||
), | ||
Fields::Unit => None, | ||
} | ||
} | ||
|
||
#[allow(clippy::cast_possible_truncation)] | ||
fn get_range(index: &LineIndex, span: Span) -> Result<LspRange> { | ||
let range = Range::from(span); | ||
let (start, end) = index | ||
.to_wide( | ||
line_index::WideEncoding::Utf16, | ||
index.line_col(TextSize::new(range.start as u32)), | ||
) | ||
.zip(index.to_wide( | ||
line_index::WideEncoding::Utf16, | ||
index.line_col(TextSize::new(range.end as u32)), | ||
)) | ||
.context("missing utf-16 positions")?; | ||
|
||
Ok(LspRange::new( | ||
Position::new(start.line, start.col), | ||
Position::new(end.line, end.col), | ||
)) | ||
} |
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