Skip to content

Commit

Permalink
fix: complete hash expression in math mode (#1071)
Browse files Browse the repository at this point in the history
* fix: complete hash expression in math mode

* fix: `interpret_mode_at_kind` on hash
  • Loading branch information
Myriad-Dreamin authored Dec 26, 2024
1 parent 98a0e60 commit 1dcb034
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,27 @@ snapshot_kind: text
[
{
"isIncomplete": false,
"items": []
"items": [
{
"kind": 3,
"label": "pagebreak",
"labelDetails": {
"description": "(to: \"even\" | \"odd\" | none, weak: bool) => pagebreak"
},
"textEdit": {
"newText": "pagebreak()${1:}",
"range": {
"end": {
"character": 3,
"line": 2
},
"start": {
"character": 3,
"line": 2
}
}
}
}
]
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,27 @@ snapshot_kind: text
[
{
"isIncomplete": false,
"items": []
"items": [
{
"kind": 3,
"label": "pagebreak",
"labelDetails": {
"description": "(to: \"even\" | \"odd\" | none, weak: bool) => pagebreak"
},
"textEdit": {
"newText": "pagebreak()${1:}",
"range": {
"end": {
"character": 5,
"line": 2
},
"start": {
"character": 3,
"line": 2
}
}
}
}
]
}
]
11 changes: 10 additions & 1 deletion crates/tinymist-query/src/syntax/matcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,14 @@ pub fn interpret_mode_at(mut leaf: Option<&LinkedNode>) -> InterpretMode {
break mode;
}

if !t.kind().is_trivia() && {
// Previous leaf is hash
t.prev_leaf()
.map_or(false, |n| n.kind() == SyntaxKind::Hash)
} {
return InterpretMode::Code;
}

leaf = t.parent();
} else {
break InterpretMode::Markup;
Expand All @@ -294,8 +302,9 @@ pub(crate) fn interpret_mode_at_kind(kind: SyntaxKind) -> Option<InterpretMode>
CodeBlock | Code => InterpretMode::Code,
ContentBlock | Markup => InterpretMode::Markup,
Equation | Math => InterpretMode::Math,
Hash => InterpretMode::Code,
Label | Text | Ident | FieldAccess | Bool | Int | Float | Numeric | Space | Linebreak
| Parbreak | Escape | Shorthand | SmartQuote | RawLang | RawDelim | RawTrimmed | Hash
| Parbreak | Escape | Shorthand | SmartQuote | RawLang | RawDelim | RawTrimmed
| LeftBrace | RightBrace | LeftBracket | RightBracket | LeftParen | RightParen | Comma
| Semicolon | Colon | Star | Underscore | Dollar | Plus | Minus | Slash | Hat | Prime
| Dot | Eq | EqEq | ExclEq | Lt | LtEq | Gt | GtEq | PlusEq | HyphEq | StarEq | SlashEq
Expand Down
8 changes: 8 additions & 0 deletions crates/tinymist-query/src/upstream/complete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,14 @@ fn complete_math(ctx: &mut CompletionContext) -> bool {
if ctx.leaf.kind() == SyntaxKind::Hash {
ctx.from = ctx.cursor;
code_completions(ctx, true);

return true;
}

// Start of an interpolated identifier: "#pa|".
if ctx.leaf.kind() == SyntaxKind::Ident {
ctx.from = ctx.leaf.offset();
code_completions(ctx, true);
return true;
}

Expand Down
9 changes: 2 additions & 7 deletions crates/tinymist-query/src/upstream/complete/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,8 @@ impl CompletionContext<'_> {
docs: Default::default(),
};

let in_math = matches!(
self.leaf.parent_kind(),
Some(SyntaxKind::Equation)
| Some(SyntaxKind::Math)
| Some(SyntaxKind::MathFrac)
| Some(SyntaxKind::MathAttach)
);
let mode = interpret_mode_at(Some(&self.leaf));
let in_math = matches!(mode, InterpretMode::Math);

let lib = self.world().library();
let scope = if in_math { &lib.math } else { &lib.global }
Expand Down

0 comments on commit 1dcb034

Please sign in to comment.