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
[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
" 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
// 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
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