Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

arch: migrate lsp schema attr completion to new sema model #883

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 39 additions & 19 deletions kclvm/tools/src/LSP/src/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use std::{fs, path::Path};

use crate::goto_def::find_def_with_gs;
use indexmap::IndexSet;
use kclvm_ast::ast::{Expr, ImportStmt, Node, Program, Stmt};
use kclvm_ast::ast::{Expr, ImportStmt, Program, Stmt};
use kclvm_ast::pos::GetPos;
use kclvm_ast::MAIN_PKG;
use kclvm_config::modfile::KCL_FILE_EXTENSION;
Expand All @@ -29,7 +29,7 @@ use kclvm_sema::core::global_state::GlobalState;
use kclvm_error::Position as KCLPos;
use kclvm_sema::builtin::{STANDARD_SYSTEM_MODULES, STRING_MEMBER_FUNCTIONS};
use kclvm_sema::resolver::doc::{parse_doc_string, Doc};
use kclvm_sema::resolver::scope::{ProgramScope, ScopeObjectKind};
use kclvm_sema::resolver::scope::ProgramScope;
use kclvm_sema::ty::{FunctionType, SchemaType, Type};
use lsp_types::{CompletionItem, CompletionItemKind};

Expand Down Expand Up @@ -93,7 +93,7 @@ pub(crate) fn completion(

completions.extend(completion_variable(pos, prog_scope));

completions.extend(completion_attr(program, pos, prog_scope));
completions.extend(completion_attr(program, pos, gs));

completions.extend(completion_import_builtin_pkg(program, pos, prog_scope));

Expand Down Expand Up @@ -336,40 +336,58 @@ fn completion_import_builtin_pkg(
/// Complete schema attr
///
/// ```no_run
/// #[cfg(not(test))]
/// p = Person {
/// n<cursor>
/// }
/// ```
/// complete to
/// ```no_run
/// #[cfg(not(test))]
/// p = Person {
/// name<cursor>
/// }
/// ```
fn completion_attr(
program: &Program,
pos: &KCLPos,
prog_scope: &ProgramScope,
gs: &GlobalState,
) -> IndexSet<KCLCompletionItem> {
let mut completions: IndexSet<KCLCompletionItem> = IndexSet::new();

if let Some((node, schema_expr)) = is_in_schema_expr(program, pos) {
let schema_def = find_def(node, &schema_expr.name.get_end_pos(), prog_scope);
if let Some(schema) = schema_def {
if let Definition::Object(obj, _) = schema {
let schema_type = obj.ty.into_schema_type();
completions.extend(schema_type.attrs.iter().map(|(name, attr)| {
KCLCompletionItem {
label: name.clone(),
detail: Some(format!("{}: {}", name, attr.ty.ty_str())),
documentation: attr.doc.clone(),
kind: Some(KCLCompletionItemKind::SchemaAttr),
let mut items = IndexSet::new();
if let Some((_, schema_expr)) = is_in_schema_expr(program, pos) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this function is_in_schema_expr with the new semantic and how to ensure the correctness of the function.

let pos = schema_expr.name.get_end_pos();
if let Some(symbol_ref) = find_def_with_gs(&pos, &gs, true) {
if let Some(symbol) = gs.get_symbols().get_symbol(symbol_ref) {
if let Some(def) = symbol.get_definition() {
match def.get_kind() {
kclvm_sema::core::symbol::SymbolKind::Schema => {
if let Some(ty) = &symbol.get_sema_info().ty {
match &ty.kind {
kclvm_sema::ty::TypeKind::Schema(schema_ty) => {
items.extend(schema_ty.attrs.iter().map(|(name, attr)| {
KCLCompletionItem {
label: name.clone(),
detail: Some(format!(
"{}: {}",
name,
attr.ty.ty_str()
)),
documentation: attr.doc.clone(),
kind: Some(KCLCompletionItemKind::SchemaAttr),
}
}))
}
_ => {}
}
}
}
_ => {}
}
}));
}
}
}
}
completions
items
}

/// Complete all usable scope obj in inner_most_scope
Expand Down Expand Up @@ -418,10 +436,12 @@ fn completion_variable(pos: &KCLPos, prog_scope: &ProgramScope) -> IndexSet<KCLC
/// Complete schema name
///
/// ```no_run
/// #[cfg(not(test))]
/// p = P<cursor>
/// ```
/// complete to
/// ```no_run
/// #[cfg(not(test))]
/// p = Person(param1, param2){}<cursor>
/// ```
fn schema_ty_completion_item(schema_ty: &SchemaType) -> KCLCompletionItem {
Expand Down
16 changes: 0 additions & 16 deletions kclvm/tools/src/LSP/src/goto_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -497,22 +497,6 @@ fn goto_def_for_import(
positions_to_goto_def_resp(&positions)
}

#[deprecated = "Wait for migrate to new sema model"]
pub(crate) fn get_identifier_last_name(id: &Identifier) -> String {
match id.names.len() {
0 => "".to_string(),
1 => id.names[0].node.clone(),
_ => {
if id.names.last().unwrap().node == *"" {
// MissingExpr
id.names.get(id.names.len() - 2).unwrap().node.clone()
} else {
id.names.last().unwrap().node.clone()
}
}
}
}

#[cfg(test)]
mod tests {
use super::goto_definition_with_gs;
Expand Down