Skip to content

Commit

Permalink
add markup
Browse files Browse the repository at this point in the history
Signed-off-by: shruti2522 <[email protected]>
  • Loading branch information
shruti2522 committed May 19, 2024
1 parent 69f9492 commit d43c077
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 28 deletions.
10 changes: 5 additions & 5 deletions kclvm/sema/src/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub use unify::*;
pub use walker::walk_type;

use super::resolver::doc::Example;
use kclvm_utils::markup::add_markup;

#[cfg(test)]
mod tests;
Expand Down Expand Up @@ -313,10 +314,9 @@ impl SchemaType {
)
};

format!(
"{}\n\n```kcl\nschema {}{}{}\n```",
self.pkgpath, self.name, params, base
)
let schema_str = add_markup(&format!("schema {}{}{}", self.name, params, base));

format!("{}\n\n{}", self.pkgpath, schema_str)
}
}

Expand Down Expand Up @@ -473,4 +473,4 @@ pub struct Parameter {
pub name: String,
pub ty: TypeRef,
pub has_default: bool,
}
}
42 changes: 19 additions & 23 deletions kclvm/tools/src/LSP/src/hover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use kclvm_sema::{
ty::{FunctionType, ANY_TYPE_STR},
};
use lsp_types::{Hover, HoverContents, MarkedString};

use crate::goto_def::find_def_with_gs;
use kclvm_utils::markup::add_markup;

