-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Show code lens on entrypoints to invoke commands (#1142)
This adds "Run | Histogram | Estimate | Debug" commands above the entrypoint in source files. ![image](https://github.com/microsoft/qsharp/assets/16928427/6c07576f-07ce-4f3a-9134-3212caf06327)
- Loading branch information
1 parent
32d0405
commit 7766915
Showing
12 changed files
with
459 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
#[cfg(test)] | ||
mod tests; | ||
|
||
use crate::{ | ||
compilation::{Compilation, CompilationKind}, | ||
protocol::{CodeLens, CodeLensCommand}, | ||
qsc_utils::{into_range, span_contains}, | ||
}; | ||
use qsc::{ | ||
hir::{Attr, ItemKind}, | ||
line_column::Encoding, | ||
}; | ||
|
||
pub(crate) fn get_code_lenses( | ||
compilation: &Compilation, | ||
source_name: &str, | ||
position_encoding: Encoding, | ||
) -> Vec<CodeLens> { | ||
if matches!(compilation.kind, CompilationKind::Notebook) { | ||
// entrypoint actions don't work in notebooks | ||
return vec![]; | ||
} | ||
|
||
let user_unit = compilation.user_unit(); | ||
let source_span = compilation.package_span_of_source(source_name); | ||
|
||
// Get callables in the current source file with the @EntryPoint() attribute. | ||
// If there is more than one entrypoint, not our problem, we'll go ahead | ||
// and return code lenses for all. The duplicate entrypoint diagnostic | ||
// will be reported from elsewhere. | ||
let entry_point_decls = user_unit.package.items.values().filter_map(|item| { | ||
if span_contains(source_span, item.span.lo) { | ||
if let ItemKind::Callable(decl) = &item.kind { | ||
if item.attrs.iter().any(|a| a == &Attr::EntryPoint) { | ||
return Some(decl); | ||
} | ||
} | ||
} | ||
None | ||
}); | ||
|
||
entry_point_decls | ||
.flat_map(|decl| { | ||
let range = into_range(position_encoding, decl.span, &user_unit.sources); | ||
|
||
[ | ||
CodeLens { | ||
range, | ||
command: CodeLensCommand::Run, | ||
}, | ||
CodeLens { | ||
range, | ||
command: CodeLensCommand::Histogram, | ||
}, | ||
CodeLens { | ||
range, | ||
command: CodeLensCommand::Estimate, | ||
}, | ||
CodeLens { | ||
range, | ||
command: CodeLensCommand::Debug, | ||
}, | ||
] | ||
}) | ||
.collect() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
#![allow(clippy::needless_raw_string_hashes)] | ||
|
||
use super::get_code_lenses; | ||
use crate::{ | ||
test_utils::{ | ||
compile_notebook_with_fake_stdlib, compile_with_fake_stdlib_and_markers_no_cursor, | ||
}, | ||
Encoding, | ||
}; | ||
use expect_test::{expect, Expect}; | ||
|
||
fn check(source_with_markers: &str, expect: &Expect) { | ||
let (compilation, expected_code_lens_ranges) = | ||
compile_with_fake_stdlib_and_markers_no_cursor(source_with_markers); | ||
let actual_code_lenses = get_code_lenses(&compilation, "<source>", Encoding::Utf8); | ||
|
||
for expected_range in &expected_code_lens_ranges { | ||
assert!( | ||
actual_code_lenses | ||
.iter() | ||
.any(|cl| cl.range == *expected_range), | ||
"expected range not found in actual code lenses: {expected_range:?}" | ||
); | ||
} | ||
|
||
for actual_range in actual_code_lenses.iter().map(|cl| cl.range) { | ||
assert!( | ||
expected_code_lens_ranges.iter().any(|r| r == &actual_range), | ||
"got code lens for unexpected range: {actual_range:?}" | ||
); | ||
} | ||
|
||
let actual = expected_code_lens_ranges | ||
.iter() | ||
.enumerate() | ||
.map(|(i, r)| { | ||
( | ||
i, | ||
actual_code_lenses | ||
.iter() | ||
.filter(|cl| cl.range == *r) | ||
.map(|cl| cl.command) | ||
.collect::<Vec<_>>(), | ||
) | ||
}) | ||
.collect::<Vec<_>>(); | ||
expect.assert_debug_eq(&actual); | ||
} | ||
|
||
#[test] | ||
fn one_entrypoint() { | ||
check( | ||
r#" | ||
namespace Test { | ||
@EntryPoint() | ||
◉operation Main() : Unit{ | ||
}◉ | ||
}"#, | ||
&expect![[r#" | ||
[ | ||
( | ||
0, | ||
[ | ||
Run, | ||
Histogram, | ||
Estimate, | ||
Debug, | ||
], | ||
), | ||
] | ||
"#]], | ||
); | ||
} | ||
|
||
#[test] | ||
fn two_entrypoints() { | ||
check( | ||
r#" | ||
namespace Test { | ||
@EntryPoint() | ||
◉operation Main() : Unit{ | ||
}◉ | ||
@EntryPoint() | ||
◉operation Foo() : Unit{ | ||
}◉ | ||
}"#, | ||
&expect![[r#" | ||
[ | ||
( | ||
0, | ||
[ | ||
Run, | ||
Histogram, | ||
Estimate, | ||
Debug, | ||
], | ||
), | ||
( | ||
1, | ||
[ | ||
Run, | ||
Histogram, | ||
Estimate, | ||
Debug, | ||
], | ||
), | ||
] | ||
"#]], | ||
); | ||
} | ||
|
||
#[test] | ||
fn no_entrypoint_code_lens_in_notebook() { | ||
let compilation = compile_notebook_with_fake_stdlib( | ||
[( | ||
"cell1", | ||
"@EntryPoint() | ||
operation Main() : Unit {}", | ||
)] | ||
.into_iter(), | ||
); | ||
|
||
let lenses = get_code_lenses(&compilation, "cell1", Encoding::Utf8); | ||
assert!( | ||
lenses.is_empty(), | ||
"entrypoint code lenses should not be present in notebooks" | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.