Skip to content

Commit 6c79794

Browse files
committed
fix: sort_text and search through orderred collections
1 parent ccf0975 commit 6c79794

File tree

5 files changed

+28
-22
lines changed

5 files changed

+28
-22
lines changed

src/lib.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use anyhow::Result;
33
use ropey::Rope;
44
use serde::Deserialize;
55
use std::borrow::Cow;
6-
use std::collections::{HashMap, HashSet};
6+
use std::collections::{BTreeMap, HashMap, HashSet};
77
use std::io::prelude::*;
88
use tokio::sync::{mpsc, oneshot};
99
use tower_lsp::lsp_types::*;
@@ -286,7 +286,7 @@ pub struct BackendState {
286286
settings: BackendSettings,
287287
docs: HashMap<Url, Document>,
288288
snippets: Vec<Snippet>,
289-
unicode_input: HashMap<String, String>,
289+
unicode_input: BTreeMap<String, String>,
290290
max_unicude_input_prefix_len: usize,
291291
min_unicude_input_prefix_len: usize,
292292
max_snippet_input_prefix_len: usize,
@@ -301,7 +301,7 @@ impl BackendState {
301301
pub async fn new(
302302
home_dir: String,
303303
snippets: Vec<Snippet>,
304-
unicode_input: HashMap<String, String>,
304+
unicode_input: BTreeMap<String, String>,
305305
) -> (mpsc::UnboundedSender<BackendRequest>, Self) {
306306
let (request_tx, request_rx) = mpsc::unbounded_channel::<BackendRequest>();
307307

@@ -562,6 +562,7 @@ impl BackendState {
562562
};
563563
CompletionItem {
564564
label: s.prefix.to_owned(),
565+
sort_text: Some(s.prefix.to_string()),
565566
filter_text: Some(if filter_text_prefix.is_empty() {
566567
s.prefix.to_string()
567568
} else {
@@ -668,6 +669,7 @@ impl BackendState {
668669
};
669670
Some(CompletionItem {
670671
label: body.to_string(),
672+
sort_text: Some(prefix.to_string()),
671673
filter_text: Some(format!("{word_prefix}{prefix}")),
672674
kind: Some(CompletionItemKind::TEXT),
673675
text_edit: Some(CompletionTextEdit::InsertAndReplace(InsertReplaceEdit {
@@ -789,6 +791,7 @@ impl BackendState {
789791
};
790792
Some(CompletionItem {
791793
label: full_path.to_string(),
794+
sort_text: Some(full_path.to_string()),
792795
filter_text: Some(format!("{word_prefix}{full_path}")),
793796
kind: Some(if path.is_dir() {
794797
CompletionItemKind::FOLDER
@@ -941,6 +944,7 @@ impl BackendState {
941944
};
942945
Some(CompletionItem {
943946
label: format!("@{}", b.key),
947+
sort_text: Some(word_prefix.to_string()),
944948
filter_text: Some(word_prefix.to_string()),
945949
kind: Some(CompletionItemKind::REFERENCE),
946950
text_edit: Some(CompletionTextEdit::InsertAndReplace(

src/main.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use etcetera::base_strategy::{choose_base_strategy, BaseStrategy};
2-
use std::collections::HashMap;
2+
use std::collections::BTreeMap;
33
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
44
use xshell::{cmd, Shell};
55

@@ -45,7 +45,7 @@ async fn serve(start_options: &StartOptions) {
4545
let unicode_input = load_unicode_input_from_path(&start_options.unicode_input_path)
4646
.unwrap_or_else(|e| {
4747
tracing::error!("On read 'unicode input' config: {e}");
48-
HashMap::new()
48+
BTreeMap::new()
4949
});
5050

5151
server::start(

src/server.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::{snippets::Snippet, BackendRequest, BackendResponse, BackendState};
2-
use std::collections::HashMap;
2+
use std::collections::BTreeMap;
33
use tokio::io::{AsyncRead, AsyncWrite};
44
use tokio::sync::{mpsc, oneshot};
55
use tower_lsp::jsonrpc::Result;
@@ -113,7 +113,7 @@ pub async fn start<I, O>(
113113
read: I,
114114
write: O,
115115
snippets: Vec<Snippet>,
116-
unicode_input: HashMap<String, String>,
116+
unicode_input: BTreeMap<String, String>,
117117
home_dir: String,
118118
) where
119119
I: AsyncRead + Unpin,

src/snippets/config.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::snippets::vscode::VSSnippetsConfig;
33
use crate::StartOptions;
44
use anyhow::Result;
55
use serde::Deserialize;
6-
use std::collections::HashMap;
6+
use std::collections::BTreeMap;
77

88
#[derive(Deserialize)]
99
pub struct SnippetsConfig {
@@ -21,7 +21,7 @@ pub struct Snippet {
2121
#[derive(Deserialize)]
2222
pub struct UnicodeInputConfig {
2323
#[serde(flatten)]
24-
pub inner: HashMap<String, String>,
24+
pub inner: BTreeMap<String, String>,
2525
}
2626

2727
pub fn load_snippets(start_options: &StartOptions) -> Result<Vec<Snippet>> {
@@ -70,6 +70,8 @@ pub fn load_snippets(start_options: &StartOptions) -> Result<Vec<Snippet>> {
7070
}
7171
}
7272

73+
snippets.sort_unstable_by(|a, b| a.prefix.cmp(&b.prefix));
74+
7375
Ok(snippets)
7476
}
7577

@@ -169,7 +171,7 @@ pub fn load_snippets_from_path(
169171
Ok(snippets)
170172
}
171173

172-
pub fn load_unicode_input_from_file(path: &std::path::PathBuf) -> Result<HashMap<String, String>> {
174+
pub fn load_unicode_input_from_file(path: &std::path::PathBuf) -> Result<BTreeMap<String, String>> {
173175
tracing::info!("Try load 'unicode input' config from: {path:?}");
174176

175177
let content = std::fs::read_to_string(path)?;
@@ -188,12 +190,12 @@ pub fn load_unicode_input_from_file(path: &std::path::PathBuf) -> Result<HashMap
188190

189191
pub fn load_unicode_input_from_path(
190192
snippets_path: &std::path::PathBuf,
191-
) -> Result<HashMap<String, String>> {
193+
) -> Result<BTreeMap<String, String>> {
192194
if snippets_path.is_file() {
193195
return load_unicode_input_from_file(snippets_path);
194196
}
195197

196-
let mut result = HashMap::new();
198+
let mut result = BTreeMap::new();
197199
match std::fs::read_dir(snippets_path) {
198200
Ok(entries) => {
199201
for entry in entries {

tests/basic.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use simple_completion_language_server::{ac_searcher, search, server, snippets, RopeReader};
2-
use std::collections::HashMap;
2+
use std::collections::BTreeMap;
33
use std::io::Read;
44

55
use std::pin::Pin;
@@ -75,7 +75,7 @@ struct TestContext {
7575
impl TestContext {
7676
pub async fn new(
7777
snippets: Vec<snippets::Snippet>,
78-
unicode_input: HashMap<String, String>,
78+
unicode_input: BTreeMap<String, String>,
7979
home_dir: String,
8080
) -> anyhow::Result<Self> {
8181
let (request_tx, rx) = mpsc::unbounded_channel::<String>();
@@ -227,7 +227,7 @@ fn words_search() -> anyhow::Result<()> {
227227

228228
#[test_log::test(tokio::test)]
229229
async fn initialize() -> anyhow::Result<()> {
230-
let mut context = TestContext::new(Vec::new(), HashMap::new(), String::new()).await?;
230+
let mut context = TestContext::new(Vec::new(), BTreeMap::new(), String::new()).await?;
231231

232232
let request = jsonrpc::Request::build("initialize")
233233
.id(1)
@@ -258,7 +258,7 @@ async fn initialize() -> anyhow::Result<()> {
258258

259259
#[test_log::test(tokio::test)]
260260
async fn completion() -> anyhow::Result<()> {
261-
let mut context = TestContext::new(Vec::new(), HashMap::new(), String::new()).await?;
261+
let mut context = TestContext::new(Vec::new(), BTreeMap::new(), String::new()).await?;
262262
context.initialize().await?;
263263
context.send_all(&[
264264
r#"{"jsonrpc":"2.0","method":"textDocument/didOpen","params":{"textDocument":{"languageId":"python","text":"hello\nhe","uri":"file:///tmp/main.py","version":0}}}"#,
@@ -295,7 +295,7 @@ async fn completion() -> anyhow::Result<()> {
295295

296296
#[test_log::test(tokio::test)]
297297
async fn completion_by_quoted_word() -> anyhow::Result<()> {
298-
let mut context = TestContext::new(Vec::new(), HashMap::new(), String::new()).await?;
298+
let mut context = TestContext::new(Vec::new(), BTreeMap::new(), String::new()).await?;
299299
context.initialize().await?;
300300
context.send_all(&[
301301
r#"{"jsonrpc":"2.0","method":"textDocument/didOpen","params":{"textDocument":{"languageId":"python","text":"function(\"hello\")\nhe","uri":"file:///tmp/main.py","version":0}}}"#,
@@ -334,7 +334,7 @@ async fn snippets() -> anyhow::Result<()> {
334334
description: None,
335335
},
336336
],
337-
HashMap::new(),
337+
BTreeMap::new(),
338338
String::new(),
339339
)
340340
.await?;
@@ -374,7 +374,7 @@ async fn snippets_inline_by_word_tail() -> anyhow::Result<()> {
374374
body: "^2".to_string(),
375375
description: None,
376376
}],
377-
HashMap::new(),
377+
BTreeMap::new(),
378378
String::new(),
379379
)
380380
.await?;
@@ -428,7 +428,7 @@ async fn snippets_inline_by_word_tail() -> anyhow::Result<()> {
428428
async fn unicode_input() -> anyhow::Result<()> {
429429
let mut context = TestContext::new(
430430
Vec::new(),
431-
HashMap::from_iter([
431+
BTreeMap::from_iter([
432432
("alpha".to_string(), "α".to_string()),
433433
("betta".to_string(), "β".to_string()),
434434
]),
@@ -484,7 +484,7 @@ async fn unicode_input() -> anyhow::Result<()> {
484484
async fn paths() -> anyhow::Result<()> {
485485
std::fs::create_dir_all("/tmp/scls-test/sub-folder")?;
486486

487-
let mut context = TestContext::new(Vec::new(), HashMap::new(), "/tmp".to_string()).await?;
487+
let mut context = TestContext::new(Vec::new(), BTreeMap::new(), "/tmp".to_string()).await?;
488488
context.initialize().await?;
489489

490490
let request = jsonrpc::Request::from_str(&serde_json::to_string(&serde_json::json!(
@@ -625,7 +625,7 @@ bibliography: "/tmp/scls-test-citation/test.bib" # could also be surrounded by b
625625

626626
std::fs::write("/tmp/scls-test-citation/test.bib", bib)?;
627627

628-
let mut context = TestContext::new(Vec::new(), HashMap::new(), String::new()).await?;
628+
let mut context = TestContext::new(Vec::new(), BTreeMap::new(), String::new()).await?;
629629
context.initialize().await?;
630630

631631
let request = jsonrpc::Request::from_str(&serde_json::to_string(&serde_json::json!(

0 commit comments

Comments
 (0)