Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8862803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:48:59+00:00 2026-06-14T15:48:59+00:00

I’m working on a pretty-printer for Lua, and I’m stuck with handling indentation for

  • 0

I’m working on a pretty-printer for Lua, and I’m stuck with handling indentation for function syntax.

What I want is this: if function definition is short enough, all definition could be written in one line, but if it’s not that short, then all body is needed to be printed in new line and with indentation. After that, end keyword should be placed in a new line, without indentation.

I think I don’t fully understand how Leijen pretty-printer works, but this is what I’ve got so far:

import Text.PrettyPrint.Leijen
import System.IO (stdout)

sometext = text "short text" </> text "this is some long text at least long enough for my purposes"
p = parens (align (cat (punctuate (comma <> space) (map text ["first parameter", "second parameter", "third"]))))

doc3 = indent 4 (sep [text "function" </> p, indent 4 sometext, text "end"])

main :: IO ()
main = do
    displayIO stdout (renderPretty 1 133 (doc3 <$> line))
    displayIO stdout (renderPretty 1 134 (doc3 <$> line))
    displayIO stdout (renderPretty 1 40 (doc3 <$> line))

This actually works pretty well except the second part of the output:

function (first parameter, second parameter, third)
    short text this is some long text at least long enough for my purposes
end

function (first parameter, second parameter, third)     short text this is some long text at least long enough for my purposes end

function (first parameter, 
          second parameter, 
          third)
    short text
    this is some long text at least long enough for my purposes
end

Here I don’t want that 4 spaces indentation, in second output. But I couldn’t find a way to do that without ruining other outputs.

Any help will be appreciated.

EDIT: Here’s another test case:

doc4 = sep [ text "if" </> text "test" </> text "then"
           , indent 4 then_body
           , text "elseif" </> elseif_conditions </> text "then"
           , indent 4 elseif_body
           , text "end"
           ]

What I want from it is, if the output is short enough, print all of it at the same line(but without indentation spaces). It works great when splitting multiple lines, but puts superfluous spaces when prints to same line.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T15:49:00+00:00Added an answer on June 14, 2026 at 3:49 pm

    First let’s make a way of trying out documents at every width starting with a maximum, and only print the ones that are different. This will help check the Doc behaves as you hope.

    import Text.PrettyPrint.Leijen
    import System.IO (stdout)
    import Data.List
    import Data.Function
    
    testFrom :: Int -> Doc -> IO ()
    testFrom w d = mapM_ printone . nubBy ((==) `on` snd) .map (`showAt` d). reverse $ [1..w] where
       showAt n doc = (n,displayS (renderPretty 1 n doc) "")
       printone (n,xs) = putStrLn (show n ++ ":") >> putStrLn xs
    

    What I (now) think you want

    Now let’s take advantage of the extra info in your comments. Sorry I misinterpreted you the first time round.

    I think you mean it’s permissible to all be on one line, but if not, there should be at least three, with the function head on the first, the function body indented and the end unindented. You don’t mind if the sometext is split across lines at the break, comes after the parameters etc, as long as it’s indented by 4 if it’s not inline.

    The nest function allows us to indent subsequent lines if there are any, whereas indent definitely puts spaces, which is why you had the ugly extra 4 lines.

    So, there are three parts to your function:

    functionhead = text "function" </> p
    sometext = text "short text" 
               </> text "this is some long text at least long enough for my purposes"
    end = text "end"
    

    with a slightly refactored way of making parameters:

    params = parens.align.cat.punctuate (comma <> space).map text 
    p = params ["first parameter", "second parameter", "third"]
    

    Lay it out with the breaks specified

    Let’s specify how we’d like them laid out if we need line breaks:

    tidyfunction = nest 4 (functionhead <$> sometext) <$> end
    

    Which demonstrates the way we want the line breaks to go:

    *Main> testFrom 140 tidy
    140:
    function (first parameter, second parameter, third)
        short text this is some long text at least long enough for my purposes
    end
    73:
    function (first parameter, second parameter, third)
        short text
        this is some long text at least long enough for my purposes
    end
    50:
    function (first parameter, 
              second parameter, 
              third)
        short text
        this is some long text at least long enough for my purposes
    end
    26:
    function
        (first parameter, 
         second parameter, 
         third)
        short text
        this is some long text at least long enough for my purposes
    end
    

    Pop it all on one line if it fits

    The pretty printing library has a combinator for getting rid of all line breaks if it fits on one line, group. Let’s indent the whole lot by 4 and group.

    nicefunction = group $ indent 4 tidyfunction
    

    Which gives:

    *Main> testFrom 140 nicefunction
    140:
        function (first parameter, second parameter, third) short text this is some long text at least long enough for my purposes end
    129:
        function (first parameter, second parameter, third)
            short text this is some long text at least long enough for my purposes
        end
    77:
        function (first parameter, second parameter, third)
            short text
            this is some long text at least long enough for my purposes
        end
    54:
        function (first parameter, 
                  second parameter, 
                  third)
            short text
            this is some long text at least long enough for my purposes
        end
    30:
        function
            (first parameter, 
             second parameter, 
             third)
            short text
            this is some long text at least long enough for my purposes
        end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I want to construct a data frame in an Rcpp function, but when I
I'm not entirely sure how I managed to jack this up. http://pretty-senshi.com If you
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I want use html5's new tag to play a wav file (currently only supported

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.