]> granicus.if.org Git - clang/commitdiff
Added new options to ClangFormat VSIX package.
authorMarek Kurdej <marek.kurdej@gmail.com>
Mon, 19 Oct 2015 10:08:35 +0000 (10:08 +0000)
committerMarek Kurdej <marek.kurdej@gmail.com>
Mon, 19 Oct 2015 10:08:35 +0000 (10:08 +0000)
Summary:
Added new options to ClangFormat VSIX package:
* fallback-style
* assume-filename
* sort-includes.
Changed version to 1.1 (otherwise one couldn't update).

Fixed clang-format escaping of XML reserved characters.

Reviewers: hans, aaron.ballman, klimek, rnk, zturner

Subscribers: djasper, cfe-commits

Differential Revision: http://reviews.llvm.org/D13549

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@250694 91177308-0d34-0410-b5e6-96231b3b80d8

tools/clang-format-vs/ClangFormat/ClangFormatPackage.cs
tools/clang-format-vs/ClangFormat/Properties/AssemblyInfo.cs

index c3aa8fecded5832ac179c5d56d062c08a23b2bae..df872b2e2198235fd507047e2c5afbac45cf426a 100644 (file)
@@ -19,10 +19,10 @@ using Microsoft.VisualStudio.Text;
 using Microsoft.VisualStudio.Text.Editor;\r
 using Microsoft.VisualStudio.TextManager.Interop;\r
 using System;\r
+using System.Collections;\r
 using System.ComponentModel;\r
 using System.ComponentModel.Design;\r
 using System.IO;\r
-using System.Reflection;\r
 using System.Runtime.InteropServices;\r
 using System.Xml.Linq;\r
 \r
@@ -32,13 +32,53 @@ namespace LLVM.ClangFormat
     [CLSCompliant(false), ComVisible(true)]\r
     public class OptionPageGrid : DialogPage\r
     {\r
-        private string style = "File";\r
+        private string assumeFilename = "";\r
+        private string fallbackStyle = "LLVM";\r
+        private bool sortIncludes = false;\r
+        private string style = "file";\r
+\r
+        public class StyleConverter : TypeConverter\r
+        {\r
+            protected ArrayList values;\r
+            public StyleConverter()\r
+            {\r
+                // Initializes the standard values list with defaults.\r
+                values = new ArrayList(new string[] { "file", "Chromium", "Google", "LLVM", "Mozilla", "WebKit" });\r
+            }\r
+\r
+            public override bool GetStandardValuesSupported(ITypeDescriptorContext context)\r
+            {\r
+                return true;\r
+            }\r
+\r
+            public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)\r
+            {\r
+                return new StandardValuesCollection(values);\r
+            }\r
+\r
+            public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)\r
+            {\r
+                if (sourceType == typeof(string))\r
+                    return true;\r
+\r
+                return base.CanConvertFrom(context, sourceType);\r
+            }\r
+\r
+            public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)\r
+            {\r
+                string s = value as string;\r
+                if (s == null)\r
+                    return base.ConvertFrom(context, culture, value);\r
+\r
+                return value;\r
+            }\r
+        }\r
 \r
         [Category("LLVM/Clang")]\r
         [DisplayName("Style")]\r
         [Description("Coding style, currently supports:\n" +\r
-                     "  - Predefined styles ('LLVM', 'Google', 'Chromium', 'Mozilla').\n" +\r
-                     "  - 'File' to search for a YAML .clang-format or _clang-format\n" +\r
+                     "  - Predefined styles ('LLVM', 'Google', 'Chromium', 'Mozilla', 'WebKit').\n" +\r
+                     "  - 'file' to search for a YAML .clang-format or _clang-format\n" +\r
                      "    configuration file.\n" +\r
                      "  - A YAML configuration snippet.\n\n" +\r
                      "'File':\n" +\r
@@ -48,11 +88,81 @@ namespace LLVM.ClangFormat
                      "  The content of a .clang-format configuration file, as string.\n" +\r
                      "  Example: '{BasedOnStyle: \"LLVM\", IndentWidth: 8}'\n\n" +\r
                      "See also: http://clang.llvm.org/docs/ClangFormatStyleOptions.html.")]\r
+        [TypeConverter(typeof(StyleConverter))]\r
         public string Style\r
         {\r
             get { return style; }\r
             set { style = value; }\r
         }\r
+\r
+        public sealed class FilenameConverter : TypeConverter\r
+        {\r
+            public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)\r
+            {\r
+                if (sourceType == typeof(string))\r
+                    return true;\r
+\r
+                return base.CanConvertFrom(context, sourceType);\r
+            }\r
+\r
+            public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)\r
+            {\r
+                string s = value as string;\r
+                if (s == null)\r
+                    return base.ConvertFrom(context, culture, value);\r
+\r
+                // Check if string contains quotes. On Windows, file names cannot contain quotes.\r
+                // We do not accept them however to avoid hard-to-debug problems.\r
+                // A quote in user input would end the parameter quote and so break the command invocation.\r
+                if (s.IndexOf('\"') != -1)\r
+                    throw new NotSupportedException("Filename cannot contain quotes");\r
+\r
+                return value;\r
+            }\r
+        }\r
+\r
+        [Category("LLVM/Clang")]\r
+        [DisplayName("Assume Filename")]\r
+        [Description("When reading from stdin, clang-format assumes this " +\r
+                     "filename to look for a style config file (with 'file' style) " +\r
+                     "and to determine the language.")]\r
+        [TypeConverter(typeof(FilenameConverter))]\r
+        public string AssumeFilename\r
+        {\r
+            get { return assumeFilename; }\r
+            set { assumeFilename = value; }\r
+        }\r
+\r
+        public sealed class FallbackStyleConverter : StyleConverter\r
+        {\r
+            public FallbackStyleConverter()\r
+            {\r
+                // Add "none" to the list of styles.\r
+                values.Insert(0, "none");\r
+            }\r
+        }\r
+\r
+        [Category("LLVM/Clang")]\r
+        [DisplayName("Fallback Style")]\r
+        [Description("The name of the predefined style used as a fallback in case clang-format " +\r
+                     "is invoked with 'file' style, but can not find the configuration file.\n" +\r
+                     "Use 'none' fallback style to skip formatting.")]\r
+        [TypeConverter(typeof(FallbackStyleConverter))]\r
+        public string FallbackStyle\r
+        {\r
+            get { return fallbackStyle; }\r
+            set { fallbackStyle = value; }\r
+        }\r
+\r
+        [Category("LLVM/Clang")]\r
+        [DisplayName("Sort includes")]\r
+        [Description("Sort touched include lines.\n\n" +\r
+                     "See also: http://clang.llvm.org/docs/ClangFormat.html.")]\r
+        public bool SortIncludes\r
+        {\r
+            get { return sortIncludes; }\r
+            set { sortIncludes = value; }\r
+        }\r
     }\r
 \r
     [PackageRegistration(UseManagedResourcesOnly = true)]\r
@@ -138,10 +248,17 @@ namespace LLVM.ClangFormat
             // Poor man's escaping - this will not work when quotes are already escaped\r
             // in the input (but we don't need more).\r
             string style = GetStyle().Replace("\"", "\\\"");\r
+            string fallbackStyle = GetFallbackStyle().Replace("\"", "\\\"");\r
             process.StartInfo.Arguments = " -offset " + offset +\r
                                           " -length " + length +\r
                                           " -output-replacements-xml " +\r
-                                          " -style \"" + style + "\"";\r
+                                          " -style \"" + style + "\"" +\r
+                                          " -fallback-style \"" + fallbackStyle + "\"";\r
+            if (GetSortIncludes())\r
+              process.StartInfo.Arguments += " -sort-includes ";\r
+            string assumeFilename = GetAssumeFilename();\r
+            if (!string.IsNullOrEmpty(assumeFilename))\r
+              process.StartInfo.Arguments += " -assume-filename \"" + assumeFilename + "\"";\r
             process.StartInfo.CreateNoWindow = true;\r
             process.StartInfo.RedirectStandardInput = true;\r
             process.StartInfo.RedirectStandardOutput = true;\r
@@ -211,6 +328,24 @@ namespace LLVM.ClangFormat
             return page.Style;\r
         }\r
 \r
+        private string GetAssumeFilename()\r
+        {\r
+            var page = (OptionPageGrid)GetDialogPage(typeof(OptionPageGrid));\r
+            return page.AssumeFilename;\r
+        }\r
+\r
+        private string GetFallbackStyle()\r
+        {\r
+            var page = (OptionPageGrid)GetDialogPage(typeof(OptionPageGrid));\r
+            return page.FallbackStyle;\r
+        }\r
+\r
+        private bool GetSortIncludes()\r
+        {\r
+            var page = (OptionPageGrid)GetDialogPage(typeof(OptionPageGrid));\r
+            return page.SortIncludes;\r
+        }\r
+\r
         private string GetDocumentParent(IWpfTextView view)\r
         {\r
             ITextDocument document;\r
index e6e4de488012a546b85ab9eda21591eca085a0a2..b1cef49414b5eba4d19a7080624388258590984d 100644 (file)
@@ -29,5 +29,5 @@ using System.Runtime.InteropServices;
 // You can specify all the values or you can default the Revision and Build Numbers \r
 // by using the '*' as shown below:\r
 \r
-[assembly: AssemblyVersion("1.0.0.0")]\r
-[assembly: AssemblyFileVersion("1.0.0.0")]\r
+[assembly: AssemblyVersion("1.1.0.0")]\r
+[assembly: AssemblyFileVersion("1.1.0.0")]\r