RefactorMCP catalog

Rename File to Match Type

Renames a source file after the single top-level type it declares, so that Ledger.cs holding class Account becomes Account.cs.

Arguments

ArgumentMeaning
target.filethe file to rename

Precondition

  • The file declares exactly one top-level type. Types nested inside it do not count, and the parts of a partial type in the same file count once.
  • The file is not already named after the type.
  • No other file with the new name exists in the same folder.

Transformation

  • The file is renamed to the type's name followed by .cs, in the same folder and project. A generic type's file takes the name without its type parameters: Box<T> lives in Box.cs.

Preserved

  • The file's text, byte for byte: comments, usings and layout are untouched.
  • Every reference to the type, since no code changes.

Limitations

  • Generic arity is not written into the name, so Box<T> and Box<T, U> cannot both be renamed into one folder.
  • Enums, delegates, records, structs and interfaces are named the same way as classes.

Error codes

CodeMeaning
multiple-typesthe file declares more than one top-level type
no-typethe file declares no top-level type
name-already-matchesthe file is already named after its type
file-existsa file with the new name already exists in the folder

Cases

·

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

#already-matches-rejected

Refuses when the file is already named after its type

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

#file-exists-rejected

Refuses when a file with the type's name already exists beside it

refusal
TargetOrders.cs
Refusesfile-exists; every file is left unchanged
Order.csinput
1 namespace Shop
2 {
3 public static class OrderDefaults
4 {
5 public const int Quantity = 1;
6 }
7 }
Orders.csinput
1 namespace Shop
2 {
3 public class Order
4 {
5 }
6 }

#generic-type

A generic type's file takes the type's name without its type parameters

success
TargetContainers.cs
Box.csadded
1+namespace Shop
2+{
3+ public class Box<T>
4+ {
5+ public T Content { get; set; }
6+ }
7+}
Containers.csdeleted
1−namespace Shop
2−{
3− public class Box<T>
4− {
5− public T Content { get; set; }
6− }
7−}

#in-folder

A file in a folder is renamed in place, staying in its folder

success
TargetBilling/Types.cs
Billing/InvoiceState.csadded
1+namespace Shop.Billing;
2+
3+public enum InvoiceState
4+{
5+ Draft,
6+ Sent,
7+ Paid,
8+}
Billing/Types.csdeleted
1−namespace Shop.Billing;
2−
3−public enum InvoiceState
4−{
5− Draft,
6− Sent,
7− Paid,
8−}

#multiple-types-rejected

Refuses a file declaring two top-level types, since no single name fits

refusal
TargetModel.cs
Refusesmultiple-types; every file is left unchanged
Model.csinput
1 namespace Shop
2 {
3 public class Order
4 {
5 }
6
7 public class Customer
8 {
9 }
10 }

#nested-types-ignored

Nested types do not count; the file is named after its single top-level type

success
TargetOrders.cs
Order.csadded
1+using System.Collections.Generic;
2+
3+namespace Shop
4+{
5+ public class Order
6+ {
7+ private readonly List<Line> _lines = new List<Line>();
8+
9+ public void Add(string sku) => _lines.Add(new Line { Sku = sku });
10+
11+ private class Line
12+ {
13+ public string Sku;
14+ }
15+ }
16+}
Orders.csdeleted
1−using System.Collections.Generic;
2−
3−namespace Shop
4−{
5− public class Order
6− {
7− private readonly List<Line> _lines = new List<Line>();
8−
9− public void Add(string sku) => _lines.Add(new Line { Sku = sku });
10−
11− private class Line
12− {
13− public string Sku;
14− }
15− }
16−}

#single-type

Renames a file to the name of the one type it declares, keeping its text, comments included

success
TargetLedger.cs
Account.csadded
1+// Accounts held in the general ledger.
2+using System.Collections.Generic;
3+
4+namespace Shop
5+{
6+ /// <summary>An account and its postings.</summary>
7+ public class Account
8+ {
9+ private readonly List<decimal> _postings = new List<decimal>();
10+
11+ public void Post(decimal amount) => _postings.Add(amount);
12+ }
13+}
Ledger.csdeleted
1−// Accounts held in the general ledger.
2−using System.Collections.Generic;
3−
4−namespace Shop
5−{
6− /// <summary>An account and its postings.</summary>
7− public class Account
8− {
9− private readonly List<decimal> _postings = new List<decimal>();
10−
11− public void Post(decimal amount) => _postings.Add(amount);
12− }
13−}
Checkout.csunchanged
11 namespace Shop
22 {
33 public class Checkout
44 {
55 public void Pay(Account account) => account.Post(10m);
66 }
77 }