mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-11-14 17:07:26 +00:00
Merge commit 'f5fde4df4' into HEAD
This commit is contained in:
commit
969e25033b
76 changed files with 1613 additions and 654 deletions
2
Cargo.lock
generated
2
Cargo.lock
generated
|
@ -394,6 +394,7 @@ dependencies = [
|
|||
"crossbeam-channel",
|
||||
"jod-thread",
|
||||
"paths",
|
||||
"rustc-hash",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"stdx",
|
||||
|
@ -660,6 +661,7 @@ dependencies = [
|
|||
"indexmap",
|
||||
"itertools",
|
||||
"limit",
|
||||
"memchr",
|
||||
"once_cell",
|
||||
"parser",
|
||||
"profile",
|
||||
|
|
|
@ -13,6 +13,7 @@ doctest = false
|
|||
crossbeam-channel = "0.5.5"
|
||||
tracing = "0.1.35"
|
||||
cargo_metadata = "0.15.0"
|
||||
rustc-hash = "1.1.0"
|
||||
serde = { version = "1.0.137", features = ["derive"] }
|
||||
serde_json = "1.0.81"
|
||||
jod-thread = "0.1.2"
|
||||
|
|
|
@ -12,6 +12,7 @@ use std::{
|
|||
|
||||
use crossbeam_channel::{never, select, unbounded, Receiver, Sender};
|
||||
use paths::AbsPathBuf;
|
||||
use rustc_hash::FxHashMap;
|
||||
use serde::Deserialize;
|
||||
use stdx::{process::streaming_output, JodChild};
|
||||
|
||||
|
@ -30,10 +31,12 @@ pub enum FlycheckConfig {
|
|||
all_features: bool,
|
||||
features: Vec<String>,
|
||||
extra_args: Vec<String>,
|
||||
extra_env: FxHashMap<String, String>,
|
||||
},
|
||||
CustomCommand {
|
||||
command: String,
|
||||
args: Vec<String>,
|
||||
extra_env: FxHashMap<String, String>,
|
||||
},
|
||||
}
|
||||
|
||||
|
@ -41,7 +44,7 @@ impl fmt::Display for FlycheckConfig {
|
|||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
FlycheckConfig::CargoCommand { command, .. } => write!(f, "cargo {}", command),
|
||||
FlycheckConfig::CustomCommand { command, args } => {
|
||||
FlycheckConfig::CustomCommand { command, args, .. } => {
|
||||
write!(f, "{} {}", command, args.join(" "))
|
||||
}
|
||||
}
|
||||
|
@ -256,6 +259,7 @@ impl FlycheckActor {
|
|||
all_features,
|
||||
extra_args,
|
||||
features,
|
||||
extra_env,
|
||||
} => {
|
||||
let mut cmd = Command::new(toolchain::cargo());
|
||||
cmd.arg(command);
|
||||
|
@ -281,11 +285,13 @@ impl FlycheckActor {
|
|||
}
|
||||
}
|
||||
cmd.args(extra_args);
|
||||
cmd.envs(extra_env);
|
||||
cmd
|
||||
}
|
||||
FlycheckConfig::CustomCommand { command, args } => {
|
||||
FlycheckConfig::CustomCommand { command, args, extra_env } => {
|
||||
let mut cmd = Command::new(command);
|
||||
cmd.args(args);
|
||||
cmd.envs(extra_env);
|
||||
cmd
|
||||
}
|
||||
};
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
//! An algorithm to find a path to refer to a certain item.
|
||||
|
||||
use std::iter;
|
||||
use std::{cmp::Ordering, iter};
|
||||
|
||||
use hir_expand::name::{known, AsName, Name};
|
||||
use rustc_hash::FxHashSet;
|
||||
|
@ -16,9 +16,14 @@ use crate::{
|
|||
|
||||
/// Find a path that can be used to refer to a certain item. This can depend on
|
||||
/// *from where* you're referring to the item, hence the `from` parameter.
|
||||
pub fn find_path(db: &dyn DefDatabase, item: ItemInNs, from: ModuleId) -> Option<ModPath> {
|
||||
pub fn find_path(
|
||||
db: &dyn DefDatabase,
|
||||
item: ItemInNs,
|
||||
from: ModuleId,
|
||||
prefer_no_std: bool,
|
||||
) -> Option<ModPath> {
|
||||
let _p = profile::span("find_path");
|
||||
find_path_inner(db, item, from, None)
|
||||
find_path_inner(db, item, from, None, prefer_no_std)
|
||||
}
|
||||
|
||||
pub fn find_path_prefixed(
|
||||
|
@ -26,47 +31,14 @@ pub fn find_path_prefixed(
|
|||
item: ItemInNs,
|
||||
from: ModuleId,
|
||||
prefix_kind: PrefixKind,
|
||||
prefer_no_std: bool,
|
||||
) -> Option<ModPath> {
|
||||
let _p = profile::span("find_path_prefixed");
|
||||
find_path_inner(db, item, from, Some(prefix_kind))
|
||||
find_path_inner(db, item, from, Some(prefix_kind), prefer_no_std)
|
||||
}
|
||||
|
||||
const MAX_PATH_LEN: usize = 15;
|
||||
|
||||
trait ModPathExt {
|
||||
fn starts_with_std(&self) -> bool;
|
||||
fn can_start_with_std(&self) -> bool;
|
||||
}
|
||||
|
||||
impl ModPathExt for ModPath {
|
||||
fn starts_with_std(&self) -> bool {
|
||||
self.segments().first() == Some(&known::std)
|
||||
}
|
||||
|
||||
// Can we replace the first segment with `std::` and still get a valid, identical path?
|
||||
fn can_start_with_std(&self) -> bool {
|
||||
let first_segment = self.segments().first();
|
||||
first_segment == Some(&known::alloc) || first_segment == Some(&known::core)
|
||||
}
|
||||
}
|
||||
|
||||
fn check_self_super(def_map: &DefMap, item: ItemInNs, from: ModuleId) -> Option<ModPath> {
|
||||
if item == ItemInNs::Types(from.into()) {
|
||||
// - if the item is the module we're in, use `self`
|
||||
Some(ModPath::from_segments(PathKind::Super(0), None))
|
||||
} else if let Some(parent_id) = def_map[from.local_id].parent {
|
||||
// - if the item is the parent module, use `super` (this is not used recursively, since `super::super` is ugly)
|
||||
let parent_id = def_map.module_id(parent_id);
|
||||
if item == ItemInNs::Types(ModuleDefId::ModuleId(parent_id)) {
|
||||
Some(ModPath::from_segments(PathKind::Super(1), None))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
||||
pub enum PrefixKind {
|
||||
/// Causes paths to always start with either `self`, `super`, `crate` or a crate-name.
|
||||
|
@ -94,104 +66,60 @@ impl PrefixKind {
|
|||
self == &PrefixKind::ByCrate
|
||||
}
|
||||
}
|
||||
|
||||
/// Attempts to find a path to refer to the given `item` visible from the `from` ModuleId
|
||||
fn find_path_inner(
|
||||
db: &dyn DefDatabase,
|
||||
item: ItemInNs,
|
||||
from: ModuleId,
|
||||
prefixed: Option<PrefixKind>,
|
||||
prefer_no_std: bool,
|
||||
) -> Option<ModPath> {
|
||||
// FIXME: Do fast path for std/core libs?
|
||||
|
||||
let mut visited_modules = FxHashSet::default();
|
||||
let def_map = from.def_map(db);
|
||||
find_path_inner_(db, &def_map, from, item, MAX_PATH_LEN, prefixed, &mut visited_modules)
|
||||
}
|
||||
|
||||
fn find_path_inner_(
|
||||
db: &dyn DefDatabase,
|
||||
def_map: &DefMap,
|
||||
from: ModuleId,
|
||||
item: ItemInNs,
|
||||
max_len: usize,
|
||||
mut prefixed: Option<PrefixKind>,
|
||||
visited_modules: &mut FxHashSet<ModuleId>,
|
||||
) -> Option<ModPath> {
|
||||
if max_len == 0 {
|
||||
return None;
|
||||
// - if the item is a builtin, it's in scope
|
||||
if let ItemInNs::Types(ModuleDefId::BuiltinType(builtin)) = item {
|
||||
return Some(ModPath::from_segments(PathKind::Plain, Some(builtin.as_name())));
|
||||
}
|
||||
|
||||
// Base cases:
|
||||
let def_map = from.def_map(db);
|
||||
let crate_root = def_map.crate_root(db);
|
||||
// - if the item is a module, jump straight to module search
|
||||
if let ItemInNs::Types(ModuleDefId::ModuleId(module_id)) = item {
|
||||
let mut visited_modules = FxHashSet::default();
|
||||
return find_path_for_module(
|
||||
db,
|
||||
&def_map,
|
||||
&mut visited_modules,
|
||||
crate_root,
|
||||
from,
|
||||
module_id,
|
||||
MAX_PATH_LEN,
|
||||
prefixed,
|
||||
prefer_no_std || db.crate_supports_no_std(crate_root.krate),
|
||||
);
|
||||
}
|
||||
|
||||
// - if the item is already in scope, return the name under which it is
|
||||
let scope_name = def_map.with_ancestor_maps(db, from.local_id, &mut |def_map, local_id| {
|
||||
def_map[local_id].scope.name_of(item).map(|(name, _)| name.clone())
|
||||
});
|
||||
let scope_name = find_in_scope(db, &def_map, from, item);
|
||||
if prefixed.is_none() {
|
||||
if let Some(scope_name) = scope_name {
|
||||
return Some(ModPath::from_segments(PathKind::Plain, Some(scope_name)));
|
||||
}
|
||||
}
|
||||
|
||||
// - if the item is a builtin, it's in scope
|
||||
if let ItemInNs::Types(ModuleDefId::BuiltinType(builtin)) = item {
|
||||
return Some(ModPath::from_segments(PathKind::Plain, Some(builtin.as_name())));
|
||||
}
|
||||
|
||||
// - if the item is the crate root, return `crate`
|
||||
let crate_root = def_map.crate_root(db);
|
||||
if item == ItemInNs::Types(ModuleDefId::ModuleId(crate_root)) {
|
||||
return Some(ModPath::from_segments(PathKind::Crate, None));
|
||||
}
|
||||
|
||||
if prefixed.filter(PrefixKind::is_absolute).is_none() {
|
||||
if let modpath @ Some(_) = check_self_super(&def_map, item, from) {
|
||||
return modpath;
|
||||
}
|
||||
}
|
||||
|
||||
// - if the item is the crate root of a dependency crate, return the name from the extern prelude
|
||||
let root_def_map = crate_root.def_map(db);
|
||||
if let ItemInNs::Types(ModuleDefId::ModuleId(item)) = item {
|
||||
for (name, &def_id) in root_def_map.extern_prelude() {
|
||||
if item == def_id {
|
||||
let name = scope_name.unwrap_or_else(|| name.clone());
|
||||
|
||||
let name_already_occupied_in_type_ns = def_map
|
||||
.with_ancestor_maps(db, from.local_id, &mut |def_map, local_id| {
|
||||
def_map[local_id]
|
||||
.scope
|
||||
.type_(&name)
|
||||
.filter(|&(id, _)| id != ModuleDefId::ModuleId(def_id))
|
||||
})
|
||||
.is_some();
|
||||
let kind = if name_already_occupied_in_type_ns {
|
||||
cov_mark::hit!(ambiguous_crate_start);
|
||||
PathKind::Abs
|
||||
} else {
|
||||
PathKind::Plain
|
||||
};
|
||||
return Some(ModPath::from_segments(kind, Some(name)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// - if the item is in the prelude, return the name from there
|
||||
if let Some(prelude_module) = root_def_map.prelude() {
|
||||
// Preludes in block DefMaps are ignored, only the crate DefMap is searched
|
||||
let prelude_def_map = prelude_module.def_map(db);
|
||||
let prelude_scope = &prelude_def_map[prelude_module.local_id].scope;
|
||||
if let Some((name, vis)) = prelude_scope.name_of(item) {
|
||||
if vis.is_visible_from(db, from) {
|
||||
return Some(ModPath::from_segments(PathKind::Plain, Some(name.clone())));
|
||||
}
|
||||
}
|
||||
if let Some(value) = find_in_prelude(db, &crate_root.def_map(db), item, from) {
|
||||
return value;
|
||||
}
|
||||
|
||||
// Recursive case:
|
||||
// - if the item is an enum variant, refer to it via the enum
|
||||
if let Some(ModuleDefId::EnumVariantId(variant)) = item.as_module_def_id() {
|
||||
if let Some(mut path) = find_path(db, ItemInNs::Types(variant.parent.into()), from) {
|
||||
// - if the item is an enum variant, refer to it via the enum
|
||||
if let Some(mut path) = find_path_inner(
|
||||
db,
|
||||
ItemInNs::Types(variant.parent.into()),
|
||||
from,
|
||||
prefixed,
|
||||
prefer_no_std,
|
||||
) {
|
||||
let data = db.enum_data(variant.parent);
|
||||
path.push_segment(data.variants[variant.local_id].name.clone());
|
||||
return Some(path);
|
||||
|
@ -201,28 +129,184 @@ fn find_path_inner_(
|
|||
// variant somewhere
|
||||
}
|
||||
|
||||
// - otherwise, look for modules containing (reexporting) it and import it from one of those
|
||||
let prefer_no_std = db.crate_supports_no_std(crate_root.krate);
|
||||
let mut best_path = None;
|
||||
let mut best_path_len = max_len;
|
||||
let mut visited_modules = FxHashSet::default();
|
||||
|
||||
calculate_best_path(
|
||||
db,
|
||||
&def_map,
|
||||
&mut visited_modules,
|
||||
crate_root,
|
||||
MAX_PATH_LEN,
|
||||
item,
|
||||
from,
|
||||
prefixed,
|
||||
prefer_no_std || db.crate_supports_no_std(crate_root.krate),
|
||||
scope_name,
|
||||
)
|
||||
}
|
||||
|
||||
fn find_path_for_module(
|
||||
db: &dyn DefDatabase,
|
||||
def_map: &DefMap,
|
||||
visited_modules: &mut FxHashSet<ModuleId>,
|
||||
crate_root: ModuleId,
|
||||
from: ModuleId,
|
||||
module_id: ModuleId,
|
||||
max_len: usize,
|
||||
prefixed: Option<PrefixKind>,
|
||||
prefer_no_std: bool,
|
||||
) -> Option<ModPath> {
|
||||
if max_len == 0 {
|
||||
return None;
|
||||
}
|
||||
|
||||
// Base cases:
|
||||
// - if the item is already in scope, return the name under which it is
|
||||
let scope_name = find_in_scope(db, def_map, from, ItemInNs::Types(module_id.into()));
|
||||
if prefixed.is_none() {
|
||||
if let Some(scope_name) = scope_name {
|
||||
return Some(ModPath::from_segments(PathKind::Plain, Some(scope_name)));
|
||||
}
|
||||
}
|
||||
|
||||
// - if the item is the crate root, return `crate`
|
||||
if module_id == crate_root {
|
||||
return Some(ModPath::from_segments(PathKind::Crate, None));
|
||||
}
|
||||
|
||||
// - if relative paths are fine, check if we are searching for a parent
|
||||
if prefixed.filter(PrefixKind::is_absolute).is_none() {
|
||||
if let modpath @ Some(_) = find_self_super(&def_map, module_id, from) {
|
||||
return modpath;
|
||||
}
|
||||
}
|
||||
|
||||
// - if the item is the crate root of a dependency crate, return the name from the extern prelude
|
||||
let root_def_map = crate_root.def_map(db);
|
||||
for (name, &def_id) in root_def_map.extern_prelude() {
|
||||
if module_id == def_id {
|
||||
let name = scope_name.unwrap_or_else(|| name.clone());
|
||||
|
||||
let name_already_occupied_in_type_ns = def_map
|
||||
.with_ancestor_maps(db, from.local_id, &mut |def_map, local_id| {
|
||||
def_map[local_id]
|
||||
.scope
|
||||
.type_(&name)
|
||||
.filter(|&(id, _)| id != ModuleDefId::ModuleId(def_id))
|
||||
})
|
||||
.is_some();
|
||||
let kind = if name_already_occupied_in_type_ns {
|
||||
cov_mark::hit!(ambiguous_crate_start);
|
||||
PathKind::Abs
|
||||
} else {
|
||||
PathKind::Plain
|
||||
};
|
||||
return Some(ModPath::from_segments(kind, Some(name)));
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(value) = find_in_prelude(db, &root_def_map, ItemInNs::Types(module_id.into()), from)
|
||||
{
|
||||
return value;
|
||||
}
|
||||
calculate_best_path(
|
||||
db,
|
||||
def_map,
|
||||
visited_modules,
|
||||
crate_root,
|
||||
max_len,
|
||||
ItemInNs::Types(module_id.into()),
|
||||
from,
|
||||
prefixed,
|
||||
prefer_no_std,
|
||||
scope_name,
|
||||
)
|
||||
}
|
||||
|
||||
fn find_in_scope(
|
||||
db: &dyn DefDatabase,
|
||||
def_map: &DefMap,
|
||||
from: ModuleId,
|
||||
item: ItemInNs,
|
||||
) -> Option<Name> {
|
||||
def_map.with_ancestor_maps(db, from.local_id, &mut |def_map, local_id| {
|
||||
def_map[local_id].scope.name_of(item).map(|(name, _)| name.clone())
|
||||
})
|
||||
}
|
||||
|
||||
fn find_in_prelude(
|
||||
db: &dyn DefDatabase,
|
||||
root_def_map: &DefMap,
|
||||
item: ItemInNs,
|
||||
from: ModuleId,
|
||||
) -> Option<Option<ModPath>> {
|
||||
if let Some(prelude_module) = root_def_map.prelude() {
|
||||
// Preludes in block DefMaps are ignored, only the crate DefMap is searched
|
||||
let prelude_def_map = prelude_module.def_map(db);
|
||||
let prelude_scope = &prelude_def_map[prelude_module.local_id].scope;
|
||||
if let Some((name, vis)) = prelude_scope.name_of(item) {
|
||||
if vis.is_visible_from(db, from) {
|
||||
return Some(Some(ModPath::from_segments(PathKind::Plain, Some(name.clone()))));
|
||||
}
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
fn find_self_super(def_map: &DefMap, item: ModuleId, from: ModuleId) -> Option<ModPath> {
|
||||
if item == from {
|
||||
// - if the item is the module we're in, use `self`
|
||||
Some(ModPath::from_segments(PathKind::Super(0), None))
|
||||
} else if let Some(parent_id) = def_map[from.local_id].parent {
|
||||
// - if the item is the parent module, use `super` (this is not used recursively, since `super::super` is ugly)
|
||||
let parent_id = def_map.module_id(parent_id);
|
||||
if item == parent_id {
|
||||
Some(ModPath::from_segments(PathKind::Super(1), None))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
fn calculate_best_path(
|
||||
db: &dyn DefDatabase,
|
||||
def_map: &DefMap,
|
||||
visited_modules: &mut FxHashSet<ModuleId>,
|
||||
crate_root: ModuleId,
|
||||
max_len: usize,
|
||||
item: ItemInNs,
|
||||
from: ModuleId,
|
||||
mut prefixed: Option<PrefixKind>,
|
||||
prefer_no_std: bool,
|
||||
scope_name: Option<Name>,
|
||||
) -> Option<ModPath> {
|
||||
if max_len <= 1 {
|
||||
return None;
|
||||
}
|
||||
let mut best_path = None;
|
||||
// Recursive case:
|
||||
// - otherwise, look for modules containing (reexporting) it and import it from one of those
|
||||
if item.krate(db) == Some(from.krate) {
|
||||
let mut best_path_len = max_len;
|
||||
// Item was defined in the same crate that wants to import it. It cannot be found in any
|
||||
// dependency in this case.
|
||||
// FIXME: this should have a fast path that doesn't look through the prelude again?
|
||||
for (module_id, name) in find_local_import_locations(db, item, from) {
|
||||
if !visited_modules.insert(module_id) {
|
||||
cov_mark::hit!(recursive_imports);
|
||||
continue;
|
||||
}
|
||||
if let Some(mut path) = find_path_inner_(
|
||||
if let Some(mut path) = find_path_for_module(
|
||||
db,
|
||||
def_map,
|
||||
visited_modules,
|
||||
crate_root,
|
||||
from,
|
||||
ItemInNs::Types(ModuleDefId::ModuleId(module_id)),
|
||||
module_id,
|
||||
best_path_len - 1,
|
||||
prefixed,
|
||||
visited_modules,
|
||||
prefer_no_std,
|
||||
) {
|
||||
path.push_segment(name);
|
||||
|
||||
|
@ -245,14 +329,16 @@ fn find_path_inner_(
|
|||
import_map.import_info_for(item).and_then(|info| {
|
||||
// Determine best path for containing module and append last segment from `info`.
|
||||
// FIXME: we should guide this to look up the path locally, or from the same crate again?
|
||||
let mut path = find_path_inner_(
|
||||
let mut path = find_path_for_module(
|
||||
db,
|
||||
def_map,
|
||||
from,
|
||||
ItemInNs::Types(ModuleDefId::ModuleId(info.container)),
|
||||
best_path_len - 1,
|
||||
prefixed,
|
||||
visited_modules,
|
||||
from,
|
||||
crate_root,
|
||||
info.container,
|
||||
max_len - 1,
|
||||
prefixed,
|
||||
prefer_no_std,
|
||||
)?;
|
||||
cov_mark::hit!(partially_imported);
|
||||
path.push_segment(info.path.segments.last()?.clone());
|
||||
|
@ -268,16 +354,12 @@ fn find_path_inner_(
|
|||
best_path = Some(new_path);
|
||||
}
|
||||
}
|
||||
|
||||
// If the item is declared inside a block expression, don't use a prefix, as we don't handle
|
||||
// that correctly (FIXME).
|
||||
if let Some(item_module) = item.as_module_def_id().and_then(|did| did.module(db)) {
|
||||
if item_module.def_map(db).block_id().is_some() && prefixed.is_some() {
|
||||
if let Some(module) = item.module(db) {
|
||||
if module.def_map(db).block_id().is_some() && prefixed.is_some() {
|
||||
cov_mark::hit!(prefixed_in_block_expression);
|
||||
prefixed = Some(PrefixKind::Plain);
|
||||
}
|
||||
}
|
||||
|
||||
match prefixed.map(PrefixKind::prefix) {
|
||||
Some(prefix) => best_path.or_else(|| {
|
||||
scope_name.map(|scope_name| ModPath::from_segments(prefix, Some(scope_name)))
|
||||
|
@ -287,29 +369,48 @@ fn find_path_inner_(
|
|||
}
|
||||
|
||||
fn select_best_path(old_path: ModPath, new_path: ModPath, prefer_no_std: bool) -> ModPath {
|
||||
if old_path.starts_with_std() && new_path.can_start_with_std() {
|
||||
if prefer_no_std {
|
||||
cov_mark::hit!(prefer_no_std_paths);
|
||||
new_path
|
||||
} else {
|
||||
cov_mark::hit!(prefer_std_paths);
|
||||
old_path
|
||||
const STD_CRATES: [Name; 3] = [known::std, known::core, known::alloc];
|
||||
match (old_path.segments().first(), new_path.segments().first()) {
|
||||
(Some(old), Some(new)) if STD_CRATES.contains(old) && STD_CRATES.contains(new) => {
|
||||
let rank = match prefer_no_std {
|
||||
false => |name: &Name| match name {
|
||||
name if name == &known::core => 0,
|
||||
name if name == &known::alloc => 0,
|
||||
name if name == &known::std => 1,
|
||||
_ => unreachable!(),
|
||||
},
|
||||
true => |name: &Name| match name {
|
||||
name if name == &known::core => 2,
|
||||
name if name == &known::alloc => 1,
|
||||
name if name == &known::std => 0,
|
||||
_ => unreachable!(),
|
||||
},
|
||||
};
|
||||
let nrank = rank(new);
|
||||
let orank = rank(old);
|
||||
match nrank.cmp(&orank) {
|
||||
Ordering::Less => old_path,
|
||||
Ordering::Equal => {
|
||||
if new_path.len() < old_path.len() {
|
||||
new_path
|
||||
} else {
|
||||
old_path
|
||||
}
|
||||
}
|
||||
Ordering::Greater => new_path,
|
||||
}
|
||||
}
|
||||
} else if new_path.starts_with_std() && old_path.can_start_with_std() {
|
||||
if prefer_no_std {
|
||||
cov_mark::hit!(prefer_no_std_paths);
|
||||
old_path
|
||||
} else {
|
||||
cov_mark::hit!(prefer_std_paths);
|
||||
new_path
|
||||
_ => {
|
||||
if new_path.len() < old_path.len() {
|
||||
new_path
|
||||
} else {
|
||||
old_path
|
||||
}
|
||||
}
|
||||
} else if new_path.len() < old_path.len() {
|
||||
new_path
|
||||
} else {
|
||||
old_path
|
||||
}
|
||||
}
|
||||
|
||||
// FIXME: Remove allocations
|
||||
/// Finds locations in `from.krate` from which `item` can be imported by `from`.
|
||||
fn find_local_import_locations(
|
||||
db: &dyn DefDatabase,
|
||||
|
@ -428,7 +529,8 @@ mod tests {
|
|||
.take_types()
|
||||
.unwrap();
|
||||
|
||||
let found_path = find_path_inner(&db, ItemInNs::Types(resolved), module, prefix_kind);
|
||||
let found_path =
|
||||
find_path_inner(&db, ItemInNs::Types(resolved), module, prefix_kind, false);
|
||||
assert_eq!(found_path, Some(mod_path), "{:?}", prefix_kind);
|
||||
}
|
||||
|
||||
|
@ -468,8 +570,8 @@ $0
|
|||
"#,
|
||||
"E::A",
|
||||
"E::A",
|
||||
"E::A",
|
||||
"E::A",
|
||||
"crate::E::A",
|
||||
"self::E::A",
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -788,7 +890,6 @@ pub use super::foo;
|
|||
|
||||
#[test]
|
||||
fn prefer_std_paths_over_alloc() {
|
||||
cov_mark::check!(prefer_std_paths);
|
||||
check_found_path(
|
||||
r#"
|
||||
//- /main.rs crate:main deps:alloc,std
|
||||
|
@ -813,7 +914,6 @@ pub mod sync {
|
|||
|
||||
#[test]
|
||||
fn prefer_core_paths_over_std() {
|
||||
cov_mark::check!(prefer_no_std_paths);
|
||||
check_found_path(
|
||||
r#"
|
||||
//- /main.rs crate:main deps:core,std
|
||||
|
|
|
@ -18,7 +18,7 @@ use crate::{
|
|||
ConstId, HasModule, ImplId, LocalModuleId, MacroId, ModuleDefId, ModuleId, TraitId,
|
||||
};
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
#[derive(Copy, Clone, Debug)]
|
||||
pub(crate) enum ImportType {
|
||||
Glob,
|
||||
Named,
|
||||
|
@ -302,13 +302,13 @@ impl ItemScope {
|
|||
$changed = true;
|
||||
}
|
||||
Entry::Occupied(mut entry)
|
||||
if $glob_imports.$field.contains(&$lookup)
|
||||
&& matches!($def_import_type, ImportType::Named) =>
|
||||
if matches!($def_import_type, ImportType::Named) =>
|
||||
{
|
||||
cov_mark::hit!(import_shadowed);
|
||||
$glob_imports.$field.remove(&$lookup);
|
||||
entry.insert(fld);
|
||||
$changed = true;
|
||||
if $glob_imports.$field.remove(&$lookup) {
|
||||
cov_mark::hit!(import_shadowed);
|
||||
entry.insert(fld);
|
||||
$changed = true;
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
@ -457,8 +457,15 @@ impl ItemInNs {
|
|||
/// Returns the crate defining this item (or `None` if `self` is built-in).
|
||||
pub fn krate(&self, db: &dyn DefDatabase) -> Option<CrateId> {
|
||||
match self {
|
||||
ItemInNs::Types(did) | ItemInNs::Values(did) => did.module(db).map(|m| m.krate),
|
||||
ItemInNs::Types(id) | ItemInNs::Values(id) => id.module(db).map(|m| m.krate),
|
||||
ItemInNs::Macros(id) => Some(id.module(db).krate),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn module(&self, db: &dyn DefDatabase) -> Option<ModuleId> {
|
||||
match self {
|
||||
ItemInNs::Types(id) | ItemInNs::Values(id) => id.module(db),
|
||||
ItemInNs::Macros(id) => Some(id.module(db)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -534,6 +534,7 @@ impl DefCollector<'_> {
|
|||
match per_ns.types {
|
||||
Some((ModuleDefId::ModuleId(m), _)) => {
|
||||
self.def_map.prelude = Some(m);
|
||||
break;
|
||||
}
|
||||
types => {
|
||||
tracing::debug!(
|
||||
|
|
|
@ -150,6 +150,14 @@ pub trait HirDatabase: DefDatabase + Upcast<dyn DefDatabase> {
|
|||
id: chalk_db::AssociatedTyValueId,
|
||||
) -> Arc<chalk_db::AssociatedTyValue>;
|
||||
|
||||
#[salsa::invoke(crate::traits::normalize_projection_query)]
|
||||
#[salsa::transparent]
|
||||
fn normalize_projection(
|
||||
&self,
|
||||
projection: crate::ProjectionTy,
|
||||
env: Arc<crate::TraitEnvironment>,
|
||||
) -> Ty;
|
||||
|
||||
#[salsa::invoke(trait_solve_wait)]
|
||||
#[salsa::transparent]
|
||||
fn trait_solve(
|
||||
|
|
|
@ -533,6 +533,7 @@ impl HirDisplay for Ty {
|
|||
f.db.upcast(),
|
||||
ItemInNs::Types((*def_id).into()),
|
||||
module_id,
|
||||
false,
|
||||
) {
|
||||
write!(f, "{}", path)?;
|
||||
} else {
|
||||
|
|
|
@ -673,10 +673,6 @@ impl<'a> InferenceContext<'a> {
|
|||
)
|
||||
}
|
||||
|
||||
fn resolve_obligations_as_possible(&mut self) {
|
||||
self.table.resolve_obligations_as_possible();
|
||||
}
|
||||
|
||||
fn push_obligation(&mut self, o: DomainGoal) {
|
||||
self.table.register_obligation(o.cast(Interner));
|
||||
}
|
||||
|
@ -696,7 +692,6 @@ impl<'a> InferenceContext<'a> {
|
|||
}
|
||||
|
||||
fn resolve_ty_shallow(&mut self, ty: &Ty) -> Ty {
|
||||
self.resolve_obligations_as_possible();
|
||||
self.table.resolve_ty_shallow(ty)
|
||||
}
|
||||
|
||||
|
|
|
@ -196,20 +196,6 @@ pub(crate) fn make_binders<T: HasInterner<Interner = Interner>>(
|
|||
make_binders_with_count(db, usize::MAX, generics, value)
|
||||
}
|
||||
|
||||
// FIXME: get rid of this
|
||||
pub fn make_canonical<T: HasInterner<Interner = Interner>>(
|
||||
value: T,
|
||||
kinds: impl IntoIterator<Item = TyVariableKind>,
|
||||
) -> Canonical<T> {
|
||||
let kinds = kinds.into_iter().map(|tk| {
|
||||
chalk_ir::CanonicalVarKind::new(
|
||||
chalk_ir::VariableKind::Ty(tk),
|
||||
chalk_ir::UniverseIndex::ROOT,
|
||||
)
|
||||
});
|
||||
Canonical { value, binders: chalk_ir::CanonicalVarKinds::from_iter(Interner, kinds) }
|
||||
}
|
||||
|
||||
// FIXME: get rid of this, just replace it by FnPointer
|
||||
/// A function signature as seen by type inference: Several parameter types and
|
||||
/// one return type.
|
||||
|
|
|
@ -914,22 +914,10 @@ fn iterate_trait_method_candidates(
|
|||
let db = table.db;
|
||||
let env = table.trait_env.clone();
|
||||
let self_is_array = matches!(self_ty.kind(Interner), chalk_ir::TyKind::Array(..));
|
||||
// if ty is `dyn Trait`, the trait doesn't need to be in scope
|
||||
let inherent_trait =
|
||||
self_ty.dyn_trait().into_iter().flat_map(|t| all_super_traits(db.upcast(), t));
|
||||
let env_traits = matches!(self_ty.kind(Interner), TyKind::Placeholder(_))
|
||||
// if we have `T: Trait` in the param env, the trait doesn't need to be in scope
|
||||
.then(|| {
|
||||
env.traits_in_scope_from_clauses(self_ty.clone())
|
||||
.flat_map(|t| all_super_traits(db.upcast(), t))
|
||||
})
|
||||
.into_iter()
|
||||
.flatten();
|
||||
let traits = inherent_trait.chain(env_traits).chain(traits_in_scope.iter().copied());
|
||||
|
||||
let canonical_self_ty = table.canonicalize(self_ty.clone()).value;
|
||||
|
||||
'traits: for t in traits {
|
||||
'traits: for &t in traits_in_scope {
|
||||
let data = db.trait_data(t);
|
||||
|
||||
// Traits annotated with `#[rustc_skip_array_during_method_dispatch]` are skipped during
|
||||
|
@ -979,6 +967,44 @@ fn iterate_inherent_methods(
|
|||
) -> ControlFlow<()> {
|
||||
let db = table.db;
|
||||
let env = table.trait_env.clone();
|
||||
|
||||
// For trait object types and placeholder types with trait bounds, the methods of the trait and
|
||||
// its super traits are considered inherent methods. This matters because these methods have
|
||||
// higher priority than the other traits' methods, which would be considered in
|
||||
// `iterate_trait_method_candidates()` only after this function.
|
||||
match self_ty.kind(Interner) {
|
||||
TyKind::Placeholder(_) => {
|
||||
let env = table.trait_env.clone();
|
||||
let traits = env
|
||||
.traits_in_scope_from_clauses(self_ty.clone())
|
||||
.flat_map(|t| all_super_traits(db.upcast(), t));
|
||||
iterate_inherent_trait_methods(
|
||||
self_ty,
|
||||
table,
|
||||
name,
|
||||
receiver_ty,
|
||||
receiver_adjustments.clone(),
|
||||
callback,
|
||||
traits,
|
||||
)?;
|
||||
}
|
||||
TyKind::Dyn(_) => {
|
||||
if let Some(principal_trait) = self_ty.dyn_trait() {
|
||||
let traits = all_super_traits(db.upcast(), principal_trait);
|
||||
iterate_inherent_trait_methods(
|
||||
self_ty,
|
||||
table,
|
||||
name,
|
||||
receiver_ty,
|
||||
receiver_adjustments.clone(),
|
||||
callback,
|
||||
traits.into_iter(),
|
||||
)?;
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
let def_crates = match def_crates(db, self_ty, env.krate) {
|
||||
Some(k) => k,
|
||||
None => return ControlFlow::Continue(()),
|
||||
|
@ -1020,6 +1046,28 @@ fn iterate_inherent_methods(
|
|||
}
|
||||
return ControlFlow::Continue(());
|
||||
|
||||
fn iterate_inherent_trait_methods(
|
||||
self_ty: &Ty,
|
||||
table: &mut InferenceTable<'_>,
|
||||
name: Option<&Name>,
|
||||
receiver_ty: Option<&Ty>,
|
||||
receiver_adjustments: Option<ReceiverAdjustments>,
|
||||
callback: &mut dyn FnMut(ReceiverAdjustments, AssocItemId) -> ControlFlow<()>,
|
||||
traits: impl Iterator<Item = TraitId>,
|
||||
) -> ControlFlow<()> {
|
||||
let db = table.db;
|
||||
for t in traits {
|
||||
let data = db.trait_data(t);
|
||||
for &(_, item) in data.items.iter() {
|
||||
// We don't pass `visible_from_module` as all trait items should be visible.
|
||||
if is_valid_candidate(table, name, receiver_ty, item, self_ty, None) {
|
||||
callback(receiver_adjustments.clone().unwrap_or_default(), item)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
ControlFlow::Continue(())
|
||||
}
|
||||
|
||||
fn impls_for_self_ty(
|
||||
impls: &InherentImpls,
|
||||
self_ty: &Ty,
|
||||
|
|
|
@ -1218,6 +1218,40 @@ fn main() {
|
|||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dyn_trait_method_priority() {
|
||||
check_types(
|
||||
r#"
|
||||
//- minicore: from
|
||||
trait Trait {
|
||||
fn into(&self) -> usize { 0 }
|
||||
}
|
||||
|
||||
fn foo(a: &dyn Trait) {
|
||||
let _ = a.into();
|
||||
//^usize
|
||||
}
|
||||
"#,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn trait_method_priority_for_placeholder_type() {
|
||||
check_types(
|
||||
r#"
|
||||
//- minicore: from
|
||||
trait Trait {
|
||||
fn into(&self) -> usize { 0 }
|
||||
}
|
||||
|
||||
fn foo<T: Trait>(a: &T) {
|
||||
let _ = a.into();
|
||||
//^usize
|
||||
}
|
||||
"#,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn autoderef_visibility_field() {
|
||||
check(
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
//! Trait solving using Chalk.
|
||||
|
||||
use std::env::var;
|
||||
use std::{env::var, sync::Arc};
|
||||
|
||||
use chalk_ir::GoalData;
|
||||
use chalk_recursive::Cache;
|
||||
|
@ -12,8 +12,9 @@ use stdx::panic_context;
|
|||
use syntax::SmolStr;
|
||||
|
||||
use crate::{
|
||||
db::HirDatabase, AliasEq, AliasTy, Canonical, DomainGoal, Goal, Guidance, InEnvironment,
|
||||
Interner, Solution, TraitRefExt, Ty, TyKind, WhereClause,
|
||||
db::HirDatabase, infer::unify::InferenceTable, AliasEq, AliasTy, Canonical, DomainGoal, Goal,
|
||||
Guidance, InEnvironment, Interner, ProjectionTy, Solution, TraitRefExt, Ty, TyKind,
|
||||
WhereClause,
|
||||
};
|
||||
|
||||
/// This controls how much 'time' we give the Chalk solver before giving up.
|
||||
|
@ -64,6 +65,16 @@ impl TraitEnvironment {
|
|||
}
|
||||
}
|
||||
|
||||
pub(crate) fn normalize_projection_query(
|
||||
db: &dyn HirDatabase,
|
||||
projection: ProjectionTy,
|
||||
env: Arc<TraitEnvironment>,
|
||||
) -> Ty {
|
||||
let mut table = InferenceTable::new(db, env);
|
||||
let ty = table.normalize_projection_ty(projection);
|
||||
table.resolve_completely(ty)
|
||||
}
|
||||
|
||||
/// Solve a trait goal using Chalk.
|
||||
pub(crate) fn trait_solve_query(
|
||||
db: &dyn HirDatabase,
|
||||
|
|
|
@ -63,10 +63,9 @@ use hir_ty::{
|
|||
primitive::UintTy,
|
||||
subst_prefix,
|
||||
traits::FnTrait,
|
||||
AliasEq, AliasTy, BoundVar, CallableDefId, CallableSig, Canonical, CanonicalVarKinds, Cast,
|
||||
ClosureId, DebruijnIndex, GenericArgData, InEnvironment, Interner, ParamKind,
|
||||
QuantifiedWhereClause, Scalar, Solution, Substitution, TraitEnvironment, TraitRefExt, Ty,
|
||||
TyBuilder, TyDefId, TyExt, TyKind, TyVariableKind, WhereClause,
|
||||
AliasTy, CallableDefId, CallableSig, Canonical, CanonicalVarKinds, Cast, ClosureId,
|
||||
GenericArgData, Interner, ParamKind, QuantifiedWhereClause, Scalar, Substitution,
|
||||
TraitEnvironment, TraitRefExt, Ty, TyBuilder, TyDefId, TyExt, TyKind, WhereClause,
|
||||
};
|
||||
use itertools::Itertools;
|
||||
use nameres::diagnostics::DefDiagnosticKind;
|
||||
|
@ -582,8 +581,13 @@ impl Module {
|
|||
|
||||
/// Finds a path that can be used to refer to the given item from within
|
||||
/// this module, if possible.
|
||||
pub fn find_use_path(self, db: &dyn DefDatabase, item: impl Into<ItemInNs>) -> Option<ModPath> {
|
||||
hir_def::find_path::find_path(db, item.into().into(), self.into())
|
||||
pub fn find_use_path(
|
||||
self,
|
||||
db: &dyn DefDatabase,
|
||||
item: impl Into<ItemInNs>,
|
||||
prefer_no_std: bool,
|
||||
) -> Option<ModPath> {
|
||||
hir_def::find_path::find_path(db, item.into().into(), self.into(), prefer_no_std)
|
||||
}
|
||||
|
||||
/// Finds a path that can be used to refer to the given item from within
|
||||
|
@ -593,8 +597,15 @@ impl Module {
|
|||
db: &dyn DefDatabase,
|
||||
item: impl Into<ItemInNs>,
|
||||
prefix_kind: PrefixKind,
|
||||
prefer_no_std: bool,
|
||||
) -> Option<ModPath> {
|
||||
hir_def::find_path::find_path_prefixed(db, item.into().into(), self.into(), prefix_kind)
|
||||
hir_def::find_path::find_path_prefixed(
|
||||
db,
|
||||
item.into().into(),
|
||||
self.into(),
|
||||
prefix_kind,
|
||||
prefer_no_std,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -2880,27 +2891,12 @@ impl Type {
|
|||
}
|
||||
})
|
||||
.build();
|
||||
let goal = hir_ty::make_canonical(
|
||||
InEnvironment::new(
|
||||
&self.env.env,
|
||||
AliasEq {
|
||||
alias: AliasTy::Projection(projection),
|
||||
ty: TyKind::BoundVar(BoundVar::new(DebruijnIndex::INNERMOST, 0))
|
||||
.intern(Interner),
|
||||
}
|
||||
.cast(Interner),
|
||||
),
|
||||
[TyVariableKind::General].into_iter(),
|
||||
);
|
||||
|
||||
match db.trait_solve(self.env.krate, goal)? {
|
||||
Solution::Unique(s) => s
|
||||
.value
|
||||
.subst
|
||||
.as_slice(Interner)
|
||||
.first()
|
||||
.map(|ty| self.derived(ty.assert_ty_ref(Interner).clone())),
|
||||
Solution::Ambig(_) => None,
|
||||
let ty = db.normalize_projection(projection, self.env.clone());
|
||||
if ty.is_unknown() {
|
||||
None
|
||||
} else {
|
||||
Some(self.derived(ty))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -13,4 +13,5 @@ pub struct AssistConfig {
|
|||
pub snippet_cap: Option<SnippetCap>,
|
||||
pub allowed: Option<Vec<AssistKind>>,
|
||||
pub insert_use: InsertUseConfig,
|
||||
pub prefer_no_std: bool,
|
||||
}
|
||||
|
|
|
@ -87,7 +87,7 @@ pub(crate) fn add_missing_match_arms(acc: &mut Assists, ctx: &AssistContext<'_>)
|
|||
.into_iter()
|
||||
.filter_map(|variant| {
|
||||
Some((
|
||||
build_pat(ctx.db(), module, variant)?,
|
||||
build_pat(ctx.db(), module, variant, ctx.config.prefer_no_std)?,
|
||||
variant.should_be_hidden(ctx.db(), module.krate()),
|
||||
))
|
||||
})
|
||||
|
@ -132,8 +132,9 @@ pub(crate) fn add_missing_match_arms(acc: &mut Assists, ctx: &AssistContext<'_>)
|
|||
let is_hidden = variants
|
||||
.iter()
|
||||
.any(|variant| variant.should_be_hidden(ctx.db(), module.krate()));
|
||||
let patterns =
|
||||
variants.into_iter().filter_map(|variant| build_pat(ctx.db(), module, variant));
|
||||
let patterns = variants.into_iter().filter_map(|variant| {
|
||||
build_pat(ctx.db(), module, variant, ctx.config.prefer_no_std)
|
||||
});
|
||||
|
||||
(ast::Pat::from(make::tuple_pat(patterns)), is_hidden)
|
||||
})
|
||||
|
@ -349,10 +350,16 @@ fn resolve_tuple_of_enum_def(
|
|||
.collect()
|
||||
}
|
||||
|
||||
fn build_pat(db: &RootDatabase, module: hir::Module, var: ExtendedVariant) -> Option<ast::Pat> {
|
||||
fn build_pat(
|
||||
db: &RootDatabase,
|
||||
module: hir::Module,
|
||||
var: ExtendedVariant,
|
||||
prefer_no_std: bool,
|
||||
) -> Option<ast::Pat> {
|
||||
match var {
|
||||
ExtendedVariant::Variant(var) => {
|
||||
let path = mod_path_to_ast(&module.find_use_path(db, ModuleDef::from(var))?);
|
||||
let path =
|
||||
mod_path_to_ast(&module.find_use_path(db, ModuleDef::from(var), prefer_no_std)?);
|
||||
|
||||
// FIXME: use HIR for this; it doesn't currently expose struct vs. tuple vs. unit variants though
|
||||
let pat: ast::Pat = match var.source(db)?.value.kind() {
|
||||
|
|
|
@ -89,8 +89,11 @@ use crate::{AssistContext, AssistId, AssistKind, Assists, GroupLabel};
|
|||
// ```
|
||||
pub(crate) fn auto_import(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
|
||||
let (import_assets, syntax_under_caret) = find_importable_node(ctx)?;
|
||||
let mut proposed_imports =
|
||||
import_assets.search_for_imports(&ctx.sema, ctx.config.insert_use.prefix_kind);
|
||||
let mut proposed_imports = import_assets.search_for_imports(
|
||||
&ctx.sema,
|
||||
ctx.config.insert_use.prefix_kind,
|
||||
ctx.config.prefer_no_std,
|
||||
);
|
||||
if proposed_imports.is_empty() {
|
||||
return None;
|
||||
}
|
||||
|
|
|
@ -50,7 +50,7 @@ pub(crate) fn convert_into_to_from(acc: &mut Assists, ctx: &AssistContext<'_>) -
|
|||
_ => return None,
|
||||
};
|
||||
|
||||
mod_path_to_ast(&module.find_use_path(ctx.db(), src_type_def)?)
|
||||
mod_path_to_ast(&module.find_use_path(ctx.db(), src_type_def, ctx.config.prefer_no_std)?)
|
||||
};
|
||||
|
||||
let dest_type = match &ast_trait {
|
||||
|
|
|
@ -152,6 +152,7 @@ pub(crate) fn extract_function(acc: &mut Assists, ctx: &AssistContext<'_>) -> Op
|
|||
ctx.sema.db,
|
||||
ModuleDef::from(control_flow_enum),
|
||||
ctx.config.insert_use.prefix_kind,
|
||||
ctx.config.prefer_no_std,
|
||||
);
|
||||
|
||||
if let Some(mod_path) = mod_path {
|
||||
|
|
|
@ -409,6 +409,7 @@ fn process_references(
|
|||
ctx.sema.db,
|
||||
*enum_module_def,
|
||||
ctx.config.insert_use.prefix_kind,
|
||||
ctx.config.prefer_no_std,
|
||||
);
|
||||
if let Some(mut mod_path) = mod_path {
|
||||
mod_path.pop_segment();
|
||||
|
|
|
@ -58,7 +58,8 @@ fn generate_record_deref(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<(
|
|||
|
||||
let module = ctx.sema.to_def(&strukt)?.module(ctx.db());
|
||||
let trait_ = deref_type_to_generate.to_trait(&ctx.sema, module.krate())?;
|
||||
let trait_path = module.find_use_path(ctx.db(), ModuleDef::Trait(trait_))?;
|
||||
let trait_path =
|
||||
module.find_use_path(ctx.db(), ModuleDef::Trait(trait_), ctx.config.prefer_no_std)?;
|
||||
|
||||
let field_type = field.ty()?;
|
||||
let field_name = field.name()?;
|
||||
|
@ -98,7 +99,8 @@ fn generate_tuple_deref(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()
|
|||
|
||||
let module = ctx.sema.to_def(&strukt)?.module(ctx.db());
|
||||
let trait_ = deref_type_to_generate.to_trait(&ctx.sema, module.krate())?;
|
||||
let trait_path = module.find_use_path(ctx.db(), ModuleDef::Trait(trait_))?;
|
||||
let trait_path =
|
||||
module.find_use_path(ctx.db(), ModuleDef::Trait(trait_), ctx.config.prefer_no_std)?;
|
||||
|
||||
let field_type = field.ty()?;
|
||||
let target = field.syntax().text_range();
|
||||
|
|
|
@ -60,8 +60,11 @@ pub(crate) fn generate_new(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option
|
|||
|
||||
let item_in_ns = hir::ItemInNs::from(hir::ModuleDef::from(ty.as_adt()?));
|
||||
|
||||
let type_path = current_module
|
||||
.find_use_path(ctx.sema.db, item_for_path_search(ctx.sema.db, item_in_ns)?)?;
|
||||
let type_path = current_module.find_use_path(
|
||||
ctx.sema.db,
|
||||
item_for_path_search(ctx.sema.db, item_in_ns)?,
|
||||
ctx.config.prefer_no_std,
|
||||
)?;
|
||||
|
||||
let expr = use_trivial_constructor(
|
||||
&ctx.sema.db,
|
||||
|
|
268
crates/ide-assists/src/handlers/move_format_string_arg.rs
Normal file
268
crates/ide-assists/src/handlers/move_format_string_arg.rs
Normal file
|
@ -0,0 +1,268 @@
|
|||
use crate::{AssistContext, Assists};
|
||||
use ide_db::{
|
||||
assists::{AssistId, AssistKind},
|
||||
syntax_helpers::{
|
||||
format_string::is_format_string,
|
||||
format_string_exprs::{parse_format_exprs, Arg},
|
||||
},
|
||||
};
|
||||
use itertools::Itertools;
|
||||
use syntax::{ast, AstNode, AstToken, NodeOrToken, SyntaxKind::COMMA, TextRange};
|
||||
|
||||
// Assist: move_format_string_arg
|
||||
//
|
||||
// Move an expression out of a format string.
|
||||
//
|
||||
// ```
|
||||
// macro_rules! format_args {
|
||||
// ($lit:literal $(tt:tt)*) => { 0 },
|
||||
// }
|
||||
// macro_rules! print {
|
||||
// ($($arg:tt)*) => (std::io::_print(format_args!($($arg)*)));
|
||||
// }
|
||||
//
|
||||
// fn main() {
|
||||
// print!("{x + 1}$0");
|
||||
// }
|
||||
// ```
|
||||
// ->
|
||||
// ```
|
||||
// macro_rules! format_args {
|
||||
// ($lit:literal $(tt:tt)*) => { 0 },
|
||||
// }
|
||||
// macro_rules! print {
|
||||
// ($($arg:tt)*) => (std::io::_print(format_args!($($arg)*)));
|
||||
// }
|
||||
//
|
||||
// fn main() {
|
||||
// print!("{}"$0, x + 1);
|
||||
// }
|
||||
// ```
|
||||
|
||||
pub(crate) fn move_format_string_arg(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
|
||||
let fmt_string = ctx.find_token_at_offset::<ast::String>()?;
|
||||
let tt = fmt_string.syntax().parent().and_then(ast::TokenTree::cast)?;
|
||||
|
||||
let expanded_t = ast::String::cast(
|
||||
ctx.sema.descend_into_macros_with_kind_preference(fmt_string.syntax().clone()),
|
||||
)?;
|
||||
if !is_format_string(&expanded_t) {
|
||||
return None;
|
||||
}
|
||||
|
||||
let (new_fmt, extracted_args) = parse_format_exprs(fmt_string.text()).ok()?;
|
||||
if extracted_args.is_empty() {
|
||||
return None;
|
||||
}
|
||||
|
||||
acc.add(
|
||||
AssistId(
|
||||
"move_format_string_arg",
|
||||
// if there aren't any expressions, then make the assist a RefactorExtract
|
||||
if extracted_args.iter().filter(|f| matches!(f, Arg::Expr(_))).count() == 0 {
|
||||
AssistKind::RefactorExtract
|
||||
} else {
|
||||
AssistKind::QuickFix
|
||||
},
|
||||
),
|
||||
"Extract format args",
|
||||
tt.syntax().text_range(),
|
||||
|edit| {
|
||||
let fmt_range = fmt_string.syntax().text_range();
|
||||
|
||||
// Replace old format string with new format string whose arguments have been extracted
|
||||
edit.replace(fmt_range, new_fmt);
|
||||
|
||||
// Insert cursor at end of format string
|
||||
edit.insert(fmt_range.end(), "$0");
|
||||
|
||||
// Extract existing arguments in macro
|
||||
let tokens =
|
||||
tt.token_trees_and_tokens().filter_map(NodeOrToken::into_token).collect_vec();
|
||||
|
||||
let mut existing_args: Vec<String> = vec![];
|
||||
|
||||
let mut current_arg = String::new();
|
||||
if let [_opening_bracket, format_string, _args_start_comma, tokens @ .., end_bracket] =
|
||||
tokens.as_slice()
|
||||
{
|
||||
for t in tokens {
|
||||
if t.kind() == COMMA {
|
||||
existing_args.push(current_arg.trim().into());
|
||||
current_arg.clear();
|
||||
} else {
|
||||
current_arg.push_str(t.text());
|
||||
}
|
||||
}
|
||||
existing_args.push(current_arg.trim().into());
|
||||
|
||||
// delete everything after the format string till end bracket
|
||||
// we're going to insert the new arguments later
|
||||
edit.delete(TextRange::new(
|
||||
format_string.text_range().end(),
|
||||
end_bracket.text_range().start(),
|
||||
));
|
||||
}
|
||||
|
||||
// Start building the new args
|
||||
let mut existing_args = existing_args.into_iter();
|
||||
let mut args = String::new();
|
||||
|
||||
let mut placeholder_idx = 1;
|
||||
|
||||
for extracted_args in extracted_args {
|
||||
// remove expr from format string
|
||||
args.push_str(", ");
|
||||
|
||||
match extracted_args {
|
||||
Arg::Ident(s) | Arg::Expr(s) => {
|
||||
// insert arg
|
||||
args.push_str(&s);
|
||||
}
|
||||
Arg::Placeholder => {
|
||||
// try matching with existing argument
|
||||
match existing_args.next() {
|
||||
Some(ea) => {
|
||||
args.push_str(&ea);
|
||||
}
|
||||
None => {
|
||||
// insert placeholder
|
||||
args.push_str(&format!("${placeholder_idx}"));
|
||||
placeholder_idx += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Insert new args
|
||||
edit.insert(fmt_range.end(), args);
|
||||
},
|
||||
);
|
||||
|
||||
Some(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::tests::check_assist;
|
||||
|
||||
const MACRO_DECL: &'static str = r#"
|
||||
macro_rules! format_args {
|
||||
($lit:literal $(tt:tt)*) => { 0 },
|
||||
}
|
||||
macro_rules! print {
|
||||
($($arg:tt)*) => (std::io::_print(format_args!($($arg)*)));
|
||||
}
|
||||
"#;
|
||||
|
||||
fn add_macro_decl(s: &'static str) -> String {
|
||||
MACRO_DECL.to_string() + s
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn multiple_middle_arg() {
|
||||
check_assist(
|
||||
move_format_string_arg,
|
||||
&add_macro_decl(
|
||||
r#"
|
||||
fn main() {
|
||||
print!("{} {x + 1:b} {}$0", y + 2, 2);
|
||||
}
|
||||
"#,
|
||||
),
|
||||
&add_macro_decl(
|
||||
r#"
|
||||
fn main() {
|
||||
print!("{} {:b} {}"$0, y + 2, x + 1, 2);
|
||||
}
|
||||
"#,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn single_arg() {
|
||||
check_assist(
|
||||
move_format_string_arg,
|
||||
&add_macro_decl(
|
||||
r#"
|
||||
fn main() {
|
||||
print!("{obj.value:b}$0",);
|
||||
}
|
||||
"#,
|
||||
),
|
||||
&add_macro_decl(
|
||||
r#"
|
||||
fn main() {
|
||||
print!("{:b}"$0, obj.value);
|
||||
}
|
||||
"#,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn multiple_middle_placeholders_arg() {
|
||||
check_assist(
|
||||
move_format_string_arg,
|
||||
&add_macro_decl(
|
||||
r#"
|
||||
fn main() {
|
||||
print!("{} {x + 1:b} {} {}$0", y + 2, 2);
|
||||
}
|
||||
"#,
|
||||
),
|
||||
&add_macro_decl(
|
||||
r#"
|
||||
fn main() {
|
||||
print!("{} {:b} {} {}"$0, y + 2, x + 1, 2, $1);
|
||||
}
|
||||
"#,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn multiple_trailing_args() {
|
||||
check_assist(
|
||||
move_format_string_arg,
|
||||
&add_macro_decl(
|
||||
r#"
|
||||
fn main() {
|
||||
print!("{} {x + 1:b} {Struct(1, 2)}$0", 1);
|
||||
}
|
||||
"#,
|
||||
),
|
||||
&add_macro_decl(
|
||||
r#"
|
||||
fn main() {
|
||||
print!("{} {:b} {}"$0, 1, x + 1, Struct(1, 2));
|
||||
}
|
||||
"#,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn improper_commas() {
|
||||
check_assist(
|
||||
move_format_string_arg,
|
||||
&add_macro_decl(
|
||||
r#"
|
||||
fn main() {
|
||||
print!("{} {x + 1:b} {Struct(1, 2)}$0", 1,);
|
||||
}
|
||||
"#,
|
||||
),
|
||||
&add_macro_decl(
|
||||
r#"
|
||||
fn main() {
|
||||
print!("{} {:b} {}"$0, 1, x + 1, Struct(1, 2));
|
||||
}
|
||||
"#,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
|
@ -44,8 +44,11 @@ pub(crate) fn qualify_method_call(acc: &mut Assists, ctx: &AssistContext<'_>) ->
|
|||
let current_module = ctx.sema.scope(call.syntax())?.module();
|
||||
let target_module_def = ModuleDef::from(resolved_call);
|
||||
let item_in_ns = ItemInNs::from(target_module_def);
|
||||
let receiver_path = current_module
|
||||
.find_use_path(ctx.sema.db, item_for_path_search(ctx.sema.db, item_in_ns)?)?;
|
||||
let receiver_path = current_module.find_use_path(
|
||||
ctx.sema.db,
|
||||
item_for_path_search(ctx.sema.db, item_in_ns)?,
|
||||
ctx.config.prefer_no_std,
|
||||
)?;
|
||||
|
||||
let qualify_candidate = QualifyCandidate::ImplMethod(ctx.sema.db, call, resolved_call);
|
||||
|
||||
|
|
|
@ -37,7 +37,8 @@ use crate::{
|
|||
// ```
|
||||
pub(crate) fn qualify_path(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
|
||||
let (import_assets, syntax_under_caret) = find_importable_node(ctx)?;
|
||||
let mut proposed_imports = import_assets.search_for_relative_paths(&ctx.sema);
|
||||
let mut proposed_imports =
|
||||
import_assets.search_for_relative_paths(&ctx.sema, ctx.config.prefer_no_std);
|
||||
if proposed_imports.is_empty() {
|
||||
return None;
|
||||
}
|
||||
|
|
|
@ -85,7 +85,7 @@ pub(crate) fn replace_derive_with_manual_impl(
|
|||
})
|
||||
.flat_map(|trait_| {
|
||||
current_module
|
||||
.find_use_path(ctx.sema.db, hir::ModuleDef::Trait(trait_))
|
||||
.find_use_path(ctx.sema.db, hir::ModuleDef::Trait(trait_), ctx.config.prefer_no_std)
|
||||
.as_ref()
|
||||
.map(mod_path_to_ast)
|
||||
.zip(Some(trait_))
|
||||
|
|
|
@ -67,6 +67,7 @@ pub(crate) fn replace_qualified_name_with_use(
|
|||
ctx.sema.db,
|
||||
module,
|
||||
ctx.config.insert_use.prefix_kind,
|
||||
ctx.config.prefer_no_std,
|
||||
)
|
||||
})
|
||||
.flatten();
|
||||
|
|
|
@ -136,6 +136,7 @@ mod handlers {
|
|||
mod flip_binexpr;
|
||||
mod flip_comma;
|
||||
mod flip_trait_bound;
|
||||
mod move_format_string_arg;
|
||||
mod generate_constant;
|
||||
mod generate_default_from_enum_variant;
|
||||
mod generate_default_from_new;
|
||||
|
@ -254,6 +255,7 @@ mod handlers {
|
|||
merge_imports::merge_imports,
|
||||
merge_match_arms::merge_match_arms,
|
||||
move_bounds::move_bounds_to_where_clause,
|
||||
move_format_string_arg::move_format_string_arg,
|
||||
move_guard::move_arm_cond_to_match_guard,
|
||||
move_guard::move_guard_to_arm_body,
|
||||
move_module_to_file::move_module_to_file,
|
||||
|
|
|
@ -29,6 +29,7 @@ pub(crate) const TEST_CONFIG: AssistConfig = AssistConfig {
|
|||
group: true,
|
||||
skip_glob_imports: true,
|
||||
},
|
||||
prefer_no_std: false,
|
||||
};
|
||||
|
||||
pub(crate) fn with_single_file(text: &str) -> (RootDatabase, FileId) {
|
||||
|
|
|
@ -1591,6 +1591,37 @@ fn apply<T, U, F>(f: F, x: T) -> U where F: FnOnce(T) -> U {
|
|||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn doctest_move_format_string_arg() {
|
||||
check_doc_test(
|
||||
"move_format_string_arg",
|
||||
r#####"
|
||||
macro_rules! format_args {
|
||||
($lit:literal $(tt:tt)*) => { 0 },
|
||||
}
|
||||
macro_rules! print {
|
||||
($($arg:tt)*) => (std::io::_print(format_args!($($arg)*)));
|
||||
}
|
||||
|
||||
fn main() {
|
||||
print!("{x + 1}$0");
|
||||
}
|
||||
"#####,
|
||||
r#####"
|
||||
macro_rules! format_args {
|
||||
($lit:literal $(tt:tt)*) => { 0 },
|
||||
}
|
||||
macro_rules! print {
|
||||
($($arg:tt)*) => (std::io::_print(format_args!($($arg)*)));
|
||||
}
|
||||
|
||||
fn main() {
|
||||
print!("{}"$0, x + 1);
|
||||
}
|
||||
"#####,
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn doctest_move_from_mod_rs() {
|
||||
check_doc_test(
|
||||
|
|
|
@ -551,7 +551,11 @@ fn enum_variants_with_paths(
|
|||
}
|
||||
|
||||
for variant in variants {
|
||||
if let Some(path) = ctx.module.find_use_path(ctx.db, hir::ModuleDef::from(variant)) {
|
||||
if let Some(path) = ctx.module.find_use_path(
|
||||
ctx.db,
|
||||
hir::ModuleDef::from(variant),
|
||||
ctx.config.prefer_no_std,
|
||||
) {
|
||||
// Variants with trivial paths are already added by the existing completion logic,
|
||||
// so we should avoid adding these twice
|
||||
if path.segments().len() > 1 {
|
||||
|
|
|
@ -165,7 +165,11 @@ pub(crate) fn complete_expr_path(
|
|||
hir::Adt::Struct(strukt) => {
|
||||
let path = ctx
|
||||
.module
|
||||
.find_use_path(ctx.db, hir::ModuleDef::from(strukt))
|
||||
.find_use_path(
|
||||
ctx.db,
|
||||
hir::ModuleDef::from(strukt),
|
||||
ctx.config.prefer_no_std,
|
||||
)
|
||||
.filter(|it| it.len() > 1);
|
||||
|
||||
acc.add_struct_literal(ctx, path_ctx, strukt, path, None);
|
||||
|
@ -183,7 +187,11 @@ pub(crate) fn complete_expr_path(
|
|||
hir::Adt::Union(un) => {
|
||||
let path = ctx
|
||||
.module
|
||||
.find_use_path(ctx.db, hir::ModuleDef::from(un))
|
||||
.find_use_path(
|
||||
ctx.db,
|
||||
hir::ModuleDef::from(un),
|
||||
ctx.config.prefer_no_std,
|
||||
)
|
||||
.filter(|it| it.len() > 1);
|
||||
|
||||
acc.add_union_literal(ctx, un, path, None);
|
||||
|
|
|
@ -262,7 +262,11 @@ fn import_on_the_fly(
|
|||
|
||||
acc.add_all(
|
||||
import_assets
|
||||
.search_for_imports(&ctx.sema, ctx.config.insert_use.prefix_kind)
|
||||
.search_for_imports(
|
||||
&ctx.sema,
|
||||
ctx.config.insert_use.prefix_kind,
|
||||
ctx.config.prefer_no_std,
|
||||
)
|
||||
.into_iter()
|
||||
.filter(ns_filter)
|
||||
.filter(|import| {
|
||||
|
@ -306,7 +310,11 @@ fn import_on_the_fly_pat_(
|
|||
|
||||
acc.add_all(
|
||||
import_assets
|
||||
.search_for_imports(&ctx.sema, ctx.config.insert_use.prefix_kind)
|
||||
.search_for_imports(
|
||||
&ctx.sema,
|
||||
ctx.config.insert_use.prefix_kind,
|
||||
ctx.config.prefer_no_std,
|
||||
)
|
||||
.into_iter()
|
||||
.filter(ns_filter)
|
||||
.filter(|import| {
|
||||
|
@ -344,7 +352,7 @@ fn import_on_the_fly_method(
|
|||
let user_input_lowercased = potential_import_name.to_lowercase();
|
||||
|
||||
import_assets
|
||||
.search_for_imports(&ctx.sema, ctx.config.insert_use.prefix_kind)
|
||||
.search_for_imports(&ctx.sema, ctx.config.insert_use.prefix_kind, ctx.config.prefer_no_std)
|
||||
.into_iter()
|
||||
.filter(|import| {
|
||||
!ctx.is_item_hidden(&import.item_to_import)
|
||||
|
|
|
@ -145,6 +145,7 @@ pub(crate) fn complete_pattern_path(
|
|||
u.ty(ctx.db)
|
||||
}
|
||||
hir::PathResolution::Def(hir::ModuleDef::BuiltinType(ty)) => ty.ty(ctx.db),
|
||||
hir::PathResolution::Def(hir::ModuleDef::TypeAlias(ty)) => ty.ty(ctx.db),
|
||||
_ => return,
|
||||
};
|
||||
|
||||
|
|
|
@ -16,8 +16,11 @@
|
|||
//
|
||||
// image::https://user-images.githubusercontent.com/48062697/113020656-b560f500-917a-11eb-87de-02991f61beb8.gif[]
|
||||
|
||||
use ide_db::SnippetCap;
|
||||
use syntax::ast::{self, AstToken};
|
||||
use ide_db::{
|
||||
syntax_helpers::format_string_exprs::{parse_format_exprs, with_placeholders},
|
||||
SnippetCap,
|
||||
};
|
||||
use syntax::{ast, AstToken};
|
||||
|
||||
use crate::{
|
||||
completions::postfix::build_postfix_snippet_builder, context::CompletionContext, Completions,
|
||||
|
@ -43,250 +46,24 @@ pub(crate) fn add_format_like_completions(
|
|||
cap: SnippetCap,
|
||||
receiver_text: &ast::String,
|
||||
) {
|
||||
let input = match string_literal_contents(receiver_text) {
|
||||
// It's not a string literal, do not parse input.
|
||||
Some(input) => input,
|
||||
None => return,
|
||||
};
|
||||
|
||||
let postfix_snippet = match build_postfix_snippet_builder(ctx, cap, dot_receiver) {
|
||||
Some(it) => it,
|
||||
None => return,
|
||||
};
|
||||
let mut parser = FormatStrParser::new(input);
|
||||
|
||||
if parser.parse().is_ok() {
|
||||
if let Ok((out, exprs)) = parse_format_exprs(receiver_text.text()) {
|
||||
let exprs = with_placeholders(exprs);
|
||||
for (label, macro_name) in KINDS {
|
||||
let snippet = parser.to_suggestion(macro_name);
|
||||
let snippet = format!(r#"{}({}, {})"#, macro_name, out, exprs.join(", "));
|
||||
|
||||
postfix_snippet(label, macro_name, &snippet).add_to(acc);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Checks whether provided item is a string literal.
|
||||
fn string_literal_contents(item: &ast::String) -> Option<String> {
|
||||
let item = item.text();
|
||||
if item.len() >= 2 && item.starts_with('\"') && item.ends_with('\"') {
|
||||
return Some(item[1..item.len() - 1].to_owned());
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
/// Parser for a format-like string. It is more allowing in terms of string contents,
|
||||
/// as we expect variable placeholders to be filled with expressions.
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct FormatStrParser {
|
||||
input: String,
|
||||
output: String,
|
||||
extracted_expressions: Vec<String>,
|
||||
state: State,
|
||||
parsed: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||
enum State {
|
||||
NotExpr,
|
||||
MaybeExpr,
|
||||
Expr,
|
||||
MaybeIncorrect,
|
||||
FormatOpts,
|
||||
}
|
||||
|
||||
impl FormatStrParser {
|
||||
pub(crate) fn new(input: String) -> Self {
|
||||
Self {
|
||||
input,
|
||||
output: String::new(),
|
||||
extracted_expressions: Vec::new(),
|
||||
state: State::NotExpr,
|
||||
parsed: false,
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn parse(&mut self) -> Result<(), ()> {
|
||||
let mut current_expr = String::new();
|
||||
|
||||
let mut placeholder_id = 1;
|
||||
|
||||
// Count of open braces inside of an expression.
|
||||
// We assume that user knows what they're doing, thus we treat it like a correct pattern, e.g.
|
||||
// "{MyStruct { val_a: 0, val_b: 1 }}".
|
||||
let mut inexpr_open_count = 0;
|
||||
|
||||
// We need to escape '\' and '$'. See the comments on `get_receiver_text()` for detail.
|
||||
let mut chars = self.input.chars().peekable();
|
||||
while let Some(chr) = chars.next() {
|
||||
match (self.state, chr) {
|
||||
(State::NotExpr, '{') => {
|
||||
self.output.push(chr);
|
||||
self.state = State::MaybeExpr;
|
||||
}
|
||||
(State::NotExpr, '}') => {
|
||||
self.output.push(chr);
|
||||
self.state = State::MaybeIncorrect;
|
||||
}
|
||||
(State::NotExpr, _) => {
|
||||
if matches!(chr, '\\' | '$') {
|
||||
self.output.push('\\');
|
||||
}
|
||||
self.output.push(chr);
|
||||
}
|
||||
(State::MaybeIncorrect, '}') => {
|
||||
// It's okay, we met "}}".
|
||||
self.output.push(chr);
|
||||
self.state = State::NotExpr;
|
||||
}
|
||||
(State::MaybeIncorrect, _) => {
|
||||
// Error in the string.
|
||||
return Err(());
|
||||
}
|
||||
(State::MaybeExpr, '{') => {
|
||||
self.output.push(chr);
|
||||
self.state = State::NotExpr;
|
||||
}
|
||||
(State::MaybeExpr, '}') => {
|
||||
// This is an empty sequence '{}'. Replace it with placeholder.
|
||||
self.output.push(chr);
|
||||
self.extracted_expressions.push(format!("${}", placeholder_id));
|
||||
placeholder_id += 1;
|
||||
self.state = State::NotExpr;
|
||||
}
|
||||
(State::MaybeExpr, _) => {
|
||||
if matches!(chr, '\\' | '$') {
|
||||
current_expr.push('\\');
|
||||
}
|
||||
current_expr.push(chr);
|
||||
self.state = State::Expr;
|
||||
}
|
||||
(State::Expr, '}') => {
|
||||
if inexpr_open_count == 0 {
|
||||
self.output.push(chr);
|
||||
self.extracted_expressions.push(current_expr.trim().into());
|
||||
current_expr = String::new();
|
||||
self.state = State::NotExpr;
|
||||
} else {
|
||||
// We're closing one brace met before inside of the expression.
|
||||
current_expr.push(chr);
|
||||
inexpr_open_count -= 1;
|
||||
}
|
||||
}
|
||||
(State::Expr, ':') if chars.peek().copied() == Some(':') => {
|
||||
// path separator
|
||||
current_expr.push_str("::");
|
||||
chars.next();
|
||||
}
|
||||
(State::Expr, ':') => {
|
||||
if inexpr_open_count == 0 {
|
||||
// We're outside of braces, thus assume that it's a specifier, like "{Some(value):?}"
|
||||
self.output.push(chr);
|
||||
self.extracted_expressions.push(current_expr.trim().into());
|
||||
current_expr = String::new();
|
||||
self.state = State::FormatOpts;
|
||||
} else {
|
||||
// We're inside of braced expression, assume that it's a struct field name/value delimiter.
|
||||
current_expr.push(chr);
|
||||
}
|
||||
}
|
||||
(State::Expr, '{') => {
|
||||
current_expr.push(chr);
|
||||
inexpr_open_count += 1;
|
||||
}
|
||||
(State::Expr, _) => {
|
||||
if matches!(chr, '\\' | '$') {
|
||||
current_expr.push('\\');
|
||||
}
|
||||
current_expr.push(chr);
|
||||
}
|
||||
(State::FormatOpts, '}') => {
|
||||
self.output.push(chr);
|
||||
self.state = State::NotExpr;
|
||||
}
|
||||
(State::FormatOpts, _) => {
|
||||
if matches!(chr, '\\' | '$') {
|
||||
self.output.push('\\');
|
||||
}
|
||||
self.output.push(chr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if self.state != State::NotExpr {
|
||||
return Err(());
|
||||
}
|
||||
|
||||
self.parsed = true;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn to_suggestion(&self, macro_name: &str) -> String {
|
||||
assert!(self.parsed, "Attempt to get a suggestion from not parsed expression");
|
||||
|
||||
let expressions_as_string = self.extracted_expressions.join(", ");
|
||||
format!(r#"{}("{}", {})"#, macro_name, self.output, expressions_as_string)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use expect_test::{expect, Expect};
|
||||
|
||||
fn check(input: &str, expect: &Expect) {
|
||||
let mut parser = FormatStrParser::new((*input).to_owned());
|
||||
let outcome_repr = if parser.parse().is_ok() {
|
||||
// Parsing should be OK, expected repr is "string; expr_1, expr_2".
|
||||
if parser.extracted_expressions.is_empty() {
|
||||
parser.output
|
||||
} else {
|
||||
format!("{}; {}", parser.output, parser.extracted_expressions.join(", "))
|
||||
}
|
||||
} else {
|
||||
// Parsing should fail, expected repr is "-".
|
||||
"-".to_owned()
|
||||
};
|
||||
|
||||
expect.assert_eq(&outcome_repr);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn format_str_parser() {
|
||||
let test_vector = &[
|
||||
("no expressions", expect![["no expressions"]]),
|
||||
(r"no expressions with \$0$1", expect![r"no expressions with \\\$0\$1"]),
|
||||
("{expr} is {2 + 2}", expect![["{} is {}; expr, 2 + 2"]]),
|
||||
("{expr:?}", expect![["{:?}; expr"]]),
|
||||
("{expr:1$}", expect![[r"{:1\$}; expr"]]),
|
||||
("{$0}", expect![[r"{}; \$0"]]),
|
||||
("{malformed", expect![["-"]]),
|
||||
("malformed}", expect![["-"]]),
|
||||
("{{correct", expect![["{{correct"]]),
|
||||
("correct}}", expect![["correct}}"]]),
|
||||
("{correct}}}", expect![["{}}}; correct"]]),
|
||||
("{correct}}}}}", expect![["{}}}}}; correct"]]),
|
||||
("{incorrect}}", expect![["-"]]),
|
||||
("placeholders {} {}", expect![["placeholders {} {}; $1, $2"]]),
|
||||
("mixed {} {2 + 2} {}", expect![["mixed {} {} {}; $1, 2 + 2, $2"]]),
|
||||
(
|
||||
"{SomeStruct { val_a: 0, val_b: 1 }}",
|
||||
expect![["{}; SomeStruct { val_a: 0, val_b: 1 }"]],
|
||||
),
|
||||
("{expr:?} is {2.32f64:.5}", expect![["{:?} is {:.5}; expr, 2.32f64"]]),
|
||||
(
|
||||
"{SomeStruct { val_a: 0, val_b: 1 }:?}",
|
||||
expect![["{:?}; SomeStruct { val_a: 0, val_b: 1 }"]],
|
||||
),
|
||||
("{ 2 + 2 }", expect![["{}; 2 + 2"]]),
|
||||
("{strsim::jaro_winkle(a)}", expect![["{}; strsim::jaro_winkle(a)"]]),
|
||||
("{foo::bar::baz()}", expect![["{}; foo::bar::baz()"]]),
|
||||
("{foo::bar():?}", expect![["{:?}; foo::bar()"]]),
|
||||
];
|
||||
|
||||
for (input, output) in test_vector {
|
||||
check(input, output)
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_into_suggestion() {
|
||||
|
@ -302,10 +79,10 @@ mod tests {
|
|||
];
|
||||
|
||||
for (kind, input, output) in test_vector {
|
||||
let mut parser = FormatStrParser::new((*input).to_owned());
|
||||
parser.parse().expect("Parsing must succeed");
|
||||
|
||||
assert_eq!(&parser.to_suggestion(*kind), output);
|
||||
let (parsed_string, exprs) = parse_format_exprs(input).unwrap();
|
||||
let exprs = with_placeholders(exprs);
|
||||
let snippet = format!(r#"{}("{}", {})"#, kind, parsed_string, exprs.join(", "));
|
||||
assert_eq!(&snippet, output);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,6 +17,7 @@ pub struct CompletionConfig {
|
|||
pub callable: Option<CallableSnippets>,
|
||||
pub snippet_cap: Option<SnippetCap>,
|
||||
pub insert_use: InsertUseConfig,
|
||||
pub prefer_no_std: bool,
|
||||
pub snippets: Vec<Snippet>,
|
||||
}
|
||||
|
||||
|
|
|
@ -234,7 +234,12 @@ pub fn resolve_completion_edits(
|
|||
);
|
||||
let import = items_with_name
|
||||
.filter_map(|candidate| {
|
||||
current_module.find_use_path_prefixed(db, candidate, config.insert_use.prefix_kind)
|
||||
current_module.find_use_path_prefixed(
|
||||
db,
|
||||
candidate,
|
||||
config.insert_use.prefix_kind,
|
||||
config.prefer_no_std,
|
||||
)
|
||||
})
|
||||
.find(|mod_path| mod_path.to_string() == full_import_path);
|
||||
if let Some(import_path) = import {
|
||||
|
|
|
@ -174,8 +174,12 @@ fn import_edits(ctx: &CompletionContext<'_>, requires: &[GreenNode]) -> Option<V
|
|||
hir::PathResolution::Def(def) => def.into(),
|
||||
_ => return None,
|
||||
};
|
||||
let path =
|
||||
ctx.module.find_use_path_prefixed(ctx.db, item, ctx.config.insert_use.prefix_kind)?;
|
||||
let path = ctx.module.find_use_path_prefixed(
|
||||
ctx.db,
|
||||
item,
|
||||
ctx.config.insert_use.prefix_kind,
|
||||
ctx.config.prefer_no_std,
|
||||
)?;
|
||||
Some((path.len() > 1).then(|| LocatedImport::new(path.clone(), item, item, None)))
|
||||
};
|
||||
let mut res = Vec::with_capacity(requires.len());
|
||||
|
|
|
@ -66,6 +66,7 @@ pub(crate) const TEST_CONFIG: CompletionConfig = CompletionConfig {
|
|||
enable_private_editable: false,
|
||||
callable: Some(CallableSnippets::FillArguments),
|
||||
snippet_cap: SnippetCap::new(true),
|
||||
prefer_no_std: false,
|
||||
insert_use: InsertUseConfig {
|
||||
granularity: ImportGranularity::Crate,
|
||||
prefix_kind: PrefixKind::Plain,
|
||||
|
|
|
@ -714,3 +714,30 @@ impl Ty {
|
|||
"#]],
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn through_alias() {
|
||||
check_empty(
|
||||
r#"
|
||||
enum Enum<T> {
|
||||
Unit,
|
||||
Tuple(T),
|
||||
}
|
||||
|
||||
type EnumAlias<T> = Enum<T>;
|
||||
|
||||
fn f(x: EnumAlias<u8>) {
|
||||
match x {
|
||||
EnumAlias::$0 => (),
|
||||
_ => (),
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
"#,
|
||||
expect![[r#"
|
||||
bn Tuple(…) Tuple($1)$0
|
||||
bn Unit Unit$0
|
||||
"#]],
|
||||
);
|
||||
}
|
||||
|
|
|
@ -20,6 +20,7 @@ either = "1.7.0"
|
|||
itertools = "0.10.3"
|
||||
arrayvec = "0.7.2"
|
||||
indexmap = "1.9.1"
|
||||
memchr = "2.5.0"
|
||||
|
||||
stdx = { path = "../stdx", version = "0.0.0" }
|
||||
parser = { path = "../parser", version = "0.0.0" }
|
||||
|
|
|
@ -212,18 +212,20 @@ impl ImportAssets {
|
|||
&self,
|
||||
sema: &Semantics<'_, RootDatabase>,
|
||||
prefix_kind: PrefixKind,
|
||||
prefer_no_std: bool,
|
||||
) -> Vec<LocatedImport> {
|
||||
let _p = profile::span("import_assets::search_for_imports");
|
||||
self.search_for(sema, Some(prefix_kind))
|
||||
self.search_for(sema, Some(prefix_kind), prefer_no_std)
|
||||
}
|
||||
|
||||
/// This may return non-absolute paths if a part of the returned path is already imported into scope.
|
||||
pub fn search_for_relative_paths(
|
||||
&self,
|
||||
sema: &Semantics<'_, RootDatabase>,
|
||||
prefer_no_std: bool,
|
||||
) -> Vec<LocatedImport> {
|
||||
let _p = profile::span("import_assets::search_for_relative_paths");
|
||||
self.search_for(sema, None)
|
||||
self.search_for(sema, None, prefer_no_std)
|
||||
}
|
||||
|
||||
pub fn path_fuzzy_name_to_exact(&mut self, case_sensitive: bool) {
|
||||
|
@ -242,6 +244,7 @@ impl ImportAssets {
|
|||
&self,
|
||||
sema: &Semantics<'_, RootDatabase>,
|
||||
prefixed: Option<PrefixKind>,
|
||||
prefer_no_std: bool,
|
||||
) -> Vec<LocatedImport> {
|
||||
let _p = profile::span("import_assets::search_for");
|
||||
|
||||
|
@ -252,6 +255,7 @@ impl ImportAssets {
|
|||
item_for_path_search(sema.db, item)?,
|
||||
&self.module_with_candidate,
|
||||
prefixed,
|
||||
prefer_no_std,
|
||||
)
|
||||
};
|
||||
|
||||
|
@ -564,11 +568,12 @@ fn get_mod_path(
|
|||
item_to_search: ItemInNs,
|
||||
module_with_candidate: &Module,
|
||||
prefixed: Option<PrefixKind>,
|
||||
prefer_no_std: bool,
|
||||
) -> Option<ModPath> {
|
||||
if let Some(prefix_kind) = prefixed {
|
||||
module_with_candidate.find_use_path_prefixed(db, item_to_search, prefix_kind)
|
||||
module_with_candidate.find_use_path_prefixed(db, item_to_search, prefix_kind, prefer_no_std)
|
||||
} else {
|
||||
module_with_candidate.find_use_path(db, item_to_search)
|
||||
module_with_candidate.find_use_path(db, item_to_search, prefer_no_std)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -38,6 +38,7 @@ pub mod syntax_helpers {
|
|||
pub mod node_ext;
|
||||
pub mod insert_whitespace_into_node;
|
||||
pub mod format_string;
|
||||
pub mod format_string_exprs;
|
||||
|
||||
pub use parser::LexedStr;
|
||||
}
|
||||
|
|
|
@ -173,6 +173,7 @@ impl<'a> Ctx<'a> {
|
|||
let found_path = self.target_module.find_use_path(
|
||||
self.source_scope.db.upcast(),
|
||||
hir::ModuleDef::Trait(trait_ref),
|
||||
false,
|
||||
)?;
|
||||
match ast::make::ty_path(mod_path_to_ast(&found_path)) {
|
||||
ast::Type::PathType(path_ty) => Some(path_ty),
|
||||
|
@ -209,7 +210,7 @@ impl<'a> Ctx<'a> {
|
|||
}
|
||||
|
||||
let found_path =
|
||||
self.target_module.find_use_path(self.source_scope.db.upcast(), def)?;
|
||||
self.target_module.find_use_path(self.source_scope.db.upcast(), def, false)?;
|
||||
let res = mod_path_to_ast(&found_path).clone_for_update();
|
||||
if let Some(args) = path.segment().and_then(|it| it.generic_arg_list()) {
|
||||
if let Some(segment) = res.segment() {
|
||||
|
|
|
@ -8,7 +8,9 @@ use std::{mem, sync::Arc};
|
|||
|
||||
use base_db::{FileId, FileRange, SourceDatabase, SourceDatabaseExt};
|
||||
use hir::{DefWithBody, HasAttrs, HasSource, InFile, ModuleSource, Semantics, Visibility};
|
||||
use memchr::memmem::Finder;
|
||||
use once_cell::unsync::Lazy;
|
||||
use parser::SyntaxKind;
|
||||
use stdx::hash::NoHashHashMap;
|
||||
use syntax::{ast, match_ast, AstNode, TextRange, TextSize};
|
||||
|
||||
|
@ -67,6 +69,7 @@ pub enum ReferenceCategory {
|
|||
// Create
|
||||
Write,
|
||||
Read,
|
||||
Import,
|
||||
// FIXME: Some day should be able to search in doc comments. Would probably
|
||||
// need to switch from enum to bitflags then?
|
||||
// DocComment
|
||||
|
@ -409,14 +412,17 @@ impl<'a> FindUsages<'a> {
|
|||
Some(s) => s.as_str(),
|
||||
None => return,
|
||||
};
|
||||
let finder = &Finder::new(name);
|
||||
let include_self_kw_refs =
|
||||
self.include_self_kw_refs.as_ref().map(|ty| (ty, Finder::new("Self")));
|
||||
|
||||
// these can't be closures because rust infers the lifetimes wrong ...
|
||||
// for<'a> |text: &'a str, name: &'a str, search_range: TextRange| -> impl Iterator<Item = TextSize> + 'a { ... }
|
||||
fn match_indices<'a>(
|
||||
text: &'a str,
|
||||
name: &'a str,
|
||||
finder: &'a Finder<'a>,
|
||||
search_range: TextRange,
|
||||
) -> impl Iterator<Item = TextSize> + 'a {
|
||||
text.match_indices(name).filter_map(move |(idx, _)| {
|
||||
finder.find_iter(text.as_bytes()).filter_map(move |idx| {
|
||||
let offset: TextSize = idx.try_into().unwrap();
|
||||
if !search_range.contains_inclusive(offset) {
|
||||
return None;
|
||||
|
@ -425,6 +431,7 @@ impl<'a> FindUsages<'a> {
|
|||
})
|
||||
}
|
||||
|
||||
// for<'a> |scope: &'a SearchScope| -> impl Iterator<Item = (Arc<String>, FileId, TextRange)> + 'a { ... }
|
||||
fn scope_files<'a>(
|
||||
sema: &'a Semantics<'_, RootDatabase>,
|
||||
scope: &'a SearchScope,
|
||||
|
@ -448,7 +455,7 @@ impl<'a> FindUsages<'a> {
|
|||
let tree = Lazy::new(move || sema.parse(file_id).syntax().clone());
|
||||
|
||||
// Search for occurrences of the items name
|
||||
for offset in match_indices(&text, name, search_range) {
|
||||
for offset in match_indices(&text, finder, search_range) {
|
||||
for name in sema.find_nodes_at_offset_with_descend(&tree, offset) {
|
||||
if match name {
|
||||
ast::NameLike::NameRef(name_ref) => self.found_name_ref(&name_ref, sink),
|
||||
|
@ -460,8 +467,8 @@ impl<'a> FindUsages<'a> {
|
|||
}
|
||||
}
|
||||
// Search for occurrences of the `Self` referring to our type
|
||||
if let Some(self_ty) = &self.include_self_kw_refs {
|
||||
for offset in match_indices(&text, "Self", search_range) {
|
||||
if let Some((self_ty, finder)) = &include_self_kw_refs {
|
||||
for offset in match_indices(&text, finder, search_range) {
|
||||
for name_ref in sema.find_nodes_at_offset_with_descend(&tree, offset) {
|
||||
if self.found_self_ty_name_ref(self_ty, &name_ref, sink) {
|
||||
return;
|
||||
|
@ -477,20 +484,22 @@ impl<'a> FindUsages<'a> {
|
|||
let scope = search_scope
|
||||
.intersection(&SearchScope::module_and_children(self.sema.db, module));
|
||||
|
||||
let is_crate_root = module.is_crate_root(self.sema.db);
|
||||
let is_crate_root =
|
||||
module.is_crate_root(self.sema.db).then(|| Finder::new("crate"));
|
||||
let finder = &Finder::new("super");
|
||||
|
||||
for (text, file_id, search_range) in scope_files(sema, &scope) {
|
||||
let tree = Lazy::new(move || sema.parse(file_id).syntax().clone());
|
||||
|
||||
for offset in match_indices(&text, "super", search_range) {
|
||||
for offset in match_indices(&text, finder, search_range) {
|
||||
for name_ref in sema.find_nodes_at_offset_with_descend(&tree, offset) {
|
||||
if self.found_name_ref(&name_ref, sink) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
if is_crate_root {
|
||||
for offset in match_indices(&text, "crate", search_range) {
|
||||
if let Some(finder) = &is_crate_root {
|
||||
for offset in match_indices(&text, finder, search_range) {
|
||||
for name_ref in sema.find_nodes_at_offset_with_descend(&tree, offset) {
|
||||
if self.found_name_ref(&name_ref, sink) {
|
||||
return;
|
||||
|
@ -531,8 +540,9 @@ impl<'a> FindUsages<'a> {
|
|||
search_range.unwrap_or_else(|| TextRange::up_to(TextSize::of(text.as_str())));
|
||||
|
||||
let tree = Lazy::new(|| sema.parse(file_id).syntax().clone());
|
||||
let finder = &Finder::new("self");
|
||||
|
||||
for offset in match_indices(&text, "self", search_range) {
|
||||
for offset in match_indices(&text, finder, search_range) {
|
||||
for name_ref in sema.find_nodes_at_offset_with_descend(&tree, offset) {
|
||||
if self.found_self_module_name_ref(&name_ref, sink) {
|
||||
return;
|
||||
|
@ -577,7 +587,7 @@ impl<'a> FindUsages<'a> {
|
|||
let reference = FileReference {
|
||||
range,
|
||||
name: ast::NameLike::NameRef(name_ref.clone()),
|
||||
category: None,
|
||||
category: is_name_ref_in_import(name_ref).then(|| ReferenceCategory::Import),
|
||||
};
|
||||
sink(file_id, reference)
|
||||
}
|
||||
|
@ -756,7 +766,7 @@ impl ReferenceCategory {
|
|||
fn new(def: &Definition, r: &ast::NameRef) -> Option<ReferenceCategory> {
|
||||
// Only Locals and Fields have accesses for now.
|
||||
if !matches!(def, Definition::Local(_) | Definition::Field(_)) {
|
||||
return None;
|
||||
return is_name_ref_in_import(r).then(|| ReferenceCategory::Import);
|
||||
}
|
||||
|
||||
let mode = r.syntax().ancestors().find_map(|node| {
|
||||
|
@ -783,3 +793,12 @@ impl ReferenceCategory {
|
|||
mode.or(Some(ReferenceCategory::Read))
|
||||
}
|
||||
}
|
||||
|
||||
fn is_name_ref_in_import(name_ref: &ast::NameRef) -> bool {
|
||||
name_ref
|
||||
.syntax()
|
||||
.parent()
|
||||
.and_then(ast::PathSegment::cast)
|
||||
.and_then(|it| it.parent_path().top_path().syntax().parent())
|
||||
.map_or(false, |it| it.kind() == SyntaxKind::USE_TREE)
|
||||
}
|
||||
|
|
267
crates/ide-db/src/syntax_helpers/format_string_exprs.rs
Normal file
267
crates/ide-db/src/syntax_helpers/format_string_exprs.rs
Normal file
|
@ -0,0 +1,267 @@
|
|||
//! Tools to work with expressions present in format string literals for the `format_args!` family of macros.
|
||||
//! Primarily meant for assists and completions.
|
||||
|
||||
/// Enum for represenging extraced format string args.
|
||||
/// Can either be extracted expressions (which includes identifiers),
|
||||
/// or placeholders `{}`.
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub enum Arg {
|
||||
Placeholder,
|
||||
Ident(String),
|
||||
Expr(String),
|
||||
}
|
||||
|
||||
/**
|
||||
Add placeholders like `$1` and `$2` in place of [`Arg::Placeholder`],
|
||||
and unwraps the [`Arg::Ident`] and [`Arg::Expr`] enums.
|
||||
```rust
|
||||
# use ide_db::syntax_helpers::format_string_exprs::*;
|
||||
assert_eq!(with_placeholders(vec![Arg::Ident("ident".to_owned()), Arg::Placeholder, Arg::Expr("expr + 2".to_owned())]), vec!["ident".to_owned(), "$1".to_owned(), "expr + 2".to_owned()])
|
||||
```
|
||||
*/
|
||||
|
||||
pub fn with_placeholders(args: Vec<Arg>) -> Vec<String> {
|
||||
let mut placeholder_id = 1;
|
||||
args.into_iter()
|
||||
.map(move |a| match a {
|
||||
Arg::Expr(s) | Arg::Ident(s) => s,
|
||||
Arg::Placeholder => {
|
||||
let s = format!("${placeholder_id}");
|
||||
placeholder_id += 1;
|
||||
s
|
||||
}
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/**
|
||||
Parser for a format-like string. It is more allowing in terms of string contents,
|
||||
as we expect variable placeholders to be filled with expressions.
|
||||
|
||||
Built for completions and assists, and escapes `\` and `$` in output.
|
||||
(See the comments on `get_receiver_text()` for detail.)
|
||||
Splits a format string that may contain expressions
|
||||
like
|
||||
```rust
|
||||
assert_eq!(parse("{ident} {} {expr + 42} ").unwrap(), ("{} {} {}", vec![Arg::Ident("ident"), Arg::Placeholder, Arg::Expr("expr + 42")]));
|
||||
```
|
||||
*/
|
||||
pub fn parse_format_exprs(input: &str) -> Result<(String, Vec<Arg>), ()> {
|
||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||
enum State {
|
||||
NotArg,
|
||||
MaybeArg,
|
||||
Expr,
|
||||
Ident,
|
||||
MaybeIncorrect,
|
||||
FormatOpts,
|
||||
}
|
||||
|
||||
let mut state = State::NotArg;
|
||||
let mut current_expr = String::new();
|
||||
let mut extracted_expressions = Vec::new();
|
||||
let mut output = String::new();
|
||||
|
||||
// Count of open braces inside of an expression.
|
||||
// We assume that user knows what they're doing, thus we treat it like a correct pattern, e.g.
|
||||
// "{MyStruct { val_a: 0, val_b: 1 }}".
|
||||
let mut inexpr_open_count = 0;
|
||||
|
||||
let mut chars = input.chars().peekable();
|
||||
while let Some(chr) = chars.next() {
|
||||
match (state, chr) {
|
||||
(State::NotArg, '{') => {
|
||||
output.push(chr);
|
||||
state = State::MaybeArg;
|
||||
}
|
||||
(State::NotArg, '}') => {
|
||||
output.push(chr);
|
||||
state = State::MaybeIncorrect;
|
||||
}
|
||||
(State::NotArg, _) => {
|
||||
if matches!(chr, '\\' | '$') {
|
||||
output.push('\\');
|
||||
}
|
||||
output.push(chr);
|
||||
}
|
||||
(State::MaybeIncorrect, '}') => {
|
||||
// It's okay, we met "}}".
|
||||
output.push(chr);
|
||||
state = State::NotArg;
|
||||
}
|
||||
(State::MaybeIncorrect, _) => {
|
||||
// Error in the string.
|
||||
return Err(());
|
||||
}
|
||||
// Escaped braces `{{`
|
||||
(State::MaybeArg, '{') => {
|
||||
output.push(chr);
|
||||
state = State::NotArg;
|
||||
}
|
||||
(State::MaybeArg, '}') => {
|
||||
// This is an empty sequence '{}'.
|
||||
output.push(chr);
|
||||
extracted_expressions.push(Arg::Placeholder);
|
||||
state = State::NotArg;
|
||||
}
|
||||
(State::MaybeArg, _) => {
|
||||
if matches!(chr, '\\' | '$') {
|
||||
current_expr.push('\\');
|
||||
}
|
||||
current_expr.push(chr);
|
||||
|
||||
// While Rust uses the unicode sets of XID_start and XID_continue for Identifiers
|
||||
// this is probably the best we can do to avoid a false positive
|
||||
if chr.is_alphabetic() || chr == '_' {
|
||||
state = State::Ident;
|
||||
} else {
|
||||
state = State::Expr;
|
||||
}
|
||||
}
|
||||
(State::Ident | State::Expr, '}') => {
|
||||
if inexpr_open_count == 0 {
|
||||
output.push(chr);
|
||||
|
||||
if matches!(state, State::Expr) {
|
||||
extracted_expressions.push(Arg::Expr(current_expr.trim().into()));
|
||||
} else {
|
||||
extracted_expressions.push(Arg::Ident(current_expr.trim().into()));
|
||||
}
|
||||
|
||||
current_expr = String::new();
|
||||
state = State::NotArg;
|
||||
} else {
|
||||
// We're closing one brace met before inside of the expression.
|
||||
current_expr.push(chr);
|
||||
inexpr_open_count -= 1;
|
||||
}
|
||||
}
|
||||
(State::Ident | State::Expr, ':') if matches!(chars.peek(), Some(':')) => {
|
||||
// path separator
|
||||
state = State::Expr;
|
||||
current_expr.push_str("::");
|
||||
chars.next();
|
||||
}
|
||||
(State::Ident | State::Expr, ':') => {
|
||||
if inexpr_open_count == 0 {
|
||||
// We're outside of braces, thus assume that it's a specifier, like "{Some(value):?}"
|
||||
output.push(chr);
|
||||
|
||||
if matches!(state, State::Expr) {
|
||||
extracted_expressions.push(Arg::Expr(current_expr.trim().into()));
|
||||
} else {
|
||||
extracted_expressions.push(Arg::Ident(current_expr.trim().into()));
|
||||
}
|
||||
|
||||
current_expr = String::new();
|
||||
state = State::FormatOpts;
|
||||
} else {
|
||||
// We're inside of braced expression, assume that it's a struct field name/value delimiter.
|
||||
current_expr.push(chr);
|
||||
}
|
||||
}
|
||||
(State::Ident | State::Expr, '{') => {
|
||||
state = State::Expr;
|
||||
current_expr.push(chr);
|
||||
inexpr_open_count += 1;
|
||||
}
|
||||
(State::Ident | State::Expr, _) => {
|
||||
if !(chr.is_alphanumeric() || chr == '_' || chr == '#') {
|
||||
state = State::Expr;
|
||||
}
|
||||
|
||||
if matches!(chr, '\\' | '$') {
|
||||
current_expr.push('\\');
|
||||
}
|
||||
current_expr.push(chr);
|
||||
}
|
||||
(State::FormatOpts, '}') => {
|
||||
output.push(chr);
|
||||
state = State::NotArg;
|
||||
}
|
||||
(State::FormatOpts, _) => {
|
||||
if matches!(chr, '\\' | '$') {
|
||||
output.push('\\');
|
||||
}
|
||||
output.push(chr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if state != State::NotArg {
|
||||
return Err(());
|
||||
}
|
||||
|
||||
Ok((output, extracted_expressions))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use expect_test::{expect, Expect};
|
||||
|
||||
fn check(input: &str, expect: &Expect) {
|
||||
let (output, exprs) = parse_format_exprs(input).unwrap_or(("-".to_string(), vec![]));
|
||||
let outcome_repr = if !exprs.is_empty() {
|
||||
format!("{}; {}", output, with_placeholders(exprs).join(", "))
|
||||
} else {
|
||||
output
|
||||
};
|
||||
|
||||
expect.assert_eq(&outcome_repr);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn format_str_parser() {
|
||||
let test_vector = &[
|
||||
("no expressions", expect![["no expressions"]]),
|
||||
(r"no expressions with \$0$1", expect![r"no expressions with \\\$0\$1"]),
|
||||
("{expr} is {2 + 2}", expect![["{} is {}; expr, 2 + 2"]]),
|
||||
("{expr:?}", expect![["{:?}; expr"]]),
|
||||
("{expr:1$}", expect![[r"{:1\$}; expr"]]),
|
||||
("{$0}", expect![[r"{}; \$0"]]),
|
||||
("{malformed", expect![["-"]]),
|
||||
("malformed}", expect![["-"]]),
|
||||
("{{correct", expect![["{{correct"]]),
|
||||
("correct}}", expect![["correct}}"]]),
|
||||
("{correct}}}", expect![["{}}}; correct"]]),
|
||||
("{correct}}}}}", expect![["{}}}}}; correct"]]),
|
||||
("{incorrect}}", expect![["-"]]),
|
||||
("placeholders {} {}", expect![["placeholders {} {}; $1, $2"]]),
|
||||
("mixed {} {2 + 2} {}", expect![["mixed {} {} {}; $1, 2 + 2, $2"]]),
|
||||
(
|
||||
"{SomeStruct { val_a: 0, val_b: 1 }}",
|
||||
expect![["{}; SomeStruct { val_a: 0, val_b: 1 }"]],
|
||||
),
|
||||
("{expr:?} is {2.32f64:.5}", expect![["{:?} is {:.5}; expr, 2.32f64"]]),
|
||||
(
|
||||
"{SomeStruct { val_a: 0, val_b: 1 }:?}",
|
||||
expect![["{:?}; SomeStruct { val_a: 0, val_b: 1 }"]],
|
||||
),
|
||||
("{ 2 + 2 }", expect![["{}; 2 + 2"]]),
|
||||
("{strsim::jaro_winkle(a)}", expect![["{}; strsim::jaro_winkle(a)"]]),
|
||||
("{foo::bar::baz()}", expect![["{}; foo::bar::baz()"]]),
|
||||
("{foo::bar():?}", expect![["{:?}; foo::bar()"]]),
|
||||
];
|
||||
|
||||
for (input, output) in test_vector {
|
||||
check(input, output)
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn arg_type() {
|
||||
assert_eq!(
|
||||
parse_format_exprs("{_ident} {r#raw_ident} {expr.obj} {name {thing: 42} } {}")
|
||||
.unwrap()
|
||||
.1,
|
||||
vec![
|
||||
Arg::Ident("_ident".to_owned()),
|
||||
Arg::Ident("r#raw_ident".to_owned()),
|
||||
Arg::Expr("expr.obj".to_owned()),
|
||||
Arg::Expr("name {thing: 42}".to_owned()),
|
||||
Arg::Placeholder
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
|
@ -137,6 +137,7 @@ pub(crate) fn json_in_items(
|
|||
sema.db,
|
||||
it,
|
||||
config.insert_use.prefix_kind,
|
||||
config.prefer_no_std,
|
||||
) {
|
||||
insert_use(
|
||||
&scope,
|
||||
|
@ -152,6 +153,7 @@ pub(crate) fn json_in_items(
|
|||
sema.db,
|
||||
it,
|
||||
config.insert_use.prefix_kind,
|
||||
config.prefer_no_std,
|
||||
) {
|
||||
insert_use(
|
||||
&scope,
|
||||
|
|
|
@ -124,6 +124,7 @@ fn fixes(ctx: &DiagnosticsContext<'_>, d: &hir::MissingFields) -> Option<Vec<Ass
|
|||
let type_path = current_module?.find_use_path(
|
||||
ctx.sema.db,
|
||||
item_for_path_search(ctx.sema.db, item_in_ns)?,
|
||||
ctx.config.prefer_no_std,
|
||||
)?;
|
||||
|
||||
use_trivial_constructor(
|
||||
|
|
|
@ -59,9 +59,6 @@ fn add_reference(
|
|||
d: &hir::TypeMismatch,
|
||||
acc: &mut Vec<Assist>,
|
||||
) -> Option<()> {
|
||||
let root = ctx.sema.db.parse_or_expand(d.expr.file_id)?;
|
||||
let expr_node = d.expr.value.to_node(&root);
|
||||
|
||||
let range = ctx.sema.diagnostics_display_range(d.expr.clone().map(|it| it.into())).range;
|
||||
|
||||
let (_, mutability) = d.expected.as_reference()?;
|
||||
|
@ -72,7 +69,7 @@ fn add_reference(
|
|||
|
||||
let ampersands = format!("&{}", mutability.as_keyword_for_ref());
|
||||
|
||||
let edit = TextEdit::insert(expr_node.syntax().text_range().start(), ampersands);
|
||||
let edit = TextEdit::insert(range.start(), ampersands);
|
||||
let source_change =
|
||||
SourceChange::from_text_edit(d.expr.file_id.original_file(ctx.sema.db), edit);
|
||||
acc.push(fix("add_reference_here", "Add reference here", source_change, range));
|
||||
|
@ -314,6 +311,34 @@ fn main() {
|
|||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_add_reference_to_macro_call() {
|
||||
check_fix(
|
||||
r#"
|
||||
macro_rules! thousand {
|
||||
() => {
|
||||
1000_u64
|
||||
};
|
||||
}
|
||||
fn test(foo: &u64) {}
|
||||
fn main() {
|
||||
test($0thousand!());
|
||||
}
|
||||
"#,
|
||||
r#"
|
||||
macro_rules! thousand {
|
||||
() => {
|
||||
1000_u64
|
||||
};
|
||||
}
|
||||
fn test(foo: &u64) {}
|
||||
fn main() {
|
||||
test(&thousand!());
|
||||
}
|
||||
"#,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_add_mutable_reference_to_let_stmt() {
|
||||
check_fix(
|
||||
|
|
|
@ -150,6 +150,7 @@ pub struct DiagnosticsConfig {
|
|||
pub expr_fill_default: ExprFillDefaultMode,
|
||||
// FIXME: We may want to include a whole `AssistConfig` here
|
||||
pub insert_use: InsertUseConfig,
|
||||
pub prefer_no_std: bool,
|
||||
}
|
||||
|
||||
impl DiagnosticsConfig {
|
||||
|
@ -170,6 +171,7 @@ impl DiagnosticsConfig {
|
|||
group: false,
|
||||
skip_glob_imports: false,
|
||||
},
|
||||
prefer_no_std: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -648,9 +648,10 @@ impl Match {
|
|||
.module();
|
||||
for (path, resolved_path) in &template.resolved_paths {
|
||||
if let hir::PathResolution::Def(module_def) = resolved_path.resolution {
|
||||
let mod_path = module.find_use_path(sema.db, module_def).ok_or_else(|| {
|
||||
match_error!("Failed to render template path `{}` at match location")
|
||||
})?;
|
||||
let mod_path =
|
||||
module.find_use_path(sema.db, module_def, false).ok_or_else(|| {
|
||||
match_error!("Failed to render template path `{}` at match location")
|
||||
})?;
|
||||
self.rendered_template_paths.insert(path.clone(), mod_path);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -41,6 +41,12 @@ pub struct AnnotationConfig {
|
|||
pub annotate_references: bool,
|
||||
pub annotate_method_references: bool,
|
||||
pub annotate_enum_variant_references: bool,
|
||||
pub location: AnnotationLocation,
|
||||
}
|
||||
|
||||
pub enum AnnotationLocation {
|
||||
AboveName,
|
||||
AboveWholeItem,
|
||||
}
|
||||
|
||||
pub(crate) fn annotations(
|
||||
|
@ -65,10 +71,10 @@ pub(crate) fn annotations(
|
|||
visit_file_defs(&Semantics::new(db), file_id, &mut |def| {
|
||||
let range = match def {
|
||||
Definition::Const(konst) if config.annotate_references => {
|
||||
konst.source(db).and_then(|node| name_range(db, node, file_id))
|
||||
konst.source(db).and_then(|node| name_range(db, config, node, file_id))
|
||||
}
|
||||
Definition::Trait(trait_) if config.annotate_references || config.annotate_impls => {
|
||||
trait_.source(db).and_then(|node| name_range(db, node, file_id))
|
||||
trait_.source(db).and_then(|node| name_range(db, config, node, file_id))
|
||||
}
|
||||
Definition::Adt(adt) => match adt {
|
||||
hir::Adt::Enum(enum_) => {
|
||||
|
@ -77,7 +83,9 @@ pub(crate) fn annotations(
|
|||
.variants(db)
|
||||
.into_iter()
|
||||
.map(|variant| {
|
||||
variant.source(db).and_then(|node| name_range(db, node, file_id))
|
||||
variant
|
||||
.source(db)
|
||||
.and_then(|node| name_range(db, config, node, file_id))
|
||||
})
|
||||
.flatten()
|
||||
.for_each(|range| {
|
||||
|
@ -88,14 +96,14 @@ pub(crate) fn annotations(
|
|||
})
|
||||
}
|
||||
if config.annotate_references || config.annotate_impls {
|
||||
enum_.source(db).and_then(|node| name_range(db, node, file_id))
|
||||
enum_.source(db).and_then(|node| name_range(db, config, node, file_id))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
if config.annotate_references || config.annotate_impls {
|
||||
adt.source(db).and_then(|node| name_range(db, node, file_id))
|
||||
adt.source(db).and_then(|node| name_range(db, config, node, file_id))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
|
@ -113,6 +121,7 @@ pub(crate) fn annotations(
|
|||
annotations
|
||||
.push(Annotation { range, kind: AnnotationKind::HasImpls { file_id, data: None } });
|
||||
}
|
||||
|
||||
if config.annotate_references {
|
||||
annotations.push(Annotation {
|
||||
range,
|
||||
|
@ -122,12 +131,18 @@ pub(crate) fn annotations(
|
|||
|
||||
fn name_range<T: HasName>(
|
||||
db: &RootDatabase,
|
||||
config: &AnnotationConfig,
|
||||
node: InFile<T>,
|
||||
source_file_id: FileId,
|
||||
) -> Option<TextRange> {
|
||||
if let Some(InFile { file_id, value }) = node.original_ast_node(db) {
|
||||
if file_id == source_file_id.into() {
|
||||
return value.name().map(|it| it.syntax().text_range());
|
||||
return match config.location {
|
||||
AnnotationLocation::AboveName => {
|
||||
value.name().map(|name| name.syntax().text_range())
|
||||
}
|
||||
AnnotationLocation::AboveWholeItem => Some(value.syntax().text_range()),
|
||||
};
|
||||
}
|
||||
}
|
||||
None
|
||||
|
@ -188,21 +203,23 @@ mod tests {
|
|||
|
||||
use crate::{fixture, Annotation, AnnotationConfig};
|
||||
|
||||
fn check(ra_fixture: &str, expect: Expect) {
|
||||
use super::AnnotationLocation;
|
||||
|
||||
const DEFAULT_CONFIG: AnnotationConfig = AnnotationConfig {
|
||||
binary_target: true,
|
||||
annotate_runnables: true,
|
||||
annotate_impls: true,
|
||||
annotate_references: true,
|
||||
annotate_method_references: true,
|
||||
annotate_enum_variant_references: true,
|
||||
location: AnnotationLocation::AboveName,
|
||||
};
|
||||
|
||||
fn check_with_config(ra_fixture: &str, expect: Expect, config: &AnnotationConfig) {
|
||||
let (analysis, file_id) = fixture::file(ra_fixture);
|
||||
|
||||
let annotations: Vec<Annotation> = analysis
|
||||
.annotations(
|
||||
&AnnotationConfig {
|
||||
binary_target: true,
|
||||
annotate_runnables: true,
|
||||
annotate_impls: true,
|
||||
annotate_references: true,
|
||||
annotate_method_references: true,
|
||||
annotate_enum_variant_references: true,
|
||||
},
|
||||
file_id,
|
||||
)
|
||||
.annotations(config, file_id)
|
||||
.unwrap()
|
||||
.into_iter()
|
||||
.map(|annotation| analysis.resolve_annotation(annotation).unwrap())
|
||||
|
@ -211,6 +228,10 @@ mod tests {
|
|||
expect.assert_debug_eq(&annotations);
|
||||
}
|
||||
|
||||
fn check(ra_fixture: &str, expect: Expect) {
|
||||
check_with_config(ra_fixture, expect, &DEFAULT_CONFIG);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn const_annotations() {
|
||||
check(
|
||||
|
@ -786,4 +807,40 @@ m!();
|
|||
"#]],
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_annotations_appear_above_whole_item_when_configured_to_do_so() {
|
||||
check_with_config(
|
||||
r#"
|
||||
/// This is a struct named Foo, obviously.
|
||||
#[derive(Clone)]
|
||||
struct Foo;
|
||||
"#,
|
||||
expect![[r#"
|
||||
[
|
||||
Annotation {
|
||||
range: 0..71,
|
||||
kind: HasImpls {
|
||||
file_id: FileId(
|
||||
0,
|
||||
),
|
||||
data: Some(
|
||||
[],
|
||||
),
|
||||
},
|
||||
},
|
||||
Annotation {
|
||||
range: 0..71,
|
||||
kind: HasReferences {
|
||||
file_id: FileId(
|
||||
0,
|
||||
),
|
||||
data: None,
|
||||
},
|
||||
},
|
||||
]
|
||||
"#]],
|
||||
&AnnotationConfig { location: AnnotationLocation::AboveWholeItem, ..DEFAULT_CONFIG },
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -377,6 +377,7 @@ mod tests {
|
|||
match it {
|
||||
ReferenceCategory::Read => "read",
|
||||
ReferenceCategory::Write => "write",
|
||||
ReferenceCategory::Import => "import",
|
||||
}
|
||||
.to_string()
|
||||
}),
|
||||
|
@ -423,12 +424,12 @@ struct Foo;
|
|||
check(
|
||||
r#"
|
||||
use crate$0;
|
||||
//^^^^^
|
||||
//^^^^^ import
|
||||
use self;
|
||||
//^^^^
|
||||
//^^^^ import
|
||||
mod __ {
|
||||
use super;
|
||||
//^^^^^
|
||||
//^^^^^ import
|
||||
}
|
||||
"#,
|
||||
);
|
||||
|
@ -436,7 +437,7 @@ mod __ {
|
|||
r#"
|
||||
//- /main.rs crate:main deps:lib
|
||||
use lib$0;
|
||||
//^^^
|
||||
//^^^ import
|
||||
//- /lib.rs crate:lib
|
||||
"#,
|
||||
);
|
||||
|
@ -450,7 +451,7 @@ use lib$0;
|
|||
mod foo;
|
||||
//- /foo.rs
|
||||
use self$0;
|
||||
// ^^^^
|
||||
// ^^^^ import
|
||||
"#,
|
||||
);
|
||||
}
|
||||
|
|
|
@ -1687,6 +1687,74 @@ fn main() {
|
|||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn iterator_hint_regression_issue_12674() {
|
||||
// Ensure we don't crash while solving the projection type of iterators.
|
||||
check_expect(
|
||||
InlayHintsConfig { chaining_hints: true, ..DISABLED_CONFIG },
|
||||
r#"
|
||||
//- minicore: iterators
|
||||
struct S<T>(T);
|
||||
impl<T> S<T> {
|
||||
fn iter(&self) -> Iter<'_, T> { loop {} }
|
||||
}
|
||||
struct Iter<'a, T: 'a>(&'a T);
|
||||
impl<'a, T> Iterator for Iter<'a, T> {
|
||||
type Item = &'a T;
|
||||
fn next(&mut self) -> Option<Self::Item> { loop {} }
|
||||
}
|
||||
struct Container<'a> {
|
||||
elements: S<&'a str>,
|
||||
}
|
||||
struct SliceIter<'a, T>(&'a T);
|
||||
impl<'a, T> Iterator for SliceIter<'a, T> {
|
||||
type Item = &'a T;
|
||||
fn next(&mut self) -> Option<Self::Item> { loop {} }
|
||||
}
|
||||
|
||||
fn main(a: SliceIter<'_, Container>) {
|
||||
a
|
||||
.filter_map(|c| Some(c.elements.iter().filter_map(|v| Some(v))))
|
||||
.map(|e| e);
|
||||
}
|
||||
"#,
|
||||
expect![[r#"
|
||||
[
|
||||
InlayHint {
|
||||
range: 484..554,
|
||||
kind: ChainingHint,
|
||||
label: [
|
||||
"impl Iterator<Item = impl Iterator<Item = &&str>>",
|
||||
],
|
||||
tooltip: Some(
|
||||
HoverRanged(
|
||||
FileId(
|
||||
0,
|
||||
),
|
||||
484..554,
|
||||
),
|
||||
),
|
||||
},
|
||||
InlayHint {
|
||||
range: 484..485,
|
||||
kind: ChainingHint,
|
||||
label: [
|
||||
"SliceIter<Container>",
|
||||
],
|
||||
tooltip: Some(
|
||||
HoverRanged(
|
||||
FileId(
|
||||
0,
|
||||
),
|
||||
484..485,
|
||||
),
|
||||
),
|
||||
},
|
||||
]
|
||||
"#]],
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn infer_call_method_return_associated_types_with_generic() {
|
||||
check_types(
|
||||
|
|
|
@ -74,7 +74,7 @@ use syntax::SourceFile;
|
|||
use crate::navigation_target::{ToNav, TryToNav};
|
||||
|
||||
pub use crate::{
|
||||
annotations::{Annotation, AnnotationConfig, AnnotationKind},
|
||||
annotations::{Annotation, AnnotationConfig, AnnotationKind, AnnotationLocation},
|
||||
call_hierarchy::CallItem,
|
||||
expand_macro::ExpandedMacro,
|
||||
file_structure::{StructureNode, StructureNodeKind},
|
||||
|
|
|
@ -742,7 +742,7 @@ pub struct Foo {
|
|||
expect![[r#"
|
||||
foo Module FileId(0) 0..8 4..7
|
||||
|
||||
FileId(0) 14..17
|
||||
FileId(0) 14..17 Import
|
||||
"#]],
|
||||
);
|
||||
}
|
||||
|
@ -760,7 +760,7 @@ use self$0;
|
|||
expect![[r#"
|
||||
foo Module FileId(0) 0..8 4..7
|
||||
|
||||
FileId(1) 4..8
|
||||
FileId(1) 4..8 Import
|
||||
"#]],
|
||||
);
|
||||
}
|
||||
|
@ -775,7 +775,7 @@ use self$0;
|
|||
expect![[r#"
|
||||
Module FileId(0) 0..10
|
||||
|
||||
FileId(0) 4..8
|
||||
FileId(0) 4..8 Import
|
||||
"#]],
|
||||
);
|
||||
}
|
||||
|
@ -803,7 +803,7 @@ pub(super) struct Foo$0 {
|
|||
expect![[r#"
|
||||
Foo Struct FileId(2) 0..41 18..21
|
||||
|
||||
FileId(1) 20..23
|
||||
FileId(1) 20..23 Import
|
||||
FileId(1) 47..50
|
||||
"#]],
|
||||
);
|
||||
|
@ -966,7 +966,7 @@ fn g() { f(); }
|
|||
expect![[r#"
|
||||
f Function FileId(0) 22..31 25..26
|
||||
|
||||
FileId(1) 11..12
|
||||
FileId(1) 11..12 Import
|
||||
FileId(1) 24..25
|
||||
"#]],
|
||||
);
|
||||
|
@ -1424,9 +1424,9 @@ pub use level1::Foo;
|
|||
expect![[r#"
|
||||
Foo Struct FileId(0) 0..15 11..14
|
||||
|
||||
FileId(1) 16..19
|
||||
FileId(2) 16..19
|
||||
FileId(3) 16..19
|
||||
FileId(1) 16..19 Import
|
||||
FileId(2) 16..19 Import
|
||||
FileId(3) 16..19 Import
|
||||
"#]],
|
||||
);
|
||||
}
|
||||
|
@ -1454,7 +1454,7 @@ lib::foo!();
|
|||
expect![[r#"
|
||||
foo Macro FileId(1) 0..61 29..32
|
||||
|
||||
FileId(0) 46..49
|
||||
FileId(0) 46..49 Import
|
||||
FileId(2) 0..3
|
||||
FileId(3) 5..8
|
||||
"#]],
|
||||
|
@ -1617,7 +1617,7 @@ struct Foo;
|
|||
expect![[r#"
|
||||
derive_identity Derive FileId(2) 1..107 45..60
|
||||
|
||||
FileId(0) 17..31
|
||||
FileId(0) 17..31 Import
|
||||
FileId(0) 56..70
|
||||
"#]],
|
||||
);
|
||||
|
|
|
@ -203,17 +203,16 @@ impl BindingsBuilder {
|
|||
}
|
||||
|
||||
fn build(self, idx: &BindingsIdx) -> Bindings {
|
||||
let mut bindings = Bindings::default();
|
||||
self.build_inner(&mut bindings, &self.nodes[idx.0]);
|
||||
bindings
|
||||
self.build_inner(&self.nodes[idx.0])
|
||||
}
|
||||
|
||||
fn build_inner(&self, bindings: &mut Bindings, link_nodes: &[LinkNode<Rc<BindingKind>>]) {
|
||||
fn build_inner(&self, link_nodes: &[LinkNode<Rc<BindingKind>>]) -> Bindings {
|
||||
let mut bindings = Bindings::default();
|
||||
let mut nodes = Vec::new();
|
||||
self.collect_nodes(link_nodes, &mut nodes);
|
||||
|
||||
for cmd in nodes {
|
||||
match &**cmd {
|
||||
match cmd {
|
||||
BindingKind::Empty(name) => {
|
||||
bindings.push_empty(name);
|
||||
}
|
||||
|
@ -246,13 +245,15 @@ impl BindingsBuilder {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
bindings
|
||||
}
|
||||
|
||||
fn collect_nested_ref<'a>(
|
||||
&'a self,
|
||||
id: usize,
|
||||
len: usize,
|
||||
nested_refs: &mut Vec<&'a Vec<LinkNode<Rc<BindingKind>>>>,
|
||||
nested_refs: &mut Vec<&'a [LinkNode<Rc<BindingKind>>]>,
|
||||
) {
|
||||
self.nested[id].iter().take(len).for_each(|it| match it {
|
||||
LinkNode::Node(id) => nested_refs.push(&self.nodes[*id]),
|
||||
|
@ -262,26 +263,16 @@ impl BindingsBuilder {
|
|||
|
||||
fn collect_nested(&self, idx: usize, nested_idx: usize, nested: &mut Vec<Bindings>) {
|
||||
let last = &self.nodes[idx];
|
||||
let mut nested_refs = Vec::new();
|
||||
let mut nested_refs: Vec<&[_]> = Vec::new();
|
||||
self.nested[nested_idx].iter().for_each(|it| match *it {
|
||||
LinkNode::Node(idx) => nested_refs.push(&self.nodes[idx]),
|
||||
LinkNode::Parent { idx, len } => self.collect_nested_ref(idx, len, &mut nested_refs),
|
||||
});
|
||||
nested_refs.push(last);
|
||||
|
||||
nested_refs.into_iter().for_each(|iter| {
|
||||
let mut child_bindings = Bindings::default();
|
||||
self.build_inner(&mut child_bindings, iter);
|
||||
nested.push(child_bindings)
|
||||
})
|
||||
nested.extend(nested_refs.into_iter().map(|iter| self.build_inner(iter)));
|
||||
}
|
||||
|
||||
fn collect_nodes_ref<'a>(
|
||||
&'a self,
|
||||
id: usize,
|
||||
len: usize,
|
||||
nodes: &mut Vec<&'a Rc<BindingKind>>,
|
||||
) {
|
||||
fn collect_nodes_ref<'a>(&'a self, id: usize, len: usize, nodes: &mut Vec<&'a BindingKind>) {
|
||||
self.nodes[id].iter().take(len).for_each(|it| match it {
|
||||
LinkNode::Node(it) => nodes.push(it),
|
||||
LinkNode::Parent { idx, len } => self.collect_nodes_ref(*idx, *len, nodes),
|
||||
|
@ -291,7 +282,7 @@ impl BindingsBuilder {
|
|||
fn collect_nodes<'a>(
|
||||
&'a self,
|
||||
link_nodes: &'a [LinkNode<Rc<BindingKind>>],
|
||||
nodes: &mut Vec<&'a Rc<BindingKind>>,
|
||||
nodes: &mut Vec<&'a BindingKind>,
|
||||
) {
|
||||
link_nodes.iter().for_each(|it| match it {
|
||||
LinkNode::Node(it) => nodes.push(it),
|
||||
|
@ -386,10 +377,10 @@ fn match_loop_inner<'t>(
|
|||
let op = match item.dot.peek() {
|
||||
None => {
|
||||
// We are at or past the end of the matcher of `item`.
|
||||
if item.up.is_some() {
|
||||
if let Some(up) = &item.up {
|
||||
if item.sep_parsed.is_none() {
|
||||
// Get the `up` matcher
|
||||
let mut new_pos = *item.up.clone().unwrap();
|
||||
let mut new_pos = (**up).clone();
|
||||
new_pos.bindings = bindings_builder.copy(&new_pos.bindings);
|
||||
// Add matches from this repetition to the `matches` of `up`
|
||||
bindings_builder.push_nested(&mut new_pos.bindings, &item.bindings);
|
||||
|
@ -402,7 +393,7 @@ fn match_loop_inner<'t>(
|
|||
|
||||
// Check if we need a separator.
|
||||
// We check the separator one by one
|
||||
let sep_idx = *item.sep_parsed.as_ref().unwrap_or(&0);
|
||||
let sep_idx = item.sep_parsed.unwrap_or(0);
|
||||
let sep_len = item.sep.as_ref().map_or(0, Separator::tt_count);
|
||||
if item.sep.is_some() && sep_idx != sep_len {
|
||||
let sep = item.sep.as_ref().unwrap();
|
||||
|
|
|
@ -43,10 +43,12 @@ impl WorkspaceBuildScripts {
|
|||
if let Some([program, args @ ..]) = config.run_build_script_command.as_deref() {
|
||||
let mut cmd = Command::new(program);
|
||||
cmd.args(args);
|
||||
cmd.envs(&config.extra_env);
|
||||
return cmd;
|
||||
}
|
||||
|
||||
let mut cmd = Command::new(toolchain::cargo());
|
||||
cmd.envs(&config.extra_env);
|
||||
|
||||
cmd.args(&["check", "--quiet", "--workspace", "--message-format=json"]);
|
||||
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
use std::iter;
|
||||
use std::path::PathBuf;
|
||||
use std::str::from_utf8;
|
||||
use std::{ops, process::Command};
|
||||
|
||||
use anyhow::{Context, Result};
|
||||
|
@ -98,6 +99,8 @@ pub struct CargoConfig {
|
|||
pub wrap_rustc_in_build_scripts: bool,
|
||||
|
||||
pub run_build_script_command: Option<Vec<String>>,
|
||||
|
||||
pub extra_env: FxHashMap<String, String>,
|
||||
}
|
||||
|
||||
impl CargoConfig {
|
||||
|
@ -263,8 +266,8 @@ impl CargoWorkspace {
|
|||
let target = config
|
||||
.target
|
||||
.clone()
|
||||
.or_else(|| cargo_config_build_target(cargo_toml))
|
||||
.or_else(|| rustc_discover_host_triple(cargo_toml));
|
||||
.or_else(|| cargo_config_build_target(cargo_toml, config))
|
||||
.or_else(|| rustc_discover_host_triple(cargo_toml, config));
|
||||
|
||||
let mut meta = MetadataCommand::new();
|
||||
meta.cargo_path(toolchain::cargo());
|
||||
|
@ -292,8 +295,27 @@ impl CargoWorkspace {
|
|||
// unclear whether cargo itself supports it.
|
||||
progress("metadata".to_string());
|
||||
|
||||
let meta =
|
||||
meta.exec().with_context(|| format!("Failed to run `{:?}`", meta.cargo_command()))?;
|
||||
fn exec_with_env(
|
||||
command: &cargo_metadata::MetadataCommand,
|
||||
extra_env: &FxHashMap<String, String>,
|
||||
) -> Result<cargo_metadata::Metadata, cargo_metadata::Error> {
|
||||
let mut command = command.cargo_command();
|
||||
command.envs(extra_env);
|
||||
let output = command.output()?;
|
||||
if !output.status.success() {
|
||||
return Err(cargo_metadata::Error::CargoMetadata {
|
||||
stderr: String::from_utf8(output.stderr)?,
|
||||
});
|
||||
}
|
||||
let stdout = from_utf8(&output.stdout)?
|
||||
.lines()
|
||||
.find(|line| line.starts_with('{'))
|
||||
.ok_or(cargo_metadata::Error::NoJson)?;
|
||||
cargo_metadata::MetadataCommand::parse(stdout)
|
||||
}
|
||||
|
||||
let meta = exec_with_env(&meta, &config.extra_env)
|
||||
.with_context(|| format!("Failed to run `{:?}`", meta.cargo_command()))?;
|
||||
|
||||
Ok(meta)
|
||||
}
|
||||
|
@ -463,8 +485,9 @@ impl CargoWorkspace {
|
|||
}
|
||||
}
|
||||
|
||||
fn rustc_discover_host_triple(cargo_toml: &ManifestPath) -> Option<String> {
|
||||
fn rustc_discover_host_triple(cargo_toml: &ManifestPath, config: &CargoConfig) -> Option<String> {
|
||||
let mut rustc = Command::new(toolchain::rustc());
|
||||
rustc.envs(&config.extra_env);
|
||||
rustc.current_dir(cargo_toml.parent()).arg("-vV");
|
||||
tracing::debug!("Discovering host platform by {:?}", rustc);
|
||||
match utf8_stdout(rustc) {
|
||||
|
@ -486,8 +509,9 @@ fn rustc_discover_host_triple(cargo_toml: &ManifestPath) -> Option<String> {
|
|||
}
|
||||
}
|
||||
|
||||
fn cargo_config_build_target(cargo_toml: &ManifestPath) -> Option<String> {
|
||||
fn cargo_config_build_target(cargo_toml: &ManifestPath, config: &CargoConfig) -> Option<String> {
|
||||
let mut cargo_config = Command::new(toolchain::cargo());
|
||||
cargo_config.envs(&config.extra_env);
|
||||
cargo_config
|
||||
.current_dir(cargo_toml.parent())
|
||||
.args(&["-Z", "unstable-options", "config", "get", "build.target"])
|
||||
|
|
|
@ -4,9 +4,13 @@ use std::process::Command;
|
|||
|
||||
use anyhow::Result;
|
||||
|
||||
use crate::{cfg_flag::CfgFlag, utf8_stdout, ManifestPath};
|
||||
use crate::{cfg_flag::CfgFlag, utf8_stdout, CargoConfig, ManifestPath};
|
||||
|
||||
pub(crate) fn get(cargo_toml: Option<&ManifestPath>, target: Option<&str>) -> Vec<CfgFlag> {
|
||||
pub(crate) fn get(
|
||||
cargo_toml: Option<&ManifestPath>,
|
||||
target: Option<&str>,
|
||||
config: &CargoConfig,
|
||||
) -> Vec<CfgFlag> {
|
||||
let _p = profile::span("rustc_cfg::get");
|
||||
let mut res = Vec::with_capacity(6 * 2 + 1);
|
||||
|
||||
|
@ -18,7 +22,7 @@ pub(crate) fn get(cargo_toml: Option<&ManifestPath>, target: Option<&str>) -> Ve
|
|||
}
|
||||
}
|
||||
|
||||
match get_rust_cfgs(cargo_toml, target) {
|
||||
match get_rust_cfgs(cargo_toml, target, config) {
|
||||
Ok(rustc_cfgs) => {
|
||||
tracing::debug!(
|
||||
"rustc cfgs found: {:?}",
|
||||
|
@ -35,9 +39,14 @@ pub(crate) fn get(cargo_toml: Option<&ManifestPath>, target: Option<&str>) -> Ve
|
|||
res
|
||||
}
|
||||
|
||||
fn get_rust_cfgs(cargo_toml: Option<&ManifestPath>, target: Option<&str>) -> Result<String> {
|
||||
fn get_rust_cfgs(
|
||||
cargo_toml: Option<&ManifestPath>,
|
||||
target: Option<&str>,
|
||||
config: &CargoConfig,
|
||||
) -> Result<String> {
|
||||
if let Some(cargo_toml) = cargo_toml {
|
||||
let mut cargo_config = Command::new(toolchain::cargo());
|
||||
cargo_config.envs(&config.extra_env);
|
||||
cargo_config
|
||||
.current_dir(cargo_toml.parent())
|
||||
.args(&["-Z", "unstable-options", "rustc", "--print", "cfg"])
|
||||
|
@ -52,6 +61,7 @@ fn get_rust_cfgs(cargo_toml: Option<&ManifestPath>, target: Option<&str>) -> Res
|
|||
}
|
||||
// using unstable cargo features failed, fall back to using plain rustc
|
||||
let mut cmd = Command::new(toolchain::rustc());
|
||||
cmd.envs(&config.extra_env);
|
||||
cmd.args(&["--print", "cfg", "-O"]);
|
||||
if let Some(target) = target {
|
||||
cmd.args(&["--target", target]);
|
||||
|
|
|
@ -10,7 +10,7 @@ use anyhow::{format_err, Result};
|
|||
use la_arena::{Arena, Idx};
|
||||
use paths::{AbsPath, AbsPathBuf};
|
||||
|
||||
use crate::{utf8_stdout, ManifestPath};
|
||||
use crate::{utf8_stdout, CargoConfig, ManifestPath};
|
||||
|
||||
#[derive(Debug, Clone, Eq, PartialEq)]
|
||||
pub struct Sysroot {
|
||||
|
@ -67,18 +67,20 @@ impl Sysroot {
|
|||
self.crates.iter().map(|(id, _data)| id)
|
||||
}
|
||||
|
||||
pub fn discover(dir: &AbsPath) -> Result<Sysroot> {
|
||||
pub fn discover(dir: &AbsPath, config: &CargoConfig) -> Result<Sysroot> {
|
||||
tracing::debug!("Discovering sysroot for {}", dir.display());
|
||||
let sysroot_dir = discover_sysroot_dir(dir)?;
|
||||
let sysroot_src_dir = discover_sysroot_src_dir(&sysroot_dir, dir)?;
|
||||
let sysroot_dir = discover_sysroot_dir(dir, config)?;
|
||||
let sysroot_src_dir = discover_sysroot_src_dir(&sysroot_dir, dir, config)?;
|
||||
let res = Sysroot::load(sysroot_dir, sysroot_src_dir)?;
|
||||
Ok(res)
|
||||
}
|
||||
|
||||
pub fn discover_rustc(cargo_toml: &ManifestPath) -> Option<ManifestPath> {
|
||||
pub fn discover_rustc(cargo_toml: &ManifestPath, config: &CargoConfig) -> Option<ManifestPath> {
|
||||
tracing::debug!("Discovering rustc source for {}", cargo_toml.display());
|
||||
let current_dir = cargo_toml.parent();
|
||||
discover_sysroot_dir(current_dir).ok().and_then(|sysroot_dir| get_rustc_src(&sysroot_dir))
|
||||
discover_sysroot_dir(current_dir, config)
|
||||
.ok()
|
||||
.and_then(|sysroot_dir| get_rustc_src(&sysroot_dir))
|
||||
}
|
||||
|
||||
pub fn load(sysroot_dir: AbsPathBuf, sysroot_src_dir: AbsPathBuf) -> Result<Sysroot> {
|
||||
|
@ -144,8 +146,9 @@ impl Sysroot {
|
|||
}
|
||||
}
|
||||
|
||||
fn discover_sysroot_dir(current_dir: &AbsPath) -> Result<AbsPathBuf> {
|
||||
fn discover_sysroot_dir(current_dir: &AbsPath, config: &CargoConfig) -> Result<AbsPathBuf> {
|
||||
let mut rustc = Command::new(toolchain::rustc());
|
||||
rustc.envs(&config.extra_env);
|
||||
rustc.current_dir(current_dir).args(&["--print", "sysroot"]);
|
||||
tracing::debug!("Discovering sysroot by {:?}", rustc);
|
||||
let stdout = utf8_stdout(rustc)?;
|
||||
|
@ -155,6 +158,7 @@ fn discover_sysroot_dir(current_dir: &AbsPath) -> Result<AbsPathBuf> {
|
|||
fn discover_sysroot_src_dir(
|
||||
sysroot_path: &AbsPathBuf,
|
||||
current_dir: &AbsPath,
|
||||
config: &CargoConfig,
|
||||
) -> Result<AbsPathBuf> {
|
||||
if let Ok(path) = env::var("RUST_SRC_PATH") {
|
||||
let path = AbsPathBuf::try_from(path.as_str())
|
||||
|
@ -170,6 +174,7 @@ fn discover_sysroot_src_dir(
|
|||
get_rust_src(sysroot_path)
|
||||
.or_else(|| {
|
||||
let mut rustup = Command::new(toolchain::rustup());
|
||||
rustup.envs(&config.extra_env);
|
||||
rustup.current_dir(current_dir).args(&["component", "add", "rust-src"]);
|
||||
utf8_stdout(rustup).ok()?;
|
||||
get_rust_src(sysroot_path)
|
||||
|
|
|
@ -10,8 +10,8 @@ use paths::{AbsPath, AbsPathBuf};
|
|||
use serde::de::DeserializeOwned;
|
||||
|
||||
use crate::{
|
||||
CargoWorkspace, CfgOverrides, ProjectJson, ProjectJsonData, ProjectWorkspace, Sysroot,
|
||||
WorkspaceBuildScripts,
|
||||
CargoConfig, CargoWorkspace, CfgOverrides, ProjectJson, ProjectJsonData, ProjectWorkspace,
|
||||
Sysroot, WorkspaceBuildScripts,
|
||||
};
|
||||
|
||||
fn load_cargo(file: &str) -> CrateGraph {
|
||||
|
@ -92,13 +92,17 @@ fn rooted_project_json(data: ProjectJsonData) -> ProjectJson {
|
|||
}
|
||||
|
||||
fn to_crate_graph(project_workspace: ProjectWorkspace) -> CrateGraph {
|
||||
project_workspace.to_crate_graph(&mut |_, _| Ok(Vec::new()), &mut {
|
||||
let mut counter = 0;
|
||||
move |_path| {
|
||||
counter += 1;
|
||||
Some(FileId(counter))
|
||||
}
|
||||
})
|
||||
project_workspace.to_crate_graph(
|
||||
&mut |_, _| Ok(Vec::new()),
|
||||
&mut {
|
||||
let mut counter = 0;
|
||||
move |_path| {
|
||||
counter += 1;
|
||||
Some(FileId(counter))
|
||||
}
|
||||
},
|
||||
&CargoConfig::default(),
|
||||
)
|
||||
}
|
||||
|
||||
fn check_crate_graph(crate_graph: CrateGraph, expect: Expect) {
|
||||
|
|
|
@ -156,11 +156,12 @@ impl ProjectWorkspace {
|
|||
})?;
|
||||
let project_location = project_json.parent().to_path_buf();
|
||||
let project_json = ProjectJson::new(&project_location, data);
|
||||
ProjectWorkspace::load_inline(project_json, config.target.as_deref())?
|
||||
ProjectWorkspace::load_inline(project_json, config.target.as_deref(), config)?
|
||||
}
|
||||
ProjectManifest::CargoToml(cargo_toml) => {
|
||||
let cargo_version = utf8_stdout({
|
||||
let mut cmd = Command::new(toolchain::cargo());
|
||||
cmd.envs(&config.extra_env);
|
||||
cmd.arg("--version");
|
||||
cmd
|
||||
})?;
|
||||
|
@ -186,7 +187,7 @@ impl ProjectWorkspace {
|
|||
let sysroot = if config.no_sysroot {
|
||||
None
|
||||
} else {
|
||||
Some(Sysroot::discover(cargo_toml.parent()).with_context(|| {
|
||||
Some(Sysroot::discover(cargo_toml.parent(), config).with_context(|| {
|
||||
format!(
|
||||
"Failed to find sysroot for Cargo.toml file {}. Is rust-src installed?",
|
||||
cargo_toml.display()
|
||||
|
@ -196,7 +197,7 @@ impl ProjectWorkspace {
|
|||
|
||||
let rustc_dir = match &config.rustc_source {
|
||||
Some(RustcSource::Path(path)) => ManifestPath::try_from(path.clone()).ok(),
|
||||
Some(RustcSource::Discover) => Sysroot::discover_rustc(&cargo_toml),
|
||||
Some(RustcSource::Discover) => Sysroot::discover_rustc(&cargo_toml, config),
|
||||
None => None,
|
||||
};
|
||||
|
||||
|
@ -216,7 +217,7 @@ impl ProjectWorkspace {
|
|||
None => None,
|
||||
};
|
||||
|
||||
let rustc_cfg = rustc_cfg::get(Some(&cargo_toml), config.target.as_deref());
|
||||
let rustc_cfg = rustc_cfg::get(Some(&cargo_toml), config.target.as_deref(), config);
|
||||
|
||||
let cfg_overrides = config.cfg_overrides();
|
||||
ProjectWorkspace::Cargo {
|
||||
|
@ -237,6 +238,7 @@ impl ProjectWorkspace {
|
|||
pub fn load_inline(
|
||||
project_json: ProjectJson,
|
||||
target: Option<&str>,
|
||||
config: &CargoConfig,
|
||||
) -> Result<ProjectWorkspace> {
|
||||
let sysroot = match (project_json.sysroot.clone(), project_json.sysroot_src.clone()) {
|
||||
(Some(sysroot), Some(sysroot_src)) => Some(Sysroot::load(sysroot, sysroot_src)?),
|
||||
|
@ -258,7 +260,7 @@ impl ProjectWorkspace {
|
|||
(None, None) => None,
|
||||
};
|
||||
|
||||
let rustc_cfg = rustc_cfg::get(None, target);
|
||||
let rustc_cfg = rustc_cfg::get(None, target, config);
|
||||
Ok(ProjectWorkspace::Json { project: project_json, sysroot, rustc_cfg })
|
||||
}
|
||||
|
||||
|
@ -268,8 +270,9 @@ impl ProjectWorkspace {
|
|||
.first()
|
||||
.and_then(|it| it.parent())
|
||||
.ok_or_else(|| format_err!("No detached files to load"))?,
|
||||
&CargoConfig::default(),
|
||||
)?;
|
||||
let rustc_cfg = rustc_cfg::get(None, None);
|
||||
let rustc_cfg = rustc_cfg::get(None, None, &CargoConfig::default());
|
||||
Ok(ProjectWorkspace::DetachedFiles { files: detached_files, sysroot, rustc_cfg })
|
||||
}
|
||||
|
||||
|
@ -416,6 +419,7 @@ impl ProjectWorkspace {
|
|||
&self,
|
||||
load_proc_macro: &mut dyn FnMut(&str, &AbsPath) -> ProcMacroLoadResult,
|
||||
load: &mut dyn FnMut(&AbsPath) -> Option<FileId>,
|
||||
config: &CargoConfig,
|
||||
) -> CrateGraph {
|
||||
let _p = profile::span("ProjectWorkspace::to_crate_graph");
|
||||
|
||||
|
@ -426,6 +430,7 @@ impl ProjectWorkspace {
|
|||
load,
|
||||
project,
|
||||
sysroot,
|
||||
config,
|
||||
),
|
||||
ProjectWorkspace::Cargo {
|
||||
cargo,
|
||||
|
@ -464,6 +469,7 @@ fn project_json_to_crate_graph(
|
|||
load: &mut dyn FnMut(&AbsPath) -> Option<FileId>,
|
||||
project: &ProjectJson,
|
||||
sysroot: &Option<Sysroot>,
|
||||
config: &CargoConfig,
|
||||
) -> CrateGraph {
|
||||
let mut crate_graph = CrateGraph::default();
|
||||
let sysroot_deps = sysroot
|
||||
|
@ -489,9 +495,9 @@ fn project_json_to_crate_graph(
|
|||
};
|
||||
|
||||
let target_cfgs = match krate.target.as_deref() {
|
||||
Some(target) => {
|
||||
cfg_cache.entry(target).or_insert_with(|| rustc_cfg::get(None, Some(target)))
|
||||
}
|
||||
Some(target) => cfg_cache
|
||||
.entry(target)
|
||||
.or_insert_with(|| rustc_cfg::get(None, Some(target), config)),
|
||||
None => &rustc_cfg,
|
||||
};
|
||||
|
||||
|
|
|
@ -80,7 +80,8 @@ impl flags::AnalysisStats {
|
|||
Some(build_scripts_sw.elapsed())
|
||||
};
|
||||
|
||||
let (host, vfs, _proc_macro) = load_workspace(workspace, &load_cargo_config)?;
|
||||
let (host, vfs, _proc_macro) =
|
||||
load_workspace(workspace, &cargo_config, &load_cargo_config)?;
|
||||
let db = host.raw_database();
|
||||
eprint!("{:<20} {}", "Database loaded:", db_load_sw.elapsed());
|
||||
eprint!(" (metadata {}", metadata_time);
|
||||
|
|
|
@ -38,7 +38,7 @@ pub fn load_workspace_at(
|
|||
workspace.set_build_scripts(build_scripts)
|
||||
}
|
||||
|
||||
load_workspace(workspace, load_config)
|
||||
load_workspace(workspace, cargo_config, load_config)
|
||||
}
|
||||
|
||||
// Note: Since this function is used by external tools that use rust-analyzer as a library
|
||||
|
@ -48,6 +48,7 @@ pub fn load_workspace_at(
|
|||
// these tools need access to `ProjectWorkspace`, too, which `load_workspace_at` hides.
|
||||
pub fn load_workspace(
|
||||
ws: ProjectWorkspace,
|
||||
cargo_config: &CargoConfig,
|
||||
load_config: &LoadCargoConfig,
|
||||
) -> Result<(AnalysisHost, vfs::Vfs, Option<ProcMacroServer>)> {
|
||||
let (sender, receiver) = unbounded();
|
||||
|
@ -75,6 +76,7 @@ pub fn load_workspace(
|
|||
vfs.set_file_contents(path.clone(), contents);
|
||||
vfs.file_id(&path)
|
||||
},
|
||||
cargo_config,
|
||||
);
|
||||
|
||||
let project_folders = ProjectFolders::new(&[ws], &[]);
|
||||
|
|
|
@ -299,7 +299,8 @@ impl flags::Lsif {
|
|||
|
||||
let workspace = ProjectWorkspace::load(manifest, &cargo_config, no_progress)?;
|
||||
|
||||
let (host, vfs, _proc_macro) = load_workspace(workspace, &load_cargo_config)?;
|
||||
let (host, vfs, _proc_macro) =
|
||||
load_workspace(workspace, &cargo_config, &load_cargo_config)?;
|
||||
let db = host.raw_database();
|
||||
let analysis = host.analysis();
|
||||
|
||||
|
|
|
@ -40,7 +40,7 @@ impl flags::Scip {
|
|||
|
||||
let workspace = ProjectWorkspace::load(manifest, &cargo_config, no_progress)?;
|
||||
|
||||
let (host, vfs, _) = load_workspace(workspace, &load_cargo_config)?;
|
||||
let (host, vfs, _) = load_workspace(workspace, &cargo_config, &load_cargo_config)?;
|
||||
let db = host.raw_database();
|
||||
let analysis = host.analysis();
|
||||
|
||||
|
|
|
@ -84,6 +84,9 @@ config_data! {
|
|||
/// Use `RUSTC_WRAPPER=rust-analyzer` when running build scripts to
|
||||
/// avoid checking unnecessary things.
|
||||
cargo_buildScripts_useRustcWrapper: bool = "true",
|
||||
/// Extra environment variables that will be set when running cargo, rustc
|
||||
/// or other commands within the workspace. Useful for setting RUSTFLAGS.
|
||||
cargo_extraEnv: FxHashMap<String, String> = "{}",
|
||||
/// List of features to activate.
|
||||
///
|
||||
/// Set this to `"all"` to pass `--all-features` to cargo.
|
||||
|
@ -105,6 +108,8 @@ config_data! {
|
|||
checkOnSave_enable: bool = "true",
|
||||
/// Extra arguments for `cargo check`.
|
||||
checkOnSave_extraArgs: Vec<String> = "[]",
|
||||
/// Extra environment variables that will be set when running `cargo check`.
|
||||
checkOnSave_extraEnv: FxHashMap<String, String> = "{}",
|
||||
/// List of features to activate. Defaults to
|
||||
/// `#rust-analyzer.cargo.features#`.
|
||||
///
|
||||
|
@ -219,7 +224,6 @@ config_data! {
|
|||
files_excludeDirs: Vec<PathBuf> = "[]",
|
||||
/// Controls file watching implementation.
|
||||
files_watcher: FilesWatcherDef = "\"client\"",
|
||||
|
||||
/// Enables highlighting of related references while the cursor is on `break`, `loop`, `while`, or `for` keywords.
|
||||
highlightRelated_breakPoints_enable: bool = "true",
|
||||
/// Enables highlighting of all exit points while the cursor is on any `return`, `?`, `fn`, or return type arrow (`->`).
|
||||
|
@ -263,6 +267,8 @@ config_data! {
|
|||
imports_group_enable: bool = "true",
|
||||
/// Whether to allow import insertion to merge new imports into single path glob imports like `use std::fmt::*;`.
|
||||
imports_merge_glob: bool = "true",
|
||||
/// Prefer to unconditionally use imports of the core and alloc crate, over the std crate.
|
||||
imports_prefer_no_std: bool = "false",
|
||||
/// The path structure for newly inserted paths to use.
|
||||
imports_prefix: ImportPrefixDef = "\"plain\"",
|
||||
|
||||
|
@ -307,6 +313,7 @@ config_data! {
|
|||
/// Join lines unwraps trivial blocks.
|
||||
joinLines_unwrapTrivialBlock: bool = "true",
|
||||
|
||||
|
||||
/// Whether to show `Debug` lens. Only applies when
|
||||
/// `#rust-analyzer.lens.enable#` is set.
|
||||
lens_debug_enable: bool = "true",
|
||||
|
@ -318,6 +325,8 @@ config_data! {
|
|||
/// Whether to show `Implementations` lens. Only applies when
|
||||
/// `#rust-analyzer.lens.enable#` is set.
|
||||
lens_implementations_enable: bool = "true",
|
||||
/// Where to render annotations.
|
||||
lens_location: AnnotationLocation = "\"above_name\"",
|
||||
/// Whether to show `References` lens for Struct, Enum, and Union.
|
||||
/// Only applies when `#rust-analyzer.lens.enable#` is set.
|
||||
lens_references_adt_enable: bool = "false",
|
||||
|
@ -359,6 +368,9 @@ config_data! {
|
|||
/// this is rust-analyzer itself, but we override this in tests).
|
||||
procMacro_server: Option<PathBuf> = "null",
|
||||
|
||||
/// Exclude imports from find-all-references.
|
||||
references_excludeImports: bool = "false",
|
||||
|
||||
/// Command to be executed instead of 'cargo' for runnables.
|
||||
runnables_command: Option<String> = "null",
|
||||
/// Additional arguments to be passed to cargo for runnables such as
|
||||
|
@ -494,6 +506,25 @@ pub struct LensConfig {
|
|||
pub refs_adt: bool, // for Struct, Enum, Union and Trait
|
||||
pub refs_trait: bool, // for Struct, Enum, Union and Trait
|
||||
pub enum_variant_refs: bool,
|
||||
|
||||
// annotations
|
||||
pub location: AnnotationLocation,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, PartialEq, Eq, Deserialize)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum AnnotationLocation {
|
||||
AboveName,
|
||||
AboveWholeItem,
|
||||
}
|
||||
|
||||
impl From<AnnotationLocation> for ide::AnnotationLocation {
|
||||
fn from(location: AnnotationLocation) -> Self {
|
||||
match location {
|
||||
AnnotationLocation::AboveName => ide::AnnotationLocation::AboveName,
|
||||
AnnotationLocation::AboveWholeItem => ide::AnnotationLocation::AboveWholeItem,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl LensConfig {
|
||||
|
@ -918,6 +949,7 @@ impl Config {
|
|||
ExprFillDefaultDef::Default => ExprFillDefaultMode::Default,
|
||||
},
|
||||
insert_use: self.insert_use_config(),
|
||||
prefer_no_std: self.data.imports_prefer_no_std,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -929,6 +961,16 @@ impl Config {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn extra_env(&self) -> &FxHashMap<String, String> {
|
||||
&self.data.cargo_extraEnv
|
||||
}
|
||||
|
||||
pub fn check_on_save_extra_env(&self) -> FxHashMap<String, String> {
|
||||
let mut extra_env = self.data.cargo_extraEnv.clone();
|
||||
extra_env.extend(self.data.checkOnSave_extraEnv.clone());
|
||||
extra_env
|
||||
}
|
||||
|
||||
pub fn lru_capacity(&self) -> Option<usize> {
|
||||
self.data.lru_capacity
|
||||
}
|
||||
|
@ -998,6 +1040,7 @@ impl Config {
|
|||
unset_test_crates: UnsetTestCrates::Only(self.data.cargo_unsetTest.clone()),
|
||||
wrap_rustc_in_build_scripts: self.data.cargo_buildScripts_useRustcWrapper,
|
||||
run_build_script_command: self.data.cargo_buildScripts_overrideCommand.clone(),
|
||||
extra_env: self.data.cargo_extraEnv.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1023,7 +1066,11 @@ impl Config {
|
|||
Some(args) if !args.is_empty() => {
|
||||
let mut args = args.clone();
|
||||
let command = args.remove(0);
|
||||
FlycheckConfig::CustomCommand { command, args }
|
||||
FlycheckConfig::CustomCommand {
|
||||
command,
|
||||
args,
|
||||
extra_env: self.check_on_save_extra_env(),
|
||||
}
|
||||
}
|
||||
Some(_) | None => FlycheckConfig::CargoCommand {
|
||||
command: self.data.checkOnSave_command.clone(),
|
||||
|
@ -1051,6 +1098,7 @@ impl Config {
|
|||
CargoFeatures::Listed(it) => it,
|
||||
},
|
||||
extra_args: self.data.checkOnSave_extraArgs.clone(),
|
||||
extra_env: self.check_on_save_extra_env(),
|
||||
},
|
||||
};
|
||||
Some(flycheck_config)
|
||||
|
@ -1133,6 +1181,7 @@ impl Config {
|
|||
CallableCompletionDef::None => None,
|
||||
},
|
||||
insert_use: self.insert_use_config(),
|
||||
prefer_no_std: self.data.imports_prefer_no_std,
|
||||
snippet_cap: SnippetCap::new(try_or_def!(
|
||||
self.caps
|
||||
.text_document
|
||||
|
@ -1147,6 +1196,10 @@ impl Config {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn find_all_refs_exclude_imports(&self) -> bool {
|
||||
self.data.references_excludeImports
|
||||
}
|
||||
|
||||
pub fn snippet_cap(&self) -> bool {
|
||||
self.experimental("snippetTextEdit")
|
||||
}
|
||||
|
@ -1156,6 +1209,7 @@ impl Config {
|
|||
snippet_cap: SnippetCap::new(self.experimental("snippetTextEdit")),
|
||||
allowed: None,
|
||||
insert_use: self.insert_use_config(),
|
||||
prefer_no_std: self.data.imports_prefer_no_std,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1185,6 +1239,7 @@ impl Config {
|
|||
refs_trait: self.data.lens_enable && self.data.lens_references_trait_enable,
|
||||
enum_variant_refs: self.data.lens_enable
|
||||
&& self.data.lens_references_enumVariant_enable,
|
||||
location: self.data.lens_location,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1921,6 +1976,14 @@ fn field_props(field: &str, ty: &str, doc: &[&str], default: &str) -> serde_json
|
|||
"Use server-side file watching",
|
||||
],
|
||||
},
|
||||
"AnnotationLocation" => set! {
|
||||
"type": "string",
|
||||
"enum": ["above_name", "above_whole_item"],
|
||||
"enumDescriptions": [
|
||||
"Render annotations above the name of the item.",
|
||||
"Render annotations above the whole item, including documentation comments and attributes."
|
||||
],
|
||||
},
|
||||
_ => panic!("missing entry for {}: {}", ty, default),
|
||||
}
|
||||
|
||||
|
|
|
@ -10,8 +10,8 @@ use std::{
|
|||
use anyhow::Context;
|
||||
use ide::{
|
||||
AnnotationConfig, AssistKind, AssistResolveStrategy, FileId, FilePosition, FileRange,
|
||||
HoverAction, HoverGotoTypeData, Query, RangeInfo, Runnable, RunnableKind, SingleResolve,
|
||||
SourceChange, TextEdit,
|
||||
HoverAction, HoverGotoTypeData, Query, RangeInfo, ReferenceCategory, Runnable, RunnableKind,
|
||||
SingleResolve, SourceChange, TextEdit,
|
||||
};
|
||||
use ide_db::SymbolKind;
|
||||
use lsp_server::ErrorCode;
|
||||
|
@ -1012,6 +1012,8 @@ pub(crate) fn handle_references(
|
|||
let _p = profile::span("handle_references");
|
||||
let position = from_proto::file_position(&snap, params.text_document_position)?;
|
||||
|
||||
let exclude_imports = snap.config.find_all_refs_exclude_imports();
|
||||
|
||||
let refs = match snap.analysis.find_all_refs(position, None)? {
|
||||
None => return Ok(None),
|
||||
Some(refs) => refs,
|
||||
|
@ -1032,7 +1034,11 @@ pub(crate) fn handle_references(
|
|||
refs.references
|
||||
.into_iter()
|
||||
.flat_map(|(file_id, refs)| {
|
||||
refs.into_iter().map(move |(range, _)| FileRange { file_id, range })
|
||||
refs.into_iter()
|
||||
.filter(|&(_, category)| {
|
||||
!exclude_imports || category != Some(ReferenceCategory::Import)
|
||||
})
|
||||
.map(move |(range, _)| FileRange { file_id, range })
|
||||
})
|
||||
.chain(decl)
|
||||
})
|
||||
|
@ -1234,6 +1240,7 @@ pub(crate) fn handle_code_lens(
|
|||
annotate_references: lens_config.refs_adt,
|
||||
annotate_method_references: lens_config.method_refs,
|
||||
annotate_enum_variant_references: lens_config.enum_variant_refs,
|
||||
location: lens_config.location.into(),
|
||||
},
|
||||
file_id,
|
||||
)?;
|
||||
|
@ -1283,7 +1290,7 @@ pub(crate) fn handle_document_highlight(
|
|||
.into_iter()
|
||||
.map(|ide::HighlightedRange { range, category }| lsp_types::DocumentHighlight {
|
||||
range: to_proto::range(&line_index, range),
|
||||
kind: category.map(to_proto::document_highlight_kind),
|
||||
kind: category.and_then(to_proto::document_highlight_kind),
|
||||
})
|
||||
.collect();
|
||||
Ok(Some(res))
|
||||
|
@ -1782,6 +1789,7 @@ fn run_rustfmt(
|
|||
let mut command = match snap.config.rustfmt() {
|
||||
RustfmtConfig::Rustfmt { extra_args, enable_range_formatting } => {
|
||||
let mut cmd = process::Command::new(toolchain::rustfmt());
|
||||
cmd.envs(snap.config.extra_env());
|
||||
cmd.args(extra_args);
|
||||
// try to chdir to the file so we can respect `rustfmt.toml`
|
||||
// FIXME: use `rustfmt --config-path` once
|
||||
|
@ -1839,6 +1847,7 @@ fn run_rustfmt(
|
|||
}
|
||||
RustfmtConfig::CustomCommand { command, args } => {
|
||||
let mut cmd = process::Command::new(command);
|
||||
cmd.envs(snap.config.extra_env());
|
||||
cmd.args(args);
|
||||
cmd
|
||||
}
|
||||
|
|
|
@ -145,6 +145,7 @@ fn integrated_completion_benchmark() {
|
|||
skip_glob_imports: true,
|
||||
},
|
||||
snippets: Vec::new(),
|
||||
prefer_no_std: false,
|
||||
};
|
||||
let position =
|
||||
FilePosition { file_id, offset: TextSize::try_from(completion_offset).unwrap() };
|
||||
|
@ -182,6 +183,7 @@ fn integrated_completion_benchmark() {
|
|||
skip_glob_imports: true,
|
||||
},
|
||||
snippets: Vec::new(),
|
||||
prefer_no_std: false,
|
||||
};
|
||||
let position =
|
||||
FilePosition { file_id, offset: TextSize::try_from(completion_offset).unwrap() };
|
||||
|
|
|
@ -143,6 +143,7 @@ impl GlobalState {
|
|||
project_model::ProjectWorkspace::load_inline(
|
||||
it.clone(),
|
||||
cargo_config.target.as_deref(),
|
||||
&cargo_config,
|
||||
)
|
||||
}
|
||||
})
|
||||
|
@ -398,7 +399,11 @@ impl GlobalState {
|
|||
dummy_replacements.get(crate_name).map(|v| &**v).unwrap_or_default(),
|
||||
)
|
||||
};
|
||||
crate_graph.extend(ws.to_crate_graph(&mut load_proc_macro, &mut load));
|
||||
crate_graph.extend(ws.to_crate_graph(
|
||||
&mut load_proc_macro,
|
||||
&mut load,
|
||||
&self.config.cargo(),
|
||||
));
|
||||
}
|
||||
crate_graph
|
||||
};
|
||||
|
|
|
@ -83,10 +83,11 @@ pub(crate) fn structure_node_kind(kind: StructureNodeKind) -> lsp_types::SymbolK
|
|||
|
||||
pub(crate) fn document_highlight_kind(
|
||||
category: ReferenceCategory,
|
||||
) -> lsp_types::DocumentHighlightKind {
|
||||
) -> Option<lsp_types::DocumentHighlightKind> {
|
||||
match category {
|
||||
ReferenceCategory::Read => lsp_types::DocumentHighlightKind::READ,
|
||||
ReferenceCategory::Write => lsp_types::DocumentHighlightKind::WRITE,
|
||||
ReferenceCategory::Read => Some(lsp_types::DocumentHighlightKind::READ),
|
||||
ReferenceCategory::Write => Some(lsp_types::DocumentHighlightKind::WRITE),
|
||||
ReferenceCategory::Import => None,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -46,6 +46,12 @@ cargo check --quiet --workspace --message-format=json --all-targets
|
|||
Use `RUSTC_WRAPPER=rust-analyzer` when running build scripts to
|
||||
avoid checking unnecessary things.
|
||||
--
|
||||
[[rust-analyzer.cargo.extraEnv]]rust-analyzer.cargo.extraEnv (default: `{}`)::
|
||||
+
|
||||
--
|
||||
Extra environment variables that will be set when running cargo, rustc
|
||||
or other commands within the workspace. Useful for setting RUSTFLAGS.
|
||||
--
|
||||
[[rust-analyzer.cargo.features]]rust-analyzer.cargo.features (default: `[]`)::
|
||||
+
|
||||
--
|
||||
|
@ -93,6 +99,11 @@ Run specified `cargo check` command for diagnostics on save.
|
|||
--
|
||||
Extra arguments for `cargo check`.
|
||||
--
|
||||
[[rust-analyzer.checkOnSave.extraEnv]]rust-analyzer.checkOnSave.extraEnv (default: `{}`)::
|
||||
+
|
||||
--
|
||||
Extra environment variables that will be set when running `cargo check`.
|
||||
--
|
||||
[[rust-analyzer.checkOnSave.features]]rust-analyzer.checkOnSave.features (default: `null`)::
|
||||
+
|
||||
--
|
||||
|
@ -353,6 +364,11 @@ Group inserted imports by the https://rust-analyzer.github.io/manual.html#auto-i
|
|||
--
|
||||
Whether to allow import insertion to merge new imports into single path glob imports like `use std::fmt::*;`.
|
||||
--
|
||||
[[rust-analyzer.imports.prefer.no.std]]rust-analyzer.imports.prefer.no.std (default: `false`)::
|
||||
+
|
||||
--
|
||||
Prefer to unconditionally use imports of the core and alloc crate, over the std crate.
|
||||
--
|
||||
[[rust-analyzer.imports.prefix]]rust-analyzer.imports.prefix (default: `"plain"`)::
|
||||
+
|
||||
--
|
||||
|
@ -474,6 +490,11 @@ client doesn't set the corresponding capability.
|
|||
Whether to show `Implementations` lens. Only applies when
|
||||
`#rust-analyzer.lens.enable#` is set.
|
||||
--
|
||||
[[rust-analyzer.lens.location]]rust-analyzer.lens.location (default: `"above_name"`)::
|
||||
+
|
||||
--
|
||||
Where to render annotations.
|
||||
--
|
||||
[[rust-analyzer.lens.references.adt.enable]]rust-analyzer.lens.references.adt.enable (default: `false`)::
|
||||
+
|
||||
--
|
||||
|
@ -546,6 +567,11 @@ This config takes a map of crate names with the exported proc-macro names to ign
|
|||
Internal config, path to proc-macro server executable (typically,
|
||||
this is rust-analyzer itself, but we override this in tests).
|
||||
--
|
||||
[[rust-analyzer.references.excludeImports]]rust-analyzer.references.excludeImports (default: `false`)::
|
||||
+
|
||||
--
|
||||
Exclude imports from find-all-references.
|
||||
--
|
||||
[[rust-analyzer.runnables.command]]rust-analyzer.runnables.command (default: `null`)::
|
||||
+
|
||||
--
|
||||
|
|
|
@ -206,11 +206,6 @@
|
|||
"title": "Show RA Version",
|
||||
"category": "rust-analyzer"
|
||||
},
|
||||
{
|
||||
"command": "rust-analyzer.toggleInlayHints",
|
||||
"title": "Toggle inlay hints",
|
||||
"category": "rust-analyzer"
|
||||
},
|
||||
{
|
||||
"command": "rust-analyzer.openDocs",
|
||||
"title": "Open docs under cursor",
|
||||
|
@ -442,6 +437,11 @@
|
|||
"default": true,
|
||||
"type": "boolean"
|
||||
},
|
||||
"rust-analyzer.cargo.extraEnv": {
|
||||
"markdownDescription": "Extra environment variables that will be set when running cargo, rustc\nor other commands within the workspace. Useful for setting RUSTFLAGS.",
|
||||
"default": {},
|
||||
"type": "object"
|
||||
},
|
||||
"rust-analyzer.cargo.features": {
|
||||
"markdownDescription": "List of features to activate.\n\nSet this to `\"all\"` to pass `--all-features` to cargo.",
|
||||
"default": [],
|
||||
|
@ -514,6 +514,11 @@
|
|||
"type": "string"
|
||||
}
|
||||
},
|
||||
"rust-analyzer.checkOnSave.extraEnv": {
|
||||
"markdownDescription": "Extra environment variables that will be set when running `cargo check`.",
|
||||
"default": {},
|
||||
"type": "object"
|
||||
},
|
||||
"rust-analyzer.checkOnSave.features": {
|
||||
"markdownDescription": "List of features to activate. Defaults to\n`#rust-analyzer.cargo.features#`.\n\nSet to `\"all\"` to pass `--all-features` to Cargo.",
|
||||
"default": null,
|
||||
|
@ -803,6 +808,11 @@
|
|||
"default": true,
|
||||
"type": "boolean"
|
||||
},
|
||||
"rust-analyzer.imports.prefer.no.std": {
|
||||
"markdownDescription": "Prefer to unconditionally use imports of the core and alloc crate, over the std crate.",
|
||||
"default": false,
|
||||
"type": "boolean"
|
||||
},
|
||||
"rust-analyzer.imports.prefix": {
|
||||
"markdownDescription": "The path structure for newly inserted paths to use.",
|
||||
"default": "plain",
|
||||
|
@ -963,6 +973,19 @@
|
|||
"default": true,
|
||||
"type": "boolean"
|
||||
},
|
||||
"rust-analyzer.lens.location": {
|
||||
"markdownDescription": "Where to render annotations.",
|
||||
"default": "above_name",
|
||||
"type": "string",
|
||||
"enum": [
|
||||
"above_name",
|
||||
"above_whole_item"
|
||||
],
|
||||
"enumDescriptions": [
|
||||
"Render annotations above the name of the item.",
|
||||
"Render annotations above the whole item, including documentation comments and attributes."
|
||||
]
|
||||
},
|
||||
"rust-analyzer.lens.references.adt.enable": {
|
||||
"markdownDescription": "Whether to show `References` lens for Struct, Enum, and Union.\nOnly applies when `#rust-analyzer.lens.enable#` is set.",
|
||||
"default": false,
|
||||
|
@ -1036,6 +1059,11 @@
|
|||
"string"
|
||||
]
|
||||
},
|
||||
"rust-analyzer.references.excludeImports": {
|
||||
"markdownDescription": "Exclude imports from find-all-references.",
|
||||
"default": false,
|
||||
"type": "boolean"
|
||||
},
|
||||
"rust-analyzer.runnables.command": {
|
||||
"markdownDescription": "Command to be executed instead of 'cargo' for runnables.",
|
||||
"default": null,
|
||||
|
@ -1633,10 +1661,6 @@
|
|||
"command": "rust-analyzer.serverVersion",
|
||||
"when": "inRustProject"
|
||||
},
|
||||
{
|
||||
"command": "rust-analyzer.toggleInlayHints",
|
||||
"when": "inRustProject"
|
||||
},
|
||||
{
|
||||
"command": "rust-analyzer.openDocs",
|
||||
"when": "inRustProject"
|
||||
|
|
|
@ -321,30 +321,6 @@ export function serverVersion(ctx: Ctx): Cmd {
|
|||
};
|
||||
}
|
||||
|
||||
export function toggleInlayHints(_ctx: Ctx): Cmd {
|
||||
return async () => {
|
||||
const config = vscode.workspace.getConfiguration("editor.inlayHints", {
|
||||
languageId: "rust",
|
||||
});
|
||||
|
||||
const value = config.get("enabled");
|
||||
let stringValue;
|
||||
if (typeof value === "string") {
|
||||
stringValue = value;
|
||||
} else {
|
||||
stringValue = value ? "on" : "off";
|
||||
}
|
||||
const nextValues: Record<string, string> = {
|
||||
on: "off",
|
||||
off: "on",
|
||||
onUnlessPressed: "offUnlessPressed",
|
||||
offUnlessPressed: "onUnlessPressed",
|
||||
};
|
||||
const nextValue = nextValues[stringValue] ?? "on";
|
||||
await config.update("enabled", nextValue, vscode.ConfigurationTarget.Global);
|
||||
};
|
||||
}
|
||||
|
||||
// Opens the virtual file that will show the syntax tree
|
||||
//
|
||||
// The contents of the file come from the `TextDocumentContentProvider`
|
||||
|
|
|
@ -180,7 +180,6 @@ async function initCommonContext(context: vscode.ExtensionContext, ctx: Ctx) {
|
|||
|
||||
ctx.registerCommand("ssr", commands.ssr);
|
||||
ctx.registerCommand("serverVersion", commands.serverVersion);
|
||||
ctx.registerCommand("toggleInlayHints", commands.toggleInlayHints);
|
||||
|
||||
// Internal commands which are invoked by the server.
|
||||
ctx.registerCommand("runSingle", commands.runSingle);
|
||||
|
|
Loading…
Reference in a new issue