Skip to content

Commit

Permalink
! Refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
wmjordan committed Nov 24, 2024
1 parent 69dec29 commit db119bf
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 51 deletions.
3 changes: 2 additions & 1 deletion Codist/AutoBuildVersion/SolutionDocumentEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ public int OnActiveProjectCfgChange(IVsHierarchy vsHierarchy) {
if (vsHierarchy != null) {
Project p = vsHierarchy.GetExtObjectAs<Project>();
if (p != null) {
WriteText($"Configuration of {p.UniqueName} changed to {p.ConfigurationManager.ActiveConfiguration.ConfigurationName}|{p.ConfigurationManager.ActiveConfiguration.PlatformName}.");
var c = p.ConfigurationManager.ActiveConfiguration;
WriteText($"Configuration of {p.UniqueName} changed to {c.ConfigurationName}|{c.PlatformName}.");
}
}
#endif
Expand Down
4 changes: 2 additions & 2 deletions Codist/Controls/CSharpSymbolContextMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ bool IsExternallyCallable(MethodKind methodKind) {
}

void CreateCommandForNamedType(INamedTypeSymbol t) {
if (t.TypeKind.CeqAny(TypeKind.Class, TypeKind.Struct)) {
if (t.IsAnyKind(TypeKind.Class, TypeKind.Struct)) {
var ctor = _Host.Node?.GetObjectCreationNode();
if (ctor != null) {
var symbol = _Host.Context.SemanticModel.GetSymbolOrFirstCandidate(ctor);
Expand Down Expand Up @@ -246,7 +246,7 @@ void CreateCommandForNamedType(INamedTypeSymbol t) {
void CreateCommandsForReturnTypeCommand() {
var rt = _Host.Symbol.GetReturnType();
if (rt.SpecialType.CeqAny(SpecialType.System_Void, SpecialType.System_Object)
|| rt.TypeKind.CeqAny(TypeKind.TypeParameter, TypeKind.Error, TypeKind.Dynamic)
|| rt.IsAnyKind(TypeKind.TypeParameter, TypeKind.Error, TypeKind.Dynamic)
|| rt.IsTupleType) {
return;
}
Expand Down
Binary file modified Codist/Lib/CLR.dll
Binary file not shown.
12 changes: 5 additions & 7 deletions Codist/Margins/CSharpMargin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -439,11 +439,11 @@ void DrawRegions(DrawingContext drawingContext, PenStore penStore, int labelSize
}

static bool IsType(CodeMemberType type) {
return type > CodeMemberType.Root && type < CodeMemberType.Member;
return type.IsInside(CodeMemberType.Root, CodeMemberType.Member);
}

static bool IsMember(CodeMemberType type) {
return type > CodeMemberType.Member && type < CodeMemberType.Other;
return type.IsInside(CodeMemberType.Member, CodeMemberType.Other);
}

[DebuggerDisplay("{GetDebuggerString()}")]
Expand Down Expand Up @@ -651,13 +651,11 @@ public async Task<bool> UpdateAsync(SemanticState state, CancellationToken cance
return false;
}
if (symbol == null) {
if (_References != null) {
_References = null;
return true;
}
else {
if (_References == null) {
return false;
}
_References = null;
return true;
}
_References = GetReferenceItems(
await SymbolFinder.FindReferencesAsync(symbol.GetAliasTarget(), state.Document.Project.Solution, ImmutableSortedSet.Create(state.Document), cancellationToken).ConfigureAwait(false),
Expand Down
34 changes: 16 additions & 18 deletions Codist/QuickInfo/CSharpQuickInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -459,48 +459,46 @@ static ISymbol GetSymbol(SemanticModel semanticModel, SyntaxNode node, ref Immut
return semanticModel.GetDeclaredSymbol(node, cancellationToken);
}
if (node is QueryClauseSyntax q) {
if (node is OrderByClauseSyntax o && o.Orderings.Count != 0) {
return semanticModel.GetSymbolInfo(o.Orderings[0], cancellationToken).Symbol;
}
return semanticModel.GetQueryClauseInfo(q, cancellationToken).OperationInfo.Symbol;
return node is OrderByClauseSyntax o && o.Orderings.Count != 0
? semanticModel.GetSymbolInfo(o.Orderings[0], cancellationToken).Symbol
: semanticModel.GetQueryClauseInfo(q, cancellationToken).OperationInfo.Symbol;
}
var symbolInfo = semanticModel.GetSymbolInfo(node, cancellationToken);
if (symbolInfo.CandidateReason != CandidateReason.None) {
candidates = symbolInfo.CandidateSymbols;
return symbolInfo.CandidateSymbols.FirstOrDefault();
return (candidates = symbolInfo.CandidateSymbols).FirstOrDefault();
}
return symbolInfo.Symbol
?? (kind.IsDeclaration()
|| kind == SyntaxKind.VariableDeclarator
|| kind == SyntaxKind.CatchDeclaration
|| kind.CeqAny(SyntaxKind.VariableDeclarator, SyntaxKind.CatchDeclaration)
|| kind == SyntaxKind.SingleVariableDesignation
&& node.Parent.IsAnyKind(SyntaxKind.DeclarationExpression, SyntaxKind.DeclarationPattern, SyntaxKind.ParenthesizedVariableDesignation)
? semanticModel.GetDeclaredSymbol(node, cancellationToken)
// : kind == SyntaxKind.ArrowExpressionClause
// ? semanticModel.GetDeclaredSymbol(node.Parent, cancellationToken)
: kind == SyntaxKind.IdentifierName && node.Parent.IsKind(SyntaxKind.NameEquals) && (node = node.Parent.Parent) != null && node.IsKind(SyntaxKind.UsingDirective)
: kind == SyntaxKind.IdentifierName && node.Parent.IsKind(SyntaxKind.NameEquals) && (node = node.Parent.Parent).IsKind(SyntaxKind.UsingDirective)
? semanticModel.GetDeclaredSymbol(node, cancellationToken)?.GetAliasTarget()
: semanticModel.GetSymbolExt(node, cancellationToken));
}

static void LocateNodeInParameterList(ref SyntaxNode node, ref SyntaxToken token) {
if (node.IsKind(SyntaxKind.Argument)) {
node = ((ArgumentSyntax)node).Expression;
return;
}
else if (node.IsKind(SyntaxKind.ArgumentList)) {
if (node.IsKind(SyntaxKind.ArgumentList)) {
var al = node as ArgumentListSyntax;
if (al.OpenParenToken == token) {
node = al.Arguments.FirstOrDefault() ?? node;
return;
}
else if (al.CloseParenToken == token) {
if (al.CloseParenToken == token) {
node = al.Arguments.LastOrDefault() ?? node;
return;
}
else {
foreach (var item in al.Arguments) {
if (item.FullSpan.Contains(token.SpanStart, true)) {
node = item;
break;
}
foreach (var item in al.Arguments) {
if (item.FullSpan.Contains(token.SpanStart, true)) {
node = item;
return;
}
}
}
Expand Down Expand Up @@ -943,7 +941,7 @@ void ShowTypeInfo(InfoContainer qiContent, SyntaxNode node, INamedTypeSymbol typ
&& typeSymbol.TypeParameters[0] != typeSymbol.TypeArguments[0]) {
ShowTypeArguments(qiContent, typeSymbol.TypeArguments, typeSymbol.TypeParameters);
}
if (typeSymbol.TypeKind.CeqAny(TypeKind.Class, TypeKind.Struct)
if (typeSymbol.IsAnyKind(TypeKind.Class, TypeKind.Struct)
&& options.MatchFlags(QuickInfoOptions.MethodOverload)) {
if (node.IsAnyKind(SyntaxKind.ClassDeclaration, SyntaxKind.StructDeclaration)
&& ((TypeDeclarationSyntax)node).GetParameterList() != null) {
Expand Down
13 changes: 6 additions & 7 deletions Codist/Refactorings/RefactoringAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,13 @@ public RefactoringAction(ActionType action, List<SyntaxNode> delete, List<Syntax
ActionType = action;
Original = delete;
Insert = insert;
if (insert != null) {
Annotation = new SyntaxAnnotation();
for (int i = 0; i < insert.Count; i++) {
insert[i] = insert[i].WithAdditionalAnnotations(Annotation);
}
}
else {
if (insert == null) {
Annotation = null;
return;
}
Annotation = new SyntaxAnnotation();
for (int i = 0; i < insert.Count; i++) {
insert[i] = insert[i].WithAdditionalAnnotations(Annotation);
}
}

Expand Down
4 changes: 2 additions & 2 deletions Codist/Refactorings/ReplaceToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public override bool Accept(RefactoringContext ctx) {
if (token.IsAnyKind(SyntaxKind.NullKeyword, SyntaxKind.DefaultKeyword)
&& ctx.Node.FirstAncestorOrSelf<SyntaxNode>(n => n.IsKind(SyntaxKind.ParameterList)) == null
&& ctx.SemanticContext.SemanticModel.GetTypeInfo(ctx.Node).ConvertedType is ITypeSymbol type) {
if (type.TypeKind.CeqAny(TypeKind.Class, TypeKind.Struct)) {
if (type.IsAnyKind(TypeKind.Class, TypeKind.Struct)) {
switch (type.SpecialType) {
case SpecialType.System_Boolean:
_Title = R.CMD_UseDefault.Replace("default", "false");
Expand Down Expand Up @@ -115,7 +115,7 @@ protected override string GetReplacement(SemanticContext ctx, SyntaxToken token)
if (!(ctx.SemanticModel.GetTypeInfo(ctx.Node).ConvertedType is ITypeSymbol type)) {
return String.Empty;
}
if (type.TypeKind.CeqAny(TypeKind.Class, TypeKind.Struct)) {
if (type.IsAnyKind(TypeKind.Class, TypeKind.Struct)) {
switch (type.SpecialType) {
case SpecialType.System_Boolean:
return "false";
Expand Down
27 changes: 14 additions & 13 deletions Codist/Semantics/CodeAnalysisHelper.Node.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,31 @@ partial class CodeAnalysisHelper
{
#region Node info
public static bool IsAnyKind(this SyntaxNode node, SyntaxKind kind1, SyntaxKind kind2) {
return Op.Cast<int, SyntaxKind>(node.RawKind).CeqAny(kind1, kind2);
return node.RawKind.CeqAny(kind1, kind2);
}
public static bool IsAnyKind(this SyntaxNode node, SyntaxKind kind1, SyntaxKind kind2, SyntaxKind kind3) {
return Op.Cast<int, SyntaxKind>(node.RawKind).CeqAny(kind1, kind2, kind3);
return node.RawKind.CeqAny(kind1, kind2, kind3);
}
public static bool IsAnyKind(this SyntaxNode node, SyntaxKind kind1, SyntaxKind kind2, SyntaxKind kind3, SyntaxKind kind4) {
return Op.Cast<int, SyntaxKind>(node.RawKind).CeqAny(kind1, kind2, kind3, kind4);
return node.RawKind.CeqAny(kind1, kind2, kind3, kind4);
}
public static bool IsAnyKind(this SyntaxToken token, SyntaxKind kind1, SyntaxKind kind2) {
return Op.Cast<int, SyntaxKind>(token.RawKind).CeqAny(kind1, kind2);
return token.RawKind.CeqAny(kind1, kind2);
}
public static bool IsAnyKind(this SyntaxToken token, SyntaxKind kind1, SyntaxKind kind2, SyntaxKind kind3) {
return Op.Cast<int, SyntaxKind>(token.RawKind).CeqAny(kind1, kind2, kind3);
return token.RawKind.CeqAny(kind1, kind2, kind3);
}
public static bool IsAnyKind(this SyntaxToken token, SyntaxKind kind1, SyntaxKind kind2, SyntaxKind kind3, SyntaxKind kind4) {
return Op.Cast<int, SyntaxKind>(token.RawKind).CeqAny(kind1, kind2, kind3, kind4);
return token.RawKind.CeqAny(kind1, kind2, kind3, kind4);
}
public static bool IsAnyKind(this ITypeSymbol type, TypeKind kind1, TypeKind kind2) {
return type.TypeKind.CeqAny(kind1, kind2);
}
public static bool IsAnyKind(this ITypeSymbol type, TypeKind kind1, TypeKind kind2, TypeKind kind3) {
return type.TypeKind.CeqAny(kind1, kind2, kind3);
}
public static bool IsPredefinedSystemType(this SyntaxKind kind) {
return kind >= SyntaxKind.BoolKeyword && kind <= SyntaxKind.ObjectKeyword;
return kind.IsBetween(SyntaxKind.BoolKeyword, SyntaxKind.ObjectKeyword);
}
public static bool IsDeclaration(this SyntaxKind kind) {
switch (kind) {
Expand Down Expand Up @@ -148,12 +154,7 @@ public static bool IsNonDelegateTypeDeclaration(this SyntaxKind kind) {
return false;
}
public static bool IsNamespaceDeclaration(this SyntaxKind kind) {
switch (kind) {
case SyntaxKind.NamespaceDeclaration:
case FileScopedNamespaceDeclaration:
return true;
}
return false;
return kind.CeqAny(SyntaxKind.NamespaceDeclaration, FileScopedNamespaceDeclaration);
}
public static bool IsMemberDeclaration(this SyntaxKind kind) {
switch (kind) {
Expand Down
2 changes: 1 addition & 1 deletion Codist/Semantics/XmlDocParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ void InheritDocumentation(ISymbol symbol, ISymbol querySymbol) {
}
}
}
if (t.BaseType != null && t.TypeKind.CeqAny(TypeKind.Class, TypeKind.Struct)) {
if (t.BaseType != null && t.IsAnyKind(TypeKind.Class, TypeKind.Struct)) {
InheritDocumentation(t.BaseType, querySymbol);
}
// inherit from implemented interfaces
Expand Down

0 comments on commit db119bf

Please sign in to comment.