-
Notifications
You must be signed in to change notification settings - Fork 546
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
LS: add support for struct members (#6145)
- Loading branch information
1 parent
f28ab51
commit aa3865e
Showing
12 changed files
with
365 additions
and
28 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,38 @@ | ||
use cairo_lang_defs::db::DefsGroup; | ||
use cairo_lang_defs::diagnostic_utils::StableLocation; | ||
use cairo_lang_defs::ids::{LanguageElementId, LookupItemId, MemberId, VariantId}; | ||
|
||
/// Item which documentation can be fetched from source code. | ||
#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)] | ||
pub enum DocumentableItemId { | ||
LookupItem(LookupItemId), | ||
Member(MemberId), | ||
Variant(VariantId), | ||
} | ||
|
||
impl DocumentableItemId { | ||
pub fn stable_location(&self, db: &dyn DefsGroup) -> StableLocation { | ||
match self { | ||
DocumentableItemId::LookupItem(lookup_item_id) => lookup_item_id.stable_location(db), | ||
DocumentableItemId::Member(member_id) => member_id.stable_location(db), | ||
DocumentableItemId::Variant(variant_id) => variant_id.stable_location(db), | ||
} | ||
} | ||
} | ||
|
||
impl From<LookupItemId> for DocumentableItemId { | ||
fn from(value: LookupItemId) -> Self { | ||
DocumentableItemId::LookupItem(value) | ||
} | ||
} | ||
|
||
impl From<MemberId> for DocumentableItemId { | ||
fn from(value: MemberId) -> Self { | ||
DocumentableItemId::Member(value) | ||
} | ||
} | ||
impl From<VariantId> for DocumentableItemId { | ||
fn from(value: VariantId) -> Self { | ||
DocumentableItemId::Variant(value) | ||
} | ||
} |
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,2 +1,3 @@ | ||
pub mod db; | ||
pub mod documentable_item; | ||
mod markdown; |
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
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,86 @@ | ||
use cairo_lang_test_utils::parse_test_file::TestRunnerResult; | ||
use cairo_lang_utils::ordered_hash_map::OrderedHashMap; | ||
use tower_lsp::lsp_types::{ | ||
lsp_request, ClientCapabilities, GotoCapability, GotoDefinitionParams, GotoDefinitionResponse, | ||
TextDocumentClientCapabilities, TextDocumentIdentifier, TextDocumentPositionParams, | ||
}; | ||
|
||
use crate::support::cursor::{peek_caret, peek_selection}; | ||
use crate::support::{cursors, sandbox}; | ||
|
||
cairo_lang_test_utils::test_file_test!( | ||
goto, | ||
"tests/test_data/goto", | ||
{ | ||
struct_members: "struct_members.txt", | ||
}, | ||
test_goto_members | ||
); | ||
|
||
fn caps(base: ClientCapabilities) -> ClientCapabilities { | ||
ClientCapabilities { | ||
text_document: base.text_document.or_else(Default::default).map(|it| { | ||
TextDocumentClientCapabilities { | ||
definition: Some(GotoCapability { | ||
dynamic_registration: Some(false), | ||
link_support: None, | ||
}), | ||
..it | ||
} | ||
}), | ||
..base | ||
} | ||
} | ||
|
||
/// Perform hover test. | ||
/// | ||
/// This function spawns a sandbox language server with the given code in the `src/lib.cairo` file. | ||
/// The Cairo source code is expected to contain caret markers. | ||
/// The function then requests goto definition information at each caret position and compares | ||
/// the result with the expected hover information from the snapshot file. | ||
fn test_goto_members( | ||
inputs: &OrderedHashMap<String, String>, | ||
_args: &OrderedHashMap<String, String>, | ||
) -> TestRunnerResult { | ||
let (cairo, cursors) = cursors(&inputs["cairo_code"]); | ||
|
||
let mut ls = sandbox! { | ||
files { | ||
"cairo_project.toml" => inputs["cairo_project.toml"].clone(), | ||
"src/lib.cairo" => cairo.clone(), | ||
} | ||
client_capabilities = caps; | ||
}; | ||
ls.open_and_wait_for_diagnostics("src/lib.cairo"); | ||
|
||
let mut goto_definitions = OrderedHashMap::default(); | ||
|
||
for (n, position) in cursors.carets().into_iter().enumerate() { | ||
let mut report = String::new(); | ||
|
||
report.push_str(&peek_caret(&cairo, position)); | ||
let code_action_params = GotoDefinitionParams { | ||
text_document_position_params: TextDocumentPositionParams { | ||
text_document: TextDocumentIdentifier { uri: ls.doc_id("src/lib.cairo").uri }, | ||
position, | ||
}, | ||
work_done_progress_params: Default::default(), | ||
partial_result_params: Default::default(), | ||
}; | ||
let goto_definition_response = | ||
ls.send_request::<lsp_request!("textDocument/definition")>(code_action_params); | ||
|
||
if let Some(goto_definition_response) = goto_definition_response { | ||
if let GotoDefinitionResponse::Scalar(location) = goto_definition_response { | ||
report.push_str(&peek_selection(&cairo, &location.range)); | ||
} else { | ||
panic!("Unexpected GotoDefinitionResponse variant.") | ||
} | ||
} else { | ||
panic!("Goto definition request failed."); | ||
} | ||
goto_definitions.insert(format!("Goto definition #{}", n), report); | ||
} | ||
|
||
TestRunnerResult::success(goto_definitions) | ||
} |
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,6 +1,7 @@ | ||
mod analysis; | ||
mod code_actions; | ||
mod completions; | ||
mod goto; | ||
mod hover; | ||
mod semantic_tokens; | ||
mod support; | ||
|
Oops, something went wrong.