Filed on behalf of a customer from interviews
Syncing namespace with folder structure would also be very good.
Also something I should probably create a new issues for: the ability to override the default namespace at a per folder level.
Option to treat the folder as the namespaces, so if I move a file to a new location. It offer the option to change the namespace use within the file.
But this as the issue, how to handle multiple different namespace with the same file.
namespace Foo { }
namespace Bar { }
@AdamSpeight2008,
Why would you have two namespaces in one file? :confused:
Why? cos I can. Maybe I'm just quickly trying stuff out, does warrant multiple files.
The grammars allow them.
I think this should actually be an analyzer that tells you the filename doesn't match, which can be turned down to a Hint level. (And I thought I had filed it ages ago)
@Pilchie,
Yes it should be an analyzer (that can be turned up to Error level :grinning:) as well as being a code-fix that renames the file.
Generic file names must be supported though, eg SomeType{T}.cs and SomeType.cs should both be deemed to match the name of the SomeType<T> type and neither should result in the analyzer complaining.
@Pilchie @DavidArno
What happens in the case of multiple classes / namespace in a single file?
@AdamSpeight2008,
I'd assume the analyzer would just ignore such files as there isn't really anything meaningful that can be done with them, with regard to synchronising names.
@DavidArno @AdamSpeight2008 @Pilchie
What about a refactoring to move all other class declarations to a new file of the same name and where filename conflicts occur either suffix the filename or provide some form of conflict resolution to the user?
@Giftednewt,
That sounds like a different analyzer/fixer: one that reports on more than one type being defined in one file with a "move to new file" fix.
@DavidArno,
Yes I agree, this would be a separate analyzer/fixer. My thinking was along the lines of how handling of multiple namespaces/classes in a single file might be handled as it wouldn't be done within the proposed analyzer/fixer. I'll add it as a separate feature request (EDIT: looks like this already exists in the form of #8932) .
I'd also agree that the analyzer/fixer you're proposing should ignore any documents with multiple class definitions as there is the potential that you'll always be reporting the diagnostic.
How would partial classes be handled by the analyzer/fixer?
For example these two files in the Roslyn codebase, HideBaseCodeFixProvider.cs and HideBaseCodeFixProvider.AddNewKeywordAction.cs which refer to the same partial class but cannot be named the same based on the location in which they exist.
https://github.com/dotnet/roslyn/pull/12624 addresses atleast some portions of this workitem. It provides fixes to rename type to match file name and renaming file to match typename, if there is a single type in a file whose names don't match.
Is there a way to get the possible locations fot that refactoring as warnings / errors?
if not, I'd build an analyzer.
Most helpful comment
Syncing namespace with folder structure would also be very good.
Also something I should probably create a new issues for: the ability to override the default namespace at a per folder level.