text-rope icon indicating copy to clipboard operation
text-rope copied to clipboard

Add a function to extract specific line from TextLines?

Open jhrcek opened this issue 1 year ago • 7 comments

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 r and then index to the resulting list (I assume this is inefficient - linear time in number of lines?)
  • Use Rope.splitAtLine twice (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?

jhrcek avatar Feb 28 '24 06:02 jhrcek

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.

Bodigrim avatar Feb 29 '24 20:02 Bodigrim

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 avatar Mar 01 '24 11:03 jhrcek

@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.

Bodigrim avatar Apr 28 '24 15:04 Bodigrim

Thanks, will look into it in the next few days.

jhrcek avatar Apr 30 '24 11:04 jhrcek

Is there still an interest to work on getTextAtRange? Or shall I get HEAD released as is?

Bodigrim avatar Jul 16 '24 22:07 Bodigrim

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.

jhrcek avatar Jul 17 '24 03:07 jhrcek

Thanks, I released https://hackage.haskell.org/package/text-rope-0.3

Bodigrim avatar Jul 17 '24 18:07 Bodigrim