MCPcopy Create free account
hub / github.com/CppCXY/EmmyLuaCodeStyle / RangeReformat

Method RangeReformat

CodeFormat/src/LuaFormat.cpp:74–135  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

72}
73
74bool LuaFormat::RangeReformat(const FormatContext &context) {
75 auto text = context.GetInputText();
76 auto file = LuaSource::From(std::move(text));
77 LuaLexer luaLexer(file);
78 if (context.IsNonStandardLua()) {
79 luaLexer.SupportNonStandardSymbol();
80 }
81 if (context.IsCLikeCommentsSupport()) {
82 luaLexer.SupportCLikeComments();
83 }
84
85 luaLexer.Parse();
86
87 LuaParser p(file, std::move(luaLexer.GetTokens()));
88 p.Parse();
89
90 if (p.HasError()) {
91 std::cerr << "Exist Syntax Errors" << std::endl;
92 return false;
93 }
94
95 LuaSyntaxTree t;
96 t.BuildTree(p);
97
98 auto style = context.GetStyle(context.GetInputPath());
99 style.detect_end_of_line = false;
100 style.end_of_line = EndOfLine::LF;
101 FormatRange range;
102
103 auto rangeStr = context.GetFormatRangeString();
104 auto rangeVec = string_util::Split(rangeStr, ":");
105 if (rangeVec.size() != 2) {
106 std::cerr << "Range param format error" << std::endl;
107 return false;
108 }
109 auto start = std::stoull(std::string(rangeVec[0]));
110 auto end = std::stoull(std::string(rangeVec[1]));
111 if (context.IsFormatLine()) {
112 range.StartLine = start;
113 range.EndLine = end;
114 range.StartCol = 0;
115 range.EndCol = 0;
116 } else {
117 range.StartLine = file->GetLine(start);
118 range.EndLine = file->GetLine(end);
119 }
120
121 RangeFormatBuilder f(style, range);
122 auto formattedText = f.GetFormatResult(t);
123 auto replaceRange = f.GetReplaceRange();
124
125 if (context.IsCompleteRangeFormat()) {
126 start = file->GetOffset(replaceRange.StartLine, replaceRange.StartCol);
127 end = file->GetOffset(replaceRange.EndLine + 1, 0);
128 std::string source = std::string(file->GetSource());
129 source.replace(start, end - start, formattedText);
130 formattedText = source;
131 }

Callers 1

mainFunction · 0.80

Calls 15

stringFunction · 0.85
GetInputTextMethod · 0.80
IsNonStandardLuaMethod · 0.80
GetTokensMethod · 0.80
BuildTreeMethod · 0.80
GetStyleMethod · 0.80
GetInputPathMethod · 0.80
GetFormatRangeStringMethod · 0.80
IsFormatLineMethod · 0.80
GetLineMethod · 0.80
GetReplaceRangeMethod · 0.80

Tested by

no test coverage detected