-
Notifications
You must be signed in to change notification settings - Fork 545
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
LS: Add a command to inspect analyzed crates
commit-id:26737288
- Loading branch information
Showing
16 changed files
with
289 additions
and
6 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
40 changes: 40 additions & 0 deletions
40
crates/cairo-lang-language-server/src/lang/inspect/crates.rs
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,40 @@ | ||
use cairo_lang_filesystem::db::FilesGroup; | ||
use indent::indent_by; | ||
use indoc::formatdoc; | ||
use itertools::Itertools; | ||
|
||
use crate::lang::db::AnalysisDatabase; | ||
use crate::project::Crate; | ||
|
||
/// Generates a Markdown text describing all crates in the database. | ||
pub fn inspect_analyzed_crates(db: &AnalysisDatabase) -> String { | ||
let list = db | ||
.crates() | ||
.into_iter() | ||
.flat_map(|crate_id| Crate::reconstruct(db, crate_id)) | ||
.sorted_by_key(|cr| cr.name.clone()) | ||
.map(inspect_crate) | ||
.collect::<Vec<_>>() | ||
.join(""); | ||
formatdoc! {r#" | ||
# Analyzed Crates | ||
{list} | ||
"#} | ||
} | ||
|
||
/// Generates a Markdown fragment describing a single crate. | ||
fn inspect_crate(cr: Crate) -> String { | ||
formatdoc! { | ||
r#" | ||
- `{name}`: `{source_path}` | ||
```rust | ||
{settings} | ||
``` | ||
"#, | ||
name = cr.name, | ||
source_path = cr.source_path().display(), | ||
settings = indent_by(4, format!("{:#?}", cr.settings)), | ||
} | ||
} |
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,3 +1,4 @@ | ||
//! High-level constructs for inspecting language elements from the Salsa database. | ||
//! High-level constructs for inspecting language elements from the analysis database. | ||
pub mod crates; | ||
pub mod defs; |
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,13 @@ | ||
//! CairoLS extensions to the Language Server Protocol. | ||
use tower_lsp::lsp_types::request::Request; | ||
|
||
// TODO(mkaput): Provide this as a command in VSCode. | ||
/// Collect information about all Cairo crates that are currently being analyzed. | ||
pub struct ViewAnalyzedCrates; | ||
|
||
impl Request for ViewAnalyzedCrates { | ||
type Params = (); | ||
type Result = String; | ||
const METHOD: &'static str = "cairo/viewAnalyzedCrates"; | ||
} |
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 +1,2 @@ | ||
pub mod client_capabilities; | ||
pub(crate) mod client_capabilities; | ||
pub mod ext; |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub use self::crate_data::Crate; | ||
pub use self::project_manifest_path::*; | ||
|
||
mod crate_data; | ||
|
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,38 @@ | ||
use cairo_lang_language_server::lsp; | ||
use cairo_lang_test_utils::parse_test_file::TestRunnerResult; | ||
use cairo_lang_utils::ordered_hash_map::OrderedHashMap; | ||
|
||
use crate::support::normalize::normalize; | ||
use crate::support::sandbox; | ||
|
||
cairo_lang_test_utils::test_file_test!( | ||
project, | ||
"tests/test_data/analysis/crates", | ||
{ | ||
cairo_projects: "cairo_projects.txt", | ||
}, | ||
test_analyzed_crates | ||
); | ||
|
||
fn test_analyzed_crates( | ||
inputs: &OrderedHashMap<String, String>, | ||
_args: &OrderedHashMap<String, String>, | ||
) -> TestRunnerResult { | ||
let dyn_files = inputs.iter().flat_map(|(p, c)| Some((p.strip_prefix("file: ")?, c))); | ||
|
||
let mut ls = sandbox! { | ||
dyn_files(dyn_files) | ||
}; | ||
|
||
for path_to_open in inputs["open files in order"].lines() { | ||
ls.open_and_wait_for_diagnostics(path_to_open); | ||
} | ||
|
||
let output = ls.send_request::<lsp::ext::ViewAnalyzedCrates>(()); | ||
let output = normalize(&ls, output); | ||
|
||
TestRunnerResult::success(OrderedHashMap::from([( | ||
"expected analyzed crates".to_owned(), | ||
output, | ||
)])) | ||
} |
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,3 +1,4 @@ | ||
mod analysis; | ||
mod hover; | ||
mod semantic_tokens; | ||
mod support; | ||
|
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
36 changes: 36 additions & 0 deletions
36
crates/cairo-lang-language-server/tests/e2e/support/normalize.rs
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,36 @@ | ||
use std::path::Path; | ||
|
||
use crate::support::fixture::Fixture; | ||
|
||
/// Performs various normalization steps of the input data, to remove any runtime-specific artifacts | ||
/// and make comparisons in test assertions deterministic. | ||
pub fn normalize(fixture: impl AsRef<Fixture>, data: impl ToString) -> String { | ||
let fixture = fixture.as_ref(); | ||
normalize_well_known_paths(fixture, normalize_paths(data.to_string())) | ||
} | ||
|
||
/// Replace all well-known paths/urls for a fixture with placeholders. | ||
fn normalize_well_known_paths(fixture: &Fixture, data: String) -> String { | ||
let mut data = data | ||
.replace(&fixture.root_url().to_string(), "[ROOT_URL]") | ||
.replace(&normalize_path(fixture.root_path()), "[ROOT]"); | ||
|
||
if let Ok(pwd) = std::env::current_dir() { | ||
data = data.replace(&normalize_path(&pwd), "[PWD]"); | ||
} | ||
|
||
let cairo_source = Path::new(env!("CARGO_MANIFEST_DIR")).parent().unwrap().parent().unwrap(); | ||
data = data.replace(&normalize_path(cairo_source), "[CAIRO_SOURCE]"); | ||
|
||
data | ||
} | ||
|
||
/// Normalizes path separators. | ||
fn normalize_paths(data: String) -> String { | ||
data.replace('\\', "/") | ||
} | ||
|
||
/// Normalize a path to a consistent format. | ||
fn normalize_path(path: &Path) -> String { | ||
normalize_paths(path.to_string_lossy().to_string()) | ||
} |
Oops, something went wrong.