6

This question is a follow up from Formatting notebook files

In Programmatic formatting for Mathematica code - possible? masterxilo proposed to use <<GeneralUtilities` then GeneralUtilities`PrintDefinitions@f whereby f is some mathematica code to get a pretty-printed, formatted version of ??f.

Leonid Shifrin wrote a very nice software which is aimed to format mathematica code: https://github.com/lshifr/CodeFormatter

Concerning GeneralUtilities Leonid Shifrin wrote to masterxilo:

 "I have to admit that the formatter implemented in GeneralUtilities impressed me a lot ...".

I used Leonid Shifrins code as well tried to use GeneralUtilities as masterxilo proposed with the following one-liner:

Do[x = i; Do[y = j; Do[z = k;, {k, 1, 3}];, {j, 1, 3}];, {i, 1, 3}]

Leonid Shifrins code gives nearly what I expect:

Import["https://raw.githubusercontent.com/lshifr/CodeFormatter/master/\
CodeFormatter.m"]

    Needs["CodeFormatter`"]

    prn = CellPrint[Cell[BoxData[#], "Input"]] &; prn@
     FullCodeFormat@
      CodeFormatterMakeBoxes@
       Do[x = i; Do[y = j; Do[z = k;, {k, 1, 3}];, {j, 1, 3}];, {i, 1, 3}]

Result:
enter image description here

Why does the Null appears?

masterxilos proposal opens up a new notebook with the information: Attributes[Null] := {Protected};:

<< GeneralUtilities` 

GeneralUtilities`PrintDefinitions@
 Do[x = i; Do[y = j; Do[z = k;, {k, 1, 3}];, {j, 1, 3}];, {i, 1, 3}]

enter image description here

What do I wrong?

mrz
  • 11,686
  • 2
  • 25
  • 81
  • 1
    For the last part: Try using f[] := Do[x = i; Do[y = j; Do[z = k;, {k, 1, 3}];, {j, 1, 3}];, {i, 1, 3}] with PrintDefinitionsLocal[f]. – Karsten7 Sep 27 '16 at 11:19
  • @Karsten 7: This is great, I tried it with a relatively long code and the result is nearly perfect. I wonder if there are possibilities to make some adjustments in PrintDefinitionsLocal. – mrz Sep 27 '16 at 13:57
  • 1
    Based on PrintDefinitionsLocal@PrintDefinitionsLocal, there are no options settings to do adjustments. However, you can inspect it a little deeper and figure out that, e.g., Block[{GeneralUtilities`Debugging`PackagePrivate`$PrintDefinitionsBackground = Orange}, PrintDefinitionsLocal[f]] will produce an orange background for the printed cells. – Karsten7 Sep 27 '16 at 14:14
  • When I use ?GeneralUtilities``*I do not seeDebugging` listed. How did you find it? – mrz Sep 27 '16 at 14:39
  • 1
    By clicking through the definitions. This answer contains a short description of PrintDefinitions. – Karsten7 Sep 27 '16 at 14:45
  • Evaluating ?GeneralUtilities`*`* also reveals that parameter. – Karsten7 Sep 27 '16 at 15:27

0 Answers0