Indentation is gone when pasting from VSCode

When I copy some code from the Visual Studio Code and paste it into Writer colors are preserved but all indents are gone. When I paste as “Unformatted text” it preserves the indentation but the highlighting is gone. When I copy/paste Python code from PyCharm it preserves both indents and highlighting. Paragraph style remains “Default style”. Any suggestion?

OS Arch Linux KDE Plasma
VSC 1.47.0
LO Version: 6.3.6.2 Build ID: 6.3.6-2 CPU threads: 12; OS: Linux 5.7; UI render: GL; VCL: kde5; Locale: en-US
en_US.UTF-8); UI-Language: en-US Calc: threaded
Standard KDE Plasma clipboard manager


code from VS Code


VS Code unformatted


code from PyCharm

Pasted_from_VSC.odt

Pasted_from_VSC_Unformatted.odt

Pasted_from_PyCharm.odt

Unformatted text: every formatting information is removed before pasting; you only paste a sequence of bare characters. If indentation is preserved, it means it is done either with spaces or tab characters (is your code Python?). Colours are formatting data, therefore they are eliminated.

What you paste “as usual”, what do you paste exactly? I mean how is formatted coded so that Writer seems to recognise it. What is the paragraph style of the result?

For a better and complete analysis, edit your question to attach a sample file. Don’t use an answer which is reserved for solutions.

Your screenshots are useless because 1) you didn’t even enable View>Formatted Marks, 2) I can’t analyse what’s sent to Writer. Attrach (with the paper clip tool), three sample files corresponding to the three screenshots.

Remark: it is usually not good to have too large a font size in code examples because Writer wraps long lines, which could visually change the meaning of the code. This looks like Python and you’re lucky that the wrapped line with 2==0): is "protected by ()`, otherwise your citation would be faulty code-wise (Writer doesn’t care for that). You’d be advised to lower the font size to 12pt or even 10pt. Otherwise, format your line to be syntactically correct according to the example language.

In the unformatted case, have you selected the text or already applied some banground highlighting?

Thank you for your help. Please bear with me, it’s the first time I’m asking for help here. I’ve just sent three files as attachments. In the unformatted case I didn’t select the text. I’ve noticed that sometimes it is pasted without any background color and sometimes it adds some color, like the black in this case, or sometimes blue. It is somewhat unpredictable but this bothers me less than the main issue of stripping all indentation. I use dark theme in VS Code.

I’ve just sent three files as attachments

How? I don’t see them.

To attach sample file, reopen your question with edit. Scroll to the end. Type double Return and use the paperclip tool. Repeat for the other files. The double Return will neatly separate the download links.

Just tried with VS Code on Windows 10 with a development version of LO: Imgur: The magic of the Internet. It kept all the indentation correctly…

So many unknowns here: OS? Clipboard manager? Version of VS Code? Version of LO?..

And ultimately, we would need the actual bytes from the clipboard - I have no idea how to get those on systems other than Windows (where I’d use InsideClipboard).

FTR: tried also with LO 6.1, both with spaces and tabs used for indentation. It works here - so either OS, clipboard manager, or VS Code issue. Unlikely to be related to LO version.

My VS Code is 1.47.1.

I have updated the question with all information mentioned. I believe this is not only me because there is a (now closed) thread on Reddit exactly about this issue link text

I works perfectly when I try to paste in other apps like Thunderbird or GoogleDocs. However, when I paste into MS 365 Office (OneDrive) the result is the same as with LO Writer - indentation is gone.

BTW, while we are on this, why Writer has background highlighting only in the part with text while GoogleDocs keeps background highlighting for the whole block? GoogleDocsPaste - Album on Imgur

In Writer, there are two “decorations”: highlighting and background.

Highlighting will paint only the rectangle occupied by the glyph (the character) as defined by the font metrics. It will not paint any spacing added by the formatting (or styling) such as space above or below the paragraph and the line spacing between lines (which is under user control).

When you define a paragraph background, the rectangle taken in consideration is the largest rectangle containing paragraph text and the user-added spacings above and below this text. Consequently, the full width of lines and line spacing is also covered (painted) with background colour.

Both “decorations” are controlled independently.

Hi,

I also had the problem of loosing indentation both after copy on
LibreOffice & VSCode under Ubuntu
&& OpenOffice with VSCode under Windows 8

I managed to write an attached macro to copy & paste coloured text with indentation from VSCode,

The macro can be assigned to a keyboard shortcut, for example CTRL+D

Here is the basic idea of the macro:

  • the macro accesses the clipboard for both versions of text: formatted HTML with no indentation and raw text with indentation
  • the HTML text is pasted into the document at first from the clipboard, and cursor is moved back
  • the raw text is read line by line, into an array of 1000 elements, what is also the maximum number of lines copied by default, ( what can be eventually modified )
  • the rest of the code runs in a loop, up to the total number of lines:
    • get the n-th line of raw text
    • get it’s leading spaces
    • print out the spaces in the document
    • move the cursor to the line below

Spaces as indentation setting was enabled in VSCode, rather than tabs & Prettier addon enabled

It was working in Libre Office under Ubuntu, so I share it


indentation_macro_code.odt (173.6 KB)