arcade icon indicating copy to clipboard operation
arcade copied to clipboard

Copy button double-spaces highlighted lines in documented code samples

Open alderven opened this issue 2 years ago • 11 comments

Steps to reproduce:

Copy code from following example: Sprite Rotation Around a Tank

Extra lines added to copied text. E.g.

        self.tank.position =\

            self.tank.center_x + x_dir,\

            self.tank.center_y + y_dir

should be:

        self.tank.position =\
            self.tank.center_x + x_dir,\
            self.tank.center_y + y_dir

alderven avatar Feb 01 '23 09:02 alderven

If you type in 'pip freeze' can you confirm you have 'pyglet==2.0.dev23'?

Running 2.6.17 I'm not able to reproduce.

Line 155 is a comment too, make sure the code wasn't accidentally modified.

pvcraven avatar Feb 01 '23 14:02 pvcraven

If you type in 'pip freeze' can you confirm you have 'pyglet==2.0.dev23'?

Yes, that's correct.

Line 155 is a comment too, make sure the code wasn't accidentally modified.

When I press Copy to clipboard button and then paste copied code it added extra empty lines between lines of code. Like this:

        self.tank.position =\

            self.tank.center_x + x_dir,\

            self.tank.center_y + y_dir

I don't see such issue on other examples. Probably something wrong with the code formatting on that specific case.

alderven avatar Feb 01 '23 15:02 alderven

Proably gets mangled in the copy. Safer expression is

        self.tank.position = (
            self.tank.center_x + x_dir,
            self.tank.center_y + y_dir,
        )

einarf avatar Feb 02 '23 03:02 einarf

Could we update the example on dev branch with the following?

  1. texture rotation on load using the new coordinate flip features
  2. pyglet vectors rather than risking errors from position arithmetic the long way

The end result would be more resistant to copy and paste problems and should still be compatible with setting self.tank.position.

pushfoo avatar Feb 02 '23 03:02 pushfoo

Proably gets mangled in the copy

yes, I've updated issue description accordingly

alderven avatar Feb 02 '23 08:02 alderven

@pvcraven @einarf tl;dr using a plugin-created copy button instead of copying a direct selection results in duplicated newlines on all lines which were highlighted inside the code block

I'm running Chromium under Debian, and have verified it on the following pages for 2.5.7, 2.6.0, and latest (2.6.17) on api.arcade.academy:

I suspect it affects all versions, but I can't verify the dev branch because its CSS appears to be broken at the moment: image

Steps to replicate for doc versions hosted on api.arcade.academy:

  1. Select an arcade doc version between 2.5.7 and 2.6.17
  2. Open any page containing at least 1 code box which has both a copy button and highlighted lines
  3. Do not select any text directly
  4. Click the copy button at the top right of the code box
  5. Paste the clipboard contents into an external plain text context
  6. Any lines highlighted in the source will have doubled spaces in the destination

pushfoo avatar Feb 02 '23 13:02 pushfoo

This seems to be a broken interaction between these two sphinx plugins: https://github.com/pythonarcade/arcade/blob/102c27f292bb9e8ca9490848cddeace27c0afa3e/doc/conf.py#L53-L54

The doc for sphinx_copybutton indicates there is a way to use CSS selectors to exclude matching text. I think this is the likeliest solution. Unfortunately, I need to head off for the moment, but I can try to implement this later if nobody else beats me to it.

pushfoo avatar Feb 02 '23 13:02 pushfoo

@alderven Could you please verify the following temporary workarounds?

Only the highlighted section

  1. Select a highlighted text segment manually
  2. Copy via hotkey or right-click context menu
  3. Paste into the destination

The entire code block

  1. Select the first line in the code block manually
  2. If needed, scroll down to the end of the code block (the pgdown key may be helpful)
  3. Hold down shift
  4. Click inside the code box immediately to the right of the end of the last line
  5. The entire code block should now be highlighted
  6. Release shift
  7. Copy via hotkey or right-click context menu
  8. Paste into the destination

pushfoo avatar Feb 02 '23 14:02 pushfoo

@alderven Could you please verify the following temporary workarounds?

thanks @pushfoo both workarounds are working

alderven avatar Feb 02 '23 18:02 alderven

tl;dr:

  1. This is a known issue with sphinx-copybutton
  2. Frontend help would be appreciated
  3. Parsing the highlighted source DOM in JS would be ugly, but it could work

The plugin's exclusion selector argument might not work here. To my understanding, parent selectors will exclude the highlighted line the same way excluding .hll would. Maybe there's a clever way to use psuedo-elements like ::after or a stylesheet tweak to solve this, but I haven't found one yet. Assistance from someone with strong frontend expertise would be appreciated.

Parsing the DOM in JS could work, but it's ugly and seems wasteful even if we cache the result.

pushfoo avatar Feb 09 '23 19:02 pushfoo

@pvcraven @einarf tl;dr I think this fixed in dev / 2.7

I can't replicate this in a clean local venv on the tip of development. 2.6.17 appears to exhibit it, but I haven't been able to narrow down the exact changes which eliminate it with enough certainty for a backport to 2.6.X.

pushfoo avatar Feb 10 '23 01:02 pushfoo

If any of this is still relevant it will be fixed in https://github.com/pythonarcade/arcade/issues/2243

einarf avatar Jul 12 '24 15:07 einarf