Skip to content

Commit e5c485c

Browse files
committed
Added new options to ClangFormat VSIX package.
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 llvm-svn: 250694
1 parent dc12222 commit e5c485c

File tree

2 files changed

+142
-7
lines changed

2 files changed

+142
-7
lines changed

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

Lines changed: 140 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@
1919
using Microsoft.VisualStudio.Text.Editor;
2020
using Microsoft.VisualStudio.TextManager.Interop;
2121
using System;
22+
using System.Collections;
2223
using System.ComponentModel;
2324
using System.ComponentModel.Design;
2425
using System.IO;
25-
using System.Reflection;
2626
using System.Runtime.InteropServices;
2727
using System.Xml.Linq;
2828

@@ -32,13 +32,53 @@ namespace LLVM.ClangFormat
3232
[CLSCompliant(false), ComVisible(true)]
3333
public class OptionPageGrid : DialogPage
3434
{
35-
private string style = "File";
35+
private string assumeFilename = "";
36+
private string fallbackStyle = "LLVM";
37+
private bool sortIncludes = false;
38+
private string style = "file";
39+
40+
public class StyleConverter : TypeConverter
41+
{
42+
protected ArrayList values;
43+
public StyleConverter()
44+
{
45+
// Initializes the standard values list with defaults.
46+
values = new ArrayList(new string[] { "file", "Chromium", "Google", "LLVM", "Mozilla", "WebKit" });
47+
}
48+
49+
public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
50+
{
51+
return true;
52+
}
53+
54+
public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
55+
{
56+
return new StandardValuesCollection(values);
57+
}
58+
59+
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
60+
{
61+
if (sourceType == typeof(string))
62+
return true;
63+
64+
return base.CanConvertFrom(context, sourceType);
65+
}
66+
67+
public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
68+
{
69+
string s = value as string;
70+
if (s == null)
71+
return base.ConvertFrom(context, culture, value);
72+
73+
return value;
74+
}
75+
}
3676

3777
[Category("LLVM/Clang")]
3878
[DisplayName("Style")]
3979
[Description("Coding style, currently supports:\n" +
40-
" - Predefined styles ('LLVM', 'Google', 'Chromium', 'Mozilla').\n" +
41-
" - 'File' to search for a YAML .clang-format or _clang-format\n" +
80+
" - Predefined styles ('LLVM', 'Google', 'Chromium', 'Mozilla', 'WebKit').\n" +
81+
" - 'file' to search for a YAML .clang-format or _clang-format\n" +
4282
" configuration file.\n" +
4383
" - A YAML configuration snippet.\n\n" +
4484
"'File':\n" +
@@ -48,11 +88,81 @@ public class OptionPageGrid : DialogPage
4888
" The content of a .clang-format configuration file, as string.\n" +
4989
" Example: '{BasedOnStyle: \"LLVM\", IndentWidth: 8}'\n\n" +
5090
"See also: http://clang.llvm.org/docs/ClangFormatStyleOptions.html.")]
91+
[TypeConverter(typeof(StyleConverter))]
5192
public string Style
5293
{
5394
get { return style; }
5495
set { style = value; }
5596
}
97+
98+
public sealed class FilenameConverter : TypeConverter
99+
{
100+
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
101+
{
102+
if (sourceType == typeof(string))
103+
return true;
104+
105+
return base.CanConvertFrom(context, sourceType);
106+
}
107+
108+
public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
109+
{
110+
string s = value as string;
111+
if (s == null)
112+
return base.ConvertFrom(context, culture, value);
113+
114+
// Check if string contains quotes. On Windows, file names cannot contain quotes.
115+
// We do not accept them however to avoid hard-to-debug problems.
116+
// A quote in user input would end the parameter quote and so break the command invocation.
117+
if (s.IndexOf('\"') != -1)
118+
throw new NotSupportedException("Filename cannot contain quotes");
119+
120+
return value;
121+
}
122+
}
123+
124+
[Category("LLVM/Clang")]
125+
[DisplayName("Assume Filename")]
126+
[Description("When reading from stdin, clang-format assumes this " +
127+
"filename to look for a style config file (with 'file' style) " +
128+
"and to determine the language.")]
129+
[TypeConverter(typeof(FilenameConverter))]
130+
public string AssumeFilename
131+
{
132+
get { return assumeFilename; }
133+
set { assumeFilename = value; }
134+
}
135+
136+
public sealed class FallbackStyleConverter : StyleConverter
137+
{
138+
public FallbackStyleConverter()
139+
{
140+
// Add "none" to the list of styles.
141+
values.Insert(0, "none");
142+
}
143+
}
144+
145+
[Category("LLVM/Clang")]
146+
[DisplayName("Fallback Style")]
147+
[Description("The name of the predefined style used as a fallback in case clang-format " +
148+
"is invoked with 'file' style, but can not find the configuration file.\n" +
149+
"Use 'none' fallback style to skip formatting.")]
150+
[TypeConverter(typeof(FallbackStyleConverter))]
151+
public string FallbackStyle
152+
{
153+
get { return fallbackStyle; }
154+
set { fallbackStyle = value; }
155+
}
156+
157+
[Category("LLVM/Clang")]
158+
[DisplayName("Sort includes")]
159+
[Description("Sort touched include lines.\n\n" +
160+
"See also: http://clang.llvm.org/docs/ClangFormat.html.")]
161+
public bool SortIncludes
162+
{
163+
get { return sortIncludes; }
164+
set { sortIncludes = value; }
165+
}
56166
}
57167

