mirror of
https://github.com/typst/typst
synced 2025-08-17 16:38:31 +08:00
Compare commits
4 Commits
82a5cbb0b1
...
9f8a7707a8
Author | SHA1 | Date | |
---|---|---|---|
|
9f8a7707a8 | ||
|
0264534928 | ||
|
70710deb2b | ||
|
ced5091edb |
@ -1,3 +1,5 @@
|
|||||||
|
use std::collections::HashSet;
|
||||||
|
|
||||||
use comemo::Track;
|
use comemo::Track;
|
||||||
use ecow::{eco_vec, EcoString, EcoVec};
|
use ecow::{eco_vec, EcoString, EcoVec};
|
||||||
use typst::foundations::{Label, Styles, Value};
|
use typst::foundations::{Label, Styles, Value};
|
||||||
@ -66,14 +68,22 @@ pub fn analyze_import(world: &dyn IdeWorld, source: &LinkedNode) -> Option<Value
|
|||||||
/// - All labels and descriptions for them, if available
|
/// - All labels and descriptions for them, if available
|
||||||
/// - A split offset: All labels before this offset belong to nodes, all after
|
/// - A split offset: All labels before this offset belong to nodes, all after
|
||||||
/// belong to a bibliography.
|
/// belong to a bibliography.
|
||||||
|
///
|
||||||
|
/// Note: When multiple labels in the document have the same identifier,
|
||||||
|
/// this only returns the first one.
|
||||||
pub fn analyze_labels(
|
pub fn analyze_labels(
|
||||||
document: &PagedDocument,
|
document: &PagedDocument,
|
||||||
) -> (Vec<(Label, Option<EcoString>)>, usize) {
|
) -> (Vec<(Label, Option<EcoString>)>, usize) {
|
||||||
let mut output = vec![];
|
let mut output = vec![];
|
||||||
|
let mut seen_labels = HashSet::new();
|
||||||
|
|
||||||
// Labels in the document.
|
// Labels in the document.
|
||||||
for elem in document.introspector.all() {
|
for elem in document.introspector.all() {
|
||||||
let Some(label) = elem.label() else { continue };
|
let Some(label) = elem.label() else { continue };
|
||||||
|
if !seen_labels.insert(label) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
let details = elem
|
let details = elem
|
||||||
.to_packed::<FigureElem>()
|
.to_packed::<FigureElem>()
|
||||||
.and_then(|figure| match figure.caption.as_option() {
|
.and_then(|figure| match figure.caption.as_option() {
|
||||||
|
@ -76,7 +76,7 @@ pub struct Completion {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// A kind of item that can be completed.
|
/// A kind of item that can be completed.
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||||
#[serde(rename_all = "kebab-case")]
|
#[serde(rename_all = "kebab-case")]
|
||||||
pub enum CompletionKind {
|
pub enum CompletionKind {
|
||||||
/// A syntactical structure.
|
/// A syntactical structure.
|
||||||
@ -130,7 +130,14 @@ fn complete_markup(ctx: &mut CompletionContext) -> bool {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start of a reference: "@|" or "@he|".
|
// Start of a reference: "@|".
|
||||||
|
if ctx.leaf.kind() == SyntaxKind::Text && ctx.before.ends_with("@") {
|
||||||
|
ctx.from = ctx.cursor;
|
||||||
|
ctx.label_completions();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// An existing reference: "@he|".
|
||||||
if ctx.leaf.kind() == SyntaxKind::RefMarker {
|
if ctx.leaf.kind() == SyntaxKind::RefMarker {
|
||||||
ctx.from = ctx.leaf.offset() + 1;
|
ctx.from = ctx.leaf.offset() + 1;
|
||||||
ctx.label_completions();
|
ctx.label_completions();
|
||||||
@ -1564,7 +1571,7 @@ mod tests {
|
|||||||
|
|
||||||
use typst::layout::PagedDocument;
|
use typst::layout::PagedDocument;
|
||||||
|
|
||||||
use super::{autocomplete, Completion};
|
use super::{autocomplete, Completion, CompletionKind};
|
||||||
use crate::tests::{FilePos, TestWorld, WorldLike};
|
use crate::tests::{FilePos, TestWorld, WorldLike};
|
||||||
|
|
||||||
/// Quote a string.
|
/// Quote a string.
|
||||||
@ -1644,6 +1651,19 @@ mod tests {
|
|||||||
test_with_doc(world, pos, doc.as_ref())
|
test_with_doc(world, pos, doc.as_ref())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[track_caller]
|
||||||
|
fn test_with_addition(
|
||||||
|
initial_text: &str,
|
||||||
|
addition: &str,
|
||||||
|
pos: impl FilePos,
|
||||||
|
) -> Response {
|
||||||
|
let mut world = TestWorld::new(initial_text);
|
||||||
|
let doc = typst::compile(&world).output.ok();
|
||||||
|
let end = world.main.text().len();
|
||||||
|
world.main.edit(end..end, addition);
|
||||||
|
test_with_doc(&world, pos, doc.as_ref())
|
||||||
|
}
|
||||||
|
|
||||||
#[track_caller]
|
#[track_caller]
|
||||||
fn test_with_doc(
|
fn test_with_doc(
|
||||||
world: impl WorldLike,
|
world: impl WorldLike,
|
||||||
@ -1709,6 +1729,30 @@ mod tests {
|
|||||||
.must_exclude(["bib"]);
|
.must_exclude(["bib"]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_autocomplete_ref_function() {
|
||||||
|
test_with_addition("x<test>", " #ref(<)", -2).must_include(["test"]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_autocomplete_ref_shorthand() {
|
||||||
|
test_with_addition("x<test>", " @", -1).must_include(["test"]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_autocomplete_ref_shorthand_with_partial_identifier() {
|
||||||
|
test_with_addition("x<test>", " @te", -1).must_include(["test"]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_autocomplete_ref_identical_labels_returns_single_completion() {
|
||||||
|
let result = test_with_addition("x<test> y<test>", " @t", -1);
|
||||||
|
let completions = result.completions();
|
||||||
|
let label_count =
|
||||||
|
completions.iter().filter(|c| c.kind == CompletionKind::Label).count();
|
||||||
|
assert_eq!(label_count, 1);
|
||||||
|
}
|
||||||
|
|
||||||
/// Test what kind of brackets we autocomplete for function calls depending
|
/// Test what kind of brackets we autocomplete for function calls depending
|
||||||
/// on the function and existing parens.
|
/// on the function and existing parens.
|
||||||
#[test]
|
#[test]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user