textual icon indicating copy to clipboard operation
textual copied to clipboard

Support zero-indexed line numbers in TextArea

Open royatt opened this issue 10 months ago • 10 comments

Possible alternatives:

  • Add this as a keyword argument - IMO this use-case will not be common enough to deserve it's own typed keyword argument being there until the end of time
  • Make the show_line_numbers kw accept an int (with -1 signifying False) - won't add another kw but will terribly hurt readability
  • Split up the source code in such a way it won't require overriding the entire render_line method to do this in one's own project - not worth it, will probably end up being very invasive, while this approach is fairly elegant

Something I still consider adding:

typing this class variable as : Literal[0, 1] to increase clarity of what it's use case really is

royatt avatar May 02 '24 22:05 royatt

I don't really understand the benefit of this - why would you want to have the line numbers be zero indexed?

darrenburns avatar May 03 '24 18:05 darrenburns

Chiming in with a perspective: I can't personally see any benefit in zero-based line numbers, as a specific feature, but I can see benefits to being able to set the origin number of the line numbers. For example, I might want to create a breakout editor where I'm working on a specific few lines of code, and I want to maintain the line numbers; another reason might be I want to do some sort of narrowing effect.

So perhaps if such a feature were added in some way, it would make sense that it's a way of setting the number of the "first visible line"?

davep avatar May 03 '24 18:05 davep

@davep Yeah, I can see that being useful. Rich Syntax offers something similar IIRC.

darrenburns avatar May 03 '24 18:05 darrenburns

I don't really understand the benefit of this - why would you want to have the line numbers be zero indexed?

The use case I was going for is I have a sort of a query language embedded into a TextArea that you can submit and then it shows you results (kinda like that cool SQL TUI project named harlequin).

I also have this cool feature where when the language grammar is parsed; if you have a syntax error you get a toast with the error the parser created.

The problem is, the parser shows line number based errors which are zero-index based :(

I did some googling (for this problem in text editors/ programming in general) and found some more use cases, so I'm not totally alone on this it seems (albeit, yes - This might not be a very intuitive thing to add)

royatt avatar May 04 '24 08:05 royatt

Chiming in with a perspective: I can't personally see any benefit in zero-based line numbers, as a specific feature, but I can see benefits to being able to set the origin number of the line numbers. For example, I might want to create a breakout editor where I'm working on a specific few lines of code, and I want to maintain the line numbers; another reason might be I want to do some sort of narrowing effect.

So perhaps if such a feature were added in some way, it would make sense that it's a way of setting the number of the "first visible line"?

Hmm, this can be fairly easily transformed into an instance attribute with no further cost if you think it's better

royatt avatar May 04 '24 08:05 royatt

I think this is probably best done as a reactive on the widget itself. Possibly also exposed in the constructor.

Suggest we call it line_number_start

willmcgugan avatar May 04 '24 15:05 willmcgugan

The use case I could possibly imagine:

My program is popping up a text area to modify one little section of a large annoyingly formatted data file. For example, "change some parameters for the machine Gandalf, but don't let anything else change". The relevant section would be loaded into the buffer, the buffer would be edited in a TextArea, and, after some validation, the annoying file is updated. In that case, setting the starting number of a text area would be helpful.

merriam avatar May 27 '24 23:05 merriam

@royatt I think this is a good idea, but its probably best done as an argument on the constructor. Would you like to make that change?

willmcgugan avatar Jun 17 '24 15:06 willmcgugan

@royatt I think this is a good idea, but its probably best done as an argument on the constructor. Would you like to make that change?

Hey, it would take me a couple of days to get to it but yes I'd like to do it

royatt avatar Jun 17 '24 15:06 royatt

@royatt I think this is a good idea, but its probably best done as an argument on the constructor. Would you like to make that change?

Just pushed an amended commit where line_number_start is a reactive keyword argument

royatt avatar Jun 22 '24 19:06 royatt

I added a snapshot test and updated the docs.

Works great:

https://github.com/Textualize/textual/assets/5740731/cd0298bf-b3cf-4d15-abfa-b9332ff1cd0e

darrenburns avatar Jul 11 '24 11:07 darrenburns