Skip to content

Commit

Permalink
Collect declarations in textDocument/references implementation
Browse files Browse the repository at this point in the history
commit-id:67cec2e0
  • Loading branch information
mkaput committed Dec 20, 2024
1 parent 8a54322 commit 4b1f657
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 8 deletions.
39 changes: 35 additions & 4 deletions src/ide/navigation/references.rs
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(&params.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)
}
1 change: 1 addition & 0 deletions src/lang/inspect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
pub mod crates;
pub mod defs;
pub mod methods;
pub mod refs;
36 changes: 36 additions & 0 deletions src/lang/inspect/refs.rs
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(),
})
}
10 changes: 6 additions & 4 deletions tests/test_data/references/fns.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ fn main() {

//! > References #0
fn pow<caret>2(x: felt252) -> felt252 { x * x }
none response
---

//! > References #1
let x = po<caret>w2(2) + pow2(3);
none response
---

//! > ==========================================================================

Expand All @@ -48,8 +48,10 @@ fn main() {

//! > References #0
fn pow<caret>2(x: felt252) -> felt252 { x * x }
none response
---
<sel>fn pow2(x: felt252) -> felt252 { x * x }</sel>

//! > References #1
let x = po<caret>w2(2) + pow2(3);
none response
---
<sel>fn pow2(x: felt252) -> felt252 { x * x }</sel>

0 comments on commit 4b1f657

Please sign in to comment.