Skip to content

Commit

Permalink
bugfix: fix empty identifier expr position, and completion for litera…
Browse files Browse the repository at this point in the history
…l str in lsp
  • Loading branch information
He1pa committed Jul 3, 2023
1 parent 28a3e2a commit b9a2b2c
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 15 deletions.
59 changes: 48 additions & 11 deletions kclvm/parser/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,16 +312,42 @@ 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.struct_token_loc(lo, self.prev_token),
))
}
}
}

/// Syntax:
Expand Down Expand Up @@ -2099,7 +2125,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

0 comments on commit b9a2b2c

Please sign in to comment.