test: init completion package tests (#672)

This commit is contained in:
Myriad-Dreamin 2024-10-14 13:15:27 +08:00 committed by GitHub
parent 28f2645c40
commit 7bc30bf2fa
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
21 changed files with 535 additions and 9 deletions

View file

@ -308,10 +308,13 @@ mod tests {
use super::*; use super::*;
use crate::{syntax::find_module_level_docs, tests::*}; use crate::{syntax::find_module_level_docs, tests::*};
#[test] struct TestConfig {
fn test() { pkg_mode: bool,
snapshot_testing("completion", &|ctx, path| { }
let source = ctx.source_by_path(&path).unwrap();
fn run(c: TestConfig) -> impl Fn(&mut AnalysisContext, PathBuf) {
fn test(ctx: &mut AnalysisContext, id: TypstFileId) {
let source = ctx.source_by_id(id).unwrap();
let rng = find_test_range(&source); let rng = find_test_range(&source);
let text = source.text()[rng.clone()].to_string(); let text = source.text()[rng.clone()].to_string();
@ -367,7 +370,7 @@ mod tests {
let mut results = vec![]; let mut results = vec![];
for s in rng.clone() { for s in rng.clone() {
let request = CompletionRequest { let request = CompletionRequest {
path: path.clone(), path: ctx.path_for_id(id).unwrap(),
position: ctx.to_lsp_pos(s, &source), position: ctx.to_lsp_pos(s, &source),
explicit: false, explicit: false,
}; };
@ -384,6 +387,30 @@ mod tests {
}, { }, {
assert_snapshot!(JsonRepr::new_pure(results)); assert_snapshot!(JsonRepr::new_pure(results));
}) })
}); }
move |ctx, path| {
if c.pkg_mode {
let files = ctx
.source_files()
.iter()
.filter(|id| !id.vpath().as_rootless_path().ends_with("lib.typ"));
for id in files.copied().collect::<Vec<_>>() {
test(ctx, id);
}
} else {
test(ctx, ctx.file_id_by_path(&path).unwrap());
}
}
}
#[test]
fn test_base() {
snapshot_testing("completion", &run(TestConfig { pkg_mode: false }));
}
#[test]
fn test_pkgs() {
snapshot_testing("completion-pkgs", &run(TestConfig { pkg_mode: true }));
} }
} }

View file

@ -0,0 +1,12 @@
---
source: crates/tinymist-query/src/completion.rs
description: Completion on / (65..66)
expression: "JsonRepr::new_pure(results)"
input_file: crates/tinymist-query/src/fixtures/completion-pkgs/touying-utils-_size-to-pt.typ
---
[
{
"isIncomplete": false,
"items": []
}
]

View file

@ -0,0 +1,12 @@
---
source: crates/tinymist-query/src/completion.rs
description: Completion on / (76..77)
expression: "JsonRepr::new_pure(results)"
input_file: crates/tinymist-query/src/fixtures/completion-pkgs/touying-utils-_size-to-pt.typ
---
[
{
"isIncomplete": false,
"items": []
}
]

View file

@ -0,0 +1,12 @@
---
source: crates/tinymist-query/src/completion.rs
description: Completion on / (70..71)
expression: "JsonRepr::new_pure(results)"
input_file: crates/tinymist-query/src/fixtures/completion-pkgs/touying-utils-_size-to-pt.typ
---
[
{
"isIncomplete": false,
"items": []
}
]

View file

@ -0,0 +1,12 @@
---
source: crates/tinymist-query/src/completion.rs
description: Completion on / (70..71)
expression: "JsonRepr::new_pure(results)"
input_file: crates/tinymist-query/src/fixtures/completion-pkgs/touying-utils-_size-to-pt.typ
---
[
{
"isIncomplete": false,
"items": []
}
]

View file

@ -0,0 +1,12 @@
---
source: crates/tinymist-query/src/completion.rs
description: Completion on / (65..66)
expression: "JsonRepr::new_pure(results)"
input_file: crates/tinymist-query/src/fixtures/completion-pkgs/touying-utils-current-heading.typ
---
[
{
"isIncomplete": false,
"items": []
}
]

View file

@ -0,0 +1,31 @@
---
source: crates/tinymist-query/src/completion.rs
description: Completion on / (76..77)
expression: "JsonRepr::new_pure(results)"
input_file: crates/tinymist-query/src/fixtures/completion-pkgs/touying-utils-current-heading.typ
---
[
{
"isIncomplete": false,
"items": [
{
"kind": 21,
"label": "true",
"sortText": "000",
"textEdit": {
"newText": "true",
"range": {
"end": {
"character": 30,
"line": 2
},
"start": {
"character": 30,
"line": 2
}
}
}
}
]
}
]

View file

