diff --git a/Codist/Helpers/CodeAnalysisHelper.Document.cs b/Codist/Helpers/CodeAnalysisHelper.Document.cs index 341a429a..16d0b0ad 100644 --- a/Codist/Helpers/CodeAnalysisHelper.Document.cs +++ b/Codist/Helpers/CodeAnalysisHelper.Document.cs @@ -13,21 +13,12 @@ public static Document GetDocument(this Workspace workspace, ITextBuffer textBuf if (workspace == null) { throw new ArgumentNullException(nameof(workspace)); } - var solution = workspace.CurrentSolution; - if (solution == null) { - throw new InvalidOperationException("solution is null"); - } if (textBuffer == null) { throw new InvalidOperationException("textBuffer is null"); } - var textContainer = textBuffer.AsTextContainer(); - if (textContainer == null) { - throw new InvalidOperationException("textContainer is null"); - } - var docId = workspace.GetDocumentIdInCurrentContext(textContainer); - if (docId is null) { - throw new InvalidOperationException("docId is null"); - } + var solution = workspace.CurrentSolution ?? throw new InvalidOperationException("solution is null"); + var textContainer = textBuffer.AsTextContainer() ?? throw new InvalidOperationException("textContainer is null"); + var docId = workspace.GetDocumentIdInCurrentContext(textContainer) ?? throw new InvalidOperationException("docId is null"); return solution.WithDocumentText(docId, textContainer.CurrentText, PreservationMode.PreserveIdentity).GetDocument(docId); } public static Document GetDocument(this ITextBuffer textBuffer) { @@ -39,8 +30,8 @@ public static Document GetDocument(this Project project, string filePath) { /// Gets all s from a given and referencing/referenced projects. public static IEnumerable GetRelatedProjectDocuments(this Project project) { - foreach (var proj in GetRelatedProjects(project)) { - foreach (var doc in proj.Documents) { + foreach (var p in GetRelatedProjects(project)) { + foreach (var doc in p.Documents) { yield return doc; } } @@ -54,10 +45,10 @@ static HashSet GetRelatedProjects(Project project) { var projects = new HashSet(); GetRelatedProjects(project, projects); var id = project.Id; - foreach (var proj in project.Solution.Projects) { - if (projects.Contains(proj) == false - && proj.AllProjectReferences.Any(p => p.ProjectId == id)) { - projects.Add(proj); + foreach (var p in project.Solution.Projects) { + if (projects.Contains(p) == false + && p.AllProjectReferences.Any(i => i.ProjectId == id)) { + projects.Add(p); } } return projects; diff --git a/Codist/Helpers/CodeAnalysisHelper.Symbol.cs b/Codist/Helpers/CodeAnalysisHelper.Symbol.cs index 0c5fcb4c..7b483c5f 100644 --- a/Codist/Helpers/CodeAnalysisHelper.Symbol.cs +++ b/Codist/Helpers/CodeAnalysisHelper.Symbol.cs @@ -1122,26 +1122,30 @@ public static IReadOnlyList GetExplicitInterfaceImplementations(this IS public static IMethodSymbol AsMethod(this ISymbol symbol) { switch (symbol.Kind) { - case SymbolKind.Method: return symbol as IMethodSymbol; + case SymbolKind.Method: return (IMethodSymbol)symbol; case SymbolKind.Event: return ((IEventSymbol)symbol).RaiseMethod; case SymbolKind.NamedType: - var t = symbol as INamedTypeSymbol; + var t = (INamedTypeSymbol)symbol; return t.TypeKind == TypeKind.Delegate ? t.DelegateInvokeMethod : null; default: return null; } } - public static async Task GetProjectAsync(this ISymbol symbol, Solution solution, CancellationToken cancellationToken = default) { + public static Task GetProjectAsync(this ISymbol symbol, Solution solution, CancellationToken cancellationToken = default) { var asm = symbol.ContainingAssembly; - if (asm != null) { - foreach (var item in solution.Projects) { + return asm == null + ? Task.FromResult(null) + : GetProjectAsync(asm, solution, cancellationToken); + + async Task GetProjectAsync(IAssemblySymbol a, Solution s, CancellationToken ct) { + foreach (var item in s.Projects) { if (item.SupportsCompilation - && (await item.GetCompilationAsync(cancellationToken).ConfigureAwait(false)).Assembly == asm) { + && (await item.GetCompilationAsync(ct).ConfigureAwait(false)).Assembly == a) { return item; } } + return null; } - return null; } #endregion @@ -1455,7 +1459,9 @@ int GetSymbolKindSortOrder(SymbolKind x) { public static int CompareByFieldIntegerConst(ISymbol a, ISymbol b) { IFieldSymbol fa = a as IFieldSymbol, fb = b as IFieldSymbol; - return fa == null ? -1 : fb == null ? 1 : Convert.ToInt64(fa.ConstantValue).CompareTo(Convert.ToInt64(fb.ConstantValue)); + return fa == null ? -1 + : fb == null ? 1 + : Convert.ToInt64(fa.ConstantValue).CompareTo(Convert.ToInt64(fb.ConstantValue)); } public static bool IsBoundedGenericType(this INamedTypeSymbol type) { @@ -1512,13 +1518,8 @@ public static bool MatchSignature(this ISymbol symbol, SymbolKind kind, ITypeSym } } } - if (typeParameters.IsDefault == false) { - var typeParams = method.TypeParameters; - if (typeParams.Length != typeParameters.Length) { - return false; - } - } - return true; + return typeParameters.IsDefaultOrEmpty + || method.TypeParameters.Length == typeParameters.Length; } /// Returns whether a symbol could have an override. @@ -1625,7 +1626,7 @@ static Func CreateAssemblySourceTypeFunc() { il.Emit(OpCodes.Ldc_I4_2); il.Emit(OpCodes.Ret); } - return m.CreateDelegate(typeof(Func)) as Func; + return m.CreateDelegate>(); } } diff --git a/Codist/Helpers/CodeAnalysisHelper.cs b/Codist/Helpers/CodeAnalysisHelper.cs index 30eb5082..3d8e197c 100644 --- a/Codist/Helpers/CodeAnalysisHelper.cs +++ b/Codist/Helpers/CodeAnalysisHelper.cs @@ -824,6 +824,7 @@ public static string GetDeclarationSignature(this SyntaxNode node, int position return GetEndIfSignature((EndIfDirectiveTriviaSyntax)node); } return null; + string GetTypeSignature(TypeDeclarationSyntax syntax) => GetGenericSignature(syntax.Identifier.Text, syntax.Arity); string GetMethodSignature(MethodDeclarationSyntax syntax) => GetGenericSignature(syntax.Identifier.Text, syntax.Arity); string GetDelegateSignature(DelegateDeclarationSyntax syntax) => GetGenericSignature(syntax.Identifier.Text, syntax.Arity); @@ -866,7 +867,7 @@ string GetGenericAritySignature(int arity) { case 5: return "<,,,,>"; case 6: return "<,,,,,>"; case 7: return "<,,,,,,>"; - default: return "<" + new String(',', arity - 1) + ">"; + default: return "<" + new string(',', arity - 1) + ">"; } } string GetVariableSignature(VariableDeclarationSyntax syntax, int pos) { @@ -1058,8 +1059,7 @@ public static SyntaxNode GetAncestorOrSelfDeclaration(this SyntaxNode node) { /// Gets the first expression containing current node which is of type . public static TExpression GetAncestorOrSelfExpression(this SyntaxNode node) where TExpression : ExpressionSyntax { - TExpression r; - if ((r = node as TExpression) != null) { + if (node is TExpression r) { return r; } if (node is ExpressionSyntax) { @@ -1074,14 +1074,13 @@ public static TExpression GetAncestorOrSelfExpression(this SyntaxNo } /// Gets the first node containing current node which is of type and not . public static ExpressionSyntax GetLastAncestorExpressionNode(this SyntaxNode node) { - var r = node as ExpressionSyntax; - if (r == null) { - return null; - } - while (node.Parent is ExpressionSyntax n) { - node = r = n; + if (node is ExpressionSyntax r) { + while (node.Parent is ExpressionSyntax n) { + node = r = n; + } + return r; } - return r; + return null; } public static IEnumerable GetDescendantDeclarations(this SyntaxNode root, CancellationToken cancellationToken = default) { @@ -1400,26 +1399,10 @@ public static ParameterSyntax FindParameter(this BaseMethodDeclarationSyntax nod return node?.ParameterList.Parameters.FirstOrDefault(p => p.Identifier.Text == name); } public static TypeParameterSyntax FindTypeParameter(this SyntaxNode node, string name) { - TypeParameterListSyntax tp; - var m = node as MethodDeclarationSyntax; - if (m != null && m.Arity > 0) { - tp = m.TypeParameterList; - } - else { - var t = node as TypeDeclarationSyntax; - if (t != null && t.Arity > 0) { - tp = t.TypeParameterList; - } - else { - var d = node as DelegateDeclarationSyntax; - if (d != null && d.Arity > 0) { - tp = d.TypeParameterList; - } - else { - tp = null; - } - } - } + var tp = (node is MethodDeclarationSyntax m && m.Arity > 0) ? m.TypeParameterList + : node is TypeDeclarationSyntax t && t.Arity > 0 ? t.TypeParameterList + : node is DelegateDeclarationSyntax d && d.Arity > 0 ? d.TypeParameterList + : null; return tp?.Parameters.FirstOrDefault(p => p.Identifier.Text == name); } public static bool IsLineComment(this SyntaxTrivia trivia) { @@ -1481,7 +1464,9 @@ public static int GetWarningLevel(int csErrorCode) { static partial class NonPublicOrFutureAccessors { public static readonly Func GetFileScopedNamespaceName = ReflectionHelper.CreateGetPropertyMethod("Name", typeof(NamespaceDeclarationSyntax).Assembly.GetType("Microsoft.CodeAnalysis.CSharp.Syntax.FileScopedNamespaceDeclarationSyntax")); + public static readonly Func GetImplicitObjectCreationArgumentList = ReflectionHelper.CreateGetPropertyMethod("ArgumentList", typeof(ExpressionSyntax).Assembly.GetType("Microsoft.CodeAnalysis.CSharp.Syntax.BaseObjectCreationExpressionSyntax")); + public static readonly Func GetWarningLevel = ReflectionHelper.CallStaticFunc(typeof(LanguageVersionFacts).Assembly.GetType("Microsoft.CodeAnalysis.CSharp.ErrorFacts")?.GetMethod("GetWarningLevel", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic)) ?? (Func)((int _) => 3); } } diff --git a/Codist/Helpers/VsShellHelper.cs b/Codist/Helpers/VsShellHelper.cs index aa63f703..fc2f4e87 100644 --- a/Codist/Helpers/VsShellHelper.cs +++ b/Codist/Helpers/VsShellHelper.cs @@ -109,9 +109,10 @@ public static EnvDTE.Project GetProject(string projectName) { EnvDTE.Project FindProject(EnvDTE.ProjectItems items, string pp) { for (int i = 1; i <= items.Count; i++) { - var p = items.Item(i); - if (p.Object is EnvDTE.Project proj && String.Equals(proj.FullName, pp, StringComparison.OrdinalIgnoreCase)) { - return proj; + var item = items.Item(i); + if (item.Object is EnvDTE.Project p + && String.Equals(p.FullName, pp, StringComparison.OrdinalIgnoreCase)) { + return p; } } return null; diff --git a/Codist/Margins/MarginFactories.cs b/Codist/Margins/MarginFactories.cs index fcd9a67f..b11eb0c3 100644 --- a/Codist/Margins/MarginFactories.cs +++ b/Codist/Margins/MarginFactories.cs @@ -73,7 +73,10 @@ public IWpfTextViewMargin CreateMargin(IWpfTextViewHost wpfTextViewHost, IWpfTex sealed class DisableChangeTrackerMarginFactory : IWpfTextViewMarginProvider { public IWpfTextViewMargin CreateMargin(IWpfTextViewHost wpfTextViewHost, IWpfTextViewMargin marginContainer) { - return CodistPackage.VsVersion.Major >= 17 && wpfTextViewHost.TextView.TextBuffer.MayBeEditor() ? new DisableChangeTrackerMargin(marginContainer) : null; + return CodistPackage.VsVersion.Major >= 17 + && wpfTextViewHost.TextView.TextBuffer.MayBeEditor() + ? new DisableChangeTrackerMargin(marginContainer) + : null; } } diff --git a/Codist/Margins/MarkerStyle.cs b/Codist/Margins/MarkerStyle.cs index 0c1ea1dd..63a7b3ac 100644 --- a/Codist/Margins/MarkerStyle.cs +++ b/Codist/Margins/MarkerStyle.cs @@ -10,19 +10,23 @@ public sealed class MarkerStyle Color _ForeColor, _BackColor; public int Id { get => (int)StyleID; set => StyleID = (MarkerStyleTypes)value; } + internal MarkerStyleTypes StyleID { get; set; } + /// Gets or sets the foreground color to render the marker. The color format could be #RRGGBBAA or #RRGGBB. [DefaultValue(Constants.EmptyColor)] public string ForegroundColor { get => ForeColor.ToHexString(); set => UIHelper.ParseColor(value, out _ForeColor, out _); } + /// Gets or sets the foreground color to render the marker. The color format could be #RRGGBBAA or #RRGGBB. [DefaultValue(Constants.EmptyColor)] public string BackgroundColor { get => BackColor.ToHexString(); set => UIHelper.ParseColor(value, out _BackColor, out _); } + internal Color ForeColor { get => _ForeColor; set => _ForeColor = value; } internal Color BackColor { get => _BackColor; set => _BackColor = value; }