feat: reorder snippet completion items (#865)

This commit is contained in:
Myriad-Dreamin 2024-11-20 16:19:45 +08:00 committed by GitHub
parent 18c6cdd9d4
commit 19a83bc942
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 184 additions and 188 deletions

View file

@ -8,31 +8,13 @@ input_file: crates/tinymist-query/src/fixtures/completion/show.typ
{
"isIncomplete": false,
"items": [
{
"kind": 15,
"label": "regex selector",
"sortText": "000",
"textEdit": {
"newText": "regex(\"${1:regex}\"): ${2:}",
"range": {
"end": {
"character": 7,
"line": 1
},
"start": {
"character": 6,
"line": 1
}
}
}
},
{
"kind": 3,
"label": "raw",
"labelDetails": {
"description": "(str, align: alignment, block: bool, lang: none | str, syntaxes: [syntax], tab-size: int, theme: [theme]) => raw"
},
"sortText": "096",
"sortText": "094",
"textEdit": {
"newText": "raw: ${1:}",
"range": {
@ -53,7 +35,7 @@ input_file: crates/tinymist-query/src/fixtures/completion/show.typ
"labelDetails": {
"description": "(str, align: alignment, block: bool, lang: none | str, syntaxes: [syntax], tab-size: int, theme: [theme]) => raw"
},
"sortText": "097",
"sortText": "095",
"textEdit": {
"newText": "raw.where(${1:}): ${2:}",
"range": {
@ -67,6 +49,24 @@ input_file: crates/tinymist-query/src/fixtures/completion/show.typ
}
}
}
},
{
"kind": 15,
"label": "regex selector",
"sortText": "100",
"textEdit": {
"newText": "regex(\"${1:regex}\"): ${2:}",
"range": {
"end": {
"character": 7,
"line": 1
},
"start": {
"character": 6,
"line": 1
}
}
}
}
]
}

View file

@ -8,31 +8,13 @@ input_file: crates/tinymist-query/src/fixtures/completion/show2.typ
{
"isIncomplete": false,
"items": [
{
"kind": 15,
"label": "regex selector",
"sortText": "000",
"textEdit": {
"newText": "regex(\"${1:regex}\"): ${2:}",
"range": {
"end": {
"character": 6,
"line": 1
},
"start": {
"character": 6,
"line": 1
}
}
}
},
{
"kind": 3,
"label": "raw",
"labelDetails": {
"description": "(str, align: alignment, block: bool, lang: none | str, syntaxes: [syntax], tab-size: int, theme: [theme]) => raw"
},
"sortText": "096",
"sortText": "094",
"textEdit": {
"newText": "raw: ${1:}",
"range": {
@ -53,7 +35,7 @@ input_file: crates/tinymist-query/src/fixtures/completion/show2.typ
"labelDetails": {
"description": "(str, align: alignment, block: bool, lang: none | str, syntaxes: [syntax], tab-size: int, theme: [theme]) => raw"
},
"sortText": "097",
"sortText": "095",
"textEdit": {
"newText": "raw.where(${1:}): ${2:}",
"range": {
@ -67,6 +49,24 @@ input_file: crates/tinymist-query/src/fixtures/completion/show2.typ
}
}
}
},
{
"kind": 15,
"label": "regex selector",
"sortText": "100",
"textEdit": {
"newText": "regex(\"${1:regex}\"): ${2:}",
"range": {
"end": {
"character": 6,
"line": 1
},
"start": {
"character": 6,
"line": 1
}
}
}
}
]
}

View file

@ -8,31 +8,13 @@ input_file: crates/tinymist-query/src/fixtures/completion/show3.typ
{
"isIncomplete": false,
"items": [
{
"kind": 15,
"label": "regex selector",
"sortText": "000",
"textEdit": {
"newText": "regex(\"${1:regex}\"): ${2:}",
"range": {
"end": {
"character": 6,
"line": 1
},
"start": {
"character": 6,
"line": 1
}
}
}
},
{
"kind": 3,
"label": "raw",
"labelDetails": {
"description": "(str, align: alignment, block: bool, lang: none | str, syntaxes: [syntax], tab-size: int, theme: [theme]) => raw"
},
"sortText": "096",
"sortText": "094",
"textEdit": {
"newText": "raw: ${1:}",
"range": {
@ -53,7 +35,7 @@ input_file: crates/tinymist-query/src/fixtures/completion/show3.typ
"labelDetails": {
"description": "(str, align: alignment, block: bool, lang: none | str, syntaxes: [syntax], tab-size: int, theme: [theme]) => raw"
},
"sortText": "097",
"sortText": "095",
"textEdit": {
"newText": "raw.where(${1:}): ${2:}",
"range": {
@ -67,6 +49,24 @@ input_file: crates/tinymist-query/src/fixtures/completion/show3.typ
}
}
}
},
{
"kind": 15,
"label": "regex selector",
"sortText": "100",
"textEdit": {
"newText": "regex(\"${1:regex}\"): ${2:}",
"range": {
"end": {
"character": 6,
"line": 1
},
"start": {
"character": 6,
"line": 1
}
}
}
}
]
}

View file

@ -8,31 +8,13 @@ input_file: crates/tinymist-query/src/fixtures/completion/show_transform.typ
{
"isIncomplete": false,
"items": [
{
"kind": 15,
"label": "replacement",
"sortText": "000",
"textEdit": {
"newText": "[${1:content}]",
"range": {
"end": {
"character": 12,
"line": 1
},
"start": {
"character": 11,
"line": 1
}
}
}
},
{
"kind": 3,
"label": "raw",
"labelDetails": {
"description": "(str, align: alignment, block: bool, lang: none | str, syntaxes: [syntax], tab-size: int, theme: [theme]) => raw"
},
"sortText": "157",
"sortText": "154",
"textEdit": {
"newText": "raw(${1:})",
"range": {
@ -53,7 +35,7 @@ input_file: crates/tinymist-query/src/fixtures/completion/show_transform.typ
"labelDetails": {
"description": "(str, align: alignment, block: bool, lang: none | str, syntaxes: [syntax], tab-size: int, theme: [theme]) => raw"
},
"sortText": "158",
"sortText": "155",
"textEdit": {
"newText": "raw.with(${1:})",
"range": {
@ -74,7 +56,7 @@ input_file: crates/tinymist-query/src/fixtures/completion/show_transform.typ
"labelDetails": {
"description": "([any], encoding: \"utf8\" | none) => bytes | str"
},
"sortText": "159",
"sortText": "156",
"textEdit": {
"newText": "read(${1:})",
"range": {
@ -95,7 +77,7 @@ input_file: crates/tinymist-query/src/fixtures/completion/show_transform.typ
"labelDetails": {
"description": "([any], encoding: \"utf8\" | none) => bytes | str"
},
"sortText": "160",
"sortText": "157",
"textEdit": {
"newText": "read.with(${1:})",
"range": {
@ -109,6 +91,24 @@ input_file: crates/tinymist-query/src/fixtures/completion/show_transform.typ
}
}
}
},
{
"kind": 15,
"label": "replacement",
"sortText": "166",
"textEdit": {
"newText": "[${1:content}]",
"range": {
"end": {
"character": 12,
"line": 1
},
"start": {
"character": 11,
"line": 1
}
}
}
}
]
}

View file

@ -8,31 +8,13 @@ input_file: crates/tinymist-query/src/fixtures/completion/show_transform2.typ
{
"isIncomplete": false,
"items": [
{
"kind": 15,
"label": "replacement",
"sortText": "000",
"textEdit": {
"newText": "[${1:content}]",
"range": {
"end": {
"character": 11,
"line": 1
},
"start": {
"character": 11,
"line": 1
}
}
}
},
{
"kind": 3,
"label": "raw",
"labelDetails": {
"description": "(str, align: alignment, block: bool, lang: none | str, syntaxes: [syntax], tab-size: int, theme: [theme]) => raw"
},
"sortText": "157",
"sortText": "154",
"textEdit": {
"newText": "raw(${1:})",
"range": {
@ -53,7 +35,7 @@ input_file: crates/tinymist-query/src/fixtures/completion/show_transform2.typ
"labelDetails": {
"description": "(str, align: alignment, block: bool, lang: none | str, syntaxes: [syntax], tab-size: int, theme: [theme]) => raw"
},
"sortText": "158",
"sortText": "155",
"textEdit": {
"newText": "raw.with(${1:})",
"range": {
@ -74,7 +56,7 @@ input_file: crates/tinymist-query/src/fixtures/completion/show_transform2.typ
"labelDetails": {
"description": "([any], encoding: \"utf8\" | none) => bytes | str"
},
"sortText": "159",
"sortText": "156",
"textEdit": {
"newText": "read(${1:})",
"range": {
@ -95,7 +77,7 @@ input_file: crates/tinymist-query/src/fixtures/completion/show_transform2.typ
"labelDetails": {
"description": "([any], encoding: \"utf8\" | none) => bytes | str"
},
"sortText": "160",
"sortText": "157",
"textEdit": {
"newText": "read.with(${1:})",
"range": {
@ -109,6 +91,24 @@ input_file: crates/tinymist-query/src/fixtures/completion/show_transform2.typ
}
}
}
},
{
"kind": 15,
"label": "replacement",
"sortText": "166",
"textEdit": {
"newText": "[${1:content}]",
"range": {
"end": {
"character": 11,
"line": 1
},
"start": {
"character": 11,
"line": 1
}
}
}
}
]
}

View file

@ -8,31 +8,13 @@ input_file: crates/tinymist-query/src/fixtures/completion/show_transform3.typ
{
"isIncomplete": false,
"items": [
{
"kind": 15,
"label": "replacement",
"sortText": "000",
"textEdit": {
"newText": "[${1:content}]",
"range": {
"end": {
"character": 10,
"line": 1
},
"start": {
"character": 10,
"line": 1
}
}
}
},
{
"kind": 3,
"label": "raw",
"labelDetails": {
"description": "(str, align: alignment, block: bool, lang: none | str, syntaxes: [syntax], tab-size: int, theme: [theme]) => raw"
},
"sortText": "157",
"sortText": "154",
"textEdit": {
"newText": "raw(${1:})",
"range": {
@ -53,7 +35,7 @@ input_file: crates/tinymist-query/src/fixtures/completion/show_transform3.typ
"labelDetails": {
"description": "(str, align: alignment, block: bool, lang: none | str, syntaxes: [syntax], tab-size: int, theme: [theme]) => raw"
},
"sortText": "158",
"sortText": "155",
"textEdit": {
"newText": "raw.with(${1:})",
"range": {
@ -74,7 +56,7 @@ input_file: crates/tinymist-query/src/fixtures/completion/show_transform3.typ
"labelDetails": {
"description": "([any], encoding: \"utf8\" | none) => bytes | str"
},
"sortText": "159",
"sortText": "156",
"textEdit": {
"newText": "read(${1:})",
"range": {
@ -95,7 +77,7 @@ input_file: crates/tinymist-query/src/fixtures/completion/show_transform3.typ
"labelDetails": {
"description": "([any], encoding: \"utf8\" | none) => bytes | str"
},
"sortText": "160",
"sortText": "157",
"textEdit": {
"newText": "read.with(${1:})",
"range": {
@ -109,6 +91,24 @@ input_file: crates/tinymist-query/src/fixtures/completion/show_transform3.typ
}
}
}
},
{
"kind": 15,
"label": "replacement",
"sortText": "166",
"textEdit": {
"newText": "[${1:content}]",
"range": {
"end": {
"character": 10,
"line": 1
},
"start": {
"character": 10,
"line": 1
}
}
}
}
]
}

View file

@ -930,67 +930,6 @@ fn encolsed_by(parent: &LinkedNode, s: Option<Span>, leaf: &LinkedNode) -> bool
s.and_then(|s| parent.find(s)?.find(leaf.span())).is_some()
}
fn sort_and_explicit_code_completion(ctx: &mut CompletionContext) {
let mut completions = std::mem::take(&mut ctx.completions);
let explict = ctx.explicit;
ctx.explicit = true;
let ty = Some(Ty::from_types(ctx.seen_types.iter().cloned()));
let from_ty = std::mem::replace(&mut ctx.from_ty, ty);
complete_code(ctx, true);
ctx.from_ty = from_ty;
ctx.explicit = explict;
// ctx.strict_scope_completions(false, |value| value.ty() == *ty);
// let length_ty = Type::of::<Length>();
// ctx.strict_scope_completions(false, |value| value.ty() == length_ty);
// let color_ty = Type::of::<Color>();
// ctx.strict_scope_completions(false, |value| value.ty() == color_ty);
// let ty = Type::of::<Dir>();
// ctx.strict_scope_completions(false, |value| value.ty() == ty);
log::debug!(
"sort_and_explicit_code_completion: {completions:#?} {:#?}",
ctx.completions
);
completions.sort_by(|a, b| {
a.sort_text
.as_ref()
.cmp(&b.sort_text.as_ref())
.then_with(|| a.label.cmp(&b.label))
});
ctx.completions.sort_by(|a, b| {
a.sort_text
.as_ref()
.cmp(&b.sort_text.as_ref())
.then_with(|| a.label.cmp(&b.label))
});
// todo: this is a bit messy, we can refactor for improving maintainability
// The messy code will finally gone, but to help us go over the mess stage, I
// drop some comment here.
//
// currently, there are only path completions in ctx.completions2
// and type/named param/positional param completions in completions
// and all rest less relevant completions inctx.completions
for (i, compl) in ctx.completions2.iter_mut().enumerate() {
compl.sort_text = Some(format!("{i:03}"));
}
let sort_base = ctx.completions2.len();
for (i, compl) in (completions.iter_mut().chain(ctx.completions.iter_mut())).enumerate() {
compl.sort_text = Some(eco_format!("{i:03}", i = i + sort_base));
}
log::debug!(
"sort_and_explicit_code_completion after: {completions:#?} {:#?}",
ctx.completions
);
ctx.completions.append(&mut completions);
log::debug!("sort_and_explicit_code_completion: {:?}", ctx.completions);
}
pub fn ty_to_completion_kind(ty: &Ty) -> CompletionKind {
match ty {
Ty::Value(ty) => value_to_completion_kind(&ty.val),
@ -1454,6 +1393,15 @@ pub(crate) fn complete_type(ctx: &mut CompletionContext) -> Option<()> {
}
}
let mut completions = std::mem::take(&mut ctx.completions);
let explict = ctx.explicit;
ctx.explicit = true;
let ty = Some(Ty::from_types(ctx.seen_types.iter().cloned()));
let from_ty = std::mem::replace(&mut ctx.from_ty, ty);
complete_code(ctx, true);
ctx.from_ty = from_ty;
ctx.explicit = explict;
match scope {
SurroundingSyntax::Regular => {}
SurroundingSyntax::Selector => {
@ -1491,7 +1439,55 @@ pub(crate) fn complete_type(ctx: &mut CompletionContext) -> Option<()> {
SurroundingSyntax::SetRule => {}
}
sort_and_explicit_code_completion(ctx);
// ctx.strict_scope_completions(false, |value| value.ty() == *ty);
// let length_ty = Type::of::<Length>();
// ctx.strict_scope_completions(false, |value| value.ty() == length_ty);
// let color_ty = Type::of::<Color>();
// ctx.strict_scope_completions(false, |value| value.ty() == color_ty);
// let ty = Type::of::<Dir>();
// ctx.strict_scope_completions(false, |value| value.ty() == ty);
log::debug!(
"sort_and_explicit_code_completion: {completions:#?} {:#?}",
ctx.completions
);
completions.sort_by(|a, b| {
a.sort_text
.as_ref()
.cmp(&b.sort_text.as_ref())
.then_with(|| a.label.cmp(&b.label))
});
ctx.completions.sort_by(|a, b| {
a.sort_text
.as_ref()
.cmp(&b.sort_text.as_ref())
.then_with(|| a.label.cmp(&b.label))
});
// todo: this is a bit messy, we can refactor for improving maintainability
// The messy code will finally gone, but to help us go over the mess stage, I
// drop some comment here.
//
// currently, there are only path completions in ctx.completions2
// and type/named param/positional param completions in completions
// and all rest less relevant completions inctx.completions
for (i, compl) in ctx.completions2.iter_mut().enumerate() {
compl.sort_text = Some(format!("{i:03}"));
}
let sort_base = ctx.completions2.len();
for (i, compl) in (completions.iter_mut().chain(ctx.completions.iter_mut())).enumerate() {
compl.sort_text = Some(eco_format!("{i:03}", i = i + sort_base));
}
log::debug!(
"sort_and_explicit_code_completion after: {completions:#?} {:#?}",
ctx.completions
);
ctx.completions.append(&mut completions);
log::debug!("sort_and_explicit_code_completion: {:?}", ctx.completions);
match scope {
SurroundingSyntax::Regular => {}