-
Notifications
You must be signed in to change notification settings - Fork 0
feat(hermtic): fix some edge cases and cleanup bootstrapping after ne… #14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,32 @@ | ||
| using Scribe.Ink.Shapes.Fixes; | ||
| using Scribe.Shapes; | ||
|
|
||
| namespace Scribe.Ink.Shapes; | ||
|
|
||
| /// <summary> | ||
| /// Maps a <see cref="FixKind"/> to its concrete <see cref="IShapeFix"/> | ||
| /// implementation. Shared between <see cref="ShapeCodeFixProvider"/> and | ||
| /// <see cref="ShapeFixAllProvider"/>. | ||
| /// </summary> | ||
| internal static class FixResolver | ||
| { | ||
| public static IShapeFix? Resolve(FixKind kind) => kind switch | ||
| { | ||
| FixKind.AddPartialModifier => new AddPartialModifierFix(), | ||
| FixKind.AddSealedModifier => new AddSealedModifierFix(), | ||
| FixKind.AddInterfaceToBaseList => new AddInterfaceToBaseListFix(), | ||
| FixKind.AddAttribute => new AddAttributeFix(), | ||
| FixKind.AddAbstractModifier => new AddAbstractModifierFix(), | ||
| FixKind.RemoveAbstractModifier => new RemoveAbstractModifierFix(), | ||
| FixKind.AddStaticModifier => new AddStaticModifierFix(), | ||
| FixKind.AddBaseClass => new AddBaseClassFix(), | ||
| FixKind.RemoveFromBaseList => new RemoveFromBaseListFix(), | ||
| FixKind.RemovePartialModifier => new RemovePartialModifierFix(), | ||
| FixKind.RemoveSealedModifier => new RemoveSealedModifierFix(), | ||
| FixKind.RemoveStaticModifier => new RemoveStaticModifierFix(), | ||
| FixKind.RemoveAttribute => new RemoveAttributeFix(), | ||
| FixKind.SetVisibility => new SetVisibilityFix(), | ||
| FixKind.AddParameterlessConstructor => new AddParameterlessConstructorFix(), | ||
| _ => null, | ||
| }; | ||
| } | ||
This file contains hidden or 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,61 @@ | ||
| using System.Linq; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.CodeAnalysis; | ||
| using Microsoft.CodeAnalysis.CSharp; | ||
| using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
|
|
||
| namespace Scribe.Ink.Shapes.Fixes; | ||
|
|
||
| internal sealed class AddBaseClassFix : IShapeFix | ||
| { | ||
| public string Title(Diagnostic diagnostic) | ||
| { | ||
| var name = BaseClassName(diagnostic); | ||
| return name is null ? "Add required base class" : $"Extend '{name}'"; | ||
| } | ||
|
|
||
| public async Task<Document> FixAsync( | ||
| Document document, | ||
| TypeDeclarationSyntax typeDecl, | ||
| Diagnostic diagnostic, | ||
| CancellationToken ct) | ||
| { | ||
| var baseClassName = BaseClassName(diagnostic); | ||
| if (baseClassName is null) | ||
| { | ||
| return document; | ||
| } | ||
|
|
||
| var root = await document.GetSyntaxRootAsync(ct).ConfigureAwait(false); | ||
| if (root is null) | ||
| { | ||
| return document; | ||
| } | ||
|
|
||
| var baseType = SyntaxFactory.SimpleBaseType(SyntaxFactory.ParseTypeName(baseClassName)); | ||
|
|
||
| // Base class must come first in C#'s base list. | ||
| TypeDeclarationSyntax newTypeDecl; | ||
| if (typeDecl.BaseList is null) | ||
| { | ||
| newTypeDecl = typeDecl.WithBaseList(SyntaxFactory.BaseList( | ||
| SyntaxFactory.SingletonSeparatedList<BaseTypeSyntax>(baseType))); | ||
| } | ||
| else | ||
| { | ||
| var existing = typeDecl.BaseList.Types; | ||
| var withBase = existing.Insert(0, baseType); | ||
| newTypeDecl = typeDecl.WithBaseList(typeDecl.BaseList.WithTypes(withBase)); | ||
| } | ||
|
|
||
| return document.WithSyntaxRoot(root.ReplaceNode(typeDecl, newTypeDecl)); | ||
| } | ||
|
|
||
| private static string? BaseClassName(Diagnostic diagnostic) | ||
| { | ||
| return diagnostic.Properties.TryGetValue("baseClass", out var name) && !string.IsNullOrEmpty(name) | ||
| ? name | ||
| : null; | ||
| } | ||
| } |
This file contains hidden or 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,48 @@ | ||||
| using System.Linq; | ||||
|
||||
| using System.Linq; |
This file contains hidden or 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,47 @@ | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.CodeAnalysis; | ||
| using Microsoft.CodeAnalysis.CSharp; | ||
| using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
|
|
||
| namespace Scribe.Ink.Shapes.Fixes; | ||
|
|
||
| internal sealed class AddStaticModifierFix : IShapeFix | ||
| { | ||
| public string Title(Diagnostic _) => "Add 'static' modifier"; | ||
|
|
||
| public async Task<Document> FixAsync( | ||
| Document document, | ||
| TypeDeclarationSyntax typeDecl, | ||
| Diagnostic _, | ||
| CancellationToken ct) | ||
| { | ||
| if (typeDecl.Modifiers.Any(SyntaxKind.StaticKeyword)) | ||
| { | ||
| return document; | ||
| } | ||
|
|
||
| var root = await document.GetSyntaxRootAsync(ct).ConfigureAwait(false); | ||
| if (root is null) | ||
| { | ||
| return document; | ||
| } | ||
|
|
||
| var staticToken = SyntaxFactory.Token(SyntaxKind.StaticKeyword) | ||
| .WithTrailingTrivia(SyntaxFactory.Space); | ||
|
|
||
| var modifiers = typeDecl.Modifiers; | ||
| var insertAt = modifiers.Count; | ||
| for (var i = 0; i < modifiers.Count; i++) | ||
| { | ||
| if (modifiers[i].IsKind(SyntaxKind.PartialKeyword)) | ||
| { | ||
| insertAt = i; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| var newTypeDecl = typeDecl.WithModifiers(modifiers.Insert(insertAt, staticToken)); | ||
| return document.WithSyntaxRoot(root.ReplaceNode(typeDecl, newTypeDecl)); | ||
| } | ||
| } |
This file contains hidden or 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,35 @@ | ||
| using System.Linq; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.CodeAnalysis; | ||
| using Microsoft.CodeAnalysis.CSharp; | ||
| using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
|
|
||
| namespace Scribe.Ink.Shapes.Fixes; | ||
|
|
||
| internal sealed class RemoveAbstractModifierFix : IShapeFix | ||
| { | ||
| public string Title(Diagnostic _) => "Remove 'abstract' modifier"; | ||
|
|
||
| public async Task<Document> FixAsync( | ||
| Document document, | ||
| TypeDeclarationSyntax typeDecl, | ||
| Diagnostic _, | ||
| CancellationToken ct) | ||
| { | ||
| var abstractToken = typeDecl.Modifiers.FirstOrDefault(m => m.IsKind(SyntaxKind.AbstractKeyword)); | ||
| if (abstractToken == default) | ||
| { | ||
| return document; | ||
| } | ||
|
|
||
| var root = await document.GetSyntaxRootAsync(ct).ConfigureAwait(false); | ||
| if (root is null) | ||
| { | ||
| return document; | ||
| } | ||
|
|
||
| var newTypeDecl = typeDecl.WithModifiers(typeDecl.Modifiers.Remove(abstractToken)); | ||
| return document.WithSyntaxRoot(root.ReplaceNode(typeDecl, newTypeDecl)); | ||
| } | ||
| } |
This file contains hidden or 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,94 @@ | ||
| using System; | ||
| using System.Linq; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.CodeAnalysis; | ||
| using Microsoft.CodeAnalysis.CSharp; | ||
| using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
|
|
||
| namespace Scribe.Ink.Shapes.Fixes; | ||
|
|
||
| internal sealed class RemoveAttributeFix : IShapeFix | ||
| { | ||
| public string Title(Diagnostic diagnostic) | ||
| { | ||
| var name = AttributeName(diagnostic); | ||
| return name is null ? "Remove forbidden attribute" : $"Remove '[{Shorten(name)}]'"; | ||
| } | ||
|
|
||
| public async Task<Document> FixAsync( | ||
| Document document, | ||
| TypeDeclarationSyntax typeDecl, | ||
| Diagnostic diagnostic, | ||
| CancellationToken ct) | ||
| { | ||
| var target = AttributeName(diagnostic); | ||
| if (target is null) | ||
| { | ||
| return document; | ||
| } | ||
|
|
||
| var shortName = Shorten(target); | ||
| var fullName = target; | ||
|
|
||
| var root = await document.GetSyntaxRootAsync(ct).ConfigureAwait(false); | ||
| if (root is null) | ||
| { | ||
| return document; | ||
| } | ||
|
|
||
| var newTypeDecl = typeDecl; | ||
|
|
||
| foreach (var list in typeDecl.AttributeLists) | ||
| { | ||
| var keepAttrs = list.Attributes | ||
| .Where(a => !Matches(a, fullName, shortName)) | ||
| .ToArray(); | ||
|
|
||
| if (keepAttrs.Length == list.Attributes.Count) | ||
| { | ||
| continue; | ||
| } | ||
|
|
||
| if (keepAttrs.Length == 0) | ||
| { | ||
| newTypeDecl = newTypeDecl.WithAttributeLists( | ||
| newTypeDecl.AttributeLists.Remove(list)); | ||
| } | ||
| else | ||
| { | ||
| var newList = list.WithAttributes(SyntaxFactory.SeparatedList(keepAttrs)); | ||
| newTypeDecl = newTypeDecl.WithAttributeLists( | ||
| newTypeDecl.AttributeLists.Replace(list, newList)); | ||
| } | ||
| } | ||
|
|
||
| return document.WithSyntaxRoot(root.ReplaceNode(typeDecl, newTypeDecl)); | ||
| } | ||
|
|
||
| private static bool Matches(AttributeSyntax attribute, string full, string shortName) | ||
| { | ||
| var text = attribute.Name.ToString(); | ||
| var bareShort = Shorten(shortName); | ||
| return string.Equals(text, full, StringComparison.Ordinal) | ||
| || string.Equals(text, shortName, StringComparison.Ordinal) | ||
| || string.Equals(text, bareShort, StringComparison.Ordinal) | ||
| || text.EndsWith("." + shortName, StringComparison.Ordinal) | ||
| || text.EndsWith("." + bareShort, StringComparison.Ordinal); | ||
| } | ||
|
|
||
| private static string? AttributeName(Diagnostic diagnostic) | ||
| { | ||
| return diagnostic.Properties.TryGetValue("attribute", out var name) && !string.IsNullOrEmpty(name) | ||
| ? name | ||
| : null; | ||
| } | ||
|
|
||
| private static string Shorten(string name) | ||
| { | ||
| const string Suffix = "Attribute"; | ||
| return name.EndsWith(Suffix, StringComparison.Ordinal) | ||
| ? name.Substring(0, name.Length - Suffix.Length) | ||
| : name; | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FixResolverreferences FixKind members (e.g., RemovePartialModifier/SetVisibility/AddParameterlessConstructor) that are not present inScribe.Shapes.FixKindin this branch, which will not compile and also prevents diagnostics from encoding these fix kinds consistently. Update the FixKind enum (and any analyzer emission) so every case handled here exists and is stable.