Coveragepy: Whitespace in every code line

Created on 25 Nov 2020  路  7Comments  路  Source: nedbat/coveragepy

https://github.com/nedbat/coveragepy/blob/3274cbad045d9ee2e67384564d772a019cad0c9e/coverage/htmlfiles/pyfile.html#L75

Every Line ends with a  
That's uncool, if you copy the line to search for it in code.

enhancement html

Most helpful comment

I am certain I put that non-breaking space there for a reason, but I will have to try to remember why. It might be that it's needed so that empty lines look right? Or maybe so that they used to look right but it isn't needed any more?

All 7 comments

I am certain I put that non-breaking space there for a reason, but I will have to try to remember why. It might be that it's needed so that empty lines look right? Or maybe so that they used to look right but it isn't needed any more?

Also if I copy a code block I have a blank line between every line. And the line number is also copied.

Here in GitHub is a good example how I expect it to work.

Yes, we used to have the code and the line numbers in separate cells of a table so that source could be copied. It changed in 5.0: https://github.com/nedbat/coveragepy/commit/bcbdf41c6bb9d4ee30842bd14d45c7d48c2c6d01, I think because the line numbers and lines could get out of synch height-wise.

Do you need a Patch to get it like GitHub?
They use a table.

<tr>
        <td id="L74" class="blob-num js-line-number" data-line-number="74"></td>

        <td id="LC74" class="blob-code blob-code-inner js-file-line">
             <span class="pl-kos">code</span>
        </td>

</tr>

It could be that we don't need different HTML, we just need different CSS so that the line numbers aren't selected.

For example, this fixes both of your issues, but puts all the copied text on one line:

diff --git a/coverage/htmlfiles/pyfile.html b/coverage/htmlfiles/pyfile.html
index ec0f416f..851f27bc 100644
--- a/coverage/htmlfiles/pyfile.html
+++ b/coverage/htmlfiles/pyfile.html
@@ -72,7 +72,7 @@
         {% joined %}
         <p id="t{{line.number}}" class="{{line.css_class}}">
             <span class="n"><a href="#t{{line.number}}">{{line.number}}</a></span>
-            <span class="t">{{line.html}}&nbsp;</span>
+            <span class="t"><span class="s">{{line.html}}</span>&nbsp;</span>
             {% if line.context_list %}
                 <input type="checkbox" id="ctxs{{line.number}}" />
             {% endif %}
diff --git a/coverage/htmlfiles/style.scss b/coverage/htmlfiles/style.scss
index 8169269e..2d1c314a 100644
--- a/coverage/htmlfiles/style.scss
+++ b/coverage/htmlfiles/style.scss
@@ -350,6 +350,7 @@ $border-indicator-width: .2em;
 #source {
     padding: 1em 0 1em $left-gutter;
     font-family: $font-code;
+    user-select: none;

     p {
         // position relative makes position:absolute pop-ups appear in the right place.
@@ -395,6 +396,10 @@ $border-indicator-width: .2em;
             border-left: $border-indicator-width solid $light-bg;
             @include border-color-dark($dark-bg);

+            .s {
+                user-select: text;
+            }
+
             &:hover {
                 background: mix($light-pln-bg, $light-fg, $hover-dark-amt);
                 @include background-dark(mix($dark-pln-bg, $dark-fg, $hover-dark-amt));

I tried some more experiments, using a table as GitHub does, but it's still not quite right. The nedbat/no-select branch has the latest. It works except in Firefox, there's an extra blank line copied when there's a right hand annotation on the line.

Was this page helpful?
0 / 5 - 0 ratings