Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gobble not working properly #18

Open
SkytAsul opened this issue Jul 16, 2024 · 11 comments
Open

Gobble not working properly #18

SkytAsul opened this issue Jul 16, 2024 · 11 comments

Comments

@SkytAsul
Copy link

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
Copy link
Owner

jneug commented Jul 16, 2024

false and none should disable gobbling, I guess.

For now, setting gobble: 0 should work.

@SkytAsul
Copy link
Author

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
Copy link
Owner

jneug commented Jul 17, 2024

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

@SkytAsul
Copy link
Author

SkytAsul commented Jul 22, 2024

I don't understand why. Slicing the code to the range is done at line 152:

code-lines = code-lines.slice(..showrange)

Whitespace removal is done at line 288:
code-lines = codelst-gobble-blanks(code-lines, gobble)

I would like to propose a PR but this does not make sense 😅

@jneug
Copy link
Owner

jneug commented Jul 22, 2024

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
Copy link
Author

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
Copy link
Owner

jneug commented Jul 22, 2024

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
Copy link
Author

It's a lot messier, I just finished it and it's really ugly. You can see my commit if you want: SkytAsul@2f86d1e (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
Copy link
Owner

jneug commented Jul 22, 2024

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

@jneug
Copy link
Owner

jneug commented Jul 22, 2024

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

@SkytAsul
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants