Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit b169e1e

Browse files
committed
Remove code duplication
1 parent 3a839ea commit b169e1e

File tree

2 files changed

+7
-18
lines changed

2 files changed

+7
-18
lines changed

crates/ide/src/moniker.rs

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
//! This module generates [moniker](https://microsoft.github.io/language-server-protocol/specifications/lsif/0.6.0/specification/#exportsImports)
22
//! for LSIF and LSP.
33
4-
use hir::{db::DefDatabase, AsAssocItem, AssocItemContainer, Crate, Name, Semantics};
4+
use hir::{AsAssocItem, AssocItemContainer, Crate, Name, Semantics};
55
use ide_db::{
6-
base_db::{CrateOrigin, FileId, FileLoader, FilePosition, LangCrateOrigin},
6+
base_db::{CrateOrigin, FilePosition, LangCrateOrigin},
77
defs::{Definition, IdentClass},
88
helpers::pick_best_token,
99
RootDatabase,
1010
};
1111
use itertools::Itertools;
1212
use syntax::{AstNode, SyntaxKind::*, T};
1313

14-
use crate::{doc_links::token_as_doc_comment, RangeInfo};
14+
use crate::{doc_links::token_as_doc_comment, parent_module::crates_for, RangeInfo};
1515

1616
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
1717
pub enum MonikerDescriptorKind {
@@ -77,25 +77,13 @@ pub struct PackageInformation {
7777
pub version: Option<String>,
7878
}
7979

80-
pub(crate) fn crate_for_file(db: &RootDatabase, file_id: FileId) -> Option<Crate> {
81-
for &krate in db.relevant_crates(file_id).iter() {
82-
let crate_def_map = db.crate_def_map(krate);
83-
for (_, data) in crate_def_map.modules() {
84-
if data.origin.file_id() == Some(file_id) {
85-
return Some(krate.into());
86-
}
87-
}
88-
}
89-
None
90-
}
91-
9280
pub(crate) fn moniker(
9381
db: &RootDatabase,
9482
FilePosition { file_id, offset }: FilePosition,
9583
) -> Option<RangeInfo<Vec<MonikerResult>>> {
9684
let sema = &Semantics::new(db);
9785
let file = sema.parse(file_id).syntax().clone();
98-
let current_crate = crate_for_file(db, file_id)?;
86+
let current_crate: hir::Crate = crates_for(db, file_id).pop()?.into();
9987
let original_token = pick_best_token(file.token_at_offset(offset), |kind| match kind {
10088
IDENT
10189
| INT_NUMBER

crates/ide/src/static_index.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ use syntax::{AstNode, SyntaxKind::*, SyntaxToken, TextRange, T};
1313

1414
use crate::{
1515
hover::hover_for_definition,
16-
moniker::{crate_for_file, def_to_moniker, MonikerResult},
16+
moniker::{def_to_moniker, MonikerResult},
17+
parent_module::crates_for,
1718
Analysis, Fold, HoverConfig, HoverDocFormat, HoverResult, InlayHint, InlayHintsConfig,
1819
TryToNav,
1920
};
@@ -99,7 +100,7 @@ fn all_modules(db: &dyn HirDatabase) -> Vec<Module> {
99100

100101
impl StaticIndex<'_> {
101102
fn add_file(&mut self, file_id: FileId) {
102-
let current_crate = crate_for_file(self.db, file_id);
103+
let current_crate = crates_for(self.db, file_id).pop().map(Into::into);
103104
let folds = self.analysis.folding_ranges(file_id).unwrap();
104105
let inlay_hints = self
105106
.analysis

0 commit comments

Comments
 (0)