mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 04:44:57 +00:00
Properly account for editions in names
This PR touches a lot of parts. But the main changes are changing `hir_expand::Name` to be raw edition-dependently and only when necessary (unrelated to how the user originally wrote the identifier), and changing `is_keyword()` and `is_raw_identifier()` to be edition-aware (this was done in #17896, but the FIXMEs were fixed here). It is possible that I missed some cases, but most IDE parts should properly escape (or not escape) identifiers now. The rules of thumb are: - If we show the identifier to the user, its rawness should be determined by the edition of the edited crate. This is nice for IDE features, but really important for changes we insert to the source code. - For tests, I chose `Edition::CURRENT` (so we only have to (maybe) update tests when an edition becomes stable, to avoid churn). - For debugging tools (helper methods and logs), I used `Edition::LATEST`.
This commit is contained in:
parent
91aa3f46b3
commit
9d3368f2c2
179 changed files with 2485 additions and 1250 deletions
|
@ -2,6 +2,7 @@
|
|||
|
||||
use ide_db::{FxHashMap, FxHashSet};
|
||||
use itertools::Itertools;
|
||||
use parser::Edition;
|
||||
use syntax::{
|
||||
ast::{self, AstNode, AstToken},
|
||||
SyntaxElement, SyntaxKind, SyntaxNode, SyntaxToken, TextRange, TextSize,
|
||||
|
@ -33,7 +34,7 @@ fn matches_to_edit_at_offset(
|
|||
for m in &matches.matches {
|
||||
edit_builder.replace(
|
||||
m.range.range.checked_sub(relative_start).unwrap(),
|
||||
render_replace(db, m, file_src, rules),
|
||||
render_replace(db, m, file_src, rules, m.range.file_id.edition()),
|
||||
);
|
||||
}
|
||||
edit_builder.finish()
|
||||
|
@ -54,6 +55,7 @@ struct ReplacementRenderer<'a> {
|
|||
// is parsed, placeholders don't get split. e.g. if a template of `$a.to_string()` results in `1
|
||||
// + 2.to_string()` then the placeholder value `1 + 2` was split and needs parenthesis.
|
||||
placeholder_tokens_requiring_parenthesis: FxHashSet<SyntaxToken>,
|
||||
edition: Edition,
|
||||
}
|
||||
|
||||
fn render_replace(
|
||||
|
@ -61,6 +63,7 @@ fn render_replace(
|
|||
match_info: &Match,
|
||||
file_src: &str,
|
||||
rules: &[ResolvedRule],
|
||||
edition: Edition,
|
||||
) -> String {
|
||||
let rule = &rules[match_info.rule_index];
|
||||
let template = rule
|
||||
|
@ -76,6 +79,7 @@ fn render_replace(
|
|||
out: String::new(),
|
||||
placeholder_tokens_requiring_parenthesis: FxHashSet::default(),
|
||||
placeholder_tokens_by_range: FxHashMap::default(),
|
||||
edition,
|
||||
};
|
||||
renderer.render_node(&template.node);
|
||||
renderer.maybe_rerender_with_extra_parenthesis(&template.node);
|
||||
|
@ -105,7 +109,7 @@ impl ReplacementRenderer<'_> {
|
|||
|
||||
fn render_node(&mut self, node: &SyntaxNode) {
|
||||
if let Some(mod_path) = self.match_info.rendered_template_paths.get(node) {
|
||||
self.out.push_str(&mod_path.display(self.db).to_string());
|
||||
self.out.push_str(&mod_path.display(self.db, self.edition).to_string());
|
||||
// Emit everything except for the segment's name-ref, since we already effectively
|
||||
// emitted that as part of `mod_path`.
|
||||
if let Some(path) = ast::Path::cast(node.clone()) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue