Skip to content

Commit

Permalink
add pkgpath without override
Browse files Browse the repository at this point in the history
Signed-off-by: shruti2522 <[email protected]>
  • Loading branch information
shruti2522 committed May 20, 2024
1 parent 60b1f37 commit 07b540a
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 32 deletions.
6 changes: 4 additions & 2 deletions kclvm/sema/src/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ impl SchemaType {
}
}

pub fn schema_ty_signature_str(&self) -> String {
pub fn schema_ty_signature_str(&self) -> (String, String) {
let base: String = if let Some(base) = &self.base {
format!("({})", base.name)
} else {
Expand All @@ -313,7 +313,9 @@ impl SchemaType {
)
};

format!("{}\n\nschema {}{}{}", self.pkgpath, self.name, params, base)
let rest_sign = format!("schema {}{}{}", self.name, params, base);

(self.pkgpath.clone(), rest_sign)
}
}

Expand Down
7 changes: 4 additions & 3 deletions kclvm/tools/src/LSP/src/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -490,8 +490,8 @@ fn schema_ty_to_value_complete_item(schema_ty: &SchemaType) -> KCLCompletionItem
);
let detail = {
let mut details = vec![];
details.push(schema_ty.schema_ty_signature_str());
details.push("Attributes:".to_string());
let (pkgpath, rest_sign) = schema_ty.schema_ty_signature_str();
details.push(format!("{}\n\n{}", pkgpath, rest_sign));
for (name, attr) in &schema_ty.attrs {
details.push(format!(
"{}{}: {}",
Expand Down Expand Up @@ -543,7 +543,8 @@ fn schema_ty_to_value_complete_item(schema_ty: &SchemaType) -> KCLCompletionItem
fn schema_ty_to_type_complete_item(schema_ty: &SchemaType) -> KCLCompletionItem {
let detail = {
let mut details = vec![];
details.push(schema_ty.schema_ty_signature_str());
let (pkgpath, rest_sign) = schema_ty.schema_ty_signature_str();
details.push(format!("{}\n\n{}", pkgpath, rest_sign));
details.push("Attributes:".to_string());
for (name, attr) in &schema_ty.attrs {
details.push(format!(
Expand Down
66 changes: 41 additions & 25 deletions kclvm/tools/src/LSP/src/hover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub(crate) fn hover(
gs: &GlobalState,
) -> Option<lsp_types::Hover> {
let mut docs: Vec<String> = vec![];
let mut pkg_path = String::new();
let def = find_def_with_gs(kcl_pos, gs, true);
match def {
Some(def_ref) => match gs.get_symbols().get_symbol(def_ref) {
Expand All @@ -36,7 +37,9 @@ pub(crate) fn hover(
// attr2? type
// ```
let schema_ty = ty.into_schema_type();
docs.push(schema_ty.schema_ty_signature_str());
let (pkgpath, rest_sign) = schema_ty.schema_ty_signature_str();
pkg_path = pkgpath;
docs.push(rest_sign);
if !schema_ty.doc.is_empty() {
docs.push(schema_ty.doc.clone());
}
Expand Down Expand Up @@ -120,34 +123,37 @@ pub(crate) fn hover(
},
None => {}
}
docs_to_hover(docs)
docs_to_hover(docs, pkg_path)
}

// Convert docs to Hover. This function will convert to
// None, Scalar or Array according to the number of positions
fn docs_to_hover(docs: Vec<String>) -> Option<lsp_types::Hover> {
fn docs_to_hover(docs: Vec<String>, pkg_path: String) -> Option<lsp_types::Hover> {
match docs.len() {
0 => None,
1 => Some(Hover {
contents: HoverContents::Scalar(MarkedString::LanguageString(LanguageString {
language: "KCL".to_string(),
language: "KCL".to_owned(),
value: docs[0].clone(),
})),
range: None,
}),
_ => Some(Hover {
contents: HoverContents::Array(
docs.iter()
.map(|doc| {
MarkedString::LanguageString(LanguageString {
language: "KCL".to_string(),
value: doc.clone(),
})
})
.collect(),
),
range: None,
}),
_ => {
let mut all_docs = Vec::new();
if !pkg_path.is_empty() {
all_docs.push(MarkedString::String(pkg_path));
}
all_docs.extend(docs.iter().map(|doc| {
MarkedString::LanguageString(LanguageString {
language: "KCL".to_owned(),
value: doc.clone(),
})
}));
Some(Hover {
contents: HoverContents::Array(all_docs),
range: None,
})
}
}
}

Expand Down Expand Up @@ -221,12 +227,15 @@ mod tests {
match got.contents {
lsp_types::HoverContents::Array(vec) => {
if let MarkedString::String(s) = vec[0].clone() {
assert_eq!(s, "pkg\n\nschema Person");
assert_eq!(s, "pkg");
}
if let MarkedString::String(s) = vec[1].clone() {
assert_eq!(s, "hover doc test");
assert_eq!(s, "schema Person");
}
if let MarkedString::String(s) = vec[2].clone() {
assert_eq!(s, "hover doc test");
}
if let MarkedString::String(s) = vec[3].clone() {
assert_eq!(s, "Attributes:\n\nname: str\n\nage: int");
}
}
Expand Down Expand Up @@ -259,7 +268,7 @@ mod tests {
];

// When converting to hover content
let hover = docs_to_hover(docs.clone());
let hover = docs_to_hover(docs.clone(), "".to_string());

// Then the result should be a Hover object with an Array of MarkedString::LanguageString
assert!(hover.is_some());
Expand Down Expand Up @@ -308,12 +317,15 @@ mod tests {
match got.contents {
lsp_types::HoverContents::Array(vec) => {
if let MarkedString::String(s) = vec[0].clone() {
assert_eq!(s, "__main__\n\nschema Person");
assert_eq!(s, "__main__");
}
if let MarkedString::String(s) = vec[1].clone() {
assert_eq!(s, "hover doc test");
assert_eq!(s, "schema Person");
}
if let MarkedString::String(s) = vec[2].clone() {
assert_eq!(s, "hover doc test");
}
if let MarkedString::String(s) = vec[3].clone() {
assert_eq!(s, "Attributes:\n\nname: str\n\nage?: int");
}
}
Expand Down Expand Up @@ -516,11 +528,14 @@ mod tests {
let got = hover(&program, &pos, &gs).unwrap();
match got.contents {
lsp_types::HoverContents::Array(vec) => {
assert_eq!(vec.len(), 2);
assert_eq!(vec.len(), 3);
if let MarkedString::String(s) = vec[0].clone() {
assert_eq!(s, "fib\n\nschema Fib");
assert_eq!(s, "fib");
}
if let MarkedString::String(s) = vec[1].clone() {
assert_eq!(s, "schema Fib");
}
if let MarkedString::String(s) = vec[2].clone() {
assert_eq!(s, "Attributes:\n\nn: int\n\nvalue: int");
}
}
Expand Down Expand Up @@ -621,9 +636,10 @@ mod tests {
let got = hover(&program, &pos, &gs).unwrap();

let expect_content = vec![
MarkedString::String("__main__".to_string()),
MarkedString::LanguageString(LanguageString {
language: "KCL".to_string(),
value: "__main__\n\nschema Data1[m: {str:str}](Data)".to_string(),
value: "schema Data1[m: {str:str}](Data)".to_string(),
}),
MarkedString::LanguageString(LanguageString {
language: "KCL".to_string(),
Expand Down
6 changes: 4 additions & 2 deletions kclvm/tools/src/LSP/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1170,9 +1170,10 @@ fn hover_test() {
res.result.unwrap(),
to_json(Hover {
contents: HoverContents::Array(vec![
MarkedString::String("__main__".to_string()),
MarkedString::LanguageString(LanguageString {
language: "KCL".to_string(),
value: "__main__\n\nschema Person".to_string()
value: "schema Person".to_string()
}),
MarkedString::LanguageString(LanguageString {
language: "KCL".to_string(),
Expand Down Expand Up @@ -1677,9 +1678,10 @@ fn konfig_hover_test_main() {
match got.contents {
HoverContents::Array(arr) => {
let expect: Vec<MarkedString> = vec![
MarkedString::String("base.pkg.kusion_models.kube.frontend".to_string()),
MarkedString::LanguageString(LanguageString {
language: "KCL".to_string(),
value: "base.pkg.kusion_models.kube.frontend\n\nschema Server".to_string(),
value: "schema Server".to_string(),
}),
MarkedString::LanguageString(LanguageString {
language: "KCL".to_string(),
Expand Down

0 comments on commit 07b540a

Please sign in to comment.