Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
418 changes: 389 additions & 29 deletions indra/alscript/core/alscriptformatter.cpp

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion indra/alscript/core/alscriptformatter.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
// or `else` without braces -- and within a line the spaces are put
// where the language reads best: one around a binary operator, none
// around a unary one or inside brackets, one after a comma, none
// before it. What the author spaced deliberately is left alone: the
// before it, and inside Luau's braces as asked. What the author spaced deliberately is left alone: the
// gap before a trailing comment, a preprocessor line, anything inside
// a string or a comment. Runs of blank lines are shortened and trailing
// whitespace dropped, unless only some lines are asked for, in which
Expand All @@ -59,6 +59,10 @@ class ALScriptFormatter
// Whether the spaces within a line are touched at all, or only
// the indentation.
bool spacing = true;
// Luau: a space inside a table's braces, and a table type's, as
// StyLua writes them -- { 1, 2 }, { any } -- or none, {1, 2}.
// Never inside {}, nor an interpolated string's.
bool braceSpaces = true;
// How many columns a line may take before it is broken, a tab as
// wide as a level: at the commas of the widest bracket on it that
// has any, or of the table or list a call ends with, which stays
Expand Down
327 changes: 274 additions & 53 deletions indra/alscript/lsl/allsltoslua.cpp

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions indra/alscript/lsl/allsltoslua.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ class ALLSLToSLua
// Each place noted as a "-- LSL:" comment over its line, as well as
// in the notes.
bool comments = true;
// A space inside a table's braces, and a table type's, as StyLua
// and the studio's formatter write them by default -- { "a", 1 },
// { any } -- or none: {"a", 1}. Nothing inside, {} either way.
bool braceSpaces = true;
// The script's own comments, each over what it stood over, after it
// where it was after it on its line, or at the end of what held it:
// // as --, and /* */ as --[[ ]]; a rule of stars or slashes as one of
Expand Down
365 changes: 319 additions & 46 deletions indra/alscript/tests/allsltoslua_test.cpp

Large diffs are not rendered by default.

98 changes: 98 additions & 0 deletions indra/alscript/tests/alscriptformatter_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -382,4 +382,102 @@ namespace tut
tabs.width = 99;
ensure("does not", ALScriptFormatter::format(tabbed, tabs).find("\t\tllSay(\n\t\t\t0,\n") != std::string::npos);
}

template<> template<>
void alscriptformatter_object::test<12>()
{
set_test_name("Luau's braces spaced inside as asked, whatever stands next to them, a table type's too; never {} nor an "
"interpolated string's; a type's colon before a bracket spaced as written; LSL's braces as they were");
const std::string in = "local states: {[string]: {[string]:(...any) -> ()}} = {}\n"
"local t = {{1}, {2, 3}, [k] = v, -1, #t}\n"
"local params: { any } = ll.ParseStringKeepNulls(msg, { \"\\n\" }, {})\n"
"print(`a {b} c {{d}}`, `{ {1} }`)\n"
"local f: (number) -> () = obj:method(1)\n";
const std::string want = "local states: { [string]: { [string]:(...any) -> () } } = {}\n"
"local t = { { 1 }, { 2, 3 }, [k] = v, -1, #t }\n"
"local params: { any } = ll.ParseStringKeepNulls(msg, { \"\\n\" }, {})\n"
"print(`a {b} c {{d}}`, `{ {1} }`)\n"
"local f: (number) -> () = obj:method(1)\n";
ensure_equals("StyLua's, by default", lua(in), want);
ALScriptFormatter::Options none;
none.lua = true;
none.braceSpaces = false;
ensure_equals("none", ALScriptFormatter::format(in, none),
std::string("local states: {[string]: {[string]:(...any) -> ()}} = {}\n"
"local t = {{1}, {2, 3}, [k] = v, -1, #t}\n"
"local params: {any} = ll.ParseStringKeepNulls(msg, {\"\\n\"}, {})\n"
"print(`a {b} c {{d}}`, `{ {1} }`)\n"
"local f: (number) -> () = obj:method(1)\n"));
ensure_equals("a type's colon, spaced", lua("local states: {[string]: (...any) -> ()} = {}\n"),
std::string("local states: { [string]: (...any) -> () } = {}\n"));
ALScriptFormatter::Options lsl_none;
lsl_none.braceSpaces = false;
ensure_equals("LSL's braces", ALScriptFormatter::format("default { state_entry() { llSay(0, \"a\"); } }\n", lsl_none),
std::string("default { state_entry() { llSay(0, \"a\"); } }\n"));
}

template<> template<>
void alscriptformatter_object::test<13>()
{
set_test_name("a sign against the bracket or brace after it, and after a < that compares or a minus that subtracts, spaced "
"from it: -(a), #{ 1 }, i < -1, x - #t, in Luau and in LSL; LSL's vector's first sign still against its <");
ensure_equals("Luau", lua("local x = - (a)\nlocal y = # (t)\nlocal z = # {1}\nif i <#t and i <-1 and i > - (2) then end\n"
"local w = not (a)\nlocal q = a - (b)\nreturn -(x)\nlocal d = #{1} -#{2} - -#t + -#t\n"),
std::string("local x = -(a)\nlocal y = #(t)\nlocal z = #{ 1 }\nif i < #t and i < -1 and i > -(2) then end\n"
"local w = not (a)\nlocal q = a - (b)\nreturn -(x)\nlocal d = #{ 1 } - #{ 2 } - -#t + -#t\n"));
ensure_equals("LSL", lsl("default { state_entry() { integer x = - (a); vector v = < -1, 0, 0 >; if (i <-1) x = ! (b); } }\n"),
std::string("default { state_entry() { integer x = -(a); vector v = <-1, 0, 0>; if (i < -1) x = !(b); } }\n"));
ensure_equals("a list of vectors", lsl("list l = [<-1, 0, 0>, < -2, 0, 0 >];\n"), std::string("list l = [<-1, 0, 0>, <-2, 0, 0>];\n"));
}

template<> template<>
void alscriptformatter_object::test<14>()
{
set_test_name("what the author lined up stays lined up: a run's =, at the author's column or past the longest, a comment on a line "
"of its own going on past; a run's trailing comments where its code changed; not equal columns by chance; tabs to "
"their stops");
const std::string lined = "local CHANNEL = 1 -- channel\n"
"local OWNER_ONLY = true -- owner\n"
"-- a note over the next\n"
"local USE_OWNER_SAY = true -- say\n";
ensure_equals("as it was", lua(lined), lined);
ensure_equals("spaced afresh, lined up again", lua("local a =1 -- one\nlocal bbb = {2,3} -- two\n"),
std::string("local a = 1 -- one\nlocal bbb = { 2, 3 } -- two\n"));
ensure_equals("the author's column", lua("local a = 1\nlocal bb = 2\n"), std::string("local a = 1\nlocal bb = 2\n"));
ensure_equals("past the longest", lua("t[i+1] = 1\nt[ii] = 2\n"), std::string("t[i + 1] = 1\nt[ii] = 2\n"));
ensure_equals("not by chance", lua("ab=1\nc = 2\n"), std::string("ab = 1\nc = 2\n"));
ensure_equals("not across a blank line", lua("local a = 1\n\nlocal bb = 2\n"), std::string("local a = 1\n\nlocal bb = 2\n"));
const std::string fields = "local t = {\n alpha = 1,\n b = 2,\n}\n";
ensure_equals("a table's fields", lua(fields), fields);
ensure_equals("LSL, by tabs", lsl("integer a\t= 1;\ninteger bb\t= 2;\n"), std::string("integer a = 1;\ninteger bb = 2;\n"));
// The formatter's own, again: the same.
ensure_equals("again", lua(lua("local a =1 -- one\nlocal bbb = {2,3} -- two\n")),
std::string("local a = 1 -- one\nlocal bbb = { 2, 3 } -- two\n"));
}

template<> template<>
void alscriptformatter_object::test<15>()
{
set_test_name("a comment the author wrote out of the block over an elseif, else, until or end stays as far in as that line; one "
"written in the block stays in it; in an empty block, as written where the text is indented; flat text, in");
const std::string chain = "if a then\n"
" x()\n"
"-- about b\n"
"elseif b then\n"
" y()\n"
" -- the end of y\n"
"else\n"
" z()\n"
"end\n";
ensure_equals("as written", lua(chain), chain);
const std::string repeat = "local function f()\n"
" repeat\n"
" -- why\n"
" until x\n"
"end\n";
ensure_equals("an empty block, indented", lua(repeat), repeat);
ensure_equals("flat", lua("if a then\nx()\n-- note\nend\n"), std::string("if a then\n x()\n -- note\nend\n"));
ensure_equals("LSL", lsl("default\n{\n touch_start(integer n)\n {\n x();\n // about the end\n }\n}\n"),
std::string("default\n{\n touch_start(integer n)\n {\n x();\n // about the end\n }\n}\n"));
}
}
2 changes: 1 addition & 1 deletion indra/newview/alfloaterscriptpreferences.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ namespace
"ALScriptStudioAutoClose",
"ALScriptStudioCaretStyle", "ALScriptStudioCaretBlink", "ALScriptStudioHoverCards", "ALScriptStudioHoverDelay",
"ALScriptStudioVimClipboard",
"ALScriptFormatBlankLines", "ALScriptFormatSpacing", "ALScriptFormatWidth", "ALScriptFormatOnSave", "ALScriptTrimOnSave", "ALScriptFixOnSave",
"ALScriptFormatBlankLines", "ALScriptFormatSpacing", "ALScriptFormatBraceSpaces", "ALScriptFormatWidth", "ALScriptFormatOnSave", "ALScriptTrimOnSave", "ALScriptFixOnSave",
"ALScriptTemplateLSL", "ALScriptTemplateSLua", "ALScriptPreprocDefines", "ExternalEditor",
"ALScriptConvertLLTimers", "ALScriptConvertDetectedTable", "ALScriptConvertSLuaCalls", "ALScriptConvertIdioms",
"ALScriptConvertHandlerFields", "ALScriptConvertTypes", "ALScriptConvertComments", "ALScriptConvertKeepComments",
Expand Down
3 changes: 3 additions & 0 deletions indra/newview/alfloaterscriptstudio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7625,6 +7625,7 @@ void ALFloaterScriptStudio::format(Doc& doc, bool selection_only)
options.tabs = !doc.editor->getSoftTabs();
options.maxBlankLines = llclamp(gSavedSettings.getS32("ALScriptFormatBlankLines"), 0, 10);
options.spacing = gSavedSettings.getBOOL("ALScriptFormatSpacing");
options.braceSpaces = gSavedSettings.getBOOL("ALScriptFormatBraceSpaces");
options.width = llmax(0, gSavedSettings.getS32("ALScriptFormatWidth"));
const std::string text = document.text();
// Line for line, since only some lines may be asked for and the
Expand Down Expand Up @@ -8193,6 +8194,8 @@ void ALFloaterScriptStudio::convertToSLua(Doc& doc)
options.types = gSavedSettings.getBOOL("ALScriptConvertTypes");
options.comments = gSavedSettings.getBOOL("ALScriptConvertComments");
options.keepComments = gSavedSettings.getBOOL("ALScriptConvertKeepComments");
// Its tables' braces as the formatter writes them.
options.braceSpaces = gSavedSettings.getBOOL("ALScriptFormatBraceSpaces");
// Its notes, and its comments, in the skin's words.
options.words = alScriptKeyedWords;
const std::string source = doc.editor->wholeText();
Expand Down
11 changes: 11 additions & 0 deletions indra/newview/app_settings/settings_alchemy.xml
Original file line number Diff line number Diff line change
Expand Up @@ -882,6 +882,17 @@
<key>Value</key>
<integer>1</integer>
</map>
<key>ALScriptFormatBraceSpaces</key>
<map>
<key>Comment</key>
<string>Script Studio writes a space inside SLua's braces, { 1, 2 } and { any }, as StyLua does: its formatter, and Convert to SLua; off, {1, 2}.</string>
<key>Persist</key>
<integer>1</integer>
<key>Type</key>
<string>Boolean</string>
<key>Value</key>
<integer>1</integer>
</map>
<key>ALScriptFormatWidth</key>
<map>
<key>Comment</key>
Expand Down
11 changes: 11 additions & 0 deletions indra/newview/skins/default/xui/en/floater_script_studio_prefs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,17 @@ end</floater.string>
tool_tip="Formatting breaks a line with more columns than this at the commas of a bracket on it, each part on a line of its own; 0 never breaks a line"
top_delta="-2"
width="60" />
<check_box
control_name="ALScriptFormatBraceSpaces"
follows="left|top"
height="20"
label="Spaces inside SLua's braces: { 1, 2 }"
layout="topleft"
left="12"
name="format_brace_spaces"
tool_tip="Formatting and Convert to SLua put a space inside a table's braces, and a table type's, as StyLua does: { 1, 2 }, { any }. Off, {1, 2}"
top_pad="6"
width="370" />

<!-- The right column: the studio, the caret, the hover card. -->
<text
Expand Down
Loading