Skip to content

Commit

Permalink
Remove unnecessary SupportsGetImportScopes method in SemanticModelExt…
Browse files Browse the repository at this point in the history
  • Loading branch information
bjornhellander committed Jan 2, 2024
1 parent e600169 commit a7f11d5
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,8 @@ private static bool ContainsUsingAliasNoCache(SyntaxTree tree, SemanticModel sem
}

// Check for global using aliases
if (SemanticModelExtensions.SupportsGetImportScopes)
{
var scopes = semanticModel.GetImportScopes(0, cancellationToken);
return scopes.Any(x => x.Aliases.Length > 0);
}

return false;
var scopes = semanticModel.GetImportScopes(0, cancellationToken);
return scopes.Any(x => x.Aliases.Length > 0);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,39 +18,30 @@ internal static class SemanticModelExtensions

static SemanticModelExtensions()
{
CreateGetImportScopesAccessor(out var getImportScopesAccessor, out var supportsGetImportScopes);
GetImportScopesAccessor = getImportScopesAccessor;
SupportsGetImportScopes = supportsGetImportScopes;
GetImportScopesAccessor = CreateGetImportScopesAccessor();
}

public static bool SupportsGetImportScopes { get; }

public static ImmutableArray<IImportScopeWrapper> GetImportScopes(this SemanticModel semanticModel, int position, CancellationToken cancellationToken = default)
{
return GetImportScopesAccessor(semanticModel, position, cancellationToken);
}

private static void CreateGetImportScopesAccessor(
out Func<SemanticModel, int, CancellationToken, ImmutableArray<IImportScopeWrapper>> accessor,
out bool isSupported)
private static Func<SemanticModel, int, CancellationToken, ImmutableArray<IImportScopeWrapper>> CreateGetImportScopesAccessor()
{
var semanticModelType = typeof(SemanticModel);

var codeAnalysisWorkspacesAssembly = semanticModelType.GetTypeInfo().Assembly;
var nativeImportScopeType = codeAnalysisWorkspacesAssembly.GetType("Microsoft.CodeAnalysis.IImportScope");
if (nativeImportScopeType == null)
{
accessor = FallbackAccessor;
isSupported = false;
return;
return FallbackAccessor;
}

var method = semanticModelType.GetTypeInfo().GetDeclaredMethods("GetImportScopes").SingleOrDefault(IsCorrectGetImportScopesMethod);
if (method == null)
{
accessor = FallbackAccessor;
isSupported = false;
return;
// This should not happen, since this missing method and the type we successfully managed to retrieve above was added in the same Roslyn version
return FallbackAccessor;
}

var importScopeWrapperFromObjectMethod = typeof(IImportScopeWrapper).GetTypeInfo().GetDeclaredMethod("FromObject");
Expand Down Expand Up @@ -134,8 +125,8 @@ private static void CreateGetImportScopesAccessor(
block,
new[] { semanticModelParameter, positionParameter, cancellationTokenParameter });

accessor = lambda.Compile();
isSupported = true;
var accessor = lambda.Compile();
return accessor;

static bool IsCorrectGetImportScopesMethod(MethodInfo method)
{
Expand All @@ -153,8 +144,6 @@ static bool IsCorrectCreateBuilderMethod(MethodInfo method)

static ImmutableArray<IImportScopeWrapper> FallbackAccessor(SemanticModel semanticModel, int position, CancellationToken cancellationToken)
{
Debug.Assert(false, $"This method should not have been called. Check {nameof(SupportsGetImportScopes)} first!");

return ImmutableArray<IImportScopeWrapper>.Empty;
}
}
Expand Down

0 comments on commit a7f11d5

Please sign in to comment.