14

VS2019 複製貼上彩色程式碼

 2 years ago
source link: https://blog.darkthread.net/blog/copy-syntax-hilight-code-vs2019/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

VS2019 複製貼上彩色程式碼

calendar.svg 2021-06-03 08:24 PM comment.svg 2 eye.svg 1,459

從 Visual Studio 複製程式碼貼到 Word/Outlook/PowerPoint 時保留顏色標示是很常見的需求。很久以前 VS2010 剛推出時複製貼上中文有個 Bug,我還寫了土砲工具解決呢。(謎之聲:可以不要一直想當年嗎?) 之後它一直是 VS 的內建功能,但到 VS2017 時似乎被拿掉,所幸我找到 Microsoft DevLabs 寫的 Copy As HTML 套件上場救援 (Copy As HTML 也含在 Productivity Power Tools 2017/2019 裡),使用起來倒也沒啥問題。

但從某個 VS2019 版本起,複製程式碼時常會導致 Visual Studio 凍結沒反應,爬文發現我不孤單,停用它複製功能才恢復正常。

忍受貼黑白程式碼或改從 VSCode 複製好一陣子,今天決心找出解法。爬文發現 Visual Studio 有個很少人討論的選項:(我查到最的討論在 2019,說不定一開始就存在)

啟用 「Copy rich text on copy/cut」後,VS2019 就能複製貼上彩色程式碼了,至於下方的「Use accurate classifition」選項,官方文件沒提供 Advanced 類設定的說明,我只在這篇 Copy and paste C++ source into Word loses formatting 討論找到微軟 RD 的回覆:

Language services typically provide 2 kinds of colorization – quick syntactic colorization and rich but slower semantic colorization (e.g. user types). This way a newly opened file is already colorized based on syntax, but once semantic information becomes available, richer colorization appears. So when this checkbox is checked, Copy/Cut commands would copy accurate, final colors even if calculating those would take more time (a wait dialog will be shown after 2s).
The document for the Tools/Options/Text Editor/Advanced page are in progress....

大意是 Language Service 提供快速及完整兩種語意色彩標示,勾選「Use accurate classifition」會等待完整色彩標示 OK 再複製,會多等個兩秒。(另外也有提到 Advanced 部文件來不及寫的事)

不過 VS2019 複製貼上的程式碼一律採用白色背景(不管 IDE 是否設定深色主題),適合 Word/Outlook;若想沿用深色背景樣式放在深色底投影片上,可考慮改用 VSCode,或用 Copy As HTML 套件加自訂樣式[參考]。

and has 2 comments

Comments

Post a comment

Comment
Name Captcha 49 - 3 =

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK