- Visual Studio Gallery: Trim Copy
- Copy (no indent)
- Copy (fixed indent)
これの何が嬉しいかというと、Visual Studioで書いたコードをMarkdown等にコピーする場面がありますが、このときに行頭の余分な空白は無駄だし、見映えも悪いしで削除したりします。まず名前空間から始まって、クラス、メソッド、メソッド中のブロックとインデントが深くなるにつれ、その一部を切り出したいときにインデント削除が地味に面倒だったりします。標準のMarkdownだとコードブロック指定は4スペース空けですが、これに合わせて調整するのも同じく。
そこは一括置換するなり色々工夫されていると思いますが、そもそもVisual Studioからコピーした時点で処理されてればそういう手間も要らないよね、というのがこの拡張機能です。拡張機能ならではの特長として、自分で選択範囲を行頭や行末にきっちり合わせなくても、自動的に開始行の行頭から終了行の行末までに選択し直してからコピーするので、変にずれたり切れたりすることがありません。
個人的にVisual Studioのコード補完や整形の効いた快適な環境でコードを書いた後の、MarkdownやStackOverflow用の整形が割と億劫だったのですが、これを使えば一発ですし、後から一部修正しても全部コピーし直せばいいので楽です。
具体例を挙げると、以下は拡張機能からユーザーのタブサイズ設定を取得するクラスです。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Microsoft.VisualStudio.Settings; | |
using Microsoft.VisualStudio.Shell.Settings; | |
namespace TrimCopy.Models | |
{ | |
internal class UserSettings | |
{ | |
private const string TabSizeName = "Tab Size"; | |
public static int? GetTabSize(IServiceProvider serviceProvider, string languageName) | |
{ | |
return GetValueFromTextEditor(serviceProvider, languageName, TabSizeName); | |
} | |
private static int? GetValueFromTextEditor(IServiceProvider serviceProvider, string languageName, string propertyName) | |
{ | |
if (serviceProvider == null) return null; | |
if (string.IsNullOrWhiteSpace(languageName)) return null; | |
SettingsManager settingsManager = new ShellSettingsManager(serviceProvider); | |
var userSettingsStore = settingsManager.GetReadOnlySettingsStore(SettingsScope.UserSettings); | |
var textEditorLanguage = $@"Text Editor\{languageName}"; | |
if (userSettingsStore.CollectionExists(textEditorLanguage) && | |
userSettingsStore.PropertyExists(textEditorLanguage, propertyName)) | |
{ | |
return userSettingsStore.GetInt32(textEditorLanguage, propertyName); | |
} | |
return null; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static int? GetValueFromTextEditor(IServiceProvider serviceProvider, string languageName, string propertyName) | |
{ | |
if (serviceProvider == null) return null; | |
if (string.IsNullOrWhiteSpace(languageName)) return null; | |
SettingsManager settingsManager = new ShellSettingsManager(serviceProvider); | |
var userSettingsStore = settingsManager.GetReadOnlySettingsStore(SettingsScope.UserSettings); | |
var textEditorLanguage = $@"Text Editor\{languageName}"; | |
if (userSettingsStore.CollectionExists(textEditorLanguage) && | |
userSettingsStore.PropertyExists(textEditorLanguage, propertyName)) | |
{ | |
return userSettingsStore.GetInt32(textEditorLanguage, propertyName); | |
} | |
return null; | |
} |
もしMarkdown等へのコピーでフラストレーションを感じていれば試してみてください。
0 コメント :
コメントを投稿