diff --git a/README.md b/README.md index bd1a194..c494121 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,7 @@ -# convert-line-ending +# TextTools +[![Build status](https://ci.appveyor.com/api/projects/status/75w05nwsi00o7tv2/branch/vs2019?svg=true)](https://github.com/avplayer/TextTools) + + Visual Studio 实用编码格式转换插件 (Visual Studio extension for text editing) - 支持删除行尾空格 (Rmove trailing whitespace) diff --git a/TextTools/TextTools.csproj b/TextTools/TextTools.csproj index 77dc7c8..d21fe29 100644 --- a/TextTools/TextTools.csproj +++ b/TextTools/TextTools.csproj @@ -218,9 +218,6 @@ ..\packages\Nerdbank.Streams.2.4.60\lib\netstandard2.0\Nerdbank.Streams.dll - - ..\packages\Newtonsoft.Json.12.0.3\lib\net45\Newtonsoft.Json.dll - diff --git a/TextTools/TextTools/ClassificationTypes.cs b/TextTools/TextTools/ClassificationTypes.cs index 1aba68c..81fe577 100644 --- a/TextTools/TextTools/ClassificationTypes.cs +++ b/TextTools/TextTools/ClassificationTypes.cs @@ -20,10 +20,18 @@ static class TrailingClassificationTypes [UserVisible(true)] sealed class TextToolsFormatDefinition : ClassificationFormatDefinition { + public static bool IsChineseSimple() + { + return System.Threading.Thread.CurrentThread.CurrentCulture.Name == "zh-CN"; + } + public TextToolsFormatDefinition() { BackgroundColor = Color.FromRgb(255, 145, 145); - DisplayName = "Trailing Whitespace"; + if (IsChineseSimple()) + DisplayName = "行尾空白"; + else + DisplayName = "Trailing Whitespace"; } } } \ No newline at end of file diff --git a/TextTools/TextTools/FileHelpers.cs b/TextTools/TextTools/FileHelpers.cs index b742524..7ae80a9 100644 --- a/TextTools/TextTools/FileHelpers.cs +++ b/TextTools/TextTools/FileHelpers.cs @@ -1,8 +1,6 @@ using System; using System.IO; using System.Linq; -using EnvDTE80; -using Microsoft.VisualStudio.Shell; using Microsoft.VisualStudio.Shell.Interop; using Microsoft.VisualStudio.Text; using Microsoft.VisualStudio.TextManager.Interop; diff --git a/TextTools/TextTools/RemoveWhiteSpace.cs b/TextTools/TextTools/RemoveWhiteSpace.cs index 7842a24..ad80247 100644 --- a/TextTools/TextTools/RemoveWhiteSpace.cs +++ b/TextTools/TextTools/RemoveWhiteSpace.cs @@ -1,5 +1,6 @@ using Microsoft.VisualStudio.Text; using System; +using System.IO; namespace TextTools { @@ -24,6 +25,12 @@ public static void RemoveTrailingWhitespace(ITextBuffer buffer) { if (Config.RWS) { + string fileName = buffer.GetFilePath(); + if (string.IsNullOrWhiteSpace(fileName) || !Path.IsPathRooted(fileName)) + return; + + bool csharp = Path.GetExtension(fileName).Equals(".cs", StringComparison.OrdinalIgnoreCase); + using (var edit = buffer.CreateEdit()) { var snap = edit.Snapshot; @@ -34,8 +41,10 @@ public static void RemoveTrailingWhitespace(ITextBuffer buffer) string prefix = ""; string suffix = ""; - char backChar = '0円'; + const char zeroChar = '0円'; + char backChar = zeroChar; int spaceStart = 0; + bool verbatimString = false; foreach (var line in snap.Lines) { @@ -81,6 +90,19 @@ public static void RemoveTrailingWhitespace(ITextBuffer buffer) } } + // Verbatim string skip + if (verbatimString) + { + if (c == '\"') + { + backChar = zeroChar; + spaceStart = 0; + verbatimString = false; + } + + continue; + } + // Skip all \" substring if (backChar == '\\' && c == '\"') { @@ -92,7 +114,7 @@ public static void RemoveTrailingWhitespace(ITextBuffer buffer) // Skip \\ substring else if (backChar == '\\' && c == '\\') { - backChar = ' '; + backChar = zeroChar; if (spaceStart != 0) spaceStart = 0; continue; @@ -105,6 +127,13 @@ public static void RemoveTrailingWhitespace(ITextBuffer buffer) continue; } + // C# verbatim string + if (csharp && backChar == '@' && c == '\"') + { + verbatimString = true; + continue; + } + backChar = c; // String literal diff --git a/TextTools/TextTools/RemoveWhitespaceOnSave.cs b/TextTools/TextTools/RemoveWhitespaceOnSave.cs index bdc9afd..f272a99 100644 --- a/TextTools/TextTools/RemoveWhitespaceOnSave.cs +++ b/TextTools/TextTools/RemoveWhitespaceOnSave.cs @@ -27,7 +27,14 @@ public RemoveWhitespaceOnSave(IVsTextView textViewAdapter, IWpfTextView textView textViewAdapter.AddCommandFilter(this, out NextCommandTarget); } - private static uint[] _cmds = new uint[] { (uint)VSConstants.VSStd97CmdID.SaveProjectItem, (uint)VSConstants.VSStd97CmdID.SaveSolution }; + private static uint[] _cmds = new uint[] { + (uint)VSConstants.VSStd97CmdID.SaveProjectItem, + (uint)VSConstants.VSStd97CmdID.SaveSolution, + (uint)VSConstants.VSStd97CmdID.BuildSln, + (uint)VSConstants.VSStd97CmdID.RebuildSln, + (uint)VSConstants.VSStd97CmdID.BuildSel, + (uint)VSConstants.VSStd97CmdID.RebuildSel + }; public int Exec(ref Guid pguidCmdGroup, uint nCmdID, uint nCmdexecopt, IntPtr pvaIn, IntPtr pvaOut) { if (pguidCmdGroup == typeof(VSConstants.VSStd97CmdID).GUID && _cmds.Contains(nCmdID)) diff --git a/TextTools/TextTools/TextTools.cs b/TextTools/TextTools/TextTools.cs index 828ec1a..9a442db 100644 --- a/TextTools/TextTools/TextTools.cs +++ b/TextTools/TextTools/TextTools.cs @@ -98,7 +98,7 @@ public static IEnumerable GetIgnorePatterns() [PackageRegistration(UseManagedResourcesOnly = true, AllowsBackgroundLoading = true)] [InstalledProductRegistration("#110", "#112", "1.0", IconResourceID = 400)] // Info on this package for Help/About - [ProvideAutoLoad(UIContextGuids.SolutionExists, PackageAutoLoadFlags.BackgroundLoad)] + [ProvideAutoLoad(UIContextGuids.DesignMode, PackageAutoLoadFlags.BackgroundLoad)] [ProvideOptionPage(typeof(OptionPageGrid), "文本工具", "选项", 0, 0, true)] [Guid(TextTools.PackageGuidString)] [SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1650:ElementDocumentationMustBeSpelledCorrectly", Justification = "pkgdef, VS and vsixmanifest are valid VS terms")] @@ -122,6 +122,10 @@ protected override async System.Threading.Tasks.Task InitializeAsync(System.Thre documentEvents = dte.Events.DocumentEvents; documentEvents.DocumentSaved += OnDocumentSaved; + + IVsStatusbar statusBar = (IVsStatusbar)await GetServiceAsync(typeof(SVsStatusbar)); + if (statusBar != null) + statusBar.SetText("TextTools 插件初始化完成"); } void OnDocumentSaved(Document doc) @@ -231,8 +235,8 @@ private static string ConvertToCRLF(string text) public class OptionPageGrid : DialogPage { [Category("文本工具")] - [DisplayName("转换换行符")] - [Description(@"Smart 表示自动转换(统一为文本中最多的换行符)")] + [DisplayName("换行符")] + [Description(@"可根据选择自动转换转行符, 其中 Smart 表示自动转换(统一为文本中最多的换行符)")] public Config.EnumEOL OptionEOL { get { return Config.EOL; } @@ -240,8 +244,8 @@ public Config.EnumEOL OptionEOL } [Category("文本工具")] - [DisplayName("自动转换为UTF8编码")] - [Description(@"将当前源文件自动转换为UTF8编码")] + [DisplayName("UTF8编码")] + [Description(@"可将当前源文件自动转换为UTF8编码")] public Config.EnumUTF8 OptionUTF8 { get { return Config.Utf8Encoding; } diff --git a/appveyor.yml b/appveyor.yml new file mode 100644 index 0000000..0ba53b7 --- /dev/null +++ b/appveyor.yml @@ -0,0 +1,12 @@ +image: Visual Studio 2019 + +before_build: + - nuget restore . + +build_script: + - msbuild .\TextTools.sln /p:Configuration=Release + +artifacts: + - path: TextTools\bin\Release\TextTools.vsix + +test: off

AltStyle によって変換されたページ (->オリジナル) /