Skip to content

Commit b3a81a2

Browse files

File tree

2 files changed

+50
-15
lines changed

2 files changed

+50
-15
lines changed

language_service/src/completion.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,16 +152,26 @@ pub(crate) fn get_completions(
152152
}
153153

154154
fn get_first_non_whitespace_in_source(compilation: &Compilation, package_offset: u32) -> u32 {
155+
const QSHARP_MAGIC: &str = "//qsharp";
155156
let source = compilation
156157
.user_unit()
157158
.sources
158159
.find_by_offset(package_offset)
159160
.expect("source should exist in the user source map");
160161

161-
let first = source
162-
.contents
163-
.find(|c: char| !c.is_whitespace())
164-
.unwrap_or(source.contents.len());
162+
// Skip the //qsharp magic if it exists (notebook cells)
163+
let start = if let Some(qsharp_magic_start) = source.contents.find(QSHARP_MAGIC) {
164+
qsharp_magic_start + QSHARP_MAGIC.len()
165+
} else {
166+
0
167+
};
168+
169+
let source_after_magic = &source.contents[start..];
170+
171+
let first = start
172+
+ source_after_magic
173+
.find(|c: char| !c.is_whitespace())
174+
.unwrap_or(source_after_magic.len());
165175

166176
let first = u32::try_from(first).expect("source length should fit into u32");
167177

language_service/src/completion/tests.rs

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,8 +1038,20 @@ fn notebook_block() {
10381038
fn notebook_auto_open_start_of_cell_empty() {
10391039
check_notebook(
10401040
&[
1041-
("cell1", "namespace Foo { operation Bar() : Unit {} }"),
1042-
("cell2", "↘"),
1041+
(
1042+
"cell1",
1043+
indoc! {"
1044+
//qsharp
1045+
namespace Foo { operation Bar() : Unit {} }"
1046+
},
1047+
),
1048+
(
1049+
"cell2",
1050+
indoc! {"
1051+
//qsharp
1052+
↘"
1053+
},
1054+
),
10431055
],
10441056
&["Fake"],
10451057
&expect![[r#"
@@ -1060,11 +1072,11 @@ fn notebook_auto_open_start_of_cell_empty() {
10601072
new_text: "open FakeStdLib;\n",
10611073
range: Range {
10621074
start: Position {
1063-
line: 0,
1075+
line: 1,
10641076
column: 0,
10651077
},
10661078
end: Position {
1067-
line: 0,
1079+
line: 1,
10681080
column: 0,
10691081
},
10701082
},
@@ -1082,8 +1094,21 @@ fn notebook_auto_open_start_of_cell_empty() {
10821094
fn notebook_auto_open_start_of_cell() {
10831095
check_notebook(
10841096
&[
1085-
("cell1", "namespace Foo { operation Bar() : Unit {} }"),
1086-
("cell2", r#" Message("hi") ↘"#),
1097+
(
1098+
"cell1",
1099+
indoc! {"
1100+
//qsharp
1101+
namespace Foo { operation Bar() : Unit {} }"
1102+
},
1103+
),
1104+
(
1105+
"cell2",
1106+
indoc! {r#"
1107+
//qsharp
1108+
Message("hi")
1109+
↘"#
1110+
},
1111+
),
10871112
],
10881113
&["Fake"],
10891114
&expect![[r#"
@@ -1101,15 +1126,15 @@ fn notebook_auto_open_start_of_cell() {
11011126
additional_text_edits: Some(
11021127
[
11031128
TextEdit {
1104-
new_text: "open FakeStdLib;\n ",
1129+
new_text: "open FakeStdLib;\n",
11051130
range: Range {
11061131
start: Position {
1107-
line: 0,
1108-
column: 3,
1132+
line: 1,
1133+
column: 0,
11091134
},
11101135
end: Position {
1111-
line: 0,
1112-
column: 3,
1136+
line: 1,
1137+
column: 0,
11131138
},
11141139
},
11151140
},

0 commit comments

Comments
 (0)