jneug / typst-codelst

A typst package to render sourcecode
MIT License
86 stars 5 forks source link

Gobble not working properly #18

Open SkytAsul opened 2 months ago

SkytAsul commented 2 months ago

Hello!

Setting gobble: false as a parameter to sourcecode does not work:

error: expected integer, found boolean
   ┌─ @preview/codelst:2.0.1/codelst.typ:41:24
   │
41 │       line = line.slice(gobble)
   │                         ^^^^^^

help: error occurred in this call of function `codelst-gobble-blanks`
    ┌─ @preview/codelst:2.0.1/codelst.typ:288:15
    │
288 │   code-lines = codelst-gobble-blanks(code-lines, gobble)
    │                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

help: error occurred in this call of function `sourcecode`
    ┌─ @preview/codelst:2.0.1/codelst.typ:316:2
    │
316 │   sourcecode( ..args, raw(code, lang:lang, block:true))
    │   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
jneug commented 2 months ago

false and none should disable gobbling, I guess.

For now, setting gobble: 0 should work.

SkytAsul commented 2 months ago

Thanks. I have another problem: the auto gobble value does not work properly if used alongside showrange.

For instance:

#sourcecode(showrange: (5, 7))[```
    4 spaces
    4 spaces
    4 spaces
    4 spaces
        8 spaces
        8 spaces
        8 spaces
    4 spaces
```]

Will only remove 4 spaces instead of 8.

jneug commented 2 months ago

Good catch. Whitespace is removed before slicing the code to the desired range. This probably should be done the other way around.

SkytAsul commented 2 months ago

I don't understand why. Slicing the code to the range is done at line 152: https://github.com/jneug/typst-codelst/blob/bda9c7a6dad678749846eeeaa643cd19f30ca495/codelst.typ#L152 Whitespace removal is done at line 288: https://github.com/jneug/typst-codelst/blob/bda9c7a6dad678749846eeeaa643cd19f30ca495/codelst.typ#L288 I would like to propose a PR but this does not make sense :sweat_smile:

jneug commented 2 months ago

The package needs to do several steps of pre-processing to the code. The line you reference is needed for parsing labels and acts on the code before syntax highlighting. The actual slicing happens around line 197, after highlighting (to make sure things like comments are highlighted properly, before cutting of lines).

The gobbling acts on the raw text of the code, again before highlighting.

SkytAsul commented 2 months ago

I see. Unfortunately, if the gobbling code is moved inside the show rule, we lose ability to easily trim the string because we end up having to modify content. I'll try to see what I can do and if I end up with something usable, I'll make a PR.

jneug commented 2 months ago

Yes, that's why I implemented it the way it is. You can remove whitespace after highlighting, but it's a bit more messy.

But I think it should be possible to simply remove the whitespace from the complete code (even lines outside the range with less whitespace), because it won't mess with syntax highlighting.

SkytAsul commented 2 months ago

It's a lot messier, I just finished it and it's really ugly. You can see my commit if you want: https://github.com/SkytAsul/typst-codelst/commit/2f86d1e03c6bd6c00f08330fff64be798809ce8f (I could have probably done it a little cleaner, but the main idea is here.)

If what you meant in your above message is to gobble the whitespaces even in lines that contains less whitespaces that the desired gobbling amount, well that's clever but it could cause issue on languages relying on indentation? Idk if the syntax coloring is basing itself on indentation.

jneug commented 2 months ago

True. It would probably not work for e.g. Python.

jneug commented 2 months ago

I'm currently on holidays but will habe a look at your code in the next few days.

SkytAsul commented 2 months ago

No hurry. It works for me so take your time, enjoy your holidays :)