-
Notifications
You must be signed in to change notification settings - Fork 519
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[RGent] Add name and namespace to the codechanges struct. (#21857)
We add the name and a collection with the namespaces that contains the type. This way we do not need to query the symbol when generating code and we can simply use the data in the structure. --------- Co-authored-by: Copilot <[email protected]> Co-authored-by: GitHub Actions Autoformatter <[email protected]>
- Loading branch information
1 parent
f6d86a2
commit 0bedb2c
Showing
9 changed files
with
260 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
src/rgen/Microsoft.Macios.Generator/Extensions/SemanticModelExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using System.Collections.Immutable; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
|
||
namespace Microsoft.Macios.Generator.Extensions; | ||
|
||
public static class SemanticModelExtensions { | ||
/// <summary> | ||
/// Returns the name and namespace of the symbol that has been declared in the passed base type declaration | ||
/// syntax node. | ||
/// </summary> | ||
/// <param name="self">The current semantic model.</param> | ||
/// <param name="declaration">The named type declaration syntaxt.</param> | ||
/// <returns>A tuple containing the name and namespace of the type. If they could not be calculated they will | ||
/// be set to be string.Empty.</returns> | ||
public static (string Name, ImmutableArray<string> Namespace) GetNameAndNamespace (this SemanticModel self, | ||
BaseTypeDeclarationSyntax declaration) | ||
{ | ||
var symbol = self.GetDeclaredSymbol (declaration); | ||
var name = symbol?.Name ?? string.Empty; | ||
var bucket = ImmutableArray.CreateBuilder<string> (); | ||
var ns = symbol?.ContainingNamespace; | ||
while (ns is not null) { | ||
if (!string.IsNullOrWhiteSpace (ns.Name)) | ||
// prepend the namespace so that we can read from top to bottom | ||
bucket.Insert (0, ns.Name); | ||
ns = ns.ContainingNamespace; | ||
} | ||
return (name, bucket.ToImmutableArray ()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
This comment was marked as outdated.
Sorry, something went wrong.
This comment was marked as outdated.
Sorry, something went wrong.
This comment was marked as outdated.
Sorry, something went wrong.
This comment was marked as outdated.
Sorry, something went wrong.
This comment was marked as outdated.
Sorry, something went wrong.
This comment was marked as outdated.
Sorry, something went wrong.
This comment was marked as outdated.
Sorry, something went wrong.
This comment was marked as outdated.
Sorry, something went wrong.
This comment was marked as outdated.
Sorry, something went wrong.