-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix runnables code lens in multi-file sway projects (#4984)
## Description Closes #4983 The issue was that we were returning all of the runnables for the workspace, rather than just the file. We were also calling `get_range_from_span` for every runnable at request time, which @JoshuaBatty discovered is very costly. - The runnables map is now mapping [file path] => [vector of runnables for that file] - Rather than iterating over all of the runnables in the session, we just fetch the vector of runnables indexed by the file path. - Runnables are now storing the range rather than the span, so we don't need to convert it at request time. - Added another test that expects _no_ runnables to be returned for a file in a workspace that has runnables in other files. This test was failing before the change. Previously, the runnables test was running on test files from the e2e directory, so I copied that into the LSP fixtures. - Moved the code_lens logic into a capability and updated the bencher ## Checklist - [x] I have linked to any relevant issues. - [ ] I have commented my code, particularly in hard-to-understand areas. - [ ] I have updated the documentation where relevant (API docs, the reference, and the Sway book). - [ ] I have added tests that prove my fix is effective or that my feature works. - [ ] I have added (or requested a maintainer to add) the necessary `Breaking*` or `New Feature` labels where relevant. - [ ] I have done my best to ensure that my PR adheres to [the Fuel Labs Code Review Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md). - [ ] I have requested a review from the relevant team or maintainers.
- Loading branch information
Showing
12 changed files
with
119 additions
and
60 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
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,27 @@ | ||
use std::{path::PathBuf, sync::Arc}; | ||
|
||
use lsp_types::{CodeLens, Url}; | ||
|
||
use crate::core::session::Session; | ||
|
||
pub fn code_lens(session: &Arc<Session>, url: &Url) -> Vec<CodeLens> { | ||
let url_path = PathBuf::from(url.path()); | ||
|
||
// Construct code lenses for runnable functions | ||
let runnables_for_path = session.runnables.get(&url_path); | ||
let mut result: Vec<CodeLens> = runnables_for_path | ||
.map(|runnables| { | ||
runnables | ||
.iter() | ||
.map(|runnable| CodeLens { | ||
range: *runnable.range(), | ||
command: Some(runnable.command()), | ||
data: None, | ||
}) | ||
.collect() | ||
}) | ||
.unwrap_or_default(); | ||
// Sort the results | ||
result.sort_by(|a, b| a.range.start.line.cmp(&b.range.start.line)); | ||
result | ||
} |
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
pub mod code_actions; | ||
pub mod code_lens; | ||
pub mod completion; | ||
pub mod diagnostic; | ||
pub mod document_symbol; | ||
|
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
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,8 @@ | ||
[project] | ||
authors = ["Fuel Labs <[email protected]>"] | ||
entry = "main.sw" | ||
license = "Apache-2.0" | ||
name = "script_multi_test" | ||
|
||
[dependencies] | ||
std = { path = "../../../../sway-lib-std" } |
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,16 @@ | ||
script; | ||
|
||
fn main() { | ||
revert(0); | ||
} | ||
|
||
#[test] | ||
fn test_foo() { | ||
assert(true); | ||
} | ||
|
||
#[test] | ||
fn test_bar() { | ||
log("test"); | ||
assert(4 / 2 == 2); | ||
} |
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 @@ | ||
library; |
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