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

fix: fix empty identifier expr position #593

Merged
merged 1 commit into from
Jul 3, 2023
Merged
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
62 changes: 51 additions & 11 deletions kclvm/parser/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,16 +312,45 @@ impl<'a> Parser<'a> {
};
// bump .
self.bump();
let attr = self.parse_identifier();
Box::new(Node::node(
Expr::Selector(SelectorExpr {
value,
attr,
has_question,
ctx: ExprContext::Load,
}),
self.sess.struct_token_loc(lo, self.prev_token),
))
match self.token.ident() {
Some(_) => {
let attr = self.parse_identifier();
Box::new(Node::node(
Expr::Selector(SelectorExpr {
value,
attr,
has_question,
ctx: ExprContext::Load,
}),
self.sess.struct_token_loc(lo, self.prev_token),
))
}
_ => {
let attr = Box::new(Node::node(
Identifier {
names: vec!["".to_string()],
pkgpath: "".to_string(),
ctx: ExprContext::Load,
},
(
self.sess.lookup_char_pos(self.token.span.lo()),
self.sess.lookup_char_pos(self.token.span.lo()),
),
));
Box::new(Node::node(
Expr::Selector(SelectorExpr {
value,
attr,
has_question,
ctx: ExprContext::Load,
}),
(
self.sess.lookup_char_pos(lo.span.lo()),
self.sess.lookup_char_pos(self.token.span.lo()),
),
))
}
}
}

/// Syntax:
Expand Down Expand Up @@ -2099,7 +2128,18 @@ impl<'a> Parser<'a> {
{
self.sess
.struct_token_error(&[TokenKind::ident_value()], self.token);
names.push("".to_string())
names.push("".to_string());
return Box::new(Node::node(
Identifier {
names,
pkgpath: "".to_string(),
ctx: ExprContext::Load,
},
(
self.sess.lookup_char_pos(self.token.span.lo()),
self.sess.lookup_char_pos(self.token.span.lo()),
),
));
};
}
}
Expand Down
7 changes: 7 additions & 0 deletions kclvm/tools/src/LSP/src/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ fn completion_dot(
pos: &KCLPos,
prog_scope: &ProgramScope,
) -> Option<lsp_types::CompletionResponse> {
// Get the position of trigger_character '.'
let pos = &KCLPos {
filename: pos.filename.clone(),
line: pos.line,
column: pos.column.map(|c| c - 1),
};

match program.pos_to_stmt(pos) {
Some(node) => match node.node {
Stmt::Import(stmt) => completion_for_import(&stmt, pos, prog_scope, program),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ p3 = subpkg. # complete user module definition

import math
math. # complete user module definition

"a".
15 changes: 11 additions & 4 deletions kclvm/tools/src/LSP/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -589,14 +589,21 @@ fn completion_test() {
assert_eq!(got, expect);
items.clear();

// test completion for literal str builtin function
let pos = KCLPos {
filename: file,
line: 22,
column: Some(19),
line: 21,
column: Some(4),
};
assert!(completion(Some('.'), &program, &pos, &prog_scope).is_none());

items.clear();
let got = completion(Some('.'), &program, &pos, &prog_scope).unwrap();
let binding = STRING_MEMBER_FUNCTIONS;
for k in binding.keys() {
items.insert(format!("{}{}", k, "()"));
}
let expect: CompletionResponse = into_completion_items(&items).into();

assert_eq!(got, expect);
}

#[test]
Expand Down
Loading