Skip to content

Commit 5a5dba6

Browse files
authored
[ide] Fix autocomplete issues (#1092)
Release the fixes as 0.9.1
1 parent 8e0aac6 commit 5a5dba6

File tree

14 files changed

+86
-36
lines changed

14 files changed

+86
-36
lines changed

Cargo.lock

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/samlang-cli/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "samlang-cli"
3-
version = "0.9.0"
3+
version = "0.9.1"
44
edition = "2021"
55

66
[dependencies]

crates/samlang-collections/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "samlang-collections"
3-
version = "0.9.0"
3+
version = "0.9.1"
44
edition = "2021"
55

66
[dependencies]

crates/samlang-core/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "samlang-core"
3-
version = "0.9.0"
3+
version = "0.9.1"
44
edition = "2021"
55

66
[dependencies]

crates/samlang-core/src/ast/loc.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ mod tests {
9898
#[test]
9999
fn location_contains_position_tests() {
100100
assert!(Location::from_pos(1, 3, 3, 1).contains_position(Position(2, 2)));
101+
assert!(Location::from_pos(1, 3, 3, 1).contains_position(Position(1, 3)));
102+
assert!(Location::from_pos(1, 3, 3, 1).contains_position(Position(3, 1)));
101103
assert!(!Location::from_pos(1, 3, 3, 1).contains_position(Position(1, 2)));
102104
assert!(!Location::from_pos(1, 3, 3, 1).contains_position(Position(3, 2)));
103105
}

crates/samlang-core/src/parser/source_parser.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -950,7 +950,7 @@ impl<'a> SourceParser<'a> {
950950
let mut function_expression = self.parse_base_expression();
951951
loop {
952952
match self.peek() {
953-
Token(_, TokenContent::Operator(TokenOp::DOT)) => {
953+
Token(dot_loc, TokenContent::Operator(TokenOp::DOT)) => {
954954
let mut field_preceding_comments = self.collect_preceding_comments();
955955
self.consume();
956956
field_preceding_comments.append(&mut self.collect_preceding_comments());
@@ -960,10 +960,7 @@ impl<'a> SourceParser<'a> {
960960
}
961961
Token(l, t) => {
962962
self.report(l, format!("Expected identifier, but get {}", t.pretty_print(self.heap)));
963-
(
964-
Location { module_reference: self.module_reference, start: l.start, end: l.start },
965-
PStr::MISSING,
966-
)
963+
(Location { end: l.start, ..dot_loc }, PStr::MISSING)
967964
}
968965
};
969966
let mut loc = function_expression.loc().union(&field_loc);

crates/samlang-core/src/services/api.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -702,7 +702,9 @@ pub mod completion {
702702
} else {
703703
(CompletionItemKind::Interface, format!("interface {}", name))
704704
};
705-
let additional_edits = if available_names.contains(n) {
705+
let additional_edits = if available_names.contains(n)
706+
|| ModuleReference::ROOT.eq(import_mod_ref)
707+
{
706708
vec![]
707709
} else {
708710
rewrite::generate_auto_import_edits(
@@ -736,9 +738,9 @@ pub mod completion {
736738
fn_name: _,
737739
is_method: true,
738740
type_: _,
739-
} => (module_ref, class_name),
740-
LocationCoverSearchResult::Expression(expr::E::FieldAccess(e)) => {
741-
e.object.type_().as_nominal().map(|t| (t.module_reference, t.id))?
741+
}
742+
| LocationCoverSearchResult::PropertyName(_, module_ref, class_name, _) => {
743+
(module_ref, class_name)
742744
}
743745
_ => return None,
744746
};

crates/samlang-core/src/services/api_tests.rs

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ mod tests {
77
};
88
use itertools::Itertools;
99
use pretty_assertions::assert_eq;
10-
use samlang_heap::{Heap, ModuleReference};
10+
use samlang_heap::{Heap, ModuleReference, PStr};
1111
use std::collections::HashMap;
1212

1313
#[test]
@@ -992,27 +992,65 @@ class Main {
992992

993993
#[test]
994994
fn autocomplete_test_6() {
995-
let mod_ref = ModuleReference::DUMMY;
995+
let heap = &mut Heap::new();
996+
let mod_ref = heap.alloc_module_reference(vec![PStr::UPPER_A]);
996997
let state = ServerState::new(
997998
Heap::new(),
998999
false,
999-
HashMap::from([(
1000-
mod_ref,
1001-
r#"
1000+
HashMap::from([
1001+
(
1002+
mod_ref,
1003+
r#"
10021004
class Main {
1003-
function main(a: Developer): Developer = a.
1005+
function main1(a: Developer): Developer = a.
1006+
function main2(): Developer = Developer.
1007+
function main3(): Developer = Process.
10041008
}
10051009
class Developer {
10061010
private method f(): unit = {}
10071011
method b(): unit = {}
10081012
}
10091013
"#
1010-
.to_string(),
1011-
)]),
1014+
.to_string(),
1015+
),
1016+
(ModuleReference::DUMMY, "class Other {}".to_string()),
1017+
]),
10121018
);
10131019
assert_eq!(
10141020
"b [kind=Method, detail=b(): unit]",
1015-
completion::auto_complete(&state, &mod_ref, Position(2, 45))
1021+
completion::auto_complete(&state, &mod_ref, Position(2, 46))
1022+
.iter()
1023+
.map(completion::AutoCompletionItem::to_string)
1024+
.join("\n")
1025+
);
1026+
assert_eq!(
1027+
r#"
1028+
Main [kind=Class, detail=class Main]
1029+
Other [kind=Class, detail=class Other]
1030+
Process [kind=Class, detail=class Process]
1031+
Str [kind=Class, detail=class Str]
1032+
Developer [kind=Class, detail=class Developer]
1033+
"#
1034+
.trim(),
1035+
completion::auto_complete(&state, &mod_ref, Position(3, 39))
1036+
.iter()
1037+
.map(completion::AutoCompletionItem::to_string)
1038+
.join("\n")
1039+
);
1040+
assert_eq!(
1041+
"init [kind=Function, detail=init(): Developer]",
1042+
completion::auto_complete(&state, &mod_ref, Position(3, 42))
1043+
.iter()
1044+
.map(completion::AutoCompletionItem::to_string)
1045+
.join("\n")
1046+
);
1047+
assert_eq!(
1048+
r#"
1049+
panic [kind=Function, detail=panic(a0: Str): T]
1050+
println [kind=Function, detail=println(a0: Str): unit]
1051+
"#
1052+
.trim(),
1053+
completion::auto_complete(&state, &mod_ref, Position(4, 40))
10161054
.iter()
10171055
.map(completion::AutoCompletionItem::to_string)
10181056
.join("\n")

crates/samlang-core/src/services/location_cover.rs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,23 @@ fn search_expression(
4949
.as_nominal()
5050
.filter(|_| e.field_name.loc.contains_position(position))
5151
.map(|nominal_type| {
52-
LocationCoverSearchResult::PropertyName(
53-
e.field_name.loc,
54-
nominal_type.module_reference,
55-
nominal_type.id,
56-
e.field_name.name,
57-
)
52+
if nominal_type.is_class_statics {
53+
LocationCoverSearchResult::InterfaceMemberName {
54+
loc: e.field_name.loc,
55+
module_reference: nominal_type.module_reference,
56+
class_name: nominal_type.id,
57+
fn_name: e.field_name.name,
58+
is_method: false,
59+
type_: e.common.type_.clone(),
60+
}
61+
} else {
62+
LocationCoverSearchResult::PropertyName(
63+
e.field_name.loc,
64+
nominal_type.module_reference,
65+
nominal_type.id,
66+
e.field_name.name,
67+
)
68+
}
5869
});
5970
if found.is_some() {
6071
return found;

crates/samlang-heap/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "samlang-heap"
3-
version = "0.9.0"
3+
version = "0.9.1"
44
edition = "2021"
55

66
[dependencies]

0 commit comments

Comments
 (0)