58168
[PackageRegistration(UseManagedResourcesOnly = true)]
@@ -138,10 +248,17 @@ private string RunClangFormat(string text, int offset, int length, string path)
138248
// Poor man's escaping - this will not work when quotes are already escaped
139249
// in the input (but we don't need more).
140250
string style = GetStyle().Replace("\"", "\\\"");
251+
string fallbackStyle = GetFallbackStyle().Replace("\"", "\\\"");
141252
process.StartInfo.Arguments = " -offset " + offset +
142253
" -length " + length +
143254
" -output-replacements-xml " +
144-
" -style \"" + style + "\"";
255+
" -style \"" + style + "\"" +
256+
" -fallback-style \"" + fallbackStyle + "\"";
257+
if (GetSortIncludes())
258+
process.StartInfo.Arguments += " -sort-includes ";
259+
string assumeFilename = GetAssumeFilename();
260+
if (!string.IsNullOrEmpty(assumeFilename))
261+
process.StartInfo.Arguments += " -assume-filename \"" + assumeFilename + "\"";
145262
process.StartInfo.CreateNoWindow = true;
146263
process.StartInfo.RedirectStandardInput = true;
147264
process.StartInfo.RedirectStandardOutput = true;
@@ -211,6 +328,24 @@ private string GetStyle()
211328
return page.Style;
212329
}
213330

331+
private string GetAssumeFilename()
332+
{
333+
var page = (OptionPageGrid)GetDialogPage(typeof(OptionPageGrid));
334+
return page.AssumeFilename;
335+
}
336+
337+
private string GetFallbackStyle()
338+
{
339+
var page = (OptionPageGrid)GetDialogPage(typeof(OptionPageGrid));
340+
return page.FallbackStyle;
341+
}
342+
343+
private bool GetSortIncludes()
344+
{
345+
var page = (OptionPageGrid)GetDialogPage(typeof(OptionPageGrid));
346+
return page.SortIncludes;
347+
}
348+
214349
private string GetDocumentParent(IWpfTextView view)
215350
{
216351
ITextDocument document;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,5 @@
2929
// You can specify all the values or you can default the Revision and Build Numbers
3030
// by using the '*' as shown below:
3131

32-
[assembly: AssemblyVersion("1.0.0.0")]
33-
[assembly: AssemblyFileVersion("1.0.0.0")]
32+
[assembly: AssemblyVersion("1.1.0.0")]
33+
[assembly: AssemblyFileVersion("1.1.0.0")]

0 commit comments

Comments
 (0)