Skip to content
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
65 changes: 65 additions & 0 deletions compiler/qsc_formatter/src/formatter/tests.rs
Comment thread
ScottCarda-MS marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ fn check(input: &str, expect: &Expect) {
expect.assert_eq(&actual);
}

fn check_edits(input: &str, expect: &Expect) {
let actual = super::calculate_format_edits(input);
expect.assert_debug_eq(&actual);
}

// Removing trailing whitespace from lines

#[test]
Expand Down Expand Up @@ -936,6 +941,66 @@ fn preserve_comments_at_start_of_file() {
assert!(super::calculate_format_edits(input).is_empty());
}

#[test]
fn format_with_crlf() {
let content = indoc! {"//qsharp\r\n\r\noperation Foo() : Unit {\r\n\r\n}\r\n"};
check_edits(
content,
&expect![[r#"
[
TextEdit {
new_text: "",
span: Span {
lo: 36,
hi: 40,
},
},
]
"#]],
);
check(
content,
&expect![["//qsharp\r\n\r\noperation Foo() : Unit {}\r\n"]],
);
}

#[test]
fn format_does_not_edit_magic_comment() {
let content = indoc! {"\r\n\r\n //qsharp \r\n\r\noperation Foo() : Unit {\r\n\r\n}\r\n"};
check_edits(
content,
&expect![[r#"
[
TextEdit {
new_text: "",
span: Span {
lo: 0,
hi: 8,
},
},
TextEdit {
new_text: "//qsharp",
span: Span {
lo: 8,
hi: 20,
},
},
TextEdit {
new_text: "",
span: Span {
lo: 48,
hi: 52,
},
},
]
"#]],
);
check(
content,
&expect![["//qsharp\r\n\r\noperation Foo() : Unit {}\r\n"]],
);
}

#[test]
fn sample_has_no_formatting_changes() {
let input = indoc! {r#"
Expand Down
2 changes: 1 addition & 1 deletion compiler/qsc_parse/src/lex/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ impl<'a> Lexer<'a> {
CommentKind::Normal
};

self.eat_while(|c| c != '\n');
self.eat_while(|c| c != '\n' && c != '\r');
Some(kind)
} else {
None
Expand Down
25 changes: 16 additions & 9 deletions vscode/src/notebook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export function registerQSharpNotebookHandlers() {
const openQSharpNotebooks = new Set<string>();

/**
* Returns the end position of the `%%qsharp` cell magic, or `undefined`
* Returns the range of the `%%qsharp` cell magic, or `undefined`
* if it does not exist.
*/
function findQSharpCellMagic(document: vscode.TextDocument) {
Expand All @@ -115,9 +115,12 @@ function findQSharpCellMagic(document: vscode.TextDocument) {
qsharpCellMagic,
line.firstNonWhitespaceCharacterIndex,
)
? new vscode.Position(
i,
line.firstNonWhitespaceCharacterIndex + qsharpCellMagic.length,
? new vscode.Range(
new vscode.Position(i, line.firstNonWhitespaceCharacterIndex),
new vscode.Position(
i,
line.firstNonWhitespaceCharacterIndex + qsharpCellMagic.length,
),
)
: undefined;
}
Expand Down Expand Up @@ -194,15 +197,19 @@ export function registerQSharpNotebookCellUpdateHandlers(
}

function getQSharpText(document: vscode.TextDocument) {
const magicPosition = findQSharpCellMagic(document);
if (magicPosition) {
const magicOffset = document.offsetAt(magicPosition);
const magicRange = findQSharpCellMagic(document);
if (magicRange) {
const magicStartOffset = document.offsetAt(magicRange.start);
const magicEndOffset = document.offsetAt(magicRange.end);
// Erase the %%qsharp magic line if it's there.
// Replace it with whitespace so that document offsets remain the same.
// Replace it with a comment so that document offsets remain the same.
// This will save us from having to map offsets later when
// communicating with the language service.
const text = document.getText();
return (
"".padStart(magicOffset) + document.getText().substring(magicOffset)
text.substring(0, magicStartOffset) +
"//qsharp" +
text.substring(magicEndOffset)
);
} else {
// No %%qsharp magic. This can happen if the user manually sets the
Expand Down