Return a consistently formatted comment from the given `content` string. All comments (except for "##", "#!", "#:", '#'") should have a single space between the hash sign and the content. If `content` didn't start with a hash sign, one is provided. Comments containing fmt directiv
(content: str, mode: Mode)
| 150 | |
| 151 | |
| 152 | def make_comment(content: str, mode: Mode) -> str: |
| 153 | """Return a consistently formatted comment from the given `content` string. |
| 154 | |
| 155 | All comments (except for "##", "#!", "#:", '#'") should have a single |
| 156 | space between the hash sign and the content. |
| 157 | |
| 158 | If `content` didn't start with a hash sign, one is provided. |
| 159 | |
| 160 | Comments containing fmt directives are preserved exactly as-is to respect |
| 161 | user intent (e.g., `#no space # fmt: skip` stays as-is). |
| 162 | """ |
| 163 | content = content.rstrip() |
| 164 | if not content: |
| 165 | return "#" |
| 166 | |
| 167 | # Preserve comments with fmt directives exactly as-is |
| 168 | if content.startswith("#") and contains_fmt_directive(content): |
| 169 | return content |
| 170 | |
| 171 | if content[0] == "#": |
| 172 | content = content[1:] |
| 173 | if ( |
| 174 | content |
| 175 | and content[0] == "\N{NO-BREAK SPACE}" |
| 176 | and not is_type_comment_string("# " + content.lstrip(), mode=mode) |
| 177 | ): |
| 178 | content = " " + content[1:] # Replace NBSP by a simple space |
| 179 | if ( |
| 180 | content |
| 181 | and "\N{NO-BREAK SPACE}" not in content |
| 182 | and is_type_comment_string("#" + content, mode=mode) |
| 183 | ): |
| 184 | type_part, value_part = content.split(":", 1) |
| 185 | content = type_part.strip() + ": " + value_part.strip() |
| 186 | |
| 187 | if content and content[0] not in COMMENT_EXCEPTIONS: |
| 188 | content = " " + content |
| 189 | return "#" + content |
| 190 | |
| 191 | |
| 192 | def normalize_fmt_off( |
no test coverage detected