Strip trailing whitespace when inserting/removing comments

Trailing whitespace can happen when removing comments from empty lines (with indented comment markers) or when the comment marker itself has whitespace and is inserted on an empty line (e.g. prefixing empty lone with ‘# ’).