@ -0,0 +1,31 @@
---
source: crates/tinymist-query/src/completion.rs
description: Completion on / (66..67)
expression: "JsonRepr::new_pure(results)"
input_file: crates/tinymist-query/src/fixtures/completion-pkgs/touying-utils-current-heading.typ
---
[
{
"isIncomplete": false,
"items": [
{
"kind": 21,
"label": "9999",
"sortText": "000",
"textEdit": {
"newText": "9999",
"range": {
"end": {
"character": 24,
"line": 2
},
"start": {
"character": 24,
"line": 2
}
}
}
}
]
}
]

View file

@ -0,0 +1,67 @@
---
source: crates/tinymist-query/src/completion.rs
description: Completion on / (77..78)
expression: "JsonRepr::new_pure(results)"
input_file: crates/tinymist-query/src/fixtures/completion-pkgs/touying-utils-current-heading.typ
---
[
{
"isIncomplete": false,
"items": [
{
"kind": 5,
"label": "depth",
"sortText": "000",
"textEdit": {
"newText": "depth: ${1:}",
"range": {
"end": {
"character": 17,
"line": 2
},
"start": {
"character": 17,
"line": 2
}
}
}
},
{
"kind": 5,
"label": "hierachical",
"sortText": "001",
"textEdit": {
"newText": "hierachical: ${1:}",
"range": {
"end": {
"character": 17,
"line": 2
},
"start": {
"character": 17,
"line": 2
}
}
}
},
{
"kind": 5,
"label": "level",
"sortText": "002",
"textEdit": {
"newText": "level: ${1:}",
"range": {
"end": {
"character": 17,
"line": 2
},
"start": {
"character": 17,
"line": 2
}
}
}
}
]
}
]

View file

@ -0,0 +1,12 @@
---
source: crates/tinymist-query/src/completion.rs
description: Completion on / (67..68)
expression: "JsonRepr::new_pure(results)"
input_file: crates/tinymist-query/src/fixtures/completion-pkgs/touying-utils-fit-to-height.typ
---
[
{
"isIncomplete": false,
"items": []
}
]

View file

@ -0,0 +1,12 @@
---
source: crates/tinymist-query/src/completion.rs
description: Completion on / (59..60)
expression: "JsonRepr::new_pure(results)"
input_file: crates/tinymist-query/src/fixtures/completion-pkgs/touying-utils-fit-to-height.typ
---
[
{
"isIncomplete": false,
"items": []
}
]

View file

@ -0,0 +1,12 @@
---
source: crates/tinymist-query/src/completion.rs
description: Completion on / (59..60)
expression: "JsonRepr::new_pure(results)"
input_file: crates/tinymist-query/src/fixtures/completion-pkgs/touying-utils-fit-to-height.typ
---
[
{
"isIncomplete": false,
"items": []
}
]

View file

@ -0,0 +1,31 @@
---
source: crates/tinymist-query/src/completion.rs
description: Completion on / (65..66)
expression: "JsonRepr::new_pure(results)"
input_file: crates/tinymist-query/src/fixtures/completion-pkgs/touying-utils-reconstruct.typ
---
[
{
"isIncomplete": false,
"items": [
{
"kind": 21,
"label": "\"body\"",
"sortText": "000",
"textEdit": {
"newText": "\"body\"",
"range": {
"end": {
"character": 24,
"line": 2
},
"start": {
"character": 24,
"line": 2
}
}
}
}
]
}
]

View file

@ -0,0 +1,31 @@
---
source: crates/tinymist-query/src/completion.rs
description: Completion on / (68..69)
expression: "JsonRepr::new_pure(results)"
input_file: crates/tinymist-query/src/fixtures/completion-pkgs/touying-utils-reconstruct.typ
---
[
{
"isIncomplete": false,
"items": [
{
"kind": 21,
"label": "true",
"sortText": "000",
"textEdit": {
"newText": "true",
"range": {
"end": {
"character": 22,
"line": 2
},
"start": {
"character": 22,
"line": 2
}
}
}
}
]
}
]

View file

@ -0,0 +1,31 @@
---
source: crates/tinymist-query/src/completion.rs
description: Completion on / (66..67)
expression: "JsonRepr::new_pure(results)"
input_file: crates/tinymist-query/src/fixtures/completion-pkgs/touying-utils-reconstruct.typ
---
[
{
"isIncomplete": false,
"items": [
{
"kind": 21,
"label": "false",
"sortText": "000",
"textEdit": {
"newText": "false",
"range": {
"end": {
"character": 20,
"line": 2
},
"start": {
"character": 20,
"line": 2
}
}
}
}
]
}
]

View file

