diff options
author | Karsten Heimrich <karsten.heimrich@qt.io> | 2023-06-23 13:50:46 +0200 |
---|---|---|
committer | Karsten Heimrich <karsten.heimrich@qt.io> | 2023-06-29 12:25:59 +0000 |
commit | 2162b7b3a191800ec7b424d67d4f4695fbd5c750 (patch) | |
tree | 0d12cd9c52f5214e0896d55f310cbe7302948e63 /QtVsTools.Wizards | |
parent | 161d79eb5afdec8e759b4a6520ea7064247ec8ee (diff) |
Move code adjusting whitespaces and tabs into Wizards/Util
Change-Id: Ife956ad518ad2b3a087c3887a83cfab8c70df666
Reviewed-by: Miguel Costa <miguel.costa@qt.io>
Diffstat (limited to 'QtVsTools.Wizards')
5 files changed, 78 insertions, 9 deletions
diff --git a/QtVsTools.Wizards/ItemWizard/QtClass/QtClassWizard.cs b/QtVsTools.Wizards/ItemWizard/QtClass/QtClassWizard.cs index 45a60165..10a47d21 100644 --- a/QtVsTools.Wizards/ItemWizard/QtClass/QtClassWizard.cs +++ b/QtVsTools.Wizards/ItemWizard/QtClass/QtClassWizard.cs @@ -9,14 +9,13 @@ using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Windows.Controls; -using Microsoft.VisualStudio.Shell; using EnvDTE; +using Microsoft.VisualStudio.Shell; namespace QtVsTools.Wizards.ItemWizard { using Common; using Core; - using Core.MsBuild; using ProjectWizard; using QtVsTools.Common; using Util; @@ -161,7 +160,7 @@ namespace QtVsTools.Wizards.ItemWizard public override void ProjectItemFinishedGenerating(ProjectItem projectItem) { ThreadHelper.ThrowIfNotOnUIThread(); - QtProject.AdjustWhitespace(Dte, projectItem.Properties.Item("FullPath").Value.ToString()); + TextAndWhitespace.Adjust(Dte, projectItem.Properties.Item("FullPath").Value.ToString()); } } } diff --git a/QtVsTools.Wizards/ItemWizard/Translation/TranslationWizard.cs b/QtVsTools.Wizards/ItemWizard/Translation/TranslationWizard.cs index 48242590..31338de2 100644 --- a/QtVsTools.Wizards/ItemWizard/Translation/TranslationWizard.cs +++ b/QtVsTools.Wizards/ItemWizard/Translation/TranslationWizard.cs @@ -7,13 +7,12 @@ using System; using System.Collections.Generic; using System.Globalization; using System.Linq; -using Microsoft.VisualStudio.Shell; using EnvDTE; +using Microsoft.VisualStudio.Shell; namespace QtVsTools.Wizards.ItemWizard { using Common; - using Core.MsBuild; using ProjectWizard; using QtVsTools.Common; using Util; @@ -92,7 +91,7 @@ namespace QtVsTools.Wizards.ItemWizard public override void ProjectItemFinishedGenerating(ProjectItem projectItem) { ThreadHelper.ThrowIfNotOnUIThread(); - QtProject.AdjustWhitespace(Dte, projectItem.Properties.Item("FullPath").Value.ToString()); + TextAndWhitespace.Adjust(Dte, projectItem.Properties.Item("FullPath").Value.ToString()); } } } diff --git a/QtVsTools.Wizards/ItemWizard/WidgetsClass/WidgetsClassWizard.cs b/QtVsTools.Wizards/ItemWizard/WidgetsClass/WidgetsClassWizard.cs index d3f7af32..d8a1a2b3 100644 --- a/QtVsTools.Wizards/ItemWizard/WidgetsClass/WidgetsClassWizard.cs +++ b/QtVsTools.Wizards/ItemWizard/WidgetsClass/WidgetsClassWizard.cs @@ -10,14 +10,13 @@ using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Windows.Controls; -using Microsoft.VisualStudio.Shell; using EnvDTE; +using Microsoft.VisualStudio.Shell; namespace QtVsTools.Wizards.ItemWizard { using Common; using Core; - using Core.MsBuild; using ProjectWizard; using QtVsTools.Common; using Util; @@ -207,7 +206,7 @@ namespace QtVsTools.Wizards.ItemWizard public override void ProjectItemFinishedGenerating(ProjectItem projectItem) { ThreadHelper.ThrowIfNotOnUIThread(); - QtProject.AdjustWhitespace(Dte, projectItem.Properties.Item("FullPath").Value.ToString()); + TextAndWhitespace.Adjust(Dte, projectItem.Properties.Item("FullPath").Value.ToString()); } } } diff --git a/QtVsTools.Wizards/QtVsTools.Wizards.csproj b/QtVsTools.Wizards/QtVsTools.Wizards.csproj index c968feda..69dabac0 100644 --- a/QtVsTools.Wizards/QtVsTools.Wizards.csproj +++ b/QtVsTools.Wizards/QtVsTools.Wizards.csproj @@ -137,6 +137,7 @@ <Compile Include="ProjectWizard\Library\LibraryWizard.cs" /> <Compile Include="ItemWizard\QtClass\QtClassWizard.cs" /> <Compile Include="ItemWizard\WidgetsClass\WidgetsClassWizard.cs" /> + <Compile Include="Util\TextAndWhitespace.cs" /> <Compile Include="Util\VCRulePropertyStorageHelper.cs" /> <T4Template Include="Properties\AssemblyInfo.cs"> <Generator>TextTemplatingFileGenerator</Generator> diff --git a/QtVsTools.Wizards/Util/TextAndWhitespace.cs b/QtVsTools.Wizards/Util/TextAndWhitespace.cs new file mode 100644 index 00000000..b9cc6e0c --- /dev/null +++ b/QtVsTools.Wizards/Util/TextAndWhitespace.cs @@ -0,0 +1,71 @@ +/************************************************************************************************** + Copyright (C) 2023 The Qt Company Ltd. + SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only +**************************************************************************************************/ + +using System; +using System.Collections.Generic; +using System.IO; +using Microsoft.VisualStudio.Shell; + +namespace QtVsTools.Wizards.Util +{ + using Core; + + internal static class TextAndWhitespace + { + + /// <summary> + /// Adjusts the whitespaces and tabs in the given file according to VS settings. + /// </summary> + /// <param name="dte"></param> + /// <param name="file"></param> + internal static void Adjust(EnvDTE.DTE dte, string file) + { + ThreadHelper.ThrowIfNotOnUIThread(); + + if (!File.Exists(file)) + return; + + // only replace whitespaces in known types + if (!HelperFunctions.IsSourceFile(file) && !HelperFunctions.IsHeaderFile(file) + && !HelperFunctions.IsUicFile(file)) { + return; + } + + try { + var prop = dte.Properties["TextEditor", "C/C++"]; + var tabSize = Convert.ToInt64(prop.Item("TabSize").Value); + var insertTabs = Convert.ToBoolean(prop.Item("InsertTabs").Value); + + var oldValue = insertTabs ? " " : "\t"; + var newValue = insertTabs ? "\t" : GetWhitespaces(tabSize); + + var list = new List<string>(); + var reader = new StreamReader(file); + while (reader.ReadLine() is { } line) { + if (line.StartsWith(oldValue, StringComparison.Ordinal)) + line = line.Replace(oldValue, newValue); + list.Add(line); + } + reader.Close(); + + var writer = new StreamWriter(file); + foreach (var l in list) + writer.WriteLine(l); + writer.Close(); + } catch (Exception e) { + Messages.Print("Cannot adjust whitespace or tabs in file (write)." + + Environment.NewLine + $"({e})"); + } + } + + private static string GetWhitespaces(long size) + { + var whitespaces = string.Empty; + for (long i = 0; i < size; ++i) + whitespaces += " "; + return whitespaces; + } + } +} |