diff --git a/kclvm/tools/src/LSP/src/hover.rs b/kclvm/tools/src/LSP/src/hover.rs index c69c19b40..0e3a1509a 100644 --- a/kclvm/tools/src/LSP/src/hover.rs +++ b/kclvm/tools/src/LSP/src/hover.rs @@ -77,6 +77,11 @@ pub fn hover(kcl_pos: &KCLPos, gs: &GlobalState) -> Option { if !schema_ty.doc.is_empty() { docs.push((schema_ty.doc.clone(), MarkedStringType::String)); + + // Extract and include the Examples section if available + if let Some(examples_section) = extract_examples(&schema_ty.doc) { + docs.push((examples_section, MarkedStringType::LanguageString)); + } } } _ => {} @@ -159,6 +164,17 @@ pub fn hover(kcl_pos: &KCLPos, gs: &GlobalState) -> Option { docs_to_hover(docs) } +/// Extract the "Examples" section from the documentation string +fn extract_examples(doc: &str) -> Option { + if let Some(start) = doc.find("Examples") { + // Extract from the start of the Examples section to the end + let examples = &doc[start..]; + Some(examples.to_string()) + } else { + None + } +} + fn ty_hover_content(ty: &Type) -> String { ty.ty_hint() } @@ -777,4 +793,34 @@ mod tests { })); assert_eq!(got.contents, expected); } + + #[test] + #[bench_test] + fn schema_doc_with_examples_hover_test() { + let (file, _program, _, gs, _) = + compile_test_file("src/test_data/hover_test/schema_with_examples.k"); + + let pos = KCLPos { + filename: file.clone(), + line: 1, + column: Some(1), + }; + let got = hover(&pos, &gs).unwrap(); + + match got.contents { + lsp_types::HoverContents::Array(vec) => { + assert_eq!(vec.len(), 3); + if let lsp_types::MarkedString::LanguageString(s) = &vec[0] { + assert_eq!(s.value, "schema Server:\n name?: str"); + } + if let lsp_types::MarkedString::String(s) = &vec[1] { + assert_eq!(s, "Server is abstaction of Deployment and StatefulSet."); + } + if let lsp_types::MarkedString::String(s) = &vec[2] { + assert_eq!(s, "Examples\n --------\n import models.kube.frontend\n import models.kube.frontend.container\n import models.kube.templates.resource as res_tpl\n \n appConfiguration: frontend.Server {\n mainContainer = container.Main {\n name = \"php-redis\"\n env: {\n \"GET_HOSTS_FROM\": {value = \"dns\"}\n }\n }\n }"); + } + } + _ => unreachable!("test error"), + } + } } diff --git a/kclvm/tools/src/LSP/src/test_data/hover_test/schema_with_examples.k b/kclvm/tools/src/LSP/src/test_data/hover_test/schema_with_examples.k new file mode 100644 index 000000000..c9fd52362 --- /dev/null +++ b/kclvm/tools/src/LSP/src/test_data/hover_test/schema_with_examples.k @@ -0,0 +1,19 @@ +schema Server: + """Server is abstaction of Deployment and StatefulSet. + + Examples + -------- + import models.kube.frontend + import models.kube.frontend.container + import models.kube.templates.resource as res_tpl + + appConfiguration: frontend.Server { + mainContainer = container.Main { + name = "php-redis" + env: { + "GET_HOSTS_FROM": {value = "dns"} + } + } + } + """ + name?: str \ No newline at end of file