@ -0,0 +1,67 @@
---
source: crates/tinymist-query/src/completion.rs
description: Completion on / (87..88)
expression: "JsonRepr::new_pure(results)"
input_file: crates/tinymist-query/src/fixtures/completion-pkgs/touying-utils-reconstruct.typ
---
[
{
"isIncomplete": false,
"items": [
{
"kind": 5,
"label": "body-name",
"sortText": "000",
"textEdit": {
"newText": "body-name: ${1:}",
"range": {
"end": {
"character": 13,
"line": 2
},
"start": {
"character": 13,
"line": 2
}
}
}
},
{
"kind": 5,
"label": "labeled",
"sortText": "001",
"textEdit": {
"newText": "labeled: ${1:}",
"range": {
"end": {
"character": 13,
"line": 2
},
"start": {
"character": 13,
"line": 2
}
}
}
},
{
"kind": 5,
"label": "named",
"sortText": "002",
"textEdit": {
"newText": "named: ${1:}",
"range": {
"end": {
"character": 13,
"line": 2
},
"start": {
"character": 13,
"line": 2
}
}
}
}
]
}
]

View file

@ -0,0 +1,25 @@
// path: lib.typ
// - level (auto, ): The level
#let _size-to-pt(size, container-dimension) = {
let to-convert = size
if type(size) == ratio {
to-convert = container-dimension * size
}
measure(v(to-convert)).height
}
-----
// contains: level, hierachical, depth
#import "lib.typ": *
#current-heading(/* range 0..1 */)[];
-----
// contains: "body"
#import "lib.typ": *
#current-heading(level: /* range 0..1 */)[];
-----
// contains: false, true
#import "lib.typ": *
#current-heading(hierachical: /* range 0..1 */)[];
-----
// contains: false, true
#import "lib.typ": *
#current-heading(depth: /* range 0..1 */)[];

View file

@ -0,0 +1,43 @@
// path: lib.typ
// - level (auto, int): The level
#let current-heading(level: auto, hierachical: true, depth: 9999) = {
let current-page = here().page()
if not hierachical and level != auto {
let headings = query(heading).filter(h => (
h.location().page() <= current-page and h.level <= depth and h.level == level
))
return headings.at(-1, default: none)
}
let headings = query(heading).filter(h => h.location().page() <= current-page and h.level <= depth)
if headings == () {
return
}
if level == auto {
return headings.last()
}
let current-level = headings.last().level
let current-heading = headings.pop()
while headings.len() > 0 and level < current-level {
current-level = headings.last().level
current-heading = headings.pop()
}
if level == current-level {
return current-heading
}
}
-----
// contains: level, hierachical, depth
#import "lib.typ": *
#current-heading(/* range 0..1 */)[];
-----
// contains: "body"
#import "lib.typ": *
#current-heading(level: /* range 0..1 */)[];
-----
// contains: false, true
#import "lib.typ": *
#current-heading(hierachical: /* range 0..1 */)[];
-----
// contains: 9999, 1
#import "lib.typ": *
#current-heading(depth: /* range 0..1 */)[];

View file

@ -0,0 +1,31 @@
// path: lib.typ
// - level (auto, ): The level
#let fit-to-height(
width: none,
prescale-width: none,
grow: true,
shrink: true,
height,
body,
) = {
[
#show before-label: none
#show after-label: none
#v(1em)
hidden#before-label
#v(height)
hidden#after-label
]
}
-----
// contains: 1
#import "lib.typ": *
#fit-to-height(width: /* range 0..1 */)[];
-----
// contains: 1
#import "lib.typ": *
#fit-to-height(prescale-width: /* range 0..1 */)[];
-----
// contains: 1
#import "lib.typ": *
#fit-to-height(height: /* range 0..1 */)[];

View file

@ -0,0 +1,18 @@
// path: lib.typ
#let reconstruct(body-name: "body", labeled: true, named: false, it, ..new-body) = { }
-----
// contains: body-name, labeled, named, it, new-body
#import "lib.typ": *
#reconstruct(/* range 0..1 */)[];
-----
// contains: "body"
#import "lib.typ": *
#reconstruct(body-name: /* range 0..1 */)[];
-----
// contains: false, true
#import "lib.typ": *
#reconstruct(labeled: /* range 0..1 */)[];
-----
// contains: false, true
#import "lib.typ": *
#reconstruct(named: /* range 0..1 */)[];

View file

@ -142,9 +142,6 @@ pub fn run_with_sources<T>(source: &str, f: impl FnOnce(&mut LspUniverse, PathBu
.unwrap(); .unwrap();
let sources = source.split("-----"); let sources = source.split("-----");
let pw = root.join(Path::new("/main.typ"));
world.map_shadow(&pw, Bytes::from_static(b"")).unwrap();
let mut last_pw = None; let mut last_pw = None;
for (i, source) in sources.enumerate() { for (i, source) in sources.enumerate() {
// find prelude // find prelude