/// Returns a short text describing element at position.
/// Specifically, the doc for schema and schema attr(todo)
Expand Down Expand Up @@ -45,7 +45,7 @@ pub(crate) fn hover(
// Use the api provided by GlobalState to get all attrs
let module_info = gs.get_packages().get_module_info(&kcl_pos.filename);
let schema_attrs = obj.get_all_attributes(gs.get_symbols(), module_info);
let mut attrs = vec!["```kcl\nAttributes:\n```".to_string()];
let mut attrs = vec!["**Attributes:**".to_string()];
for schema_attr in schema_attrs {
if let kclvm_sema::core::symbol::SymbolKind::Attribute =
schema_attr.get_kind()
Expand All @@ -58,12 +58,13 @@ pub(crate) fn hover(
Some(ty) => ty.ty_str(),
None => ANY_TYPE_STR.to_string(),
};
attrs.push(format!(
"```kcl\n{}{}: {}\n```",
let formatted_attr = format!(
"{}{}: {}",
name,
if attr_symbol.is_optional() { "?" } else { "" },
attr_ty_str,
));
);
attrs.push(add_markup(&formatted_attr));
}
}
docs.push(attrs.join("\n\n"));
Expand All @@ -74,7 +75,7 @@ pub(crate) fn hover(
let sema_info = obj.get_sema_info();
match &sema_info.ty {
Some(ty) => {
docs.push(format!("```kcl\n{}: {}\n```", &obj.get_name(), ty.ty_str()));
docs.push(add_markup(&format!("{}: {}", &obj.get_name(), ty.ty_str())));
if let Some(doc) = &sema_info.doc {
if !doc.is_empty() {
docs.push(doc.clone());
Expand All @@ -90,7 +91,7 @@ pub(crate) fn hover(
docs.extend(build_func_hover_content(func_ty, obj.get_name().clone()));
}
_ => {
docs.push(format!("```kcl\n{}: {}\n```", &obj.get_name(), ty.ty_str()));
docs.push(add_markup(&format!("{}: {}", &obj.get_name(), ty.ty_str())));
}
},
_ => {}
Expand All @@ -113,7 +114,7 @@ pub(crate) fn hover(
Some(ty) => ty.ty_str(),
None => "".to_string(),
};
docs.push(format!("```kcl\n{}: {}\n```", &obj.get_name(), ty_str));
docs.push(add_markup(&format!("{}: {}", &obj.get_name(), ty_str)));
}
},
None => {}
Expand Down Expand Up @@ -144,34 +145,29 @@ fn docs_to_hover(docs: Vec<String>) -> Option<lsp_types::Hover> {
}

// Build hover content for function call
// ```
// pkg
// -----------------
// function func_name(arg1: type, arg2: type, ..) -> type
// -----------------
// doc
// ```
fn build_func_hover_content(func_ty: &FunctionType, name: String) -> Vec<String> {
let mut docs = vec![];
if let Some(ty) = &func_ty.self_ty {
let self_ty = format!("```kcl\n{}\n```\n\n", ty.ty_str());
let self_ty = add_markup(&format!("{}\n\n", ty.ty_str()));
docs.push(self_ty);
}

let mut sig = format!("```kcl\nfn {}(", name);
let mut sig = add_markup(&format!("fn {}(", name));
if func_ty.params.is_empty() {
sig.push(')');
} else {
for (i, p) in func_ty.params.iter().enumerate() {
sig.push_str(&format!("```kcl\n{}: {}\n```", p.name, p.ty.ty_str()));
let p_str = &format!("{}: {}", p.name, p.ty.ty_str());
sig.push_str(&add_markup(&p_str));

if i != func_ty.params.len() - 1 {
sig.push_str(", ");
}
}
sig.push(')');
}
sig.push_str(&format!(" -> ```kcl\n{}\n```", func_ty.return_ty.ty_str()));
let func_str = &format!(" -> {}", func_ty.return_ty.ty_str());
sig.push_str(&add_markup(&func_str));
docs.push(sig);

if !func_ty.doc.is_empty() {
Expand Down Expand Up @@ -219,7 +215,7 @@ mod tests {
assert_eq!(s, "hover doc test");
}
if let MarkedString::String(s) = vec[2].clone() {
assert_eq!(s, "```kcl\nAttributes:\n```\n\n```kcl\nname: str\n```\n\n```kcl\nage: int\n```");
assert_eq!(s, "**Attributes:**\n\n```kcl\nname: str\n```\n\n```kcl\nage: int\n```");
}
}
_ => unreachable!("test error"),
Expand Down Expand Up @@ -297,7 +293,7 @@ mod tests {
assert_eq!(s, "hover doc test");
}
if let MarkedString::String(s) = vec[2].clone() {
assert_eq!(s, "```kcl\nAttributes:\n```\n\n```kcl\nname: str\n```\n\n```kcl\nage?: int\n```");
assert_eq!(s, "**Attributes:**\n\n```kcl\nname: str\n```\n\n```kcl\nage?: int\n```");
}
}
_ => unreachable!("test error"),
Expand Down Expand Up @@ -504,7 +500,7 @@ mod tests {
assert_eq!(s, "fib\n\n```kcl\nschema Fib\n```");
}
if let MarkedString::String(s) = vec[1].clone() {
assert_eq!(s, "```kcl\nAttributes:\n```\n\n```kcl\nn: int\n```\n\n```kcl\nvalue: int\n```");
assert_eq!(s, "**Attributes:**\n\n```kcl\nn: int\n```\n\n```kcl\nvalue: int\n```");
}
}
_ => unreachable!("test error"),
Expand Down Expand Up @@ -603,7 +599,7 @@ mod tests {
"__main__\n\n```kcl\nschema Data1[m: {str:str}](Data)\n```".to_string(),
),
MarkedString::String(
"```kcl\nAttributes:\n```\n\n```kcl\nname: str\n```\n\n```kcl\nage: int\n```"
"**Attributes:**\n\n```kcl\nname: str\n```\n\n```kcl\nage: int\n```"
.to_string(),
),
];
Expand Down
1 change: 1 addition & 0 deletions kclvm/utils/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod fslock;
pub mod path;
pub mod pkgpath;
pub mod markup;
5 changes: 5 additions & 0 deletions kclvm/utils/src/markup.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// KCL function for highlighting

pub fn add_markup(text: &str) -> String {
format!("```kcl\n{}\n```", text)
}

0 comments on commit d43c077

Please sign in to comment.