MCPcopy Create free account
hub / github.com/Koihik/LuaFormatter / commentAfterNewLine

Method commentAfterNewLine

src/FormatVisitor.cpp:131–206  ·  view source on GitHub ↗

commentAfterNewLine returns comments between a ParseTreeNode and the next Node. This function always expect a line break. NewLineIndent: inc or dec indent after a line break.

Source from the content-addressed store, hash-verified

129// This function always expect a line break.
130// NewLineIndent: inc or dec indent after a line break.
131std::string FormatVisitor::commentAfterNewLine(tree::ParseTree* node, NewLineIndent newLineIndent) {
132 auto* ctx = dynamic_cast<ParserRuleContext*>(node);
133 if (ctx != nullptr && ctx->children.empty()) {
134 if (newLineIndent == INC_INDENT) {
135 incIndent();
136 } else if (newLineIndent == INC_CONTINUATION_INDENT) {
137 incContinuationIndent();
138 } else if (newLineIndent == DEC_INDENT) {
139 decIndent();
140 } else if (newLineIndent == DEC_CONTINUATION_INDENT) {
141 decContinuationIndent();
142 }
143 return "";
144 }
145 int l = lastNodeIndex(node);
146 if (newLineIndent == INC_INDENT) {
147 incIndent();
148 } else if (newLineIndent == INC_CONTINUATION_INDENT) {
149 incContinuationIndent();
150 }
151 std::stringstream ss;
152 bool customNewLine = false;
153 bool lastComment = true;
154 bool lastestNewLine = false;
155 for (unsigned int i = l + 1; i < tokens_.size(); i++) {
156 auto* token = tokens_[i];
157 if (token->getType() == LuaLexer::COMMENT) {
158 if (lastestNewLine) {
159 ss << indent();
160 } else if (lastComment) {
161 ss << " ";
162 }
163 ss << token->getText();
164 lastComment = true;
165 lastestNewLine = false;
166 } else if (token->getType() == LuaLexer::LINE_COMMENT) {
167 if (lastestNewLine) {
168 ss << indent();
169 } else if (lastComment) {
170 ss << " ";
171 }
172 ss << formatLineComment(token);
173 customNewLine = true;
174 lastComment = false;
175 lastestNewLine = true;
176 } else if (token->getType() == LuaLexer::WS) {
177 auto ln = token->getText().find('\n');
178 auto rn = token->getText().rfind('\n');
179 if (ln != std::string::npos) {
180 if (ln != rn && !lastestNewLine) {
181 ss << "\n\n";
182 } else {
183 ss << "\n";
184 }
185 customNewLine = true;
186 lastComment = false;
187 lastestNewLine = true;
188 }

Callers

nothing calls this directly

Calls 2

lastNodeIndexFunction · 0.85
strMethod · 0.80

Tested by

no test coverage detected