2
2
using System . Collections . Generic ;
3
3
using System . ComponentModel ;
4
4
using System . IO ;
5
+ using System . Text . RegularExpressions ;
5
6
using System . Windows . Forms ;
6
7
using ASCompletion . Context ;
7
8
using CodeFormatter . Handlers ;
@@ -19,7 +20,7 @@ public class PluginMain : IPlugin
19
20
{
20
21
private String pluginName = "CodeFormatter" ;
21
22
private String pluginGuid = "f7f1e15b-282a-4e55-ba58-5f2c02765247" ;
22
- private String pluginDesc = "Adds an MXML and ActionScript code formatter to FlashDevelop." ;
23
+ private String pluginDesc = "Adds multiple code formatters to FlashDevelop." ;
23
24
private String pluginHelp = "www.flashdevelop.org/community/" ;
24
25
private String pluginAuth = "FlashDevelop Team" ;
25
26
private ToolStripMenuItem contextMenuItem ;
@@ -167,7 +168,7 @@ private void UpdateMenuItems()
167
168
{
168
169
if ( this . mainMenuItem == null || this . contextMenuItem == null ) return ;
169
170
ITabbedDocument doc = PluginBase . MainForm . CurrentDocument ;
170
- Boolean isValid = doc != null && doc . IsEditable && this . IsSupportedLanguage ( doc . FileName ) ;
171
+ Boolean isValid = doc != null && doc . IsEditable && this . DocumentType != TYPE_UNKNOWN ;
171
172
this . mainMenuItem . Enabled = this . contextMenuItem . Enabled = isValid ;
172
173
}
173
174
@@ -199,15 +200,6 @@ public void AttachContextMenuItem(ToolStripMenuItem contextMenu)
199
200
contextMenu . DropDownItems . Insert ( 6 , this . contextMenuItem ) ;
200
201
}
201
202
202
- /// <summary>
203
- /// Checks if the language is supported
204
- /// </summary>
205
- public Boolean IsSupportedLanguage ( String file )
206
- {
207
- String lang = ScintillaControl . Configuration . GetLanguageFromFile ( file ) ;
208
- return ( lang == "as2" || lang == "as3" || lang == "xml" ) ;
209
- }
210
-
211
203
/// <summary>
212
204
/// Loads the plugin settings
213
205
/// </summary>
@@ -241,7 +233,8 @@ public void SaveSettings()
241
233
private const int TYPE_AS3 = 0 ;
242
234
private const int TYPE_MXML = 1 ;
243
235
private const int TYPE_XML = 2 ;
244
- private const int TYPE_UNKNOWN = 3 ;
236
+ private const int TYPE_CPP = 3 ;
237
+ private const int TYPE_UNKNOWN = 4 ;
245
238
246
239
/// <summary>
247
240
/// Formats the current document
@@ -267,7 +260,7 @@ private void DoFormat(ITabbedDocument doc)
267
260
{
268
261
case TYPE_AS3 :
269
262
ASPrettyPrinter asPrinter = new ASPrettyPrinter ( true , source ) ;
270
- FormatUtility . configureASPrinter ( asPrinter , this . settingObject , PluginBase . Settings . TabWidth ) ;
263
+ FormatUtility . configureASPrinter ( asPrinter , this . settingObject ) ;
271
264
String asResultData = asPrinter . print ( 0 ) ;
272
265
if ( asResultData == null )
273
266
{
@@ -284,7 +277,7 @@ private void DoFormat(ITabbedDocument doc)
284
277
case TYPE_MXML :
285
278
case TYPE_XML :
286
279
MXMLPrettyPrinter mxmlPrinter = new MXMLPrettyPrinter ( source ) ;
287
- FormatUtility . configureMXMLPrinter ( mxmlPrinter , this . settingObject , PluginBase . Settings . TabWidth ) ;
280
+ FormatUtility . configureMXMLPrinter ( mxmlPrinter , this . settingObject ) ;
288
281
String mxmlResultData = mxmlPrinter . print ( 0 ) ;
289
282
if ( mxmlResultData == null )
290
283
{
@@ -297,6 +290,27 @@ private void DoFormat(ITabbedDocument doc)
297
290
doc . SciControl . ConvertEOLs ( doc . SciControl . EOLMode ) ;
298
291
}
299
292
break ;
293
+
294
+ case TYPE_CPP :
295
+ AStyleInterface asi = new AStyleInterface ( ) ;
296
+ String optionData = this . GetOptionData ( doc . SciControl . ConfigurationLanguage . ToLower ( ) ) ;
297
+ String resultData = asi . FormatSource ( source , optionData ) ;
298
+ if ( String . IsNullOrEmpty ( resultData ) )
299
+ {
300
+ TraceManager . Add ( TextHelper . GetString ( "Info.CouldNotFormat" ) , - 3 ) ;
301
+ PluginBase . MainForm . CallCommand ( "PluginCommand" , "ResultsPanel.ShowResults" ) ;
302
+ }
303
+ else
304
+ {
305
+ // Remove all empty lines if specified
306
+ if ( optionData . Contains ( "--delete-empty-lines" ) )
307
+ {
308
+ resultData = Regex . Replace ( resultData , @"(\r?\n){3,}" , "$1" ) ;
309
+ }
310
+ doc . SciControl . Text = resultData ;
311
+ doc . SciControl . ConvertEOLs ( doc . SciControl . EOLMode ) ;
312
+ }
313
+ break ;
300
314
}
301
315
}
302
316
catch ( Exception )
@@ -310,7 +324,31 @@ private void DoFormat(ITabbedDocument doc)
310
324
}
311
325
312
326
/// <summary>
313
- ///
327
+ /// Get the options for the formatter based on FD settings or manual command
328
+ /// </summary>
329
+ private String GetOptionData ( String language )
330
+ {
331
+ String optionData ;
332
+ if ( language == "cpp" ) optionData = this . settingObject . Pref_AStyle_CPP ;
333
+ else optionData = this . settingObject . Pref_AStyle_Others ;
334
+ if ( String . IsNullOrEmpty ( optionData ) )
335
+ {
336
+ Int32 tabSize = PluginBase . Settings . TabWidth ;
337
+ Boolean useTabs = PluginBase . Settings . UseTabs ;
338
+ Int32 spaceSize = PluginBase . Settings . IndentSize ;
339
+ CodingStyle codingStyle = PluginBase . Settings . CodingStyle ;
340
+ optionData = AStyleInterface . DefaultOptions + " --mode=c" ;
341
+ if ( language != "cpp" ) optionData += "s" ; // --mode=cs
342
+ if ( useTabs ) optionData += " --indent=force-tab=" + tabSize . ToString ( ) ;
343
+ else optionData += " --indent=spaces=" + spaceSize . ToString ( ) ;
344
+ if ( codingStyle == CodingStyle . BracesAfterLine ) optionData += " --style=allman" ;
345
+ else optionData += " --style=attach" ;
346
+ }
347
+ return optionData ;
348
+ }
349
+
350
+ /// <summary>
351
+ /// Gets or sets the current position, ignoring whitespace
314
352
/// </summary>
315
353
public int CurrentPos
316
354
{
@@ -348,28 +386,29 @@ public int CurrentPos
348
386
}
349
387
350
388
/// <summary>
351
- ///
389
+ /// Gets the formatting type of the document
352
390
/// </summary>
353
391
public Int32 DocumentType
354
392
{
355
393
get
356
394
{
357
- var xmls = new List < String > { ".xml" } ;
358
395
ITabbedDocument document = PluginBase . MainForm . CurrentDocument ;
359
396
if ( ! document . IsEditable ) return TYPE_UNKNOWN ;
360
397
String ext = Path . GetExtension ( document . FileName ) . ToLower ( ) ;
398
+ String lang = document . SciControl . ConfigurationLanguage . ToLower ( ) ;
361
399
if ( ASContext . Context . CurrentModel . Context != null && ASContext . Context . CurrentModel . Context . GetType ( ) . ToString ( ) . Equals ( "AS3Context.Context" ) )
362
400
{
363
401
if ( ext == ".as" ) return TYPE_AS3 ;
364
402
else if ( ext == ".mxml" ) return TYPE_MXML ;
365
403
}
366
- else if ( xmls . Contains ( ext ) ) return TYPE_XML ;
404
+ else if ( lang == "xml" ) return TYPE_XML ;
405
+ else if ( document . SciControl . Lexer == 3 ) return TYPE_CPP ;
367
406
return TYPE_UNKNOWN ;
368
407
}
369
408
}
370
409
371
410
/// <summary>
372
- ///
411
+ /// Compress text for finding correct restore position
373
412
/// </summary>
374
413
public String CompressText ( String originalText )
375
414
{
@@ -380,14 +419,6 @@ public String CompressText(String originalText)
380
419
return compressedText ;
381
420
}
382
421
383
- /// <summary>
384
- ///
385
- /// </summary>
386
- public bool IsBlankChar ( Char ch )
387
- {
388
- return ( ch == ' ' || ch == '\t ' || ch == '\n ' || ch == '\r ' ) ;
389
- }
390
-
391
422
#endregion
392
423
393
424
}
0 commit comments