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

Different text depending on line break #50

Open
wycats opened this issue Nov 17, 2019 · 13 comments
Open

Different text depending on line break #50

wycats opened this issue Nov 17, 2019 · 13 comments

Comments

@wycats
Copy link

wycats commented Nov 17, 2019

I want to have a document that outputs a key and a value differently depending on whether the value first on one line:

description(string)

vs.

description
  table of
    row1 row2 row3 row4 

In other words, I'd like to change the document in the situation where a line break is deemed necessary. I'd be comfortable with many restrictions, and don't think backtracking is necessary (iow what happens if the new document no longer needs a line break; too bad).

@Marwes
Copy link
Owner

Marwes commented Nov 17, 2019

I believe you want flat_alt

pub fn flat_alt<E>(self, that: E) -> DocBuilder<'a, D, A>
? Just pushed out 0.6 which contains that function (thought I had already released it)

@wycats
Copy link
Author

wycats commented Nov 17, 2019

Thanks! I'll let you know if it works for me.

@wycats
Copy link
Author

wycats commented Nov 18, 2019

Relatedly, I'm struggling a little bit with nest, which seems to require me to nest a document together, and let it possibly nest the second line and forward.

What I frequently want is: nest this whole document underneath this other document. Maybe it's obvious how this is supposed to work and I've just missed it?

@brendanzab
Copy link
Contributor

brendanzab commented Nov 18, 2019

elm-pretty-printer seems to have hang, along with a bunch of other combinators. Not sure if that's what you are after though? I wonder if we could add some of them to pretty.rs? 🤔

@wycats
Copy link
Author

wycats commented Nov 18, 2019

The description of hang seems like what I'm experiencing with nest.

It seems like nest isn't exactly about nesting the document, but rather nesting the lines after the first line, but that frequently feels awkward to me.

@Marwes
Copy link
Owner

Marwes commented Nov 18, 2019

What I frequently want is: nest this whole document underneath this other document. Maybe it's obvious how this is supposed to work and I've just missed it?

Might be that you are doing doc1.append(Doc::space()).append(doc2.nest(..)) instead of doc1.append(Doc::space().append(doc2).nest(..)) ?

@Marwes
Copy link
Owner

Marwes commented Nov 18, 2019

Adding hang would be possible but it requires the Column document primitive https://hackage.haskell.org/package/prettyprinter-1.5.1/docs/Data-Text-Prettyprint-Doc.html#v:column

@wycats
Copy link
Author

wycats commented Nov 18, 2019

@Marwes I'm pretty sure I'm not doing that, but let me whip up some examples that might help identify where my confusion is coming from.

@wycats
Copy link
Author

wycats commented Nov 18, 2019

@Marwes Here's an example: https://github.com/wycats/gitpod-examples/

The output looks like this:

image

What I'm trying to do is nest the child document, but as you can see it nests the second line of the child document. This is sort of fine, except that it requires me to know more about the per-line structure of the output than (1) I want to think about, and (2) pretty.rs wants me to think about

@Marwes
Copy link
Owner

Marwes commented Nov 18, 2019

Yeah, you need the column document. Started on it at #52 . It is going to be a bit awkward since the function in column needs to produce a new document and the layouting only works with references. So the owned document needs to be stored somewhere while the layouting is done.

@wycats
Copy link
Author

wycats commented Nov 20, 2019

For my use-case I'm not too worried about the ownership issues. That sounds perfect.

@Marwes
Copy link
Owner

Marwes commented Nov 20, 2019

The ownership issues are fortunately/unfortunately an implementation detail. You don't have to worry about it but the implementation does!

@Marwes
Copy link
Owner

Marwes commented Dec 1, 2019

Released in 0.7 . Let me know if it works for you!

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

3 participants