Skip to content

Commit

Permalink
Fixup slightly-too-eager warnings about extra content before or after…
Browse files Browse the repository at this point in the history
… statements
  • Loading branch information
desplesda committed Dec 4, 2024
1 parent b115863 commit 4483a8f
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions YarnSpinner.Compiler/Visitors/StyleWarningsVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,37 +28,40 @@ public override int VisitStatement([NotNull] YarnSpinnerParser.StatementContext
// first token on the channel for that line.
if (context.Start.Type == YarnSpinnerLexer.COMMAND_START)
{
tokensOnLine ??= GetAllTokensOnLine(tokenStream, context.Start.Line);
tokensOnLine = GetAllTokensOnLine(tokenStream, context.Start.Line);

var indexOfStartToken = tokensOnLine.IndexOf(context.Start);

if (indexOfStartToken != 0)
{
AddDiagnostic(new Diagnostic(this.FileName, tokensOnLine[indexOfStartToken - 1], "Commands should start on a new line", Diagnostic.DiagnosticSeverity.Warning));
AddDiagnostic(new Diagnostic(this.FileName, tokensOnLine[indexOfStartToken - 1], $"Commands should start on a new line", Diagnostic.DiagnosticSeverity.Warning));
}
}

// If the last token is a COMMAND_END, then it should be the last
// token on the channel for that line.
if (context.Stop.Type == YarnSpinnerLexer.COMMAND_END)
{
tokensOnLine ??= GetAllTokensOnLine(tokenStream, context.Stop.Line);
tokensOnLine = GetAllTokensOnLine(tokenStream, context.Stop.Line);

var indexOfStartToken = tokensOnLine.IndexOf(context.Stop);
var indexOfStopToken = tokensOnLine.IndexOf(context.Stop);

if (indexOfStartToken != (tokensOnLine.Count - 1))
if (indexOfStopToken != (tokensOnLine.Count - 1))
{
AddDiagnostic(new Diagnostic(this.FileName, tokensOnLine[indexOfStartToken + 1], "Commands should have no text after them", Diagnostic.DiagnosticSeverity.Warning));
AddDiagnostic(new Diagnostic(this.FileName, tokensOnLine[indexOfStopToken + 1], $"Commands should have no text after them", Diagnostic.DiagnosticSeverity.Warning));
}
}

return base.VisitStatement(context);
}

private static int[] OmitTokenTypes = new[] { YarnSpinnerLexer.INDENT, YarnSpinnerLexer.DEDENT };

public static List<IToken> GetAllTokensOnLine(CommonTokenStream tokenStream, int line, int channel = Lexer.DefaultTokenChannel)
{

return tokenStream.GetTokens()
.Where(t => t.Channel == channel && t.Line == line)
.Where(t => t.Channel == channel && t.Line == line && OmitTokenTypes.Contains(t.Type) == false)
.ToList();
}
}
Expand Down

0 comments on commit 4483a8f

Please sign in to comment.