So I noticed that when copying a file from OS file manager and pasting it into the editor, its name will be pasted. A similar behaviour is solved with the commit correct tests and remove text from copy file
in PR #8494
It is not considered a big issue. But might be preferred to not allow this behaviour to happen.
Spyder: Master (currently under development)
OS: windows: 10
Python version: 3.6
So what should be the expected behavior in this case?
So what should be the expected behavior in this case?
I Think there should be no text in the clipboard when pasting clipboard data which contains none text contents (mimeData) e.g. files/folders. The text should be set to an empty string if the data within the clipboard are mimeData-Urls.
@ccordoba12
By the way. It can be fixed by changing this function as such.
@Slot()
def paste(self):
"""
Reimplement QPlainTextEdit's method to fix the following issue:
on Windows, pasted text has only 'LF' EOL chars even if the original
text has 'CRLF' EOL chars
"""
clipboard = QApplication.clipboard()
if clipboard.mimeData().hasUrls():
clipboard.setText('')
else:
text = to_text_string(clipboard.text())
if len(text.splitlines()) > 1:
eol_chars = self.get_line_separator()
text = eol_chars.join((text + eol_chars).splitlines())
clipboard.setText(text)
# Standard paste
TextEditBaseWidget.paste(self)
self.document_did_change(text)
I tested it, This change does not affect pasting text data (within Spyder editor itself or from outside Spyder) but prevent pasting file/folders names if they are copied in the clipboard from outside Spyder.
Ok, I agree with this, except for one thing: instead of using clipboard.setText('')
we should simply change that line for return
. That will avoid resetting user's clipboard and pasting its contents in our editor.
What do you think?
Ideal solution.
I tested your solution, it works perfect
It is also standard in all text editors that when clipboard contains file/folders, their names wont be pasted when doing Cntl+V
Great! Would you mind to open a pull request about it?
Glade to do that. Thanks a lot..
@ccordoba12 I consider coping and pasting file path from the file browser to be incredibly handy. I do this all the time. On Linux I get a nice ready to use file path.
If the windows file paths are unusable in a python script with the file:\\\
stuff in the front then the fix to the this issue is to make the url paste as a usable python path string like "D:\\path\\file.ext"
.
@bcolsen Good point, I am not a linux user, therefore, I do not understand how exactly file system in linux works when copying from clipboard. I can address your concerns in a new PR shortly.
I know that this behaviour does not exist in any editor I am aware of, even simple ones like notepad. windows own products, excel, word, PP does not allow pasting files naming when these files are copied into the clipboard. The same is true in VScode, Pycharm, etc.
File path in windows can be copied as shown in the figure below, instead of hacking them in a script!
The Path copied to clipboard very nicely! with double quotes
"C:\Users\kalho\Desktop\spyder\spyder\bootstrap.py"
By the way. when the PR #8494 is accepted and merged, it would be easier to copy file path than browsing OS file manager.
Thanks for your feedback.
@bcolsen @ccordoba12
The solution below works better than the previously provided solution. I am sorry about previous solution! It still prevents pasting file names with prefix file:\\\
when these files/folders are copied into the clipboard which is the standard in most text editors in windows.
@bcolsen
Would you mind testing it? I do Have only WSL without linux file browser to test how it works.
@Slot()
def paste(self):
"""
Reimplement QPlainTextEdit's method to fix the following issue:
on Windows, pasted text has only 'LF' EOL chars even if the original
text has 'CRLF' EOL chars
"""
clipboard = QApplication.clipboard()
if clipboard.mimeData().hasFormat("text/plain"):
text = to_text_string(clipboard.text())
if len(text.splitlines()) > 1:
eol_chars = self.get_line_separator()
text = eol_chars.join((text + eol_chars).splitlines())
clipboard.setText(text)
# Standard paste
TextEditBaseWidget.paste(self)
self.document_did_change(text)
Another solution might be to prevent this behaviour in windows only.
@Slot()
def paste(self):
"""
Reimplement QPlainTextEdit's method to fix the following issue:
on Windows, pasted text has only 'LF' EOL chars even if the original
text has 'CRLF' EOL chars
"""
clipboard = QApplication.clipboard()
if osp.os.name == 'nt':
if clipboard.mimeData().hasUrls():
return
text = to_text_string(clipboard.text())
if len(text.splitlines()) > 1:
eol_chars = self.get_line_separator()
text = eol_chars.join((text + eol_chars).splitlines())
clipboard.setText(text)
# Standard paste
TextEditBaseWidget.paste(self)
self.document_did_change(text)
I would like to know also what @CAM-Gerlach suggests??
Okay I just tested the first option in my previous comment in linux after installing a file browser and indeed it works. you still can paste file paths when copying them from a file manager (I tested using Nautilus) I believe other file managers in linux works similarly. The method prevents pasting file names only if copied properly from (windows file manager >> home>>copy path)..
Thanks @bcolsen very much for your feedback.
Most helpful comment
Great! Would you mind to open a pull request about it?