Skip to content

Commit 56dca7f

Browse files
committed
改进补全列表体验
1 parent 80b9d77 commit 56dca7f

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

tools/bangls/src/main.rs

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use display_source::DisplaySourceMeta;
88
use line_column::line_column;
99
use linked_hash_map::LinkedHashMap;
1010
use lsp_server::{IoThreads, Message};
11-
use lsp_types::{CompletionItem, CompletionOptions, Diagnostic, DiagnosticSeverity, InitializeParams, InitializeResult, MessageType, Position, ServerCapabilities, ShowMessageParams, TextDocumentSyncCapability, TextDocumentSyncKind, Uri, notification::{self, Notification}, request::{self, Request}};
11+
use lsp_types::{CompletionItem, CompletionItemKind, CompletionOptions, Diagnostic, DiagnosticSeverity, InitializeParams, InitializeResult, MessageType, Position, ServerCapabilities, ShowMessageParams, TextDocumentSyncCapability, TextDocumentSyncKind, Uri, notification::{self, Notification}, request::{self, Request}};
1212
use syntax::{Compile, CompileMeta, CompileMetaExtends, Emulate, EmulateInfo, Expand, LSP_DEBUG, LSP_HOVER};
1313

1414
fn main() {
@@ -332,29 +332,46 @@ fn generate_completes(infos: &[EmulateInfo]) -> Vec<CompletionItem> {
332332
kinds.push(kind);
333333
}
334334
}
335-
var_counter.iter().map(|(&var, &(count, ref kinds))| {
335+
let mut items: Vec<CompletionItem> = var_counter.iter().map(|(&var, &(count, ref kinds))| {
336336
let is_full_deps = count == full_count;
337337

338-
let kind = kinds.iter().map(|kind| match kind {
339-
Emulate::Const => format!("constant"),
340-
Emulate::Binder => format!("binder"),
341-
Emulate::ConstBind(var) => format!("const bind to `{var}`"),
342-
Emulate::NakedBind(var) => format!("naked bind to `{var}`"),
343-
}).unique().join(" & ");
338+
let mut first_kind = None;
339+
let kind = kinds.iter()
340+
.inspect(|k| _ = first_kind.get_or_insert(**k))
341+
.map(|kind| match kind {
342+
Emulate::Const => format!("constant"),
343+
Emulate::Binder => format!("binder"),
344+
Emulate::ConstBind(var) => format!("const bind to `{var}`"),
345+
Emulate::NakedBind(var) => format!("naked bind to `{var}`"),
346+
}).unique().join(" & ");
344347

345348
let (label, detail) = if is_full_deps {
346349
(var.to_string(), format!("kind: {kind}\nfull deps ({count}/{full_count})"))
347350
} else {
348351
(format!("{var}?"), format!("kind: {kind}\npartial deps ({count}/{full_count})"))
349352
};
350353

354+
let first_kind = first_kind.map(|it| match it {
355+
Emulate::Const => CompletionItemKind::CONSTANT,
356+
Emulate::Binder => CompletionItemKind::VALUE,
357+
Emulate::ConstBind(_) => CompletionItemKind::METHOD,
358+
Emulate::NakedBind(_) => CompletionItemKind::FIELD,
359+
});
360+
351361
CompletionItem {
352362
label,
353363
detail: Some(detail),
364+
kind: first_kind,
354365
insert_text: Some(var.to_string()),
355366
..Default::default()
356367
}
357-
}).collect()
368+
}).collect();
369+
items.sort_by(|a, b| {
370+
let a = a.sort_text.as_ref().unwrap_or(&a.label);
371+
let b = b.sort_text.as_ref().unwrap_or(&b.label);
372+
(a.starts_with('_'), a).cmp(&(b.starts_with('_'), b))
373+
});
374+
items
358375
}
359376

360377
trait NotificationHandler: Notification {

0 commit comments

Comments
 (0)