@@ -10,6 +10,7 @@ use itertools::Itertools;
1010use log:: warn;
1111
1212use ruff_db:: diagnostic:: { Diagnostic , SecondaryCode } ;
13+ use ruff_python_trivia:: PythonWhitespace ;
1314use ruff_python_trivia:: { CommentRanges , Cursor , indentation_at_offset} ;
1415use ruff_source_file:: { LineEnding , LineRanges } ;
1516use ruff_text_size:: { Ranged , TextLen , TextRange , TextSize } ;
@@ -958,6 +959,7 @@ struct NoqaEdit<'a> {
958959 codes : Option < & ' a Codes < ' a > > ,
959960 line_ending : LineEnding ,
960961 reason : Option < & ' a str > ,
962+ blank_line : bool ,
961963}
962964
963965impl NoqaEdit < ' _ > {
@@ -969,7 +971,10 @@ impl NoqaEdit<'_> {
969971 }
970972
971973 fn write ( & self , writer : & mut impl std:: fmt:: Write ) {
972- write ! ( writer, " # noqa: " ) . unwrap ( ) ;
974+ if !self . blank_line {
975+ write ! ( writer, " " ) . unwrap ( ) ;
976+ }
977+ write ! ( writer, "# noqa: " ) . unwrap ( ) ;
973978 match self . codes {
974979 Some ( codes) => {
975980 push_codes (
@@ -1010,11 +1015,13 @@ fn generate_noqa_edit<'a>(
10101015
10111016 let edit_range;
10121017 let codes;
1018+ let blank_line;
10131019
10141020 // Add codes.
10151021 match directive {
10161022 None => {
10171023 let trimmed_line = locator. slice ( line_range) . trim_end ( ) ;
1024+ blank_line = trimmed_line. trim_whitespace_start ( ) . is_empty ( ) ;
10181025 edit_range = TextRange :: new ( TextSize :: of ( trimmed_line) , line_range. len ( ) ) + offset;
10191026 codes = None ;
10201027 }
@@ -1023,6 +1030,7 @@ fn generate_noqa_edit<'a>(
10231030 let trimmed_line = locator
10241031 . slice ( TextRange :: new ( line_range. start ( ) , existing_codes. start ( ) ) )
10251032 . trim_end ( ) ;
1033+ blank_line = false ;
10261034 edit_range = TextRange :: new ( TextSize :: of ( trimmed_line) , line_range. len ( ) ) + offset;
10271035 codes = Some ( existing_codes) ;
10281036 }
@@ -1035,6 +1043,7 @@ fn generate_noqa_edit<'a>(
10351043 codes,
10361044 line_ending,
10371045 reason,
1046+ blank_line,
10381047 } )
10391048}
10401049
0 commit comments