mirror of
https://github.com/typst/typst
synced 2025-08-18 00:48:34 +08:00
Compare commits
3 Commits
5ecc4369c4
...
e98011a62a
Author | SHA1 | Date | |
---|---|---|---|
|
e98011a62a | ||
|
0264534928 | ||
|
39b3d94bee |
@ -85,6 +85,10 @@ impl Eval for ast::ModuleImport<'_> {
|
||||
source_span, "module name would not be a valid identifier";
|
||||
hint: "you can rename the import with `as`",
|
||||
),
|
||||
Err(BareImportError::Keyword(name)) => bail!(
|
||||
source_span, "module `{name}` would be overridden as a keyword";
|
||||
hint: "you can rename the import with `as`",
|
||||
),
|
||||
// Bad package spec would have failed the import already.
|
||||
Err(BareImportError::PackageInvalid) => unreachable!(),
|
||||
}
|
||||
|
@ -130,7 +130,14 @@ fn complete_markup(ctx: &mut CompletionContext) -> bool {
|
||||
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 {
|
||||
ctx.from = ctx.leaf.offset() + 1;
|
||||
ctx.label_completions();
|
||||
@ -1644,6 +1651,19 @@ mod tests {
|
||||
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]
|
||||
fn test_with_doc(
|
||||
world: impl WorldLike,
|
||||
@ -1709,15 +1729,24 @@ mod tests {
|
||||
.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 mut world = TestWorld::new("x<test> y<test>");
|
||||
let doc = typst::compile(&world).output.ok();
|
||||
|
||||
let end = world.main.text().len();
|
||||
world.main.edit(end..end, " @t");
|
||||
|
||||
let result = test_with_doc(&world, -1, doc.as_ref());
|
||||
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();
|
||||
|
@ -86,7 +86,7 @@ use ecow::EcoString;
|
||||
use unscanny::Scanner;
|
||||
|
||||
use crate::package::PackageSpec;
|
||||
use crate::{is_ident, is_newline, Span, SyntaxKind, SyntaxNode};
|
||||
use crate::{is_ident, is_keyword, is_newline, Span, SyntaxKind, SyntaxNode};
|
||||
|
||||
/// A typed AST node.
|
||||
pub trait AstNode<'a>: Sized {
|
||||
@ -2225,6 +2225,10 @@ impl<'a> ModuleImport<'a> {
|
||||
return Err(BareImportError::PathInvalid);
|
||||
}
|
||||
|
||||
if is_keyword(&name) {
|
||||
return Err(BareImportError::Keyword(name));
|
||||
}
|
||||
|
||||
Ok(name)
|
||||
}
|
||||
_ => Err(BareImportError::Dynamic),
|
||||
@ -2242,13 +2246,15 @@ impl<'a> ModuleImport<'a> {
|
||||
}
|
||||
|
||||
/// Reasons why a bare name cannot be determined for an import source.
|
||||
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
|
||||
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
|
||||
pub enum BareImportError {
|
||||
/// There is no statically resolvable binding name.
|
||||
Dynamic,
|
||||
/// The import source is not a valid path or the path stem not a valid
|
||||
/// identifier.
|
||||
PathInvalid,
|
||||
/// The import source is a valid path but the path stem is a keyword.
|
||||
Keyword(EcoString),
|
||||
/// The import source is not a valid package spec.
|
||||
PackageInvalid,
|
||||
}
|
||||
|
@ -1063,6 +1063,12 @@ pub fn is_ident(string: &str) -> bool {
|
||||
.is_some_and(|c| is_id_start(c) && chars.all(is_id_continue))
|
||||
}
|
||||
|
||||
/// Whether a string is a Typst keyword.
|
||||
#[inline]
|
||||
pub fn is_keyword(string: &str) -> bool {
|
||||
keyword(string).is_some()
|
||||
}
|
||||
|
||||
/// Whether a character can start an identifier.
|
||||
#[inline]
|
||||
pub fn is_id_start(c: char) -> bool {
|
||||
|
@ -20,8 +20,8 @@ pub use self::file::FileId;
|
||||
pub use self::highlight::{highlight, highlight_html, Tag};
|
||||
pub use self::kind::SyntaxKind;
|
||||
pub use self::lexer::{
|
||||
is_id_continue, is_id_start, is_ident, is_newline, is_valid_label_literal_id,
|
||||
link_prefix, split_newlines,
|
||||
is_id_continue, is_id_start, is_ident, is_keyword, is_newline,
|
||||
is_valid_label_literal_id, link_prefix, split_newlines,
|
||||
};
|
||||
pub use self::lines::Lines;
|
||||
pub use self::node::{LinkedChildren, LinkedNode, Side, SyntaxError, SyntaxNode};
|
||||
|
@ -483,3 +483,8 @@ This is never reached.
|
||||
--- import-from-file-package-lookalike ---
|
||||
// Error: 9-28 file not found (searched at tests/suite/scripting/#test/mypkg:1.0.0)
|
||||
#import "#test/mypkg:1.0.0": *
|
||||
|
||||
--- import-keyword ---
|
||||
// Error: 9-26 module `set` would be overridden as a keyword
|
||||
// Hint: 9-26 you can rename the import with `as`
|
||||
#import "modules/set.typ"
|
||||
|
1
tests/suite/scripting/modules/set.typ
Normal file
1
tests/suite/scripting/modules/set.typ
Normal file
@ -0,0 +1 @@
|
||||
// SKIP
|
Loading…
x
Reference in New Issue
Block a user