Pretty printing
GavinWraith (26) 1563 posts |
What exactly is pretty printing? Most programming languages offer the programmer considerable leeway in how the text of a program is laid out. White space usually has no semantic content outside comments and strings, for example. Each programmer may have her own preferences on how bracketing is laid out. This holds for markup too, when it comes to making HTML source more readable. This means that if text editors are to provide an automatic pretty printing facility they must be able to parse source code, at least to the extent of being able to recognize what is comment and what is a string. StrongED already has some of this built-in. Am I right in thinking that pretty printing some source text is only a matter of adding or subtracting white space or newlines (outside comments and strings) according to appropriate rules: e.g. elide sequences of blank spaces to a single blank space, ensure brackets are sandwiched by blank spaces, … etc? This is a very low priority topic, but I thought it might have some relevance to smartening up the desktop appearance. |
Rick Murray (539) 13840 posts |
Rearranging text so that it looks “nicer”, which usually means getting things to line up and/or wrapping for screen width. There’s OS_PrettyPrint, for example. ;)
Certainly. But quite a few programmers write squished up code like it was 1984. Whitespace generally (<cough>Python</cough>) has no meaning to a compiler, but it can have considerable meaning to a human.
And would rather die by their own sword than tolerate their code being messed with in that way. A sensible reformatter will either determine the style in use, or offer a selection of styles, or simply not attempt to move braces unless absolutely necessary (like, an entire function is on one line).
That’s basic pretty printing in terms of code, yes. One may want to perform a few substitutions as well, such as replacing tabs with spaces, etc. It’s also known as code beautification. There’s a wiki page: https://en.wikipedia.org/wiki/Prettyprint |
GavinWraith (26) 1563 posts |
Thanks. I had forgotten that there is syntax colouring to think of too. |
Steffen Huber (91) 1953 posts |
My first encounter with the phrase “pretty printing” was a magazine type-in that just boldened the BASIC keywords when printing out Locomotive BASIC (guess the home computer :-)) programs. I think adding/removing/converting whitespace, putting curly braces into the right position, relayouting long lines into multiple lines etc. is mostly “code formatting” nowadays. So maybe “pretty printing” is “code formatting plus syntax colouring”? |
Charles Ferguson (8243) 427 posts |
I’m not at all sure what you’re saying that you want here. Yes, source code reformatting tools are useful. They’ve been around from the 70s, as programs became more able to sacrifice compactness for readability and maintainability. Tools exist for doing reformatting on many different languages – C and C++ are covered by many tools, such as the And many others.
Depends on what you’re actually wanting. If you’re wanting to make the code more readable and not change its meaning, then you might be adding and removing whitespace. But on the other hand, you might change the case of the text to make things clearer where that isn’t important. You might reformat numbers to be more clear – some languages allow Remember that in some languages spaces are important – python is one in which it matters. BBC BASIC is another. HTML and XML matter too, but to different extents. Like Python, YAML is heavily dependant on spacing. And then there’s languages like Fortran which (in its earliest versions) which are column based and the column you write in determines the type of content that is represented. Ask yourself what you’re wanting and why you’re wanting it 2. If you’re trying to make things more readable, then maybe whitespace is useful. On the other hand, whitespace in the wrong places can obscure the intent, just as much as wrongly named variables or poorly chosen algorithms make for less clear code. On the other hand, if you’re trying to enforce a single correct representation that cannot be disputed, then strong rules are important – more so than the act of making things unclear, so whitespace may be the least of the issues, and you may want to check/replace variable naming or look for certain types of constructs that are unhelpful or forbidden. At which point you’re getting beyond stylistic elements and into program analysis and linting. Tools for reformatting content have existed for years… decades even. I’m not at all sure why you say that it has relevance to the desktop appearance though; changing the layout of source code doesn’t affect how the desktop appears – if it does, you’ve got bigger problems. So what are you actually asking for? 1 Is 2 Not least because I don’t think you’ve said anything about what you actually want in the post. |
Rick Murray (539) 13840 posts |
OS_ReadUnsigned wouldn’t like that. ;) |
GavinWraith (26) 1563 posts |
My purpose was to elicit just such wide-ranging replies. So many thanks. And you are right – it has no relevance to the desktop. What was at the back of my mind, though, was that for most languages an xml represention of a program’s parse-tree is going to be a convenient intermediate stage between the text and its visual representation. So a docx-like application, configurable by css files, would be the way to go for a general-purpose formatting tool. |