Proposal: Source modifying files - for source generation #3566
Replies: 8 comments
-
Source generators (in quite a different form than what you're proposing) are already under development. See Introducing C# Source Generators. |
Beta Was this translation helpful? Give feedback.
-
The original design of source generators allowed for the generator to modify the semantics of existing code, and that was something that the team explicitly rejected. To accomplish what you want you cannot affect existing source, but if |
Beta Was this translation helpful? Give feedback.
-
@svick , @HaloFour , this proposed feature is complementary to source generators. And via source generators, developers will generate .mod files, not modify the source .cs files. |
Beta Was this translation helpful? Give feedback.
-
You're proposing that You can certainly write a source generator that would use an external file to influence the source that it generates, but it will not be able to affect any existing source, you can only add new source. |
Beta Was this translation helpful? Give feedback.
-
One could argue that "partial" is actually a way to modify source files. But it is a limited way. It enables you to "modify" a class to add a new member. But it is limited in a way that it does not allow you to add a new line to an existing member. |
Beta Was this translation helpful? Give feedback.
-
By the way, this feature can be limited in a way to enable addition only. That is, it can be implemented in a way where only "AddText" xml elements can be used. |
Beta Was this translation helpful? Give feedback.
-
Yes, and Source Generators were explicitly designed that way because modifying existing source code makes for a terrible IDE experience like debugging, and problems with multiple generators trying to modify the same piece of code. |
Beta Was this translation helpful? Give feedback.
-
I'd suggest checking out the preview release of source generators. That's likely very close to what will ship with C# 9.0 later this year, assuming no delays. Even if there are delays the design is very, very unlikely to change much at all. Writing a source generator is not going to involve authoring XML files, nor is there going to be a way to change the source of a particular file before the compiler parses it (the generator has to be able to inspect the AST to know what to generate afterall). Also, notably, source generators are a feature of the Roslyn compiler, not of the C# language. There are no language features that are being added specifically to enable source generators. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Let me explain the proposal with an example. Let's say we have a file called SourceFile.cs with the following content (I also added line numbers):
Then, via the proposed feature, you can add a Modifier1.mod file with the following content:
Ths syntax of the mod file I put here is just from the top of my head. There might be much better syntaxes. I just want to explain the general idea.
This will not cause the SourceFile.cs file to be modified. However, when compiling, the compiler will process the "Modifier" files and "modify" the source files (not on disk so not visible in Visual Studio) before actually compiling the files. So the syntax trees that will be given to the compiler will contain the "modified" versions of the files.
In the example above, the "modified" SourceFile.cs will contain:
Tooling can be updated to show the added texts in the source files in the editor (e.g. via adornments in VS?). Of course, these pieces will be read only in the source files(in the editor, they don't exist in the source files). The developers can choose to show or hide these read-only texts.
The main use case of this feature is source generation. It will enable much more scenarios.
Source generator developers can generate these .mod files.
Beta Was this translation helpful? Give feedback.
All reactions