@@ -121,7 +121,8 @@ void SpaceAnalyzer::Analyze(FormatState &f, const LuaSyntaxTree &t) {
121121 }
122122 case TK_LONG_COMMENT:
123123 case TK_SHORT_COMMENT: {
124- SpaceAround (syntaxNode, t, f.GetStyle ().space_before_inline_comment );
124+ SpaceLeft (syntaxNode, t, f.GetStyle ().space_before_inline_comment , SpacePriority::CommentFirst);
125+ SpaceRight (syntaxNode, t, 1 );
125126 break ;
126127 }
127128 default : {
@@ -149,7 +150,7 @@ void SpaceAnalyzer::ComplexAnalyze(FormatState &f, const LuaSyntaxTree &t) {
149150 if (exprList.IsNode (t)) {
150151 auto commas = exprList.GetChildTokens (' ,' , t);
151152 for (auto &comma: commas) {
152- SpaceIgnore (comma, t );
153+ SpaceIgnore (comma);
153154 }
154155 }
155156 }
@@ -255,7 +256,7 @@ void SpaceAnalyzer::ComplexAnalyze(FormatState &f, const LuaSyntaxTree &t) {
255256 if (f.GetStyle ().ignore_space_after_colon ) {
256257 auto colon = syntaxNode.GetChildToken (' :' , t);
257258 if (colon.IsToken (t)) {
258- SpaceIgnore (colon, t );
259+ SpaceIgnore (colon);
259260 }
260261 }
261262
@@ -329,27 +330,45 @@ void SpaceAnalyzer::ComplexAnalyze(FormatState &f, const LuaSyntaxTree &t) {
329330void SpaceAnalyzer::Query (FormatState &f, LuaSyntaxNode syntaxNode, const LuaSyntaxTree &t, FormatResolve &resolve) {
330331 if (syntaxNode.IsToken (t)) {
331332 auto nextToken = syntaxNode.GetNextToken (t);
332- auto space = ProcessSpace (t, syntaxNode, nextToken);
333+ auto space = ProcessSpace (syntaxNode, nextToken, t );
333334 resolve.SetNextSpace (space);
334335 }
335336}
336337
337- void SpaceAnalyzer::SpaceAround (LuaSyntaxNode & n, const LuaSyntaxTree &t, std::size_t space) {
338- SpaceLeft (n, t, space);
339- SpaceRight (n, t, space);
338+ void SpaceAnalyzer::SpaceAround (LuaSyntaxNode n, const LuaSyntaxTree &t, std::size_t space, SpacePriority priority ) {
339+ SpaceLeft (n, t, space, priority );
340+ SpaceRight (n, t, space, priority );
340341}
341342
342- void SpaceAnalyzer::SpaceLeft (LuaSyntaxNode & n, const LuaSyntaxTree &t, std::size_t space) {
343+ void SpaceAnalyzer::SpaceLeft (LuaSyntaxNode n, const LuaSyntaxTree &t, std::size_t space, SpacePriority priority ) {
343344 auto token = n.GetPrevToken (t);
344- _rightSpaces[token.GetIndex ()] = space;
345+ auto it = _rightSpaces.find (token.GetIndex ());
346+ if (it != _rightSpaces.end ()) {
347+ if (it->second .Priority > priority) {
348+ return ;
349+ }
350+ }
351+
352+ _rightSpaces[token.GetIndex ()] = SpaceData (space, priority);
345353}
346354
347- void SpaceAnalyzer::SpaceRight (LuaSyntaxNode & n, const LuaSyntaxTree &t, std::size_t space) {
355+ void SpaceAnalyzer::SpaceRight (LuaSyntaxNode n, const LuaSyntaxTree &t, std::size_t space, SpacePriority priority ) {
348356 auto token = n.GetLastToken (t);
349- _rightSpaces[token.GetIndex ()] = space;
357+ auto it = _rightSpaces.find (token.GetIndex ());
358+ if (it != _rightSpaces.end ()) {
359+ if (it->second .Priority > priority) {
360+ return ;
361+ }
362+ }
363+
364+ _rightSpaces[token.GetIndex ()] = SpaceData (space, priority);
350365}
351366
352- SpaceAnalyzer::OptionalInt SpaceAnalyzer::GetRightSpace (LuaSyntaxNode &n) const {
367+ void SpaceAnalyzer::SpaceIgnore (LuaSyntaxNode n) {
368+ _ignoreSpace.insert (n.GetIndex ());
369+ }
370+
371+ SpaceAnalyzer::OptionalInt SpaceAnalyzer::GetRightSpace (LuaSyntaxNode n) const {
353372 if (_ignoreSpace.count (n.GetIndex ())) {
354373 return OptionalInt ();
355374 }
@@ -358,11 +377,11 @@ SpaceAnalyzer::OptionalInt SpaceAnalyzer::GetRightSpace(LuaSyntaxNode &n) const
358377 if (it == _rightSpaces.end ()) {
359378 return OptionalInt ();
360379 }
361- return OptionalInt (it->second );
380+ return OptionalInt (it->second . Value );
362381}
363382
364383std::size_t
365- SpaceAnalyzer::ProcessSpace (const LuaSyntaxTree &t , LuaSyntaxNode &left, LuaSyntaxNode &right ) {
384+ SpaceAnalyzer::ProcessSpace (LuaSyntaxNode left , LuaSyntaxNode right, const LuaSyntaxTree &t ) {
366385 auto rightSpaceOfLeftToken = GetRightSpace (left);
367386 if (rightSpaceOfLeftToken.HasValue ) {
368387 return rightSpaceOfLeftToken.Value ;
@@ -372,7 +391,3 @@ SpaceAnalyzer::ProcessSpace(const LuaSyntaxTree &t, LuaSyntaxNode &left, LuaSynt
372391 }
373392 return 0 ;
374393}
375-
376- void SpaceAnalyzer::SpaceIgnore (LuaSyntaxNode &n, const LuaSyntaxTree &t) {
377- _ignoreSpace.insert (n.GetIndex ());
378- }
0 commit comments