-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Collect declarations in
textDocument/references
implementation
commit-id:67cec2e0
- Loading branch information
Showing
4 changed files
with
78 additions
and
8 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 |
---|---|---|
@@ -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