19
19
using Microsoft . VisualStudio . Text . Editor ;
20
20
using Microsoft . VisualStudio . TextManager . Interop ;
21
21
using System ;
22
+ using System . Collections ;
22
23
using System . ComponentModel ;
23
24
using System . ComponentModel . Design ;
24
25
using System . IO ;
25
- using System . Reflection ;
26
26
using System . Runtime . InteropServices ;
27
27
using System . Xml . Linq ;
28
28
@@ -32,13 +32,53 @@ namespace LLVM.ClangFormat
32
32
[ CLSCompliant ( false ) , ComVisible ( true ) ]
33
33
public class OptionPageGrid : DialogPage
34
34
{
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
+ }
36
76
37
77
[ Category ( "LLVM/Clang" ) ]
38
78
[ DisplayName ( "Style" ) ]
39
79
[ 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 " +
42
82
" configuration file.\n " +
43
83
" - A YAML configuration snippet.\n \n " +
44
84
"'File':\n " +
@@ -48,11 +88,81 @@ public class OptionPageGrid : DialogPage
48
88
" The content of a .clang-format configuration file, as string.\n " +
49
89
" Example: '{BasedOnStyle: \" LLVM\" , IndentWidth: 8}'\n \n " +
50
90
"See also: http://clang.llvm.org/docs/ClangFormatStyleOptions.html." ) ]
91
+ [ TypeConverter ( typeof ( StyleConverter ) ) ]
51
92
public string Style
52
93
{
53
94
get { return style ; }
54
95
set { style = value ; }
55
96
}
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
+ }
56
166
}
57
167
58
168
[ PackageRegistration ( UseManagedResourcesOnly = true ) ]
@@ -138,10 +248,17 @@ private string RunClangFormat(string text, int offset, int length, string path)
138
248
// Poor man's escaping - this will not work when quotes are already escaped
139
249
// in the input (but we don't need more).
140
250
string style = GetStyle ( ) . Replace ( "\" " , "\\ \" " ) ;
251
+ string fallbackStyle = GetFallbackStyle ( ) . Replace ( "\" " , "\\ \" " ) ;
141
252
process . StartInfo . Arguments = " -offset " + offset +
142
253
" -length " + length +
143
254
" -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 + "\" " ;
145
262
process . StartInfo . CreateNoWindow = true ;
146
263
process . StartInfo . RedirectStandardInput = true ;
147
264
process . StartInfo . RedirectStandardOutput = true ;
@@ -211,6 +328,24 @@ private string GetStyle()
211
328
return page . Style ;
212
329
}
213
330
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
+
214
349
private string GetDocumentParent ( IWpfTextView view )
215
350
{
216
351
ITextDocument document ;
0 commit comments