mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-01 06:11:35 +00:00
Align config's API with usage
The config now is mostly immutable, optimize for that.
This commit is contained in:
parent
7ae4b8bdb6
commit
f8a0561178
22 changed files with 164 additions and 162 deletions
|
@ -2,7 +2,7 @@
|
|||
|
||||
mod format_like;
|
||||
|
||||
use ide_db::ty_filter::TryEnum;
|
||||
use ide_db::{helpers::SnippetCap, ty_filter::TryEnum};
|
||||
use syntax::{
|
||||
ast::{self, AstNode, AstToken},
|
||||
SyntaxKind::{BLOCK_EXPR, EXPR_STMT},
|
||||
|
@ -10,9 +10,8 @@ use syntax::{
|
|||
};
|
||||
use text_edit::TextEdit;
|
||||
|
||||
use self::format_like::add_format_like_completions;
|
||||
use crate::{
|
||||
config::SnippetCap,
|
||||
completions::postfix::format_like::add_format_like_completions,
|
||||
context::CompletionContext,
|
||||
item::{Builder, CompletionKind},
|
||||
CompletionItem, CompletionItemKind, Completions,
|
||||
|
|
|
@ -14,12 +14,11 @@
|
|||
// + `logw` -> `log::warn!(...)`
|
||||
// + `loge` -> `log::error!(...)`
|
||||
|
||||
use crate::{
|
||||
completions::postfix::postfix_snippet, config::SnippetCap, context::CompletionContext,
|
||||
Completions,
|
||||
};
|
||||
use ide_db::helpers::SnippetCap;
|
||||
use syntax::ast::{self, AstToken};
|
||||
|
||||
use crate::{completions::postfix::postfix_snippet, context::CompletionContext, Completions};
|
||||
|
||||
/// Mapping ("postfix completion item" => "macro to use")
|
||||
static KINDS: &[(&str, &str)] = &[
|
||||
("format", "format!"),
|
||||
|
|
|
@ -1,8 +1,10 @@
|
|||
//! This file provides snippet completions, like `pd` => `eprintln!(...)`.
|
||||
|
||||
use ide_db::helpers::SnippetCap;
|
||||
|
||||
use crate::{
|
||||
config::SnippetCap, item::Builder, CompletionContext, CompletionItem, CompletionItemKind,
|
||||
CompletionKind, Completions,
|
||||
item::Builder, CompletionContext, CompletionItem, CompletionItemKind, CompletionKind,
|
||||
Completions,
|
||||
};
|
||||
|
||||
fn snippet(ctx: &CompletionContext, cap: SnippetCap, label: &str, snippet: &str) -> Builder {
|
||||
|
|
|
@ -192,12 +192,14 @@ mod tests {
|
|||
use test_utils::mark;
|
||||
|
||||
use crate::{
|
||||
test_utils::{check_edit, check_edit_with_config, completion_list_with_config},
|
||||
test_utils::{
|
||||
check_edit, check_edit_with_config, completion_list_with_config, TEST_CONFIG,
|
||||
},
|
||||
CompletionConfig, CompletionKind,
|
||||
};
|
||||
|
||||
fn check(ra_fixture: &str, expect: Expect) {
|
||||
check_with_config(CompletionConfig::default(), ra_fixture, expect);
|
||||
check_with_config(TEST_CONFIG, ra_fixture, expect);
|
||||
}
|
||||
|
||||
fn check_with_config(config: CompletionConfig, ra_fixture: &str, expect: Expect) {
|
||||
|
@ -205,10 +207,6 @@ mod tests {
|
|||
expect.assert_eq(&actual)
|
||||
}
|
||||
|
||||
fn fuzzy_completion_config() -> CompletionConfig {
|
||||
CompletionConfig::default()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn self_fulfilling_completion() {
|
||||
mark::check!(self_fulfilling_completion);
|
||||
|
@ -832,7 +830,7 @@ impl My<|>
|
|||
#[test]
|
||||
fn function_fuzzy_completion() {
|
||||
check_edit_with_config(
|
||||
fuzzy_completion_config(),
|
||||
TEST_CONFIG,
|
||||
"stdin",
|
||||
r#"
|
||||
//- /lib.rs crate:dep
|
||||
|
@ -858,7 +856,7 @@ fn main() {
|
|||
#[test]
|
||||
fn macro_fuzzy_completion() {
|
||||
check_edit_with_config(
|
||||
fuzzy_completion_config(),
|
||||
TEST_CONFIG,
|
||||
"macro_with_curlies!",
|
||||
r#"
|
||||
//- /lib.rs crate:dep
|
||||
|
@ -886,7 +884,7 @@ fn main() {
|
|||
#[test]
|
||||
fn struct_fuzzy_completion() {
|
||||
check_edit_with_config(
|
||||
fuzzy_completion_config(),
|
||||
TEST_CONFIG,
|
||||
"ThirdStruct",
|
||||
r#"
|
||||
//- /lib.rs crate:dep
|
||||
|
@ -917,7 +915,7 @@ fn main() {
|
|||
fn fuzzy_completions_come_in_specific_order() {
|
||||
mark::check!(certain_fuzzy_order_test);
|
||||
check_with_config(
|
||||
fuzzy_completion_config(),
|
||||
TEST_CONFIG,
|
||||
r#"
|
||||
//- /lib.rs crate:dep
|
||||
pub struct FirstStruct;
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
//! module, and we use to statically check that we only produce snippet
|
||||
//! completions if we are allowed to.
|
||||
|
||||
use ide_db::helpers::insert_use::MergeBehavior;
|
||||
use ide_db::helpers::{insert_use::MergeBehavior, SnippetCap};
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct CompletionConfig {
|
||||
|
@ -15,27 +15,3 @@ pub struct CompletionConfig {
|
|||
pub snippet_cap: Option<SnippetCap>,
|
||||
pub merge: Option<MergeBehavior>,
|
||||
}
|
||||
|
||||
impl CompletionConfig {
|
||||
pub fn allow_snippets(&mut self, yes: bool) {
|
||||
self.snippet_cap = if yes { Some(SnippetCap { _private: () }) } else { None }
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub struct SnippetCap {
|
||||
_private: (),
|
||||
}
|
||||
|
||||
impl Default for CompletionConfig {
|
||||
fn default() -> Self {
|
||||
CompletionConfig {
|
||||
enable_postfix_completions: true,
|
||||
enable_autoimport_completions: true,
|
||||
add_call_parenthesis: true,
|
||||
add_call_argument_snippets: true,
|
||||
snippet_cap: Some(SnippetCap { _private: () }),
|
||||
merge: Some(MergeBehavior::Full),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,13 +5,11 @@ use std::fmt;
|
|||
use hir::{Documentation, ModPath, Mutability};
|
||||
use ide_db::helpers::{
|
||||
insert_use::{self, ImportScope, MergeBehavior},
|
||||
mod_path_to_ast,
|
||||
mod_path_to_ast, SnippetCap,
|
||||
};
|
||||
use syntax::{algo, TextRange};
|
||||
use text_edit::TextEdit;
|
||||
|
||||
use crate::config::SnippetCap;
|
||||
|
||||
/// `CompletionItem` describes a single completion variant in the editor pop-up.
|
||||
/// It is basically a POD with various properties. To construct a
|
||||
/// `CompletionItem`, use `new` method and the `Builder` struct.
|
||||
|
|
|
@ -158,8 +158,7 @@ pub fn resolve_completion_edits(
|
|||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::config::CompletionConfig;
|
||||
use crate::test_utils;
|
||||
use crate::test_utils::{self, TEST_CONFIG};
|
||||
|
||||
struct DetailAndDocumentation<'a> {
|
||||
detail: &'a str,
|
||||
|
@ -168,7 +167,7 @@ mod tests {
|
|||
|
||||
fn check_detail_and_documentation(ra_fixture: &str, expected: DetailAndDocumentation) {
|
||||
let (db, position) = test_utils::position(ra_fixture);
|
||||
let config = CompletionConfig::default();
|
||||
let config = TEST_CONFIG;
|
||||
let completions: Vec<_> = crate::completions(&db, &config, position).unwrap().into();
|
||||
for item in completions {
|
||||
if item.detail() == Some(expected.detail) {
|
||||
|
@ -183,7 +182,7 @@ mod tests {
|
|||
|
||||
fn check_no_completion(ra_fixture: &str) {
|
||||
let (db, position) = test_utils::position(ra_fixture);
|
||||
let config = CompletionConfig::default();
|
||||
let config = TEST_CONFIG;
|
||||
|
||||
let completions: Option<Vec<String>> = crate::completions(&db, &config, position)
|
||||
.and_then(|completions| {
|
||||
|
|
|
@ -11,13 +11,13 @@ pub(crate) mod type_alias;
|
|||
mod builder_ext;
|
||||
|
||||
use hir::{Documentation, HasAttrs, HirDisplay, Mutability, ScopeDef, Type};
|
||||
use ide_db::RootDatabase;
|
||||
use ide_db::{helpers::SnippetCap, RootDatabase};
|
||||
use syntax::TextRange;
|
||||
use test_utils::mark;
|
||||
|
||||
use crate::{
|
||||
config::SnippetCap, item::ImportEdit, CompletionContext, CompletionItem, CompletionItemKind,
|
||||
CompletionKind, CompletionScore,
|
||||
item::ImportEdit, CompletionContext, CompletionItem, CompletionItemKind, CompletionKind,
|
||||
CompletionScore,
|
||||
};
|
||||
|
||||
use crate::render::{enum_variant::render_variant, function::render_fn, macro_::render_macro};
|
||||
|
@ -320,8 +320,8 @@ mod tests {
|
|||
use test_utils::mark;
|
||||
|
||||
use crate::{
|
||||
test_utils::{check_edit, do_completion, get_all_items},
|
||||
CompletionConfig, CompletionKind, CompletionScore,
|
||||
test_utils::{check_edit, do_completion, get_all_items, TEST_CONFIG},
|
||||
CompletionKind, CompletionScore,
|
||||
};
|
||||
|
||||
fn check(ra_fixture: &str, expect: Expect) {
|
||||
|
@ -338,7 +338,7 @@ mod tests {
|
|||
}
|
||||
}
|
||||
|
||||
let mut completions = get_all_items(CompletionConfig::default(), ra_fixture);
|
||||
let mut completions = get_all_items(TEST_CONFIG, ra_fixture);
|
||||
completions.sort_by_key(|it| (Reverse(it.score()), it.label().to_string()));
|
||||
let actual = completions
|
||||
.into_iter()
|
||||
|
|
|
@ -113,7 +113,7 @@ mod tests {
|
|||
use test_utils::mark;
|
||||
|
||||
use crate::{
|
||||
test_utils::{check_edit, check_edit_with_config},
|
||||
test_utils::{check_edit, check_edit_with_config, TEST_CONFIG},
|
||||
CompletionConfig,
|
||||
};
|
||||
|
||||
|
@ -211,7 +211,7 @@ fn main() { S::foo(${1:&self})$0 }
|
|||
fn suppress_arg_snippets() {
|
||||
mark::check!(suppress_arg_snippets);
|
||||
check_edit_with_config(
|
||||
CompletionConfig { add_call_argument_snippets: false, ..CompletionConfig::default() },
|
||||
CompletionConfig { add_call_argument_snippets: false, ..TEST_CONFIG },
|
||||
"with_args",
|
||||
r#"
|
||||
fn with_args(x: i32, y: String) {}
|
||||
|
|
|
@ -1,12 +1,10 @@
|
|||
//! Renderer for patterns.
|
||||
|
||||
use hir::{db::HirDatabase, HasAttrs, HasVisibility, Name, StructKind};
|
||||
use ide_db::helpers::SnippetCap;
|
||||
use itertools::Itertools;
|
||||
|
||||
use crate::{
|
||||
config::SnippetCap, item::CompletionKind, render::RenderContext, CompletionItem,
|
||||
CompletionItemKind,
|
||||
};
|
||||
use crate::{item::CompletionKind, render::RenderContext, CompletionItem, CompletionItemKind};
|
||||
|
||||
fn visible_fields(
|
||||
ctx: &RenderContext<'_>,
|
||||
|
|
|
@ -1,8 +1,11 @@
|
|||
//! Runs completion for testing purposes.
|
||||
|
||||
use hir::Semantics;
|
||||
use ide_db::base_db::{fixture::ChangeFixture, FileLoader, FilePosition};
|
||||
use ide_db::RootDatabase;
|
||||
use ide_db::{
|
||||
base_db::{fixture::ChangeFixture, FileLoader, FilePosition},
|
||||
helpers::{insert_use::MergeBehavior, SnippetCap},
|
||||
RootDatabase,
|
||||
};
|
||||
use itertools::Itertools;
|
||||
use stdx::{format_to, trim_indent};
|
||||
use syntax::{AstNode, NodeOrToken, SyntaxElement};
|
||||
|
@ -10,6 +13,15 @@ use test_utils::{assert_eq_text, RangeOrOffset};
|
|||
|
||||
use crate::{item::CompletionKind, CompletionConfig, CompletionItem};
|
||||
|
||||
pub(crate) const TEST_CONFIG: CompletionConfig = CompletionConfig {
|
||||
enable_postfix_completions: true,
|
||||
enable_autoimport_completions: true,
|
||||
add_call_parenthesis: true,
|
||||
add_call_argument_snippets: true,
|
||||
snippet_cap: SnippetCap::new(true),
|
||||
merge: Some(MergeBehavior::Full),
|
||||
};
|
||||
|
||||
/// Creates analysis from a multi-file fixture, returns positions marked with <|>.
|
||||
pub(crate) fn position(ra_fixture: &str) -> (RootDatabase, FilePosition) {
|
||||
let change_fixture = ChangeFixture::parse(ra_fixture);
|
||||
|
@ -24,7 +36,7 @@ pub(crate) fn position(ra_fixture: &str) -> (RootDatabase, FilePosition) {
|
|||
}
|
||||
|
||||
pub(crate) fn do_completion(code: &str, kind: CompletionKind) -> Vec<CompletionItem> {
|
||||
do_completion_with_config(CompletionConfig::default(), code, kind)
|
||||
do_completion_with_config(TEST_CONFIG, code, kind)
|
||||
}
|
||||
|
||||
pub(crate) fn do_completion_with_config(
|
||||
|
@ -39,7 +51,7 @@ pub(crate) fn do_completion_with_config(
|
|||
}
|
||||
|
||||
pub(crate) fn completion_list(code: &str, kind: CompletionKind) -> String {
|
||||
completion_list_with_config(CompletionConfig::default(), code, kind)
|
||||
completion_list_with_config(TEST_CONFIG, code, kind)
|
||||
}
|
||||
|
||||
pub(crate) fn completion_list_with_config(
|
||||
|
@ -76,7 +88,7 @@ fn monospace_width(s: &str) -> usize {
|
|||
}
|
||||
|
||||
pub(crate) fn check_edit(what: &str, ra_fixture_before: &str, ra_fixture_after: &str) {
|
||||
check_edit_with_config(CompletionConfig::default(), what, ra_fixture_before, ra_fixture_after)
|
||||
check_edit_with_config(TEST_CONFIG, what, ra_fixture_before, ra_fixture_after)
|
||||
}
|
||||
|
||||
pub(crate) fn check_edit_with_config(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue