lshifr / codeformatter Goto Github PK
View Code? Open in Web Editor NEWA pretty-printer for Mathematica language, working on the box level
License: Other
A pretty-printer for Mathematica language, working on the box level
License: Other
I'd like to use CodeFormatterPrint
to serialize definitions and a canonical but readable way. For this, all the code it produces should, when evaluated, restore the definitions I had in place.
But this does not always work:
ClearAll@f
f[veryveryveryveryveryveryveryverylongname,
veryveryveryveryveryveryveryverylongname] /; a > 0 := a;
CodeFormatterPrint@f
produces
f[
veryveryveryveryveryveryveryverylongname,
veryveryveryveryveryveryveryverylongname
]
/;
(a > 0) :=
a
which cannot be parsed correctly because /;
stands on its own.
/;
should be at the end of the line above such that the expression is not yet complete.
a^b or Power[a, b] throws $Failed. Maybe typesetting boxes (like SuperscriptBox here and FractionBox in scdevries's example) should all be avoided?
f@x and x//f are replaced with f[x], which although equivalent may not what one wants to see
Formatting of fractions in the code can become rather weird:
Join[Total[
Join[1/2 Differences[
VeryLongFunctionNameVeryLongFunctionNameVeryLongFunctionName],
Most[defaults]]]/
Total[Join[1/2 Differences[optimal], Most[optimal]]], a]
CodeFormatterPrint
should IMO be named something like PrettyPrintSymbolDefintions
.
I don't think the names should include CodeFormatter
as they are already in that package. Maybe have prefixes to avoid conflicts.
FullCodeFormat
and consorts should IMO also accept non-box form expressions and do the conversion themselves.
Full documentation (with F1-ability) would also be nice once this matures some more.
https://github.com/lshifr/CodeFormatter/blob/master/README.md#installation
I noticed that: FileNameJoin[$UserBaseDirectory, "Applications"]
is missing { }
which causes errors. Replace with: FileNameJoin[{$UserBaseDirectory, "Applications"}]
Mathematica allows the use of colorsetters as part of the code (so that the programmer can conveniently choose a new color right in the middle of the source by just clicking). A line of code may read like this:
edgeColor = !(*
TagBox[
ColorSetterBox[RGBColor[1., 0., 0.], "Swatch"],
Setting]);
One enters this by DynamicSetting[ColorSetter[]] and executing-in-place (ctrl-shift-enter). Prettifying a line like this yields:
During evaluation of In[164]:= Throw::nocatch: Uncaught Throw[{$Failed,ColorSetterBox[RGBColor[1.,0.,0.],Swatch]},CodeFormatterPrivate
preformat] returned to top level. >>
Out[164]= Hold[
Throw[{$Failed, ColorSetterBox[RGBColor[1., 0., 0.], "Swatch"]},
CodeFormatterPrivate
preformat]]
[I know this is perverse coding]
This may or may not be considered an issue: bitmaps used as part of the source code (which mathematica happily allows and advocates) are replaced with elaborate Image functions.
From the code it seems as if CodeFormatterPrint
uses FullCodeFormat
, but on a given definition their output still differs:
ClearAll[f]
f[x : _a | _b] := 0;
CodeFormatterPrint@f
f[x : (Blank[a] | Blank[b])] :=
0
is not as nice as
boxes = MakeBoxes[f[x : _a | _b] := 0];
prn = CellPrint[Cell[BoxData[#], "Input"]] &;
prn@FullCodeFormat@boxes
f[x : _a | _b] :=
0
FullCodeFormat@MakeBoxes@Table[Piecewise[{
{1.999, -Pi < \[Theta] < -7 Pi/8},
{(2 Sin[-6 Pi/7])/
Sin[\[Theta]], -6 Pi/
7 < \[Theta] < -2.0909003694838066`}, {1.001, \
-2.26998149133592` < \[Theta] < -0.6662394324925154`},
{(2 Sin[-(Pi/10)])/
Sin[\[Theta]], -0.6662394324925154` < \[Theta] < -(Pi/10)},
{1.999, -(Pi/8) < \[Theta] < 0}
}, 1.999]
, {\[Theta], -1. Pi, 0., ds}
];
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.