mirror of
https://github.com/typst/typst
synced 2025-07-27 22:37:54 +08:00
Compare commits
5 Commits
5438e651e1
...
324223829b
Author | SHA1 | Date | |
---|---|---|---|
|
324223829b | ||
|
b790c6d59c | ||
|
b1c79b50d4 | ||
|
4629ede020 | ||
|
c8be1cdc36 |
@ -173,8 +173,11 @@ typst help
|
|||||||
typst help watch
|
typst help watch
|
||||||
```
|
```
|
||||||
|
|
||||||
If you prefer an integrated IDE-like experience with autocompletion and instant
|
If you prefer an integrated IDE-like experience with autocompletion and instant
|
||||||
preview, you can also check out [Typst's free web app][app].
|
preview, you can also check out our [free web app][app]. Alternatively, there is
|
||||||
|
a community-created language server called
|
||||||
|
[Tinymist](https://myriad-dreamin.github.io/tinymist/) which is integrated into
|
||||||
|
various editor extensions.
|
||||||
|
|
||||||
## Community
|
## Community
|
||||||
The main places where the community gathers are our [Forum][forum] and our
|
The main places where the community gathers are our [Forum][forum] and our
|
||||||
|
@ -732,10 +732,10 @@ fn complete_params(ctx: &mut CompletionContext) -> bool {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parameters: "func(|)", "func(hi|)", "func(12,|)".
|
// Parameters: "func(|)", "func(hi|)", "func(12, |)", "func(12,|)" [explicit mode only]
|
||||||
if_chain! {
|
if_chain! {
|
||||||
if matches!(deciding.kind(), SyntaxKind::LeftParen | SyntaxKind::Comma);
|
if matches!(deciding.kind(), SyntaxKind::LeftParen | SyntaxKind::Comma);
|
||||||
if deciding.kind() != SyntaxKind::Comma || deciding.range().end < ctx.cursor;
|
if deciding.kind() != SyntaxKind::Comma || deciding.range().end < ctx.cursor || ctx.explicit;
|
||||||
then {
|
then {
|
||||||
if let Some(next) = deciding.next_leaf() {
|
if let Some(next) = deciding.next_leaf() {
|
||||||
ctx.from = ctx.cursor.min(next.offset());
|
ctx.from = ctx.cursor.min(next.offset());
|
||||||
@ -915,8 +915,10 @@ fn complete_code(ctx: &mut CompletionContext) -> bool {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// An existing identifier: "{ pa| }".
|
// An existing identifier (which is not the key of a named pair): "{ pa| }".
|
||||||
if ctx.leaf.kind() == SyntaxKind::Ident {
|
if ctx.leaf.kind() == SyntaxKind::Ident
|
||||||
|
&& (ctx.leaf.index() > 0 || ctx.leaf.parent_kind() != Some(SyntaxKind::Named))
|
||||||
|
{
|
||||||
ctx.from = ctx.leaf.offset();
|
ctx.from = ctx.leaf.offset();
|
||||||
code_completions(ctx, false);
|
code_completions(ctx, false);
|
||||||
return true;
|
return true;
|
||||||
@ -930,10 +932,17 @@ fn complete_code(ctx: &mut CompletionContext) -> bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Anywhere: "{ | }".
|
// Anywhere: "{ | }".
|
||||||
// But not within or after an expression.
|
// But not within or after an expression, and not a dictionary key
|
||||||
if ctx.explicit
|
if ctx.explicit
|
||||||
|
&& ctx.leaf.parent_kind() != Some(SyntaxKind::Dict)
|
||||||
&& (ctx.leaf.kind().is_trivia()
|
&& (ctx.leaf.kind().is_trivia()
|
||||||
|| matches!(ctx.leaf.kind(), SyntaxKind::LeftParen | SyntaxKind::LeftBrace))
|
|| matches!(
|
||||||
|
ctx.leaf.kind(),
|
||||||
|
SyntaxKind::LeftParen
|
||||||
|
| SyntaxKind::LeftBrace
|
||||||
|
| SyntaxKind::Comma
|
||||||
|
| SyntaxKind::Colon
|
||||||
|
))
|
||||||
{
|
{
|
||||||
ctx.from = ctx.cursor;
|
ctx.from = ctx.cursor;
|
||||||
code_completions(ctx, false);
|
code_completions(ctx, false);
|
||||||
@ -1586,6 +1595,7 @@ mod tests {
|
|||||||
trait ResponseExt {
|
trait ResponseExt {
|
||||||
fn completions(&self) -> &[Completion];
|
fn completions(&self) -> &[Completion];
|
||||||
fn labels(&self) -> BTreeSet<&str>;
|
fn labels(&self) -> BTreeSet<&str>;
|
||||||
|
fn must_be_empty(&self) -> &Self;
|
||||||
fn must_include<'a>(&self, includes: impl IntoIterator<Item = &'a str>) -> &Self;
|
fn must_include<'a>(&self, includes: impl IntoIterator<Item = &'a str>) -> &Self;
|
||||||
fn must_exclude<'a>(&self, excludes: impl IntoIterator<Item = &'a str>) -> &Self;
|
fn must_exclude<'a>(&self, excludes: impl IntoIterator<Item = &'a str>) -> &Self;
|
||||||
fn must_apply<'a>(&self, label: &str, apply: impl Into<Option<&'a str>>)
|
fn must_apply<'a>(&self, label: &str, apply: impl Into<Option<&'a str>>)
|
||||||
@ -1604,6 +1614,16 @@ mod tests {
|
|||||||
self.completions().iter().map(|c| c.label.as_str()).collect()
|
self.completions().iter().map(|c| c.label.as_str()).collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[track_caller]
|
||||||
|
fn must_be_empty(&self) -> &Self {
|
||||||
|
let labels = self.labels();
|
||||||
|
assert!(
|
||||||
|
labels.is_empty(),
|
||||||
|
"expected no suggestions (got {labels:?} instead)"
|
||||||
|
);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
#[track_caller]
|
#[track_caller]
|
||||||
fn must_include<'a>(&self, includes: impl IntoIterator<Item = &'a str>) -> &Self {
|
fn must_include<'a>(&self, includes: impl IntoIterator<Item = &'a str>) -> &Self {
|
||||||
let labels = self.labels();
|
let labels = self.labels();
|
||||||
@ -1648,7 +1668,15 @@ mod tests {
|
|||||||
let world = world.acquire();
|
let world = world.acquire();
|
||||||
let world = world.borrow();
|
let world = world.borrow();
|
||||||
let doc = typst::compile(world).output.ok();
|
let doc = typst::compile(world).output.ok();
|
||||||
test_with_doc(world, pos, doc.as_ref())
|
test_with_doc(world, pos, doc.as_ref(), true)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[track_caller]
|
||||||
|
fn test_implicit(world: impl WorldLike, pos: impl FilePos) -> Response {
|
||||||
|
let world = world.acquire();
|
||||||
|
let world = world.borrow();
|
||||||
|
let doc = typst::compile(world).output.ok();
|
||||||
|
test_with_doc(world, pos, doc.as_ref(), false)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[track_caller]
|
#[track_caller]
|
||||||
@ -1669,11 +1697,12 @@ mod tests {
|
|||||||
world: impl WorldLike,
|
world: impl WorldLike,
|
||||||
pos: impl FilePos,
|
pos: impl FilePos,
|
||||||
doc: Option<&PagedDocument>,
|
doc: Option<&PagedDocument>,
|
||||||
|
explicit: bool,
|
||||||
) -> Response {
|
) -> Response {
|
||||||
let world = world.acquire();
|
let world = world.acquire();
|
||||||
let world = world.borrow();
|
let world = world.borrow();
|
||||||
let (source, cursor) = pos.resolve(world);
|
let (source, cursor) = pos.resolve(world);
|
||||||
autocomplete(world, doc, &source, cursor, true)
|
autocomplete(world, doc, &source, cursor, explicit)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -1724,7 +1753,7 @@ mod tests {
|
|||||||
let end = world.main.text().len();
|
let end = world.main.text().len();
|
||||||
world.main.edit(end..end, " #cite()");
|
world.main.edit(end..end, " #cite()");
|
||||||
|
|
||||||
test_with_doc(&world, -2, doc.as_ref())
|
test_with_doc(&world, -2, doc.as_ref(), true)
|
||||||
.must_include(["netwok", "glacier-melt", "supplement"])
|
.must_include(["netwok", "glacier-melt", "supplement"])
|
||||||
.must_exclude(["bib"]);
|
.must_exclude(["bib"]);
|
||||||
}
|
}
|
||||||
@ -1879,26 +1908,96 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_autocomplete_fonts() {
|
fn test_autocomplete_fonts() {
|
||||||
test("#text(font:)", -2)
|
test("#text(font:)", -2)
|
||||||
.must_include(["\"Libertinus Serif\"", "\"New Computer Modern Math\""]);
|
.must_include([q!("Libertinus Serif"), q!("New Computer Modern Math")]);
|
||||||
|
|
||||||
test("#show link: set text(font: )", -2)
|
test("#show link: set text(font: )", -2)
|
||||||
.must_include(["\"Libertinus Serif\"", "\"New Computer Modern Math\""]);
|
.must_include([q!("Libertinus Serif"), q!("New Computer Modern Math")]);
|
||||||
|
|
||||||
test("#show math.equation: set text(font: )", -2)
|
test("#show math.equation: set text(font: )", -2)
|
||||||
.must_include(["\"New Computer Modern Math\""])
|
.must_include([q!("New Computer Modern Math")])
|
||||||
.must_exclude(["\"Libertinus Serif\""]);
|
.must_exclude([q!("Libertinus Serif")]);
|
||||||
|
|
||||||
test("#show math.equation: it => { set text(font: )\nit }", -7)
|
test("#show math.equation: it => { set text(font: )\nit }", -7)
|
||||||
.must_include(["\"New Computer Modern Math\""])
|
.must_include([q!("New Computer Modern Math")])
|
||||||
.must_exclude(["\"Libertinus Serif\""]);
|
.must_exclude([q!("Libertinus Serif")]);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_autocomplete_typed_html() {
|
fn test_autocomplete_typed_html() {
|
||||||
test("#html.div(translate: )", -2)
|
test("#html.div(translate: )", -2)
|
||||||
.must_include(["true", "false"])
|
.must_include(["true", "false"])
|
||||||
.must_exclude(["\"yes\"", "\"no\""]);
|
.must_exclude([q!("yes"), q!("no")]);
|
||||||
test("#html.input(value: )", -2).must_include(["float", "string", "red", "blue"]);
|
test("#html.input(value: )", -2).must_include(["float", "string", "red", "blue"]);
|
||||||
test("#html.div(role: )", -2).must_include(["\"alertdialog\""]);
|
test("#html.div(role: )", -2).must_include([q!("alertdialog")]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_autocomplete_in_function_params_after_comma_and_colon() {
|
||||||
|
let document = "#text(size: 12pt, [])";
|
||||||
|
|
||||||
|
// After colon
|
||||||
|
test(document, 11).must_include(["length"]);
|
||||||
|
test_implicit(document, 11).must_include(["length"]);
|
||||||
|
|
||||||
|
test(document, 12).must_include(["length"]);
|
||||||
|
test_implicit(document, 12).must_include(["length"]);
|
||||||
|
|
||||||
|
// After comma
|
||||||
|
test(document, 17).must_include(["font"]);
|
||||||
|
test_implicit(document, 17).must_be_empty();
|
||||||
|
|
||||||
|
test(document, 18).must_include(["font"]);
|
||||||
|
test_implicit(document, 18).must_include(["font"]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_autocomplete_in_list_literal() {
|
||||||
|
let document = "#let val = 0\n#(1, \"one\")";
|
||||||
|
|
||||||
|
// After opening paren
|
||||||
|
test(document, 15).must_include(["color", "val"]);
|
||||||
|
test_implicit(document, 15).must_be_empty();
|
||||||
|
|
||||||
|
// After first element
|
||||||
|
test(document, 16).must_be_empty();
|
||||||
|
test_implicit(document, 16).must_be_empty();
|
||||||
|
|
||||||
|
// After comma
|
||||||
|
test(document, 17).must_include(["color", "val"]);
|
||||||
|
test_implicit(document, 17).must_be_empty();
|
||||||
|
|
||||||
|
test(document, 18).must_include(["color", "val"]);
|
||||||
|
test_implicit(document, 18).must_be_empty();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_autocomplete_in_dict_literal() {
|
||||||
|
let document = "#let first = 0\n#(first: 1, second: one)";
|
||||||
|
|
||||||
|
// After opening paren
|
||||||
|
test(document, 17).must_be_empty();
|
||||||
|
test_implicit(document, 17).must_be_empty();
|
||||||
|
|
||||||
|
// After first key
|
||||||
|
test(document, 22).must_be_empty();
|
||||||
|
test_implicit(document, 22).must_be_empty();
|
||||||
|
|
||||||
|
// After colon
|
||||||
|
test(document, 23).must_include(["align", "first"]);
|
||||||
|
test_implicit(document, 23).must_be_empty();
|
||||||
|
|
||||||
|
test(document, 24).must_include(["align", "first"]);
|
||||||
|
test_implicit(document, 24).must_be_empty();
|
||||||
|
|
||||||
|
// After first value
|
||||||
|
test(document, 25).must_be_empty();
|
||||||
|
test_implicit(document, 25).must_be_empty();
|
||||||
|
|
||||||
|
// After comma
|
||||||
|
test(document, 26).must_be_empty();
|
||||||
|
test_implicit(document, 26).must_be_empty();
|
||||||
|
|
||||||
|
test(document, 27).must_be_empty();
|
||||||
|
test_implicit(document, 27).must_be_empty();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -797,7 +797,9 @@ impl Color {
|
|||||||
components
|
components
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the constructor function for this color's space:
|
/// Returns the constructor function for this color's space.
|
||||||
|
///
|
||||||
|
/// Returns one of:
|
||||||
/// - [`luma`]($color.luma)
|
/// - [`luma`]($color.luma)
|
||||||
/// - [`oklab`]($color.oklab)
|
/// - [`oklab`]($color.oklab)
|
||||||
/// - [`oklch`]($color.oklch)
|
/// - [`oklch`]($color.oklch)
|
||||||
|
@ -242,7 +242,7 @@ fn category_page(resolver: &dyn Resolver, category: Category) -> PageModel {
|
|||||||
items.push(CategoryItem {
|
items.push(CategoryItem {
|
||||||
name: group.name.clone(),
|
name: group.name.clone(),
|
||||||
route: subpage.route.clone(),
|
route: subpage.route.clone(),
|
||||||
oneliner: oneliner(docs).into(),
|
oneliner: oneliner(docs),
|
||||||
code: true,
|
code: true,
|
||||||
});
|
});
|
||||||
children.push(subpage);
|
children.push(subpage);
|
||||||
@ -296,7 +296,7 @@ fn category_page(resolver: &dyn Resolver, category: Category) -> PageModel {
|
|||||||
items.push(CategoryItem {
|
items.push(CategoryItem {
|
||||||
name: name.into(),
|
name: name.into(),
|
||||||
route: subpage.route.clone(),
|
route: subpage.route.clone(),
|
||||||
oneliner: oneliner(func.docs().unwrap_or_default()).into(),
|
oneliner: oneliner(func.docs().unwrap_or_default()),
|
||||||
code: true,
|
code: true,
|
||||||
});
|
});
|
||||||
children.push(subpage);
|
children.push(subpage);
|
||||||
@ -306,7 +306,7 @@ fn category_page(resolver: &dyn Resolver, category: Category) -> PageModel {
|
|||||||
items.push(CategoryItem {
|
items.push(CategoryItem {
|
||||||
name: ty.short_name().into(),
|
name: ty.short_name().into(),
|
||||||
route: subpage.route.clone(),
|
route: subpage.route.clone(),
|
||||||
oneliner: oneliner(ty.docs()).into(),
|
oneliner: oneliner(ty.docs()),
|
||||||
code: true,
|
code: true,
|
||||||
});
|
});
|
||||||
children.push(subpage);
|
children.push(subpage);
|
||||||
@ -637,7 +637,7 @@ fn group_page(
|
|||||||
let item = CategoryItem {
|
let item = CategoryItem {
|
||||||
name: group.name.clone(),
|
name: group.name.clone(),
|
||||||
route: model.route.clone(),
|
route: model.route.clone(),
|
||||||
oneliner: oneliner(&group.details).into(),
|
oneliner: oneliner(&group.details),
|
||||||
code: false,
|
code: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -772,8 +772,24 @@ pub fn urlify(title: &str) -> EcoString {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Extract the first line of documentation.
|
/// Extract the first line of documentation.
|
||||||
fn oneliner(docs: &str) -> &str {
|
fn oneliner(docs: &str) -> EcoString {
|
||||||
docs.lines().next().unwrap_or_default()
|
let paragraph = docs.split("\n\n").next().unwrap_or_default();
|
||||||
|
let mut depth = 0;
|
||||||
|
let mut period = false;
|
||||||
|
let mut end = paragraph.len();
|
||||||
|
for (i, c) in paragraph.char_indices() {
|
||||||
|
match c {
|
||||||
|
'(' | '[' | '{' => depth += 1,
|
||||||
|
')' | ']' | '}' => depth -= 1,
|
||||||
|
'.' if depth == 0 => period = true,
|
||||||
|
c if period && c.is_whitespace() && !docs[..i].ends_with("e.g.") => {
|
||||||
|
end = i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
_ => period = false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
EcoString::from(&docs[..end]).replace("\r\n", " ").replace("\n", " ")
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The order of types in the documentation.
|
/// The order of types in the documentation.
|
||||||
|
@ -86,7 +86,7 @@ pub struct FuncModel {
|
|||||||
pub name: EcoString,
|
pub name: EcoString,
|
||||||
pub title: &'static str,
|
pub title: &'static str,
|
||||||
pub keywords: &'static [&'static str],
|
pub keywords: &'static [&'static str],
|
||||||
pub oneliner: &'static str,
|
pub oneliner: EcoString,
|
||||||
pub element: bool,
|
pub element: bool,
|
||||||
pub contextual: bool,
|
pub contextual: bool,
|
||||||
pub deprecation: Option<&'static str>,
|
pub deprecation: Option<&'static str>,
|
||||||
@ -139,7 +139,7 @@ pub struct TypeModel {
|
|||||||
pub name: &'static str,
|
pub name: &'static str,
|
||||||
pub title: &'static str,
|
pub title: &'static str,
|
||||||
pub keywords: &'static [&'static str],
|
pub keywords: &'static [&'static str],
|
||||||
pub oneliner: &'static str,
|
pub oneliner: EcoString,
|
||||||
pub details: Html,
|
pub details: Html,
|
||||||
pub constructor: Option<FuncModel>,
|
pub constructor: Option<FuncModel>,
|
||||||
pub scope: Vec<FuncModel>,
|
pub scope: Vec<FuncModel>,
|
||||||
|
@ -127,6 +127,10 @@
|
|||||||
checks = self'.checks;
|
checks = self'.checks;
|
||||||
inputsFrom = [ typst ];
|
inputsFrom = [ typst ];
|
||||||
|
|
||||||
|
buildInputs = with pkgs; [
|
||||||
|
rust-analyzer
|
||||||
|
];
|
||||||
|
|
||||||
packages = [
|
packages = [
|
||||||
# A script for quickly running tests.
|
# A script for quickly running tests.
|
||||||
# See https://github.com/typst/typst/blob/main/tests/README.md#making-an-alias
|
# See https://github.com/typst/typst/blob/main/tests/README.md#making-an-alias
|
||||||
|
Loading…
x
Reference in New Issue
Block a user