fix: adjust offset from start when completing labels (#1051)

This commit is contained in:
Myriad-Dreamin 2024-12-22 19:19:01 +08:00 committed by GitHub
parent 362f625949
commit 8a88b50cb1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 135 additions and 28 deletions

View file

@ -0,0 +1,15 @@
/// path: base.typ
#set heading(numbering: "1.1")
#[] <abcd>
#[] <abef>
#[] <efg>
-----
/// contains: abcd, abef, efg
/// compile: base.typ
#<abe /* range -2..-1 */

View file

@ -17,14 +17,14 @@ snapshot_kind: text
},
"sortText": "000",
"textEdit": {
"newText": "<Russell:1908",
"newText": "Russell:1908",
"range": {
"end": {
"character": 16,
"line": 10
},
"start": {
"character": 16,
"character": 13,
"line": 10
}
}
@ -38,14 +38,14 @@ snapshot_kind: text
},
"sortText": "001",
"textEdit": {
"newText": "<Rus",
"newText": "Rus",
"range": {
"end": {
"character": 16,
"line": 10
},
"start": {
"character": 16,
"character": 13,
"line": 10
}
}
@ -59,14 +59,14 @@ snapshot_kind: text
},
"sortText": "003",
"textEdit": {
"newText": "<Russell:1908",
"newText": "Russell:1908",
"range": {
"end": {
"character": 16,
"line": 10
},
"start": {
"character": 16,
"character": 13,
"line": 10
}
}

View file

@ -17,14 +17,14 @@ snapshot_kind: text
},
"sortText": "000",
"textEdit": {
"newText": "<Russell:1908",
"newText": "Russell:1908",
"range": {
"end": {
"character": 31,
"line": 10
},
"start": {
"character": 31,
"character": 28,
"line": 10
}
}
@ -38,14 +38,14 @@ snapshot_kind: text
},
"sortText": "001",
"textEdit": {
"newText": "<Rus",
"newText": "Rus",
"range": {
"end": {
"character": 31,
"line": 10
},
"start": {
"character": 31,
"character": 28,
"line": 10
}
}
@ -59,14 +59,14 @@ snapshot_kind: text
},
"sortText": "003",
"textEdit": {
"newText": "<Russell:1908",
"newText": "Russell:1908",
"range": {
"end": {
"character": 31,
"line": 10
},
"start": {
"character": 31,
"character": 28,
"line": 10
}
}

View file

@ -17,14 +17,14 @@ snapshot_kind: text
},
"sortText": "000",
"textEdit": {
"newText": "<Russell:1908>",
"newText": "Russell:1908>",
"range": {
"end": {
"character": 16,
"line": 3
},
"start": {
"character": 16,
"character": 7,
"line": 3
}
}
@ -38,14 +38,14 @@ snapshot_kind: text
},
"sortText": "001",
"textEdit": {
"newText": "<Russell:1908>",
"newText": "Russell:1908>",
"range": {
"end": {
"character": 16,
"line": 3
},
"start": {
"character": 16,
"character": 7,
"line": 3
}
}

View file

@ -17,14 +17,14 @@ snapshot_kind: text
},
"sortText": "000",
"textEdit": {
"newText": "<harry>",
"newText": "harry>",
"range": {
"end": {
"character": 13,
"line": 3
},
"start": {
"character": 13,
"character": 7,
"line": 3
}
}
@ -38,14 +38,14 @@ snapshot_kind: text
},
"sortText": "001",
"textEdit": {
"newText": "<electronic>",
"newText": "electronic>",
"range": {
"end": {
"character": 13,
"line": 3
},
"start": {
"character": 13,
"character": 7,
"line": 3
}
}
@ -59,14 +59,14 @@ snapshot_kind: text
},
"sortText": "002",
"textEdit": {
"newText": "<electronic>",
"newText": "electronic>",
"range": {
"end": {
"character": 13,
"line": 3
},
"start": {
"character": 13,
"character": 7,
"line": 3
}
}
@ -80,14 +80,14 @@ snapshot_kind: text
},
"sortText": "004",
"textEdit": {
"newText": "<harry>",
"newText": "harry>",
"range": {
"end": {
"character": 13,
"line": 3
},
"start": {
"character": 13,
"character": 7,
"line": 3
}
}

View file

@ -0,0 +1,77 @@
---
source: crates/tinymist-query/src/completion.rs
description: Completion on e (57..58)
expression: "JsonRepr::new_pure(results)"
input_file: crates/tinymist-query/src/fixtures/completion/label_middle.typ
snapshot_kind: text
---
[
{
"isIncomplete": false,
"items": [
{
"kind": 18,
"label": "abcd",
"labelDetails": {
"description": "sequence(..)"
},
"sortText": "000",
"textEdit": {
"newText": "abcd>",
"range": {
"end": {
"character": 4,
"line": 3
},
"start": {
"character": 2,
"line": 3
}
}
}
},
{
"kind": 18,
"label": "abef",
"labelDetails": {
"description": "sequence(..)"
},
"sortText": "001",
"textEdit": {
"newText": "abef>",
"range": {
"end": {
"character": 4,
"line": 3
},
"start": {
"character": 2,
"line": 3
}
}
}
},
{
"kind": 18,
"label": "efg",
"labelDetails": {
"description": "sequence(..)"
},
"sortText": "002",
"textEdit": {
"newText": "efg>",
"range": {
"end": {
"character": 4,
"line": 3
},
"start": {
"character": 2,
"line": 3
}
}
}
}
]
}
]

View file

@ -596,6 +596,16 @@ impl<'a> SyntaxClass<'a> {
| SyntaxClass::Normal(_, node) => node,
}
}
/// Gets the content offset at which the completion should be triggered.
pub fn complete_offset(&self) -> Option<usize> {
match self {
// `<label`
// ^ node.offset() + 1
SyntaxClass::Label { node, .. } => Some(node.offset() + 1),
_ => None,
}
}
}
/// Classifies node's syntax (inner syntax) that can be operated on by IDE

