Add a function to extract specific line from TextLines?
I'm currently looking at implementing some functionality in haskell language server which uses this library.
I would like to implement something like
getTextAtRange :: LSP.Range -> Rope -> Maybe Text
and
getLine :: Word -> TextLines -> Maybe Text
With the current api I can do it like this
Rope.lines rand then index to the resulting list (I assume this is inefficient - linear time in number of lines?)- Use
Rope.splitAtLinetwice (example of this in hls: https://github.com/haskell/haskell-language-server/blob/50923e5c790e9c55c2b9b1bfcc8e78f7169f8ea3/ghcide/src/Development/IDE/Plugin/Completions/Logic.hs#L907-L908) - this seems like more efficient way to achieve that, but not very convenient.
What do you think about adding few high-level helpers like this to the library?
I'd prefer
getLine :: Word -> Rope -> Text
getRange :: Position -> Position -> Rope -> Text
and same for TextLines instead of Rope, but let's wait for #4 to land first.
Great, I'm not in a hurry to implement it. Just a question about your proposed types: Wouldn't it make more sense to wrap the return types in Maybe? You know, to distinguish between cases where given Word / Position x Position are "out of bounds"?
getLine 0 "hello" == Just "hello"
getLine 1 "hello" == Nothing
@jhrcek #4 has landed, so feel free to take a stab at this issue.
I'd prefer to keep new functions consistent with existing split* functions, which do not return Maybe.
Thanks, will look into it in the next few days.
Is there still an interest to work on getTextAtRange? Or shall I get HEAD released as is?
I think it would be very useful. There are bunch of places in hls which would benefit from this and I believe using ready-made helper from text-rope would improve correctness in many places, which are currently extracting text using char-based indexing. But I won't have time in the near future to implement it, so I think you can release HEAD as is.
Thanks, I released https://hackage.haskell.org/package/text-rope-0.3