mirror of
https://github.com/rust-lang/rust.git
synced 2026-05-04 18:40:57 +03:00
Make Semantics::attach_first_edition() not return Option
And instead call `EditionedFileId::current_edition_guess_origin`, as most callers do it anyway.
This commit is contained in:
@@ -384,15 +384,18 @@ pub fn first_crate(&self, file: FileId) -> Option<Crate> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn attach_first_edition(&self, file: FileId) -> Option<EditionedFileId> {
|
||||
pub fn attach_first_edition_opt(&self, file: FileId) -> Option<EditionedFileId> {
|
||||
let krate = self.file_to_module_defs(file).next()?.krate();
|
||||
Some(EditionedFileId::new(self.db, file, krate.edition(self.db), krate.id))
|
||||
}
|
||||
|
||||
pub fn attach_first_edition(&self, file: FileId) -> EditionedFileId {
|
||||
self.attach_first_edition_opt(file)
|
||||
.unwrap_or_else(|| EditionedFileId::current_edition_guess_origin(self.db, file))
|
||||
}
|
||||
|
||||
pub fn parse_guess_edition(&self, file_id: FileId) -> ast::SourceFile {
|
||||
let file_id = self
|
||||
.attach_first_edition(file_id)
|
||||
.unwrap_or_else(|| EditionedFileId::current_edition_guess_origin(self.db, file_id));
|
||||
let file_id = self.attach_first_edition(file_id);
|
||||
|
||||
let tree = self.db.parse(file_id).tree();
|
||||
self.cache(tree.syntax().clone(), file_id.into());
|
||||
@@ -401,7 +404,7 @@ pub fn parse_guess_edition(&self, file_id: FileId) -> ast::SourceFile {
|
||||
|
||||
pub fn adjust_edition(&self, file_id: HirFileId) -> HirFileId {
|
||||
if let Some(editioned_file_id) = file_id.file_id() {
|
||||
self.attach_first_edition(editioned_file_id.file_id(self.db))
|
||||
self.attach_first_edition_opt(editioned_file_id.file_id(self.db))
|
||||
.map_or(file_id, Into::into)
|
||||
} else {
|
||||
file_id
|
||||
|
||||
@@ -67,7 +67,7 @@
|
||||
pub mod utils;
|
||||
|
||||
use hir::Semantics;
|
||||
use ide_db::{EditionedFileId, RootDatabase};
|
||||
use ide_db::RootDatabase;
|
||||
use syntax::TextRange;
|
||||
|
||||
pub(crate) use crate::assist_context::{AssistContext, Assists};
|
||||
@@ -88,9 +88,7 @@ pub fn assists(
|
||||
range: ide_db::FileRange,
|
||||
) -> Vec<Assist> {
|
||||
let sema = Semantics::new(db);
|
||||
let file_id = sema
|
||||
.attach_first_edition(range.file_id)
|
||||
.unwrap_or_else(|| EditionedFileId::current_edition_guess_origin(db, range.file_id));
|
||||
let file_id = sema.attach_first_edition(range.file_id);
|
||||
let ctx = AssistContext::new(sema, config, hir::FileRange { file_id, range: range.range });
|
||||
let mut acc = Assists::new(&ctx, resolve);
|
||||
handlers::all().iter().for_each(|handler| {
|
||||
|
||||
@@ -322,8 +322,9 @@ fn check_with_config(
|
||||
let (mut db, file_with_caret_id, range_or_offset) = RootDatabase::with_range_or_offset(before);
|
||||
db.enable_proc_attr_macros();
|
||||
let sema = Semantics::new(&db);
|
||||
let file_with_caret_id =
|
||||
sema.attach_first_edition(file_with_caret_id.file_id(&db)).unwrap_or(file_with_caret_id);
|
||||
let file_with_caret_id = sema
|
||||
.attach_first_edition_opt(file_with_caret_id.file_id(&db))
|
||||
.unwrap_or(file_with_caret_id);
|
||||
let text_without_caret = db.file_text(file_with_caret_id.file_id(&db)).text(&db).to_string();
|
||||
|
||||
let frange = hir::FileRange { file_id: file_with_caret_id, range: range_or_offset.into() };
|
||||
|
||||
@@ -707,7 +707,7 @@ pub(crate) fn new(
|
||||
let _p = tracing::info_span!("CompletionContext::new").entered();
|
||||
let sema = Semantics::new(db);
|
||||
|
||||
let editioned_file_id = sema.attach_first_edition(file_id)?;
|
||||
let editioned_file_id = sema.attach_first_edition(file_id);
|
||||
let original_file = sema.parse(editioned_file_id);
|
||||
|
||||
// Insert a fake ident to get a valid parse tree. We will use this file
|
||||
|
||||
@@ -274,7 +274,7 @@ pub fn resolve_completion_edits(
|
||||
let _p = tracing::info_span!("resolve_completion_edits").entered();
|
||||
let sema = hir::Semantics::new(db);
|
||||
|
||||
let editioned_file_id = sema.attach_first_edition(file_id)?;
|
||||
let editioned_file_id = sema.attach_first_edition(file_id);
|
||||
|
||||
let original_file = sema.parse(editioned_file_id);
|
||||
let original_token =
|
||||
|
||||
@@ -90,7 +90,7 @@ mod handlers {
|
||||
Crate, DisplayTarget, InFile, Semantics, db::ExpandDatabase, diagnostics::AnyDiagnostic,
|
||||
};
|
||||
use ide_db::{
|
||||
EditionedFileId, FileId, FileRange, FxHashMap, FxHashSet, RootDatabase, Severity, SnippetCap,
|
||||
FileId, FileRange, FxHashMap, FxHashSet, RootDatabase, Severity, SnippetCap,
|
||||
assists::{Assist, AssistId, AssistResolveStrategy, ExprFillDefaultMode},
|
||||
base_db::{ReleaseChannel, RootQueryDb as _},
|
||||
generated::lints::{CLIPPY_LINT_GROUPS, DEFAULT_LINT_GROUPS, DEFAULT_LINTS, Lint, LintGroup},
|
||||
@@ -290,9 +290,7 @@ pub fn syntax_diagnostics(
|
||||
}
|
||||
|
||||
let sema = Semantics::new(db);
|
||||
let editioned_file_id = sema
|
||||
.attach_first_edition(file_id)
|
||||
.unwrap_or_else(|| EditionedFileId::current_edition_guess_origin(db, file_id));
|
||||
let editioned_file_id = sema.attach_first_edition(file_id);
|
||||
|
||||
let (file_id, _) = editioned_file_id.unpack(db);
|
||||
|
||||
@@ -321,9 +319,7 @@ pub fn semantic_diagnostics(
|
||||
) -> Vec<Diagnostic> {
|
||||
let _p = tracing::info_span!("semantic_diagnostics").entered();
|
||||
let sema = Semantics::new(db);
|
||||
let editioned_file_id = sema
|
||||
.attach_first_edition(file_id)
|
||||
.unwrap_or_else(|| EditionedFileId::current_edition_guess_origin(db, file_id));
|
||||
let editioned_file_id = sema.attach_first_edition(file_id);
|
||||
|
||||
let (file_id, edition) = editioned_file_id.unpack(db);
|
||||
let mut res = Vec::new();
|
||||
|
||||
@@ -125,9 +125,7 @@ pub fn in_context(
|
||||
) -> Result<MatchFinder<'db>, SsrError> {
|
||||
restrict_ranges.retain(|range| !range.range.is_empty());
|
||||
let sema = Semantics::new(db);
|
||||
let file_id = sema.attach_first_edition(lookup_context.file_id).unwrap_or_else(|| {
|
||||
EditionedFileId::current_edition_guess_origin(db, lookup_context.file_id)
|
||||
});
|
||||
let file_id = sema.attach_first_edition(lookup_context.file_id);
|
||||
let resolution_scope = resolving::ResolutionScope::new(
|
||||
&sema,
|
||||
hir::FilePosition { file_id, offset: lookup_context.offset },
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
};
|
||||
use hir::FileRange;
|
||||
use ide_db::{
|
||||
EditionedFileId, FileId, FxHashSet,
|
||||
FileId, FxHashSet,
|
||||
defs::Definition,
|
||||
search::{SearchScope, UsageSearchResult},
|
||||
symbol_index::LocalRoots,
|
||||
@@ -136,9 +136,7 @@ fn search_scope(&self) -> SearchScope {
|
||||
// seems to get put into a single source root.
|
||||
let mut files = Vec::new();
|
||||
self.search_files_do(|file_id| {
|
||||
files.push(self.sema.attach_first_edition(file_id).unwrap_or_else(|| {
|
||||
EditionedFileId::current_edition_guess_origin(self.sema.db, file_id)
|
||||
}));
|
||||
files.push(self.sema.attach_first_edition(file_id));
|
||||
});
|
||||
SearchScope::files(&files)
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ pub struct ExpandedMacro {
|
||||
// 
|
||||
pub(crate) fn expand_macro(db: &RootDatabase, position: FilePosition) -> Option<ExpandedMacro> {
|
||||
let sema = Semantics::new(db);
|
||||
let file_id = sema.attach_first_edition(position.file_id)?;
|
||||
let file_id = sema.attach_first_edition(position.file_id);
|
||||
let file = sema.parse(file_id);
|
||||
let krate = sema.file_to_module_def(file_id.file_id(db))?.krate().into();
|
||||
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
helpers::pick_best_token,
|
||||
};
|
||||
use itertools::Itertools;
|
||||
use span::{Edition, FileId};
|
||||
use span::FileId;
|
||||
use syntax::{
|
||||
AstNode, AstToken,
|
||||
SyntaxKind::*,
|
||||
@@ -50,8 +50,7 @@ pub(crate) fn goto_definition(
|
||||
) -> Option<RangeInfo<Vec<NavigationTarget>>> {
|
||||
let sema = &Semantics::new(db);
|
||||
let file = sema.parse_guess_edition(file_id).syntax().clone();
|
||||
let edition =
|
||||
sema.attach_first_edition(file_id).map(|it| it.edition(db)).unwrap_or(Edition::CURRENT);
|
||||
let edition = sema.attach_first_edition(file_id).edition(db);
|
||||
let original_token = pick_best_token(file.token_at_offset(offset), |kind| match kind {
|
||||
IDENT
|
||||
| INT_NUMBER
|
||||
|
||||
@@ -60,9 +60,7 @@ pub(crate) fn highlight_related(
|
||||
ide_db::FilePosition { offset, file_id }: ide_db::FilePosition,
|
||||
) -> Option<Vec<HighlightedRange>> {
|
||||
let _p = tracing::info_span!("highlight_related").entered();
|
||||
let file_id = sema
|
||||
.attach_first_edition(file_id)
|
||||
.unwrap_or_else(|| EditionedFileId::current_edition_guess_origin(sema.db, file_id));
|
||||
let file_id = sema.attach_first_edition(file_id);
|
||||
let syntax = sema.parse(file_id).syntax().clone();
|
||||
|
||||
let token = pick_best_token(syntax.token_at_offset(offset), |kind| match kind {
|
||||
|
||||
@@ -135,8 +135,7 @@ pub(crate) fn hover(
|
||||
) -> Option<RangeInfo<HoverResult>> {
|
||||
let sema = &hir::Semantics::new(db);
|
||||
let file = sema.parse_guess_edition(file_id).syntax().clone();
|
||||
let edition =
|
||||
sema.attach_first_edition(file_id).map(|it| it.edition(db)).unwrap_or(Edition::CURRENT);
|
||||
let edition = sema.attach_first_edition(file_id).edition(db);
|
||||
let display_target = sema.first_crate(file_id)?.to_display_target(db);
|
||||
let mut res = if range.is_empty() {
|
||||
hover_offset(
|
||||
|
||||
@@ -89,9 +89,7 @@ pub(crate) fn inlay_hints(
|
||||
) -> Vec<InlayHint> {
|
||||
let _p = tracing::info_span!("inlay_hints").entered();
|
||||
let sema = Semantics::new(db);
|
||||
let file_id = sema
|
||||
.attach_first_edition(file_id)
|
||||
.unwrap_or_else(|| EditionedFileId::current_edition_guess_origin(db, file_id));
|
||||
let file_id = sema.attach_first_edition(file_id);
|
||||
let file = sema.parse(file_id);
|
||||
let file = file.syntax();
|
||||
|
||||
@@ -142,9 +140,7 @@ pub(crate) fn inlay_hints_resolve(
|
||||
) -> Option<InlayHint> {
|
||||
let _p = tracing::info_span!("inlay_hints_resolve").entered();
|
||||
let sema = Semantics::new(db);
|
||||
let file_id = sema
|
||||
.attach_first_edition(file_id)
|
||||
.unwrap_or_else(|| EditionedFileId::current_edition_guess_origin(db, file_id));
|
||||
let file_id = sema.attach_first_edition(file_id);
|
||||
let file = sema.parse(file_id);
|
||||
let file = file.syntax();
|
||||
|
||||
|
||||
@@ -28,7 +28,6 @@
|
||||
use itertools::Itertools;
|
||||
use macros::UpmapFromRaFixture;
|
||||
use nohash_hasher::IntMap;
|
||||
use span::Edition;
|
||||
use syntax::AstToken;
|
||||
use syntax::{
|
||||
AstNode,
|
||||
@@ -419,10 +418,7 @@ fn handle_control_flow_keywords(
|
||||
FilePosition { file_id, offset }: FilePosition,
|
||||
) -> Option<ReferenceSearchResult> {
|
||||
let file = sema.parse_guess_edition(file_id);
|
||||
let edition = sema
|
||||
.attach_first_edition(file_id)
|
||||
.map(|it| it.edition(sema.db))
|
||||
.unwrap_or(Edition::CURRENT);
|
||||
let edition = sema.attach_first_edition(file_id).edition(sema.db);
|
||||
let token = pick_best_token(file.syntax().token_at_offset(offset), |kind| match kind {
|
||||
_ if kind.is_keyword(edition) => 4,
|
||||
T![=>] => 3,
|
||||
|
||||
@@ -121,7 +121,7 @@ pub(crate) fn rename(
|
||||
) -> RenameResult<SourceChange> {
|
||||
let sema = Semantics::new(db);
|
||||
let file_id = sema
|
||||
.attach_first_edition(position.file_id)
|
||||
.attach_first_edition_opt(position.file_id)
|
||||
.ok_or_else(|| format_err!("No references found at position"))?;
|
||||
let source_file = sema.parse(file_id);
|
||||
let syntax = source_file.syntax();
|
||||
|
||||
@@ -84,8 +84,7 @@ pub(crate) fn signature_help(
|
||||
// this prevents us from leaving the CallExpression
|
||||
.and_then(|tok| algo::skip_trivia_token(tok, Direction::Prev))?;
|
||||
let token = sema.descend_into_macros_single_exact(token);
|
||||
let edition =
|
||||
sema.attach_first_edition(file_id).map(|it| it.edition(db)).unwrap_or(Edition::CURRENT);
|
||||
let edition = sema.attach_first_edition(file_id).edition(db);
|
||||
let display_target = sema.first_crate(file_id)?.to_display_target(db);
|
||||
|
||||
for node in token.parent_ancestors() {
|
||||
|
||||
@@ -10,7 +10,6 @@
|
||||
documentation::Documentation,
|
||||
famous_defs::FamousDefs,
|
||||
};
|
||||
use span::Edition;
|
||||
use syntax::{AstNode, SyntaxKind::*, SyntaxNode, SyntaxToken, T, TextRange};
|
||||
|
||||
use crate::navigation_target::UpmappingResult;
|
||||
@@ -204,10 +203,7 @@ fn add_file(&mut self, file_id: FileId) {
|
||||
// hovers
|
||||
let sema = hir::Semantics::new(self.db);
|
||||
let root = sema.parse_guess_edition(file_id).syntax().clone();
|
||||
let edition = sema
|
||||
.attach_first_edition(file_id)
|
||||
.map(|it| it.edition(self.db))
|
||||
.unwrap_or(Edition::CURRENT);
|
||||
let edition = sema.attach_first_edition(file_id).edition(sema.db);
|
||||
let display_target = match sema.first_crate(file_id) {
|
||||
Some(krate) => krate.to_display_target(sema.db),
|
||||
None => return,
|
||||
|
||||
@@ -197,9 +197,7 @@ pub(crate) fn highlight(
|
||||
) -> Vec<HlRange> {
|
||||
let _p = tracing::info_span!("highlight").entered();
|
||||
let sema = Semantics::new(db);
|
||||
let file_id = sema
|
||||
.attach_first_edition(file_id)
|
||||
.unwrap_or_else(|| EditionedFileId::current_edition_guess_origin(db, file_id));
|
||||
let file_id = sema.attach_first_edition(file_id);
|
||||
|
||||
// Determine the root based on the given range.
|
||||
let (root, range_to_highlight) = {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
//! Renders a bit of code as HTML.
|
||||
|
||||
use hir::{EditionedFileId, Semantics};
|
||||
use hir::Semantics;
|
||||
use ide_db::MiniCore;
|
||||
use oorandom::Rand32;
|
||||
use stdx::format_to;
|
||||
@@ -18,9 +18,7 @@ pub(crate) fn highlight_as_html_with_config(
|
||||
rainbow: bool,
|
||||
) -> String {
|
||||
let sema = Semantics::new(db);
|
||||
let file_id = sema
|
||||
.attach_first_edition(file_id)
|
||||
.unwrap_or_else(|| EditionedFileId::current_edition_guess_origin(db, file_id));
|
||||
let file_id = sema.attach_first_edition(file_id);
|
||||
let file = sema.parse(file_id);
|
||||
let file = file.syntax();
|
||||
fn rainbowify(seed: u64) -> String {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use hir::{EditionedFileId, Semantics, db::DefDatabase};
|
||||
use hir::{Semantics, db::DefDatabase};
|
||||
use ide_db::{FileId, RootDatabase};
|
||||
|
||||
// Feature: Debug ItemTree
|
||||
@@ -10,8 +10,6 @@
|
||||
// | VS Code | **rust-analyzer: Debug ItemTree** |
|
||||
pub(crate) fn view_item_tree(db: &RootDatabase, file_id: FileId) -> String {
|
||||
let sema = Semantics::new(db);
|
||||
let file_id = sema
|
||||
.attach_first_edition(file_id)
|
||||
.unwrap_or_else(|| EditionedFileId::current_edition_guess_origin(db, file_id));
|
||||
let file_id = sema.attach_first_edition(file_id);
|
||||
db.file_item_tree(file_id.into()).pretty_print(db, file_id.edition(db))
|
||||
}
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
//! Reports references in code that the IDE layer cannot resolve.
|
||||
use hir::{AnyDiagnostic, Crate, Module, Semantics, db::HirDatabase, sym};
|
||||
use ide::{AnalysisHost, RootDatabase, TextRange};
|
||||
use ide_db::{
|
||||
EditionedFileId, FxHashSet, LineIndexDatabase as _, base_db::SourceDatabase, defs::NameRefClass,
|
||||
};
|
||||
use ide_db::{FxHashSet, LineIndexDatabase as _, base_db::SourceDatabase, defs::NameRefClass};
|
||||
use load_cargo::{LoadCargoConfig, ProcMacroServerChoice, load_workspace_at};
|
||||
use parser::SyntaxKind;
|
||||
use syntax::{AstNode, WalkEvent, ast};
|
||||
@@ -139,9 +137,7 @@ fn all_unresolved_references(
|
||||
sema: &Semantics<'_, RootDatabase>,
|
||||
file_id: FileId,
|
||||
) -> Vec<TextRange> {
|
||||
let file_id = sema
|
||||
.attach_first_edition(file_id)
|
||||
.unwrap_or_else(|| EditionedFileId::current_edition_guess_origin(sema.db, file_id));
|
||||
let file_id = sema.attach_first_edition(file_id);
|
||||
let file = sema.parse(file_id);
|
||||
let root = file.syntax();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user