View file

@ -22,7 +22,7 @@ use crate::analysis::{func_signature, BuiltinTy, PathPreference, Ty};
use crate::snippet::{ParsedSnippet, PostfixSnippet, PostfixSnippetScope, DEFAULT_POSTFIX_SNIPPET};
use crate::syntax::{
interpret_mode_at, is_ident_like, previous_decls, surrounding_syntax, InterpretMode,
PreviousDecl, SurroundingSyntax, SyntaxContext, VarClass,
PreviousDecl, SurroundingSyntax, SyntaxClass, SyntaxContext, VarClass,
};
use crate::ty::{
DynTypeBounds, Iface, IfaceChecker, InsTy, SigTy, TyCtx, TypeInfo, TypeInterface, TypeVar,
@ -1478,11 +1478,12 @@ pub(crate) fn complete_type_and_syntax(ctx: &mut CompletionContext) -> Option<()
use crate::syntax::classify_context;
use SurroundingSyntax::*;
let cursor_class = classify_context(ctx.leaf.clone(), Some(ctx.cursor));
crate::log_debug_ct!("complete_type: pos {:?} -> {cursor_class:#?}", ctx.leaf);
let syntax_context = classify_context(ctx.leaf.clone(), Some(ctx.cursor));
let syntax = classify_syntax(ctx.leaf.clone(), ctx.cursor);
crate::log_debug_ct!("complete_type: pos {:?} -> {syntax_context:#?}", ctx.leaf);
let mut args_node = None;
match cursor_class {
match syntax_context {
Some(SyntaxContext::Element { container, .. }) => {
if let Some(container) = container.cast::<ast::Dict>() {
for named in container.items() {
@ -1571,8 +1572,12 @@ pub(crate) fn complete_type_and_syntax(ctx: &mut CompletionContext) -> Option<()
}
// adjust the completion position
// todo: syntax class seems not being considering `is_ident_like`
// todo: merge ident_content_offset and label_content_offset
if is_ident_like(&ctx.leaf) {
ctx.from = ctx.leaf.offset();
} else if let Some(offset) = syntax.as_ref().and_then(SyntaxClass::complete_offset) {
ctx.from = offset;
}
if let Some(ty) = ty {