Skip to content

Allow importing with array syntax (like in Python) #3065

Closed

Description

@Andrew15-5

Description

Imagine writing this (ugly and unreadable!):

#import "/utils.typ": thing1, thing2, something-very-long, stuff1, stuff2, stuff3, and-maybe-something-else, ok-this-is-the-last-one

Instead of this (you can't be more readable than this):

#import "/utils.typ": (
  thing1,
  thing2,
  something-very-long,
  stuff1,
  stuff2,
  stuff3,
  and-maybe-something-else,
  ok-this-is-the-last-one,
)

I get that for = long heading it isn't great to be able to place heading's content on multiple lines (becomes less readable, but would be nice for very long heading), but for import statement it has become obvious that sometimes import lines are simply too long. Look even at Rust's imports (use) — maybe something like that also would work (but it's less understandable, than a simple array-syntax imports).

Use Case

Remove clutter on import lines and make huge imports more readable and maintainable.

Activity

  1. added
    feature request
    on Dec 25, 2023
  2. emilyyyylime commented on Dec 25, 2023

    @emilyyyylime
    Contributor

    I expected either \ as a continuation character or just a terminating comma to work as line continuations, but it seems both don't work. +1

  3. ntjess commented on Jan 5, 2024

    @ntjess

    It's not ideal, but this does work in the meantime (tested on v0.10):

    #(import "/utils.typ": 
      thing1,
      thing2,
      something-very-long,
      stuff1,
      stuff2,
      stuff3,
      and-maybe-something-else,
      ok-this-is-the-last-one
    )

    Also using python as a reference, there are times when a large amount of imports mean you should be doing #import utils.typ and calling utils.thing1, utils.thing2, etc. instead.

    Similar to import numpy as np -- you don't use from numpy import ones, zeros, sum, ..., but instead import the module as a shorthand and access things from there.

    Not saying array import is bad, just that sometimes so many imports are a sign you should just be importing the whole module instead

  4. Andrew15-5 commented on Jan 5, 2024

    @Andrew15-5
    ContributorAuthor

    @ntjess, I understand and agree. The main point is to provide an ability/choice to import things differently, in a more convenient and readable way. Sometime things that I import should just be global, because their names aren't shadowing anything else and because its reference will be shorter (i.e., ones instead of np.ones) — shorter is mainly better (fewer chars, less code, more readable, more compact, less file size).

    This is just a simple argument, but I hope I got my point across.

  5. Andrew15-5 commented on Jan 5, 2024

    @Andrew15-5
    ContributorAuthor

    It's not ideal, but this does work in the meantime (tested on v0.10):

    #(import "/utils.typ": 
      thing1,
      thing2,
      something-very-long,
      stuff1,
      stuff2,
      stuff3,
      and-maybe-something-else,
      ok-this-is-the-last-one,
    )

    Surprisingly, you are right, but only 99%. The trailing comma makes an error "unexpected closing paren" (why "paren" though?), so unfortunately you have to sacrifice it in this situation. I thought that parentheses will also create a scope like [] or {}, but today I learned that they do not create any scope (wow!). But that's a great finding/workaround anyway.

    Very simple (working) tablex example

    #(import "@preview/tablex:0.0.7":
      tablex,
      rowspanx,
      colspanx,
      cellx
    )
    
    #tablex(1, 2, 3)

  6. ntjess commented on Jan 5, 2024

    @ntjess

    You're correct; thanks for checking. I edited my answer to remove the trailing comma.

  7. added
    syntax
    on Jan 8, 2024
  8. ntjess commented on Mar 12, 2024

    @ntjess

    @laurmaedje This syntax causes an error in the 0.11 rc -- is this intentional?

    #(import "@preview/tablex:0.0.8":
    tablex,
    cellx
    )

    Compiles in 0.10, panics in 0.11 with unclosed delimiter + unexpected closing paren

  9. laurmaedje commented on Mar 12, 2024

    @laurmaedje
    Member

    This syntax causes an error in the 0.11 rc -- is this intentional?

    No, that's a regression. I'll look into it.

  10. laurmaedje commented on Mar 12, 2024

    @laurmaedje
    Member

    @ntjess I fixed the regression with #3633

  11. added
    good contribution
    on Jul 16, 2024
  12. Adham-A commented on Aug 21, 2024

    @Adham-A

    Hello I would like to work on this issue if this is still availaible as my first contribution to Typst,

    Just a few questions :

    I've looked into the code to understand how it works and as far as my quick search goes :

    I found this :

    https://github.com/typst/typst/blob/main/crates/typst-syntax/src/parser.rs#L789

    So the goal here is to modify a part of the Parser/Lexer or is it something way simpler ?

  13. laurmaedje commented on Aug 21, 2024

    @laurmaedje
    Member

    @Adham-A yep, this would need to be added in the parser, syntax tree, and code evaluation.

  14. Adham-A commented on Aug 21, 2024

    @Adham-A

    @Adham-A yep, this would need to be added in the parser, syntax tree, and code evaluation.

    Thank you I will look into this then, is there some documentation availaible on the inner workings of this to help ?

  15. laurmaedje commented on Aug 21, 2024

    @laurmaedje
    Member

    There is a bit of documentation in an unmerged PR. But beyond that, sadly not much. :/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature requestgood contributionsyntax

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions