Skip to content

Add a refactoring between namespace + nested module and root module - #20536

Open
xperiandri wants to merge 9 commits into
dotnet:mainfrom
xperiandri:feature/namespace-module-refactoring
Open

xperiandri wants to merge 9 commits into
dotnet:mainfrom
xperiandri:feature/namespace-module-refactoring

Conversation

@xperiandri

@xperiandri xperiandri commented Sep 14, 2026 •

Copy link
Copy Markdown
Contributor

Offers Ctrl+. on the header to turn namespace A.B with a single nested module C = into module A.B.C, and back. opens before the nested module move under the new header; the body is re-indented line by line, leaving multi-line strings alone.

Not offered when the namespace holds anything besides leading opens and the module, or when the file has a paired .fsi (the signature would stop matching, FS0240).

🤖 Generated with Claude Code

@github-actions

github-actions Bot commented Sep 14, 2026 •

Copy link
Copy Markdown
Contributor

✅ Release notes checked


✅ Found changes and release notes in following paths:

Change path Release notes path Description
`vsintegration/src` docs/release-notes/.VisualStudio/18.vNext.md

@xperiandri
xperiandri force-pushed the feature/namespace-module-refactoring branch from e51d4cd to 7863f7e Compare September 14, 2026 11:09
@xperiandri
xperiandri marked this pull request as ready for review September 14, 2026 19:40
@xperiandri
xperiandri requested a review from a team as a code owner September 14, 2026 19:40
@xperiandri
xperiandri force-pushed the feature/namespace-module-refactoring branch from 5f7ae33 to afc3127 Compare September 14, 2026 19:46
@github-actions github-actions Bot added the ⚠️ Affects-Design-Time Tooling check: PR touches type providers or dependency manager label Sep 14, 2026
@github-actions

This comment has been minimized.

@T-Gro T-Gro left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖🕵️ Please shorten the description using this guidance. Focus on the problem and why the change is needed, in simplified technical English. Leave the implementation inventory to the Files tab and retain necessary caveats.

@github-project-automation github-project-automation Bot moved this from New to In Progress in F# Compiler and Tooling Sep 15, 2026
@xperiandri xperiandri changed the title Refactoring to convert between a namespace with one nested module and a root-level module Add a refactoring between namespace + nested module and root module Sep 15, 2026
@xperiandri

Copy link
Copy Markdown
Contributor Author

Covered cases

Converts

  • namespace My.Company + module private Helpers = ⟷ module private My.Company.Helpers, both directions, with the caret on the namespace or the module line; covers an XML doc comment, [<AutoOpen; RequireQualifiedAccess>], let inline and a multi-line triple-quoted string whose lines are not re-indented
  • rec on the namespace, the module or both: namespace rec A.B / module rec C = → module rec A.B.C
  • A comment or XML doc comment before the nested module stays in front of the new header: // Helpers for B. above module A.B.C
  • #if DEBUG / #endif inside the body are dedented along with it
  • opens between the namespace and the module move below the new header: namespace A.B + open System + module C = → module A.B.C followed by open System; also with several opens, a doc comment on the module, and a comment above the opens (it stays above the header and the opens are not left behind)
  • module A.B.C → namespace A.B + module C = with the body indented and blank lines left empty
  • module rec A.B.C → module rec C =; header attributes and accessibility are kept: module [<AutoOpen>] internal A.B.C → module [<AutoOpen>] internal C =
  • Backticked namespace and a trailing header comment: module ``A-B``.C // header → namespace ``A-B`` + module C = // header
  • CRLF kept in both directions: namespace A.B\r\n\r\nmodule C =\r\n let x = 1\r\n ⟷ module A.B.C\r\nlet x = 1\r\n
  • Round trip: module A.B.C converted to a nested module and back gives the original text

Not offered

  • The namespace holds more than one module (module C = … module D =) or no module (type T = int)
  • namespace global with a nested module
  • A single-segment root module: module C
  • A module written as module C = begin … end
  • A comment after the namespace name: namespace A.B // B
  • Caret outside the header lines, e.g. on let x = 1, in either shape
  • An empty nested module: module C = with no declarations
  • An open after the module, or a declaration other than open before it (type T = int)
  • The implementation file has a signature file

@T-Gro T-Gro added the vsintegration-only Changes only Visual Studio integration, plus optional docs, release notes, or tests label Sep 21, 2026
@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@xperiandri

Copy link
Copy Markdown
Contributor Author

Pushed c3aadc7: the 21 implementation comment lines of the refactoring and the 2 in its tests are gone, so the PR adds one comment line, the license header (24 comment lines and 2273 characters before). What they said is in names and small functions now: hasIfDirectiveOnOrAbove, isDirectiveRecognizedWhileSkipping, startsInsideString as the complement of isCodeCommentOrSkippedState, isNamedArgumentTarget and isConstantOperator inside attributesKeepMeaningAt, and removedLineRangesWithBlankLinesAfter and insertOpensBelowHeader for the two steps of the namespace-to-root edit. Behaviour is unchanged: the 8 test methods with their 44 cases (ConvertNamespaceModuleTests) and the AddReturnTypeTests control (22) pass before and after, and FSharp.Editor also builds with Optimize=true. I left the six lines that start with // or /// inside the test input strings (// Helpers for B., /// Doc., // Opens move under the header.), because they are the sample code of the scenarios and not comments of the test file.

@T-Gro T-Gro left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖🕵️ AI review — verify independently.

xperiandri added a commit to xperiandri/fsharp that referenced this pull request Oct 19, 2026
From dotnet#20536: a comment between the namespace and the opens
left the opens in place next to their copies under the new header.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
xperiandri added a commit to xperiandri/fsharp that referenced this pull request Oct 19, 2026
This branch had only the copy without the PR link. It gives way to the bullet
the PR keeps (50669a2 of dotnet#20536).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
xperiandri added a commit to xperiandri/fsharp that referenced this pull request Oct 19, 2026
…them

Port of the dotnet#20536 compaction (feature/namespace-module-refactoring
e610d0f..c3aadc7) to all-fixes.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
xperiandri added a commit to xperiandri/fsharp that referenced this pull request Oct 19, 2026
From dotnet#20536: a comment between the namespace and the opens
left the opens in place next to their copies under the new header.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
xperiandri added a commit to xperiandri/fsharp that referenced this pull request Oct 19, 2026
This branch had only the copy without the PR link. It gives way to the bullet
the PR keeps (50669a2 of dotnet#20536).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
xperiandri added a commit to xperiandri/fsharp that referenced this pull request Oct 19, 2026
…them

Port of the dotnet#20536 compaction (feature/namespace-module-refactoring
e610d0f..c3aadc7) to all-fixes.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
xperiandri added a commit to xperiandri/fsharp that referenced this pull request Oct 23, 2026
From dotnet#20536: a comment between the namespace and the opens
left the opens in place next to their copies under the new header.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
xperiandri added a commit to xperiandri/fsharp that referenced this pull request Oct 23, 2026
This branch had only the copy without the PR link. It gives way to the bullet
the PR keeps (50669a2 of dotnet#20536).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
xperiandri added a commit to xperiandri/fsharp that referenced this pull request Oct 23, 2026
…them

Port of the dotnet#20536 compaction (feature/namespace-module-refactoring
e610d0f..c3aadc7) to all-fixes.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
xperiandri added a commit to xperiandri/fsharp that referenced this pull request Oct 23, 2026
From dotnet#20536: a comment between the namespace and the opens
left the opens in place next to their copies under the new header.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
xperiandri added a commit to xperiandri/fsharp that referenced this pull request Oct 23, 2026
This branch had only the copy without the PR link. It gives way to the bullet
the PR keeps (50669a2 of dotnet#20536).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
xperiandri added a commit to xperiandri/fsharp that referenced this pull request Oct 23, 2026
…them

Port of the dotnet#20536 compaction (feature/namespace-module-refactoring
e610d0f..c3aadc7) to all-fixes.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
xperiandri added a commit to xperiandri/fsharp that referenced this pull request Oct 29, 2026
From dotnet#20536: a comment between the namespace and the opens
left the opens in place next to their copies under the new header.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
xperiandri added a commit to xperiandri/fsharp that referenced this pull request Oct 29, 2026
This branch had only the copy without the PR link. It gives way to the bullet
the PR keeps (50669a2 of dotnet#20536).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
xperiandri added a commit to xperiandri/fsharp that referenced this pull request Oct 29, 2026
…them

Port of the dotnet#20536 compaction (feature/namespace-module-refactoring
e610d0f..c3aadc7) to all-fixes.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@xperiandri
xperiandri force-pushed the feature/namespace-module-refactoring branch from c3aadc7 to 68dd6e8 Compare October 30, 2026 01:02
xperiandri and others added 9 commits November 8, 2026 02:56
…ot-level module

With the caret on the header, 'namespace A.B' holding a single 'module C =' converts to 'module A.B.C', and 'module A.B.C' converts back. The compiler treats the two forms as the same module, so the edit only rewrites the header and moves the body by the nested module's indentation; lines inside multi-line string literals keep their text. Files with a signature file are not offered the conversion, because converting one side would unpair it from the other.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
The namespace line and the opens were deleted as one block ending at the
first kept line, so a comment between the namespace and the opens left
the opens in place while copies were added under the new header. Each
of them is now deleted on its own, with the blank lines after it.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
…e of its section

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
…enting

The lines the conversion shifts now skip lines inside multi-line strings of
inactive branches, found by lexing each inactive branch as the code it is
when taken.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…bute would not survive it

Converting a namespace with one nested module to a root module is no longer
offered when
- an `#if` stands on or above the module header line. The tree shows only
  what the active branches hold, so a `type` or an `open` in another branch
  stayed above `module A.B.C`, and the file broke once that branch was taken;
- an attribute of the module resolves a name through an `open` that moves
  below the header. The attributes of a root module are checked above those
  opens, so the file is type-checked and each name the attributes look up has
  to resolve to the same symbol at the namespace line;
- anything but a line comment follows a moved `open` on its line.

A line comment after a moved `open` now moves with it instead of being deleted
with the line.

In an inactive branch, a line that reads like a directive inside a multi-line
string no longer resets the tokenizer that tracks the string, so the lines
after it keep their indentation like the rest of the literal.

The tests now type-check every converted document.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…them

Delete the implementation comments (21 lines in the refactoring, 2 in its
tests) and let names, functions and the complement of a predicate carry what
they said:

- hasIfDirectiveOnOrAbove and isDirectiveRecognizedWhileSkipping name the two
  directive checks
- startsInsideString is the complement of isCodeCommentOrSkippedState, so the
  string states the public enum does not name need no remark
- tokenizerWithDefines / tokenizerAsIfBranchTaken and their states say how
  linesInsideInactiveLiterals lexes an inactive branch
- isNamedArgumentTarget and isConstantOperator split attributesKeepMeaningAt
  (was attributesResolveAt) into its two exemptions
- removedLineRangesWithBlankLinesAfter and insertOpensBelowHeader are the two
  steps of the namespace-to-root-module edit that had a comment each

Behaviour is unchanged.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Replaying the branch onto main left some files short of the branch's own content; this restores it.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
… name over several lines

A `namespace` with no identifier made the shape check call List.last on an empty list, so Ctrl+. threw. A name split over lines
(`namespace A.` then `B` below) left the second part behind when the header line was removed, which does not parse.
Both now offer no conversion.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@xperiandri
xperiandri force-pushed the feature/namespace-module-refactoring branch from 68dd6e8 to 5298973 Compare November 8, 2026 03:13
@xperiandri
xperiandri force-pushed the feature/namespace-module-refactoring branch from 5298973 to 5abae71 Compare November 18, 2026 22:00

@T-Gro T-Gro left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖🕵️

(HashSet<int>(), parseTree)
||> ParsedInput.fold (fun lines _ node ->
match node with
| SyntaxNode.SynExpr(SynExpr.Const(range = m))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖🕵️ Re-indents multi-line strings in attribute / TP args, so the value changes:

// before
module C =
    [<Obsolete("""a
    b""")>]
// after
[<Obsolete("""a
b""")>]

Fix: find string lines by tokenizer only, drop ParsedInput.fold.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@xperiandri would you be able to fix this finding soon? Let's get this PR merged :)

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

⚠️ Affects-Build-Infra Tooling check: PR touches build infrastructure ⚠️ Affects-Design-Time Tooling check: PR touches type providers or dependency manager vsintegration-only Changes only Visual Studio integration, plus optional docs, release notes, or tests

Projects

Status: In Progress

Development

Successfully merging this pull request may close these issues.

4 participants