-
Notifications
You must be signed in to change notification settings - Fork 0
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
Collect declarations in textDocument/references
implementation
#123
Open
mkaput
wants to merge
1
commit into
spr/main/c0e7d4d8
Choose a base branch
from
spr/main/67cec2e0
base: spr/main/c0e7d4d8
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+78
−8
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -1,8 +1,39 @@ | ||
use cairo_lang_utils::Upcast; | ||
use itertools::Itertools; | ||
use lsp_types::{Location, ReferenceParams}; | ||
|
||
use crate::lang::db::AnalysisDatabase; | ||
use crate::lang::db::{AnalysisDatabase, LsSyntaxGroup}; | ||
use crate::lang::inspect::refs::find_all_references; | ||
use crate::lang::lsp::{LsProtoGroup, ToCairo, ToLsp}; | ||
|
||
pub fn references(_params: ReferenceParams, _db: &AnalysisDatabase) -> Option<Vec<Location>> { | ||
// TODO(mkaput): Implement this. | ||
None | ||
pub fn references(params: ReferenceParams, db: &AnalysisDatabase) -> Option<Vec<Location>> { | ||
let include_declaration = params.context.include_declaration; | ||
|
||
let file = db.file_for_url(¶ms.text_document_position.text_document.uri)?; | ||
let position = params.text_document_position.position.to_cairo(); | ||
|
||
let identifier = db.find_identifier_at_position(file, position)?; | ||
|
||
let found_references = find_all_references(db, identifier)?; | ||
|
||
let locations = { | ||
let declaration = found_references.declaration.filter(|_| include_declaration); | ||
|
||
let references = found_references | ||
.usages | ||
.into_iter() | ||
.flat_map(|(file, spans)| spans.into_iter().map(move |span| (file, span))); | ||
|
||
declaration.into_iter().chain(references) | ||
} | ||
.unique() | ||
.filter_map(|(file, span)| { | ||
let found_uri = db.url_for_file(file)?; | ||
let range = span.position_in_file(db.upcast(), file)?.to_lsp(); | ||
let location = Location { uri: found_uri, range }; | ||
Some(location) | ||
}) | ||
.collect(); | ||
|
||
Some(locations) | ||
} |
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 |
---|---|---|
|
@@ -3,3 +3,4 @@ | |
pub mod crates; | ||
pub mod defs; | ||
pub mod methods; | ||
pub mod refs; |
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::collections::HashMap; | ||
|
||
use cairo_lang_filesystem::ids::FileId; | ||
use cairo_lang_filesystem::span::TextSpan; | ||
use cairo_lang_syntax::node::ast::TerminalIdentifier; | ||
|
||
use crate::lang::db::AnalysisDatabase; | ||
use crate::lang::inspect::defs::SymbolDef; | ||
|
||
pub struct FoundReferences { | ||
/// Location where searched symbol is declared. | ||
/// | ||
/// This can rarely be `None`, for example, for macros. | ||
pub declaration: Option<(FileId, TextSpan)>, | ||
|
||
/// Locations where searched symbol is used. | ||
pub usages: HashMap<FileId, Vec<TextSpan>>, | ||
} | ||
|
||
/// Finds all places in the entire analysed codebase for usages of the given identifier. | ||
pub fn find_all_references( | ||
db: &AnalysisDatabase, | ||
identifier: TerminalIdentifier, | ||
) -> Option<FoundReferences> { | ||
let symbol = SymbolDef::find(db, &identifier)?; | ||
|
||
// TODO(mkaput): Think about how to deal with `mod foo;` vs `mod foo { ... }`. | ||
// For all cases we cover here, definition == declaration. | ||
let declaration = symbol.definition_location(db); | ||
|
||
Some(FoundReferences { | ||
declaration, | ||
// TODO(mkaput): Implement this. | ||
usages: Default::default(), | ||
}) | ||
} |
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this can also be none if you're using it by clicking the declaration (it's the same request, am i right?)