RefactorMCP catalog

Convert to Block Namespace

Turns a file's namespace Shop; declaration into a namespace Shop { ... } block around everything after it, indenting that code a level. The reverse of Convert to File-Scoped Namespace.

Precondition

  • The file has a file-scoped namespace declaration.

Transformation

  • The declaration becomes namespace Name with the opening brace on the next line, and the closing brace ends the file.
  • Every line after the declaration gains one level of indentation, the file's own unit or four spaces when nothing is indented. This includes comments, documentation comments, indented directives and using directives that followed the declaration, which move inside the block.
  • Blank lines between the declaration and the first code are dropped, and blank lines stay empty rather than gaining indentation.

Preserved

  • Everything before the declaration, such as a header comment and using directives.
  • Lines inside a string literal that spans lines, including raw string literals, which are part of its value and keep their indentation.
  • Directives written at column zero, such as #if DEBUG.

Limitations

  • A comment on the same line as the declaration's semicolon is dropped.

Error codes

CodeMeaning
no-file-scoped-namespacethe file has no file-scoped namespace declaration

Cases

·

Diffs show each file from before to after. /*[*/ … /*]*/ marks a selection and /*^*/ a caret; the runner removes them before the refactoring runs.

#comments-and-directives

Keeps the header comment, usings after the namespace, documentation comments and directives, indenting all but column-zero directives

success
TargetOrder.cs
Order.csmodified
11 // Orders.
2−namespace Shop;
2+namespace Shop
3+{
4+ using System;
35
4−using System;
5−
6−/// <summary>
7−/// An order.
8−/// </summary>
9−public class Order
10−{
11− #region Dates
12− public DateTime Placed { get; set; } // local time
13− #endregion
6+ /// <summary>
7+ /// An order.
8+ /// </summary>
9+ public class Order
10+ {
11+ #region Dates
12+ public DateTime Placed { get; set; } // local time
13+ #endregion
1414
1515 #if DEBUG
16− public string Trace => "debug";
16+ public string Trace => "debug";
1717 #endif
1818
19− /* The day
20− after placing. */
21− public DateTime Next => Placed.AddDays(1);
19+ /* The day
20+ after placing. */
21+ public DateTime Next => Placed.AddDays(1);
22+ }
2223 }

#file-scoped-namespace

Wraps everything after a file-scoped namespace declaration in a namespace block, indenting it a level

success
TargetOrder.cs
Order.csmodified
11 using System;
22
3−namespace Shop.Orders;
3+namespace Shop.Orders
4+{
5+ public class Order
6+ {
7+ public DateTime Placed { get; set; }
48
5−public class Order
6−{
7− public DateTime Placed { get; set; }
9+ public bool IsLate(DateTime now)
10+ {
11+ return now - Placed > TimeSpan.FromDays(3);
12+ }
13+ }
814
9− public bool IsLate(DateTime now)
15+ public enum Status
1016 {
11− return now - Placed > TimeSpan.FromDays(3);
17+ Open,
18+ Closed,
1219 }
1320 }
14−
15−public enum Status
16−{
17− Open,
18− Closed,
19−}

#multiline-string-untouched

Leaves the lines inside a raw string literal as they were, since they are part of its value

success
TargetTemplate.cs
Template.csmodified
1−namespace Shop;
2−
3−public static class Template
1+namespace Shop
42 {
5− public const string Body = @"Dear customer,
3+ public static class Template
4+ {
5+ public const string Body = @"Dear customer,
66 Thank you.
77 Shop";
88
9− public const string Footer = """
9+ public const string Footer = """
1010 Regards
1111 """;
12+ }
1213 }

#not-file-scoped-refused

Refuses a file without a file-scoped namespace

refusal
TargetOrder.cs
Refusesno-file-scoped-namespace; every file is left unchanged
Order.csinput
1 namespace Shop
2 {
3 public class Order
4 {
5 }
6 }