File tree Expand file tree Collapse file tree 2 files changed +60
-20
lines changed Expand file tree Collapse file tree 2 files changed +60
-20
lines changed Original file line number Diff line number Diff line change @@ -102,6 +102,16 @@ fn expected_word_kinds(
102
102
source_contents : & str ,
103
103
cursor_offset : u32 ,
104
104
) -> WordKinds {
105
+ // We should not retun any completions in comments.
106
+ // This compensates for a bug in [`possible_words_at_offset_in_source`] .
107
+ // Ideally, that function would be aware of the comment context and not
108
+ // return any completions, however this is difficult to do today because
109
+ // of the parser's unawareness of comment tokens.
110
+ // So we do a simple check here where we have access to the full source contents.
111
+ if in_comment ( source_contents, cursor_offset) {
112
+ return WordKinds :: empty ( ) ;
113
+ }
114
+
105
115
match & compilation. kind {
106
116
CompilationKind :: OpenProject {
107
117
package_graph_sources,
@@ -121,6 +131,16 @@ fn expected_word_kinds(
121
131
}
122
132
}
123
133
134
+ fn in_comment ( source_contents : & str , cursor_offset : u32 ) -> bool {
135
+ // find the last newline before the cursor
136
+ let last_line_start = source_contents[ ..cursor_offset as usize ]
137
+ . rfind ( '\n' )
138
+ . unwrap_or ( 0 ) ;
139
+ // find the last comment start before the cursor
140
+ let last_comment_start = source_contents[ last_line_start..cursor_offset as usize ] . rfind ( "//" ) ;
141
+ last_comment_start. is_some ( )
142
+ }
143
+
124
144
/// Collects hardcoded completions from the given set of parser predictions.
125
145
///
126
146
/// Hardcoded words are actual keywords (`let`, etc) as well as other words that are
Original file line number Diff line number Diff line change @@ -121,6 +121,13 @@ fn check_with_dependency(
121
121
assert_no_duplicates ( actual_completions) ;
122
122
}
123
123
124
+ fn check_no_completions ( source_with_cursor : & str ) {
125
+ let ( compilation, cursor_position, _) = compile_with_markers ( source_with_cursor, true ) ;
126
+ let actual_completions =
127
+ get_completions ( & compilation, "<source>" , cursor_position, Encoding :: Utf8 ) ;
128
+ assert_eq ! ( actual_completions. items, Vec :: default ( ) ) ;
129
+ }
130
+
124
131
fn assert_no_duplicates ( mut actual_completions : CompletionList ) {
125
132
actual_completions
126
133
. items
@@ -4075,42 +4082,55 @@ fn incomplete_return_type_in_partial_callable_signature() {
4075
4082
4076
4083
#[ test]
4077
4084
fn no_path_segment_completion_inside_attr ( ) {
4078
- check (
4085
+ check_no_completions (
4079
4086
"namespace Test {
4080
4087
4081
4088
@Config(FakeStdLib.↘)
4082
4089
function Main() : Unit {
4083
4090
}
4084
4091
}" ,
4085
- & [ "Fake" , "not" , "Test" ] ,
4086
- & expect ! [ [ r#"
4087
- [
4088
- None,
4089
- None,
4090
- None,
4091
- ]
4092
- "# ] ] ,
4093
4092
) ;
4094
4093
}
4095
4094
4096
4095
#[ test]
4097
4096
fn no_completion_inside_attr ( ) {
4098
- check (
4097
+ check_no_completions (
4099
4098
"namespace Test {
4100
4099
4101
4100
@Config(↘)
4102
4101
function Main() : Unit {
4103
- let FakeStdLib = new Foo { bar = 3 };
4104
- FakeStdLib.↘
4105
4102
}
4106
4103
}" ,
4107
- & [ "Fake" , "not" , "Test" ] ,
4108
- & expect ! [ [ r#"
4109
- [
4110
- None,
4111
- None,
4112
- None,
4113
- ]
4114
- "# ] ] ,
4104
+ ) ;
4105
+ }
4106
+
4107
+ #[ test]
4108
+ fn in_comment ( ) {
4109
+ check_no_completions (
4110
+ "namespace Test {
4111
+ import Foo;
4112
+ // Hello there ↘
4113
+ import Bar;
4114
+ }" ,
4115
+ ) ;
4116
+ }
4117
+
4118
+ #[ test]
4119
+ fn in_doc_comment ( ) {
4120
+ check_no_completions (
4121
+ "namespace Test {
4122
+ import Foo;
4123
+ /// Hello there ↘
4124
+ import Bar;
4125
+ }" ,
4126
+ ) ;
4127
+ }
4128
+
4129
+ #[ test]
4130
+ fn in_trailing_comment ( ) {
4131
+ check_no_completions (
4132
+ "namespace Test {
4133
+ import Foo; // Hello there ↘
4134
+ }" ,
4115
4135
) ;
4116
4136
}
You can’t perform that action at this time.
0 commit comments