@@ -353,6 +353,19 @@ public string Fix(string scriptDefinition, Settings settings)
353
353
/// <param name="settings">The settings to use when fixing.</param>
354
354
public Task < string > FixAsync ( string scriptDefinition , Settings settings ) => Task . Run ( ( ) => Fix ( scriptDefinition , settings ) ) ;
355
355
356
+ /// <summary>
357
+ /// Create a standard settings object for Script Analyzer
358
+ /// This is the object used by analyzer internally
359
+ /// It is more functional than the AnalyzerSettings object because
360
+ /// it contains the Rule Arguments which are not passable to the Initialize method
361
+ /// </summary>
362
+ public Settings CreateSettings ( string [ ] ruleNames )
363
+ {
364
+ var s = CreateSettings ( ) ;
365
+ s . IncludeRules . AddRange ( ruleNames ) ;
366
+ return s ;
367
+ }
368
+
356
369
/// <summary>
357
370
/// Create a standard settings object for Script Analyzer
358
371
/// This is the object used by analyzer internally
@@ -437,6 +450,52 @@ public string Format(string scriptDefinition)
437
450
public Task < string > FormatAsync ( string scriptDefinition ) =>
438
451
Task . Run ( ( ) => Format ( scriptDefinition ) ) ;
439
452
453
+ /// <summary>
454
+ /// Format a script according to the formatting rules
455
+ /// PSPlaceCloseBrace
456
+ /// PSPlaceOpenBrace
457
+ /// PSUseConsistentWhitespace
458
+ /// PSUseConsistentIndentation
459
+ /// PSAlignAssignmentStatement
460
+ /// PSUseCorrectCasing
461
+ /// </summary>
462
+ /// <param name="scriptDefinition">The script to format.</param>
463
+ /// <param name="range">The range of the script to format</param>
464
+ /// <returns>The formatted script.</returns>
465
+ public string Format ( string scriptDefinition , Range range )
466
+ {
467
+ Settings settings = CreateSettings ( "CodeFormatting" ) ;
468
+ string formattedScript ;
469
+ lock ( hostedAnalyzerLock )
470
+ {
471
+ try {
472
+ formattedScript = Formatter . Format ( scriptDefinition , settings , range , ps . Runspace , writer ) ;
473
+ }
474
+ finally {
475
+ analyzer . CleanUp ( ) ;
476
+ // Reset is required because formatting leaves a number of settings behind which
477
+ // should be cleared.
478
+ Reset ( ) ;
479
+ }
480
+ }
481
+ return formattedScript ;
482
+ }
483
+
484
+ /// <summary>
485
+ /// Format a script according to the formatting rules
486
+ /// PSPlaceCloseBrace
487
+ /// PSPlaceOpenBrace
488
+ /// PSUseConsistentWhitespace
489
+ /// PSUseConsistentIndentation
490
+ /// PSAlignAssignmentStatement
491
+ /// PSUseCorrectCasing
492
+ /// </summary>
493
+ /// <param name="scriptDefinition">The script to format.</param>
494
+ /// <param name="range">The range of the script to format</param>
495
+ /// <returns>The formatted script.</returns>
496
+ public Task < string > FormatAsync ( string scriptDefinition , Range range ) =>
497
+ Task . Run ( ( ) => Format ( scriptDefinition , range ) ) ;
498
+
440
499
/// <summary>
441
500
/// Format a script based on settings
442
501
/// </summary>
@@ -497,6 +556,8 @@ internal class hostedWriter : IOutputWriter
497
556
public IList < string > Debug ;
498
557
/// <summary>The warning messages emitted during the invocation of the analyzer.</summary>
499
558
public IList < string > Warning ;
559
+ /// <summary>Add a nonterminating error the ccollection.</summary>
560
+ public void WriteError ( SMA . ErrorRecord errorRecord ) { TerminatingErrors . Add ( errorRecord ) ; }
500
561
/// <summary>Add a terminating error the ccollection.</summary>
501
562
public void ThrowTerminatingError ( SMA . ErrorRecord errorRecord ) { TerminatingErrors . Add ( errorRecord ) ; }
502
563
/// <summary>Add a verbose message to the verbose collection.</summary>
0 commit comments