Skip to content
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
wants to merge 1 commit into
base: spr/main/c0e7d4d8
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)>,
Copy link
Member

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?)


/// 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>
Loading