forked from zkSNACKs/WalletWasabi
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request zkSNACKs#10346 from ichthus1604/UI-Decoupling11
[VDG] UI Decoupling #11
- Loading branch information
Showing
23 changed files
with
737 additions
and
51 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace WalletWasabi.Fluent.Generators; | ||
|
||
public static class AnalyzerExtensions | ||
{ | ||
public static List<IdentifierNameSyntax> GetUiContextReferences(this SyntaxNode node, SemanticModel semanticModel) | ||
{ | ||
return | ||
node.DescendantNodes() | ||
.OfType<IdentifierNameSyntax>() | ||
.Where(x => x.Identifier.ValueText == "UiContext") // faster verification | ||
.Where(x => semanticModel.GetTypeInfo(x).Type?.ToDisplayString() == UiContextAnalyzer.UiContextType) // slower, but safer. Only runs if previous verification passed. | ||
.ToList(); | ||
} | ||
|
||
public static bool IsPrivate(this ConstructorDeclarationSyntax node) | ||
{ | ||
return node.Modifiers.Any(m => m.IsKind(SyntaxKind.PrivateKeyword)); | ||
} | ||
|
||
public static bool IsPublic(this ConstructorDeclarationSyntax node) | ||
{ | ||
return node.Modifiers.Any(m => m.IsKind(SyntaxKind.PublicKeyword)); | ||
} | ||
|
||
public static bool IsSourceGenerated(this SyntaxNode node) | ||
{ | ||
var filePath = node.SyntaxTree.FilePath; | ||
|
||
return filePath is null || | ||
filePath.EndsWith(UiContextAnalyzer.UiContextFileSuffix); | ||
} | ||
|
||
public static bool IsSubTypeOf(this SyntaxNode node, SemanticModel model, string baseType) | ||
{ | ||
if (node is not ClassDeclarationSyntax cls) | ||
{ | ||
return false; | ||
} | ||
|
||
var currentType = model.GetDeclaredSymbol(cls); | ||
while (currentType != null) | ||
{ | ||
if (currentType.ToDisplayString() == baseType) | ||
{ | ||
return true; | ||
} | ||
currentType = currentType.BaseType; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public static bool IsAbstractClass(this ClassDeclarationSyntax cls, SemanticModel model) | ||
{ | ||
var typeInfo = | ||
model.GetDeclaredSymbol(cls) | ||
?? throw new InvalidOperationException($"Unable to get Declared Symbol: {cls.Identifier}"); | ||
|
||
return typeInfo.IsAbstract; | ||
} | ||
|
||
public static bool IsRoutableViewModel(this SyntaxNode node, SemanticModel model) | ||
{ | ||
return node.IsSubTypeOf(model, "WalletWasabi.Fluent.ViewModels.Navigation.RoutableViewModel"); | ||
} | ||
|
||
public static bool HasUiContextParameter(this ConstructorDeclarationSyntax ctor, SemanticModel model) | ||
{ | ||
return ctor.ParameterList.Parameters.Any(p => p.Type.IsUiContextType(model)); | ||
} | ||
|
||
public static bool IsUiContextType(this TypeSyntax? typeSyntax, SemanticModel model) | ||
{ | ||
if (typeSyntax is null) | ||
{ | ||
return false; | ||
} | ||
|
||
return model.GetTypeInfo(typeSyntax).Type?.ToDisplayString() == UiContextAnalyzer.UiContextType; | ||
} | ||
|
||
public static List<string> GetNamespaces(this ITypeSymbol? typeSymbol) | ||
{ | ||
return GetNamespaceSymbols(typeSymbol) | ||
.Where(x => !x.IsGlobalNamespace) | ||
.Select(x => x.ToDisplayString()) | ||
.ToList(); | ||
} | ||
|
||
private static IEnumerable<INamespaceSymbol> GetNamespaceSymbols(this ITypeSymbol? typeSymbol) | ||
{ | ||
if (typeSymbol is null) | ||
{ | ||
yield break; | ||
} | ||
|
||
yield return typeSymbol.ContainingNamespace; | ||
|
||
if (typeSymbol is INamedTypeSymbol namedType) | ||
{ | ||
foreach (var typeArg in namedType.TypeArguments) | ||
{ | ||
yield return typeArg.ContainingNamespace; | ||
} | ||
} | ||
} | ||
} |
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
8 changes: 8 additions & 0 deletions
8
WalletWasabi.Fluent.Generators/Properties/launchSettings.json
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,8 @@ | ||
{ | ||
"profiles": { | ||
"Profile 1": { | ||
"commandName": "DebugRoslynComponent", | ||
"targetProject": "..\\WalletWasabi.Fluent\\WalletWasabi.Fluent.csproj" | ||
} | ||
} | ||
} |
Oops, something went wrong.