Файли
openidea/crates/ide-host/tests/search_encoding.rs
T
Fabio Nunes b075f5de2a feat(editorconfig): apply charset to existing files and accept the listed encodings (#5509)
An .editorconfig charset now applies to existing files when they are opened and decoded, not only to new ones, and every charset EditorConfig lists is accepted (latin1, utf-8, utf-8-bom, utf-16be, utf-16le). Find in Files decodes closed files with that charset too, so a Latin-1 file is found by its accented text.

Co-authored-by: LittleGuava <[email protected]>
2026-11-27 09:51:49 +08:00

75 рядки
3.3 KiB
Rust

mod common;
use ide_core::RequestId;
use ide_host::{Host, ops};
use std::sync::{Arc, Mutex};
async fn find(host: &Host, pattern: &str) -> Vec<ide_search::FileMatches> {
let hits = Arc::new(Mutex::new(Vec::new()));
let out = hits.clone();
let query = serde_json::from_value(serde_json::json!({"pattern": pattern, "isRegex": false, "caseSensitive": false, "wholeWord": false, "include": [], "exclude": [], "scope": {"kind": "project"}, "maxMatches": 100})).unwrap();
ops::search::search_start(
host,
"main",
RequestId::next(),
query,
ops::Channel::new(move |event| {
if let ops::search::SearchEvent::File { file } = event {
out.lock().unwrap().push(file);
}
}),
)
.await
.unwrap();
std::mem::take(&mut *hits.lock().unwrap())
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn find_in_files_decodes_closed_files_with_the_project_encoding() {
let temp = tempfile::tempdir().unwrap();
let base = dunce::canonicalize(temp.path()).unwrap();
let root = base.join("project");
std::fs::create_dir(&root).unwrap();
let text = "class CreditQuerySendUtils {\n //中文搜索验证 Chinese Chinese Chinese\n}\n";
std::fs::write(root.join("CreditQuerySendUtils.java"), ide_vfs::encode(text, "gbk", false).unwrap()).unwrap();
let host = Host::new(common::host_config(&base));
host.trust.decide(&root, true, false).unwrap();
let project = host.open_project("main", &root).unwrap();
let mut defaults = project.documents.file_defaults();
defaults.project_fallback = "gbk".into();
project.documents.set_file_defaults(defaults);
let files = find(&host, "中文搜索验证").await;
assert_eq!(files.len(), 1);
assert_eq!(files[0].matches[0].text, " //中文搜索验证 Chinese Chinese Chinese");
assert_eq!(find(&host, "Chinese").await[0].matches[0].text, " //中文搜索验证 Chinese Chinese Chinese");
// The configured encoding is used rather than a guess.
let mut defaults = project.documents.file_defaults();
defaults.project_fallback = "big5".into();
project.documents.set_file_defaults(defaults);
assert!(find(&host, "中文搜索验证").await.is_empty());
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn find_in_files_decodes_closed_files_with_the_editorconfig_charset() {
let temp = tempfile::tempdir().unwrap();
let base = dunce::canonicalize(temp.path()).unwrap();
let root = base.join("project");
std::fs::create_dir(&root).unwrap();
std::fs::write(root.join(".editorconfig"), "root = true\n[*.java]\ncharset = iso-8859-15\n").unwrap();
std::fs::write(root.join("A.java"), b"// a\xe7\xe3o \xa4 needle\n").unwrap();
let host = Host::new(common::host_config(&base));
host.trust.decide(&root, true, false).unwrap();
let project = host.open_project("main", &root).unwrap();
let mut defaults = project.documents.file_defaults();
defaults.project_fallback = "gbk".into();
project.documents.set_file_defaults(defaults);
let files = find(&host, "ação €").await;
assert_eq!(files.len(), 1);
assert_eq!(files[0].matches[0].text, "// ação € needle", "the text the editor shows");
assert_eq!(find(&host, "needle").await[0].matches[0].text, "// ação € needle");
}