Skip to content

Feature: Add a memory layout viewer #15081

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

Merged
merged 9 commits into from
Jul 9, 2023
Merged
18 changes: 17 additions & 1 deletion crates/ide-db/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ use syntax::{
AstToken, SyntaxKind, SyntaxToken, TokenAtOffset,
};

use crate::{defs::Definition, generated, RootDatabase};
use crate::{
defs::{Definition, IdentClass},
generated, RootDatabase,
};

pub fn item_name(db: &RootDatabase, item: ItemInNs) -> Option<Name> {
match item {
Expand Down Expand Up @@ -109,3 +112,16 @@ pub fn is_editable_crate(krate: Crate, db: &RootDatabase) -> bool {
let source_root_id = db.file_source_root(root_file);
!db.source_root(source_root_id).is_library
}

pub fn get_definition(
sema: &Semantics<'_, RootDatabase>,
token: SyntaxToken,
) -> Option<Definition> {
for token in sema.descend_into_macros(token) {
let def = IdentClass::classify_token(sema, &token).map(IdentClass::definitions_no_ops);
if let Some(&[x]) = def.as_deref() {
return Some(x);
}
}
None
}
9 changes: 9 additions & 0 deletions crates/ide/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ mod interpret_function;
mod view_item_tree;
mod shuffle_crate_graph;
mod fetch_crates;
mod view_memory_layout;

use std::ffi::OsStr;

Expand All @@ -74,6 +75,7 @@ use ide_db::{
};
use syntax::SourceFile;
use triomphe::Arc;
use view_memory_layout::{view_memory_layout, RecursiveMemoryLayout};

use crate::navigation_target::{ToNav, TryToNav};

Expand Down Expand Up @@ -724,6 +726,13 @@ impl Analysis {
self.with_db(|db| move_item::move_item(db, range, direction))
}

pub fn get_recursive_memory_layout(
&self,
position: FilePosition,
) -> Cancellable<Option<RecursiveMemoryLayout>> {
self.with_db(|db| view_memory_layout(db, position))
}

/// Performs an operation on the database that may be canceled.
///
/// rust-analyzer needs to be able to answer semantic questions about the
Expand Down
17 changes: 4 additions & 13 deletions crates/ide/src/static_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@

use std::collections::HashMap;

use hir::{db::HirDatabase, Crate, Module, Semantics};
use hir::{db::HirDatabase, Crate, Module};
use ide_db::helpers::get_definition;
use ide_db::{
base_db::{FileId, FileRange, SourceDatabaseExt},
defs::{Definition, IdentClass},
defs::Definition,
FxHashSet, RootDatabase,
};
use syntax::{AstNode, SyntaxKind::*, SyntaxToken, TextRange, T};
use syntax::{AstNode, SyntaxKind::*, TextRange, T};

use crate::{
hover::hover_for_definition,
Expand Down Expand Up @@ -214,16 +215,6 @@ impl StaticIndex<'_> {
}
}

fn get_definition(sema: &Semantics<'_, RootDatabase>, token: SyntaxToken) -> Option<Definition> {
for token in sema.descend_into_macros(token) {
let def = IdentClass::classify_token(sema, &token).map(IdentClass::definitions_no_ops);
if let Some(&[it]) = def.as_deref() {
return Some(it);
}
}
None
}

#[cfg(test)]
mod tests {
use crate::{fixture, StaticIndex};
Expand Down
Loading