fix: completion in string context a bit (#351)

* fix: completion in string context a bit

* fix: side effect snapshot
This commit is contained in:
Myriad-Dreamin 2024-06-27 18:30:34 +08:00 committed by GitHub
parent 6967dc5a22
commit ce8baa80b2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 134 additions and 3 deletions

1
Cargo.lock generated
View file

@ -4038,6 +4038,7 @@ dependencies = [
"triomphe",
"ttf-parser",
"typst",
"typst-assets",
"typst-ts-compiler",
"typst-ts-core",
"unscanny",

View file

@ -57,6 +57,7 @@ once_cell.workspace = true
insta.workspace = true
serde.workspace = true
serde_json.workspace = true
typst-assets = { workspace = true, features = ["fonts"] }
typst-ts-core = { workspace = true, default-features = false, features = [
"flat-vector",
"vector-bbox",

View file

@ -0,0 +1,3 @@
// contains: "New Computer Modern"
#set text(font: ""/* range -2..0 */)

View file

@ -0,0 +1,3 @@
// contains: "New Computer Modern"
#set text(font: (""/* range -2..0 */))

View file

@ -0,0 +1,54 @@
---
source: crates/tinymist-query/src/completion.rs
description: "Completion on \"\" (52..54)"
expression: "JsonRepr::new_pure(results)"
input_file: crates/tinymist-query/src/fixtures/completion/modify_string.typ
---
[
{
"isIncomplete": false,
"items": [
{
"kind": 21,
"label": "\"New Computer Modern\"",
"sortText": "003",
"textEdit": {
"newText": "\"New Computer Modern",
"range": {
"end": {
"character": 16,
"line": 2
},
"start": {
"character": 16,
"line": 2
}
}
}
}
]
},
{
"isIncomplete": false,
"items": [
{
"kind": 21,
"label": "\"New Computer Modern\"",
"sortText": "003",
"textEdit": {
"newText": "New Computer Modern",
"range": {
"end": {
"character": 17,
"line": 2
},
"start": {
"character": 16,
"line": 2
}
}
}
}
]
}
]

View file

@ -0,0 +1,54 @@
---
source: crates/tinymist-query/src/completion.rs
description: "Completion on \"\" (53..55)"
expression: "JsonRepr::new_pure(results)"
input_file: crates/tinymist-query/src/fixtures/completion/modify_string_2.typ
---
[
{
"isIncomplete": false,
"items": [
{
"kind": 21,
"label": "\"New Computer Modern\"",
"sortText": "003",
"textEdit": {
"newText": "\"New Computer Modern",
"range": {
"end": {
"character": 17,
"line": 2
},
"start": {
"character": 17,
"line": 2
}
}
}
}
]
},
{
"isIncomplete": false,
"items": [
{
"kind": 21,
"label": "\"New Computer Modern\"",
"sortText": "003",
"textEdit": {
"newText": "New Computer Modern",
"range": {
"end": {
"character": 18,
"line": 2
},
"start": {
"character": 18,
"line": 2
}
}
}
}
]
}
]

View file

@ -1,5 +1,6 @@
use core::fmt;
use std::{
borrow::Cow,
collections::{HashMap, HashSet},
ops::Range,
path::{Path, PathBuf},
@ -105,6 +106,8 @@ pub fn run_with_sources<T>(
};
let mut world = TypstSystemUniverse::new(CompileOpts {
entry: EntryOpts::new_rooted(root.as_path().into(), None),
with_embedded_fonts: typst_assets::fonts().map(Cow::Borrowed).collect(),
no_system_fonts: true,
..Default::default()
})
.unwrap();

View file

@ -1190,9 +1190,21 @@ impl<'a, 'w> CompletionContext<'a, 'w> {
}
} else if at {
apply = Some(eco_format!("at(\"{label}\")"));
} else if label.starts_with('"') && self.after.starts_with('"') {
if let Some(trimmed) = label.strip_suffix('"') {
apply = Some(trimmed.into());
} else {
let apply_label = &mut label.as_str();
if apply_label.ends_with('"') && self.after.starts_with('"') {
if let Some(trimmed) = apply_label.strip_suffix('"') {
*apply_label = trimmed;
}
}
if apply_label.starts_with('"') && self.before.ends_with('"') {
if let Some(trimmed) = apply_label.strip_prefix('"') {
*apply_label = trimmed;
}
}
if apply_label.len() != label.len() {
apply = Some((*apply_label).into());
}
}