Skip to content

Commit 94acdee

Browse files
committed
Schema hover
1 parent 1845c6d commit 94acdee

3 files changed

Lines changed: 46 additions & 10 deletions

File tree

src/hover.rs

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ use lsp_types::Range;
33
use rnix::{ast::Expr, TextRange};
44
use ropey::Rope;
55
use rowan::ast::AstNode;
6-
use std::path::PathBuf;
6+
use std::{path::PathBuf, sync::Arc};
77

88
use crate::{
99
evaluator::{proto::HoverRequest, Evaluator},
1010
file_types::FileInfo,
1111
safe_stringification::safe_stringify_attr,
12-
schema::get_schema,
12+
schema::{get_schema, Schema},
1313
syntax::{
1414
ancestor_exprs, ancestor_exprs_inclusive, find_variable, in_context, in_context_custom,
1515
in_context_with_select, locate_cursor, parse, rope_text_range_to_range, FoundVariable,
@@ -24,6 +24,7 @@ enum HoverOrigin {
2424
With(TextRange),
2525
Select,
2626
Builtin,
27+
Schema(Arc<Schema>),
2728
}
2829

2930
#[derive(Debug)]
@@ -45,6 +46,14 @@ pub async fn hover(
4546
let strategy =
4647
get_hover_strategy(source, file_info, offset).ok_or(anyhow!("can't hover this"))?;
4748

49+
if let HoverOrigin::Schema(schema) = strategy.origin {
50+
let md = schema
51+
.description()
52+
.ok_or(anyhow!("no description"))?
53+
.to_string();
54+
return Ok(HoverResult { md, position: None });
55+
}
56+
4857
let expression = strategy.expression.ok_or(anyhow!("no expression"))?;
4958

5059
let result = evaluator
@@ -72,6 +81,7 @@ pub async fn hover(
7281
HoverOrigin::With(_) => None,
7382
HoverOrigin::Select => None,
7483
HoverOrigin::Builtin => None,
84+
HoverOrigin::Schema(_) => None,
7585
};
7686

7787
let position = if let Some(origin_range) = origin_range {
@@ -204,16 +214,17 @@ fn get_hover_strategy(source: &str, file_info: &FileInfo, offset: u32) -> Option
204214
}
205215
Expr::AttrSet(_) => {
206216
let mut schema = schema;
207-
for attr in attrpath.attrs().take(index) {
217+
for attr in attrpath.attrs().take(index + 1) {
208218
schema = schema.attr_subschema(&attr).clone();
219+
eprintln!("subschema {:?}", schema);
209220
}
210221

211-
// Some(HoverStrategy {
212-
// attrs_expression: None,
213-
// range,
214-
// variables: schema.properties(),
215-
// })
216-
None
222+
Some(HoverStrategy {
223+
expression: None,
224+
range,
225+
attr: None,
226+
origin: HoverOrigin::Schema(schema),
227+
})
217228
}
218229
_ => None,
219230
},
@@ -268,6 +279,7 @@ mod test {
268279
use crate::{
269280
evaluator::Evaluator,
270281
file_types::{FileInfo, FileType},
282+
schema::HOME_MANAGER_SCHEMA,
271283
};
272284

273285
use super::hover;
@@ -496,4 +508,24 @@ mod test {
496508
)
497509
.await;
498510
}
511+
512+
#[test_log::test(tokio::test)]
513+
async fn test_hover_schema() {
514+
check_hover_with_filetype(
515+
r#"{ programs.zsh.enableAutosug$0gestions = false; }"#,
516+
expect![[r#"
517+
no position
518+
519+
### option `programs.zsh.enableAutosuggestions`
520+
Alias of {option}`programs.zsh.autosuggestion.enable`.
521+
522+
*Type:* boolean
523+
"#]],
524+
&FileType::Package {
525+
nixpkgs_path: env!("NIXPKGS").to_string(),
526+
schema: HOME_MANAGER_SCHEMA.clone(),
527+
},
528+
)
529+
.await;
530+
}
499531
}

src/lsp.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ fn handle_request(analyzer: &mut Analyzer, req: Request) -> Result<Response> {
127127
position.line,
128128
position.character,
129129
))? {
130-
Some(hover) => format!("{:?}\n\n{}", hover.position, hover.md),
130+
Some(hover) => hover.md,
131131
None => return Ok(Response::new_ok(id, None::<Hover>)),
132132
};
133133

src/schema.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ lazy_static! {
4848
}
4949

5050
impl Schema {
51+
pub fn description(&self) -> Option<&str> {
52+
self.description.as_ref().map(|x| x.as_str())
53+
}
54+
5155
pub fn properties(&self) -> Vec<String> {
5256
self.properties
5357
.as_ref()

0 commit comments

Comments
 (0)