Skip to content

Skip a preprocessor line's text when looking for verbatim strings [patch] - #195

Open
matt-edmondson wants to merge 1 commit into
mainfrom
fix/directive-line-verbatim-194
Open

matt-edmondson wants to merge 1 commit into
mainfrom
fix/directive-line-verbatim-194

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Fixes #194

Problem

The compiler does not tokenize the text after #region, #endregion, #error, #warning or #pragma, but VerbatimStringScanner did. An @" (or $@" / @$") in that free text opened a "verbatim string" that never closed inside the fragment. SplitLines then stopped splitting, so every later line of the member kept its own buffer's indentation, and an extra blank line appeared before the class's }.

Change

ScanCode now tracks whether only whitespace has been seen since the last line break. At the top level, a line whose first non-whitespace character is # is skipped to its line break, the way SkipLineComment does, without recording a comment start. The line break is left to the code around it. The check never applies:

The new logic sits in two small helpers (TrySkipNonCode, TrySkipPreprocessorLine), so ScanCode stays under Sonar's S3776 limit. The local Sonar run (.sonarlint/sonar-local.props) reports nothing new.

Tests

  • TemplateTests.RegionNameWithAVerbatimPrefixKeepsTheMemberIndented: the issue's ClassTemplate/RegionScope repro, which now gives the expected output.
  • WriteLinesTests.IndentsEveryLineAfterAVerbatimPrefixInADirective: WriteLines("#region Handles @\" prefixes\nint a;\nint b;\n#endregion\n") indents all four lines.
  • SplitLinesTests data rows for #region, an indented #error with $@", #warning with @$", and #pragma with a comment. Further rows check that a verbatim literal spanning #-led lines stays whole and that a mid-line # starts nothing.
  • With the fix reverted, 5 fail. With it applied, the full suite passes (338/338).

Generated by Claude Code

…tch]

The compiler does not tokenize the text after #region, #error, #warning or
#pragma, but VerbatimStringScanner did: an @" in a region name opened a
verbatim string that never closed, so SplitLines stopped splitting and every
later line in the member lost its indent.

At the top level, a line whose first non-whitespace character is # is now
skipped to its line break, without recording a comment start. A # inside a
verbatim literal, inside an interpolation hole, or mid-line starts nothing.

Fixes #194

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_017vfJW677dmfMN4vzuGiiZH
@sonarqubecloud

sonarqubecloud Bot commented Nov 8, 2026

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

An @" in a #region/#error/#warning line is read as an unclosed verbatim string, so every line after it loses its indent

2 participants