Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only Display Summary on Hover #449

Merged
merged 19 commits into from
Jul 14, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
25 changes: 19 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions language_service/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ log = { workspace = true }
miette = { workspace = true }
qsc = { path = "../compiler/qsc" }
enum-iterator = { workspace = true }
regex = "1.9.1"
ScottCarda-MS marked this conversation as resolved.
Show resolved Hide resolved
22 changes: 20 additions & 2 deletions language_service/src/hover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::display::CodeDisplay;
use crate::qsc_utils::{find_item, map_offset, span_contains, Compilation};
use qsc::ast::visit::{walk_callable_decl, walk_expr, walk_pat, walk_ty_def, Visitor};
use qsc::{ast, hir, resolve};
use regex::Regex;
use std::fmt::Display;
use std::rc::Rc;

Expand Down Expand Up @@ -188,18 +189,35 @@ impl Visitor<'_> for HoverVisitor<'_> {
}

fn markdown_with_doc(doc: &Rc<str>, code: impl Display) -> String {
if doc.is_empty() {
let parsed_doc = parse_doc(doc);
if parsed_doc.is_empty() {
markdown_fenced_block(code)
} else {
format!(
"{}
{}",
doc,
parsed_doc,
markdown_fenced_block(code)
)
}
}

fn parse_doc(doc: &str) -> &str {
let summary_re =
ScottCarda-MS marked this conversation as resolved.
Show resolved Hide resolved
Regex::new(r"(^|(\r?\n))\s*#\s*((S|s)ummary+)[\s\r\n]*").expect("Invalid regex");
let header_re = Regex::new(r"\r?\n\s*#\s*(\w+)[\s\n\r]*").expect("Invalid regex");
match summary_re.find(doc) {
Some(summary_header) => {
let start = summary_header.end();
match header_re.find(&doc[start..]) {
Some(next_header) => &doc[start..(start + next_header.start())],
ScottCarda-MS marked this conversation as resolved.
Show resolved Hide resolved
None => &doc[start..],
}
}
None => doc,
}
}

fn markdown_fenced_block(code: impl Display) -> String {
format!(
"```qsharp
Expand Down
138 changes: 136 additions & 2 deletions language_service/src/hover/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,13 @@ fn hover_callable_unit_types() {
check(
indoc! {r#"
namespace Test {
/// Doc comment!
/// Doc comment
/// with multiple lines!
operation ◉B↘ar◉() : Unit {}
}
"#},
&expect![[r#"
"Doc comment!\n```qsharp\noperation Bar Unit => Unit\n```\n"
"Doc comment\nwith multiple lines!\n```qsharp\noperation Bar Unit => Unit\n```\n"
"#]],
);
}
Expand Down Expand Up @@ -516,3 +517,136 @@ fn hover_foreign_call_functors() {
"#]],
);
}

#[test]
fn hover_callable_summary() {
check(
indoc! {r#"
namespace Test {
/// # Summary
/// This is a
/// multi-line summary!
operation ◉F↘oo◉() : Unit {}
}
"#},
&expect![[r#"
"This is a\nmulti-line summary!\n```qsharp\noperation Foo Unit => Unit\n```\n"
"#]],
);
}

#[test]
fn hover_callable_summary_stuff_before() {
check(
indoc! {r#"
namespace Test {
/// not the summary
/// # Summary
/// This is a
/// multi-line summary!
operation ◉F↘oo◉() : Unit {}
}
"#},
&expect![[r#"
"This is a\nmulti-line summary!\n```qsharp\noperation Foo Unit => Unit\n```\n"
"#]],
);
}

#[test]
fn hover_callable_summary_other_header_before() {
check(
indoc! {r#"
namespace Test {
/// # Not The Summary
/// This stuff is not the summary.
ScottCarda-MS marked this conversation as resolved.
Show resolved Hide resolved
/// # Summary
/// This is a
/// multi-line summary!
operation ◉F↘oo◉() : Unit {}
}
"#},
&expect![[r#"
"This is a\nmulti-line summary!\n```qsharp\noperation Foo Unit => Unit\n```\n"
"#]],
);
}

#[test]
fn hover_callable_summary_other_header_after() {
check(
indoc! {r#"
namespace Test {
/// # Summary
/// This is a
/// multi-line summary!
/// # Not The Summary
/// This stuff is not the summary.
operation ◉F↘oo◉() : Unit {}
}
"#},
&expect![[r#"
"This is a\nmulti-line summary!\n```qsharp\noperation Foo Unit => Unit\n```\n"
"#]],
);
}

#[test]
fn hover_callable_summary_other_headers() {
check(
indoc! {r#"
namespace Test {
/// # Not The Summary
/// This stuff is not the summary.
/// # Summary
/// This is a
/// multi-line summary!
/// # Also Not The Summary
/// This stuff is also not the summary.
operation ◉F↘oo◉() : Unit {}
}
"#},
&expect![[r#"
"This is a\nmulti-line summary!\n```qsharp\noperation Foo Unit => Unit\n```\n"
"#]],
);
}

#[test]
fn hover_callable_headers_but_no_summary() {
check(
indoc! {r#"
namespace Test {
/// # Not The Summary
/// This stuff is not the summary.
/// # Also Not The Summary
/// This stuff is also not the summary.
operation ◉F↘oo◉() : Unit {}
}
"#},
&expect![[r##"
"# Not The Summary\nThis stuff is not the summary.\n# Also Not The Summary\nThis stuff is also not the summary.\n```qsharp\noperation Foo Unit => Unit\n```\n"
"##]],
);
}

#[test]
fn hover_callable_summary_only_header_matches() {
check(
indoc! {r#"
namespace Test {
/// # Not The Summary
/// This stuff is not the # Summary.
/// # Summary
/// This is a
/// multi-line # Summary!
/// # Also Not The Summary
/// This stuff is also not the # Summary.
operation ◉F↘oo◉() : Unit {}
}
"#},
&expect![[r#"
"This is a\nmulti-line # Summary!\n```qsharp\noperation Foo Unit => Unit\n```\n"
"#]],
);
}