Skip to content

Commit 7c593f9

Browse files
committed
Psi/bind: check assembly-level AutoOpen namespaces
1 parent 995ef8c commit 7c593f9

File tree

12 files changed

+52
-24
lines changed

12 files changed

+52
-24
lines changed

ReSharper.FSharp/src/FSharp.Psi.Intentions/src/QuickFixes/ReplaceWithAbbreviatedTypeFix.fs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,6 @@ type ReplaceWithAbbreviatedTypeFix(error: TypeAbbreviationsCannotHaveAugmentatio
2323
override this.IsAvailable _ =
2424
isValid typeDecl &&
2525

26-
// todo: fix parameter list range
27-
let typeParamDeclList = typeDecl.TypeParameterDeclarationList
28-
(isNull typeParamDeclList || typeParamDeclList.TypeParametersEnumerable.IsEmpty()) &&
29-
3026
let fcsEntity = typeDecl.GetFcsSymbol().As<FSharpEntity>()
3127
isNotNull fcsEntity
3228

ReSharper.FSharp/src/FSharp.Psi.Services/src/Util/OpensUtil.fs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,8 @@ let addOpen (offset: DocumentOffset) (fsFile: IFSharpFile) (settings: IContextBo
319319
insertAfterAnchor ns anchor indent
320320

321321
let addOpens (reference: FSharpSymbolReference) (typeElement: ITypeElement) =
322+
if typeElement.IsAutoImported() then reference else
323+
322324
let referenceOwner = reference.GetElement()
323325
use writeCookie = WriteLockCookie.Create(referenceOwner.IsPhysical())
324326

ReSharper.FSharp/src/FSharp.Psi/src/Impl/FSharpImplUtil.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77
using JetBrains.Annotations;
88
using JetBrains.Diagnostics;
99
using JetBrains.Metadata.Reader.API;
10+
using JetBrains.ProjectModel;
1011
using JetBrains.ReSharper.Plugins.FSharp.Psi.Impl.Cache2;
1112
using JetBrains.ReSharper.Plugins.FSharp.Psi.Impl.Cache2.Compiled;
1213
using JetBrains.ReSharper.Plugins.FSharp.Psi.Impl.Cache2.Parts;
1314
using JetBrains.ReSharper.Plugins.FSharp.Psi.Impl.DeclaredElement;
1415
using JetBrains.ReSharper.Plugins.FSharp.Psi.Impl.DeclaredElement.CompilerGenerated;
1516
using JetBrains.ReSharper.Plugins.FSharp.Psi.Impl.Tree;
17+
using JetBrains.ReSharper.Plugins.FSharp.Psi.Metadata;
1618
using JetBrains.ReSharper.Plugins.FSharp.Psi.Parsing;
1719
using JetBrains.ReSharper.Plugins.FSharp.Psi.Tree;
1820
using JetBrains.ReSharper.Plugins.FSharp.Psi.Util;
@@ -690,6 +692,32 @@ public static bool MayHaveRequireQualifiedAccessAttribute([NotNull] this ITypeEl
690692
public static bool RequiresQualifiedAccess([NotNull] this ITypeElement typeElement) =>
691693
typeElement.GetAccessType() == ModuleMembersAccessKind.RequiresQualifiedAccess;
692694

695+
public static bool IsAutoImported([NotNull] this IClrDeclaredElement declaredElement)
696+
{
697+
var psiModule = declaredElement.Module;
698+
var autoOpenCache = psiModule.GetSolution().GetComponent<FSharpAssemblyAutoOpenCache>();
699+
var autoOpenedModules = autoOpenCache.GetAutoOpenedModules(psiModule);
700+
701+
// todo: assembly level auto open modules
702+
var ns = GetNamespace(declaredElement);
703+
return ns != null && autoOpenedModules.Contains(ns.QualifiedName);
704+
}
705+
706+
[CanBeNull]
707+
public static INamespace GetNamespace([NotNull] this IClrDeclaredElement declaredElement)
708+
{
709+
if (declaredElement is ITypeElement typeElement)
710+
return typeElement.GetContainingNamespace();
711+
712+
if (declaredElement.GetContainingType() is { } containingType)
713+
return containingType.GetContainingNamespace();
714+
715+
if (declaredElement is INamespace ns)
716+
return ns.GetContainingNamespace();
717+
718+
return null;
719+
}
720+
693721
[CanBeNull]
694722
private static T GetOutermostNode<T, TMatchingNode>([CanBeNull] this T node, bool singleLevel = false)
695723
where T : class, ITreeNode

ReSharper.FSharp/src/FSharp.Psi/src/Impl/FSharpReferenceBindingUtil.cs

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,21 +39,6 @@ public static string SuggestShortReferenceName(IDeclaredElement declaredElement,
3939
public static string SuggestShortReferenceName(string sourceName, PsiLanguageType language) =>
4040
NamingManager.GetNamingLanguageService(language).MangleNameIfNecessary(sourceName);
4141

42-
[CanBeNull]
43-
private static INamespace GetNamespace([NotNull] IClrDeclaredElement declaredElement)
44-
{
45-
if (declaredElement is ITypeElement typeElement)
46-
return typeElement.GetContainingNamespace();
47-
48-
if (declaredElement.GetContainingType() is { } containingType)
49-
return containingType.GetContainingNamespace();
50-
51-
if (declaredElement is INamespace ns)
52-
return ns.GetContainingNamespace();
53-
54-
return null;
55-
}
56-
5742
private static bool RequiresFullyQualifiedName([NotNull] IReference reference)
5843
{
5944
// Can't insert 'open' before top-level module declaration.
@@ -67,7 +52,10 @@ private static void SetNamespaceQualifierIfNeeded([NotNull] FSharpSymbolReferenc
6752
if (!RequiresFullyQualifiedName(reference))
6853
return;
6954

70-
var ns = GetNamespace(declaredElement);
55+
if (declaredElement.IsAutoImported())
56+
return;
57+
58+
var ns = declaredElement.GetNamespace();
7159
if (ns == null || ns.IsRootNamespace)
7260
return;
7361

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// ${COMPLETE_ITEM:RequireQualifiedAccess}
2+
[<{caret}>]
3+
module M
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// ${COMPLETE_ITEM:RequireQualifiedAccess}
2+
[<RequireQualifiedAccess{caret}>]
3+
module M
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// ${COMPLETE_ITEM:Extension (in System.Runtime.CompilerServices)}
2+
[<{caret}>]
3+
module M
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// ${COMPLETE_ITEM:Extension (in System.Runtime.CompilerServices)}
2+
[<System.Runtime.CompilerServices.Extension{caret}>]
3+
module M
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
open Microsoft.FSharp.Collections
2-
3-
type list = list<int>
1+
type list = list<int>
42

53
type List<'T>{caret} with
64
member x.P = 1
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
type List{caret}<'T> with
2+
member x.P = 1

0 commit comments

Comments
 (0)