Skip to content

Commit

Permalink
Add example highlight hover content for the schema definition doc and…
Browse files Browse the repository at this point in the history
… test

Signed-off-by: Your Name <your.email@example.com>
  • Loading branch information
Your Name committed Jan 15, 2025
1 parent 29266c0 commit 3dad8af
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
46 changes: 46 additions & 0 deletions kclvm/tools/src/LSP/src/hover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ pub fn hover(kcl_pos: &KCLPos, gs: &GlobalState) -> Option<lsp_types::Hover> {

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));
}
}
}
_ => {}
Expand Down Expand Up @@ -159,6 +164,17 @@ pub fn hover(kcl_pos: &KCLPos, gs: &GlobalState) -> Option<lsp_types::Hover> {
docs_to_hover(docs)
}

/// Extract the "Examples" section from the documentation string
fn extract_examples(doc: &str) -> Option<String> {
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()
}
Expand Down Expand Up @@ -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"),
}
}
}
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 3dad8af

Please sign in to comment.