<commit_before>﻿' Copyright (c) Microsoft.  All Rights Reserved.  Licensed under the Apache License, Version 2.0.  See License.txt in the project root for license information.

Imports System.IO
Imports System.Threading
Imports Microsoft.CodeAnalysis.Editor.Host
Imports Microsoft.CodeAnalysis.Editor.Implementation.Peek
Imports Microsoft.CodeAnalysis.Editor.Peek
Imports Microsoft.CodeAnalysis.Editor.UnitTests.Workspaces
Imports Microsoft.VisualStudio.Imaging.Interop
Imports Microsoft.VisualStudio.Language.Intellisense
Imports Microsoft.VisualStudio.Text
Imports Moq

Namespace Microsoft.CodeAnalysis.Editor.UnitTests.Peek
    Public Class PeekTests
        <Fact, WorkItem(820706), Trait(Traits.Feature, Traits.Features.Peek)>
        Public Sub InvokeInEmptyFile()
            Dim result = GetPeekResultCollection(<Workspace>
                                                     <Project Language="C#" CommonReferences="true">
                                                         <Document>$$}</Document>
                                                     </Project>
                                                 </Workspace>)

            Assert.Null(result)
        End Sub

        <Fact, WorkItem(827025), Trait(Traits.Feature, Traits.Features.Peek)>
        Public Sub WorksAcrossLanguages()
            Using workspace = TestWorkspaceFactory.CreateWorkspace(<Workspace>
                                                                       <Project Language="C#" AssemblyName="Reference" CommonReferences="true">
                                                                           <Document>public class {|Identifier:TestClass|} { }</Document>
                                                                       </Project>
                                                                       <Project Language="Visual Basic" CommonReferences="true">
                                                                           <ProjectReference>Reference</ProjectReference>
                                                                           <Document>
                                                                                Public Class Blah : Inherits $$TestClass : End Class
                                                                          </Document>
                                                                       </Project>
                                                                   </Workspace>)
                Dim result = GetPeekResultCollection(workspace)

                Assert.Equal(1, result.Items.Count)
                result.AssertNavigatesToIdentifier(index:=0, name:="Identifier")
            End Using
        End Sub

        <Fact, WorkItem(824336), Trait(Traits.Feature, Traits.Features.Peek)>
        Public Sub PeekDefinitionWhenInvokedOnLiteral()
            Using workspace = TestWorkspaceFactory.CreateWorkspace(<Workspace>
                                                                       <Project Language="C#" CommonReferences="true">
                                                                           <Document>class C { string s = $$"Foo"; }</Document>
                                                                       </Project>
                                                                   </Workspace>)
                Dim result = GetPeekResultCollection(workspace)

                Assert.Equal(1, result.Items.Count)
                Assert.Equal($"String [{EditorFeaturesResources.FromMetadata}]", result(0).DisplayInfo.Label)
                Assert.Equal($"String [{EditorFeaturesResources.FromMetadata}]", result(0).DisplayInfo.Title)
                Assert.True(result.GetRemainingIdentifierLineTextOnDisk(index:=0).StartsWith("String", StringComparison.Ordinal))
            End Using
        End Sub

        <Fact, WorkItem(824331), WorkItem(820289), Trait(Traits.Feature, Traits.Features.Peek)>
        Public Sub PeekDefinitionWhenExtensionMethodFromMetadata()
            Using workspace = TestWorkspaceFactory.CreateWorkspace(<Workspace>
                                                                       <Project Language="C#" CommonReferences="true">
                                                                           <Document>
                                                                               using System.Linq;
                                                                               class C { void M() { int[] a; a.$$Distinct(); }</Document>
                                                                       </Project>
                                                                   </Workspace>)
                Dim result = GetPeekResultCollection(workspace)

                Assert.Equal(1, result.Items.Count)
                Assert.Equal($"Enumerable [{EditorFeaturesResources.FromMetadata}]", result(0).DisplayInfo.Label)
                Assert.Equal($"Enumerable [{EditorFeaturesResources.FromMetadata}]", result(0).DisplayInfo.Title)
                Assert.True(result.GetRemainingIdentifierLineTextOnDisk(index:=0).StartsWith("Distinct", StringComparison.Ordinal))
            End Using
        End Sub

        <Fact, WorkItem(819660), Trait(Traits.Feature, Traits.Features.Peek)>
        Public Sub PeekDefinitionFromVisualBasicMetadataAsSource()
            Using workspace = TestWorkspaceFactory.CreateWorkspace(<Workspace>
                                                                       <Project Language="Visual Basic" CommonReferences="true">
                                                                           <Document><![CDATA[<System.$$Serializable()>
Class AA
End Class
</Document>
                                                          ]]></Document>
                                                                       </Project>
                                                                   </Workspace>)
                Dim result = GetPeekResultCollection(workspace)

                Assert.Equal(1, result.Items.Count)
                Assert.Equal($"SerializableAttribute [{EditorFeaturesResources.FromMetadata}]", result(0).DisplayInfo.Label)
                Assert.Equal($"SerializableAttribute [{EditorFeaturesResources.FromMetadata}]", result(0).DisplayInfo.Title)
                Assert.True(result.GetRemainingIdentifierLineTextOnDisk(index:=0).StartsWith("New()", StringComparison.Ordinal)) ' Navigates to constructor
            End Using
        End Sub

        <Fact, WorkItem(819602), Trait(Traits.Feature, Traits.Features.Peek)>
        Public Sub PeekDefinitionOnParamNameXmlDocComment()
            Using workspace = TestWorkspaceFactory.CreateWorkspace(<Workspace>
                                                                       <Project Language="Visual Basic" CommonReferences="true">
                                                                           <Document><![CDATA[
Class C
''' <param name="$$exePath"></param>
Public Sub ddd(ByVal {|Identifier:exePath|} As String)
End Sub
End Class
                                                          ]]></Document>
                                                                       </Project>
                                                                   </Workspace>)
                Dim result = GetPeekResultCollection(workspace)

                Assert.Equal(1, result.Items.Count)
                result.AssertNavigatesToIdentifier(0, "Identifier")
            End Using
        End Sub


        <Fact, WorkItem(820363), Trait(Traits.Feature, Traits.Features.Peek)>
        Public Sub PeekDefinitionOnLinqVariable()
            Using workspace = TestWorkspaceFactory.CreateWorkspace(<Workspace>
                                                                       <Project Language="Visual Basic" CommonReferences="true">
                                                                           <Document><![CDATA[
Module M
    Sub S()
        Dim arr = {3, 4, 5}
        Dim q = From i In arr Select {|Identifier:$$d|} = i.GetType
    End Sub
End Module
                                                          ]]></Document>
                                                                       </Project>
                                                                   </Workspace>)
                Dim result = GetPeekResultCollection(workspace)

                Assert.Equal(1, result.Items.Count)
                result.AssertNavigatesToIdentifier(0, "Identifier")
            End Using
        End Sub


        <Fact>
        <WorkItem(1091211)>
        Public Sub PeekAcrossProjectsInvolvingPortableReferences()
            Dim workspaceDefinition =
<Workspace>
    <Project Language="C#" AssemblyName="CSharpAssembly" CommonReferencesPortable="true">
        <Document>
            namespace N
            {
                public class CSClass
                {
                    public void  {|Identifier:M|}(int i) { }
                }
            }
        </Document>
    </Project>
    <Project Language="Visual Basic" AssemblyName="VBAssembly" CommonReferences="true" CommonReferenceFacadeSystemRuntime="true">
        <ProjectReference>CSharpAssembly</ProjectReference>
        <Document>
            Imports N

            Public Class VBClass
                Sub Test()
                    Dim x As New CSClass()
                    x.M$$(5)
                End Sub
            End Class
        </Document>
    </Project>
</Workspace>

            Using workspace = TestWorkspaceFactory.CreateWorkspace(workspaceDefinition)
                Dim result = GetPeekResultCollection(workspace)

                Assert.Equal(1, result.Items.Count)
                result.AssertNavigatesToIdentifier(0, "Identifier")
            End Using

        End Sub

        Private Function GetPeekResultCollection(workspace As XElement) As PeekResultCollection
            Using testWorkspace = TestWorkspaceFactory.CreateWorkspace(workspace)
                Return GetPeekResultCollection(testWorkspace)
            End Using
        End Function

        Private Function GetPeekResultCollection(workspace As TestWorkspace) As PeekResultCollection
            Dim document = workspace.Documents.FirstOrDefault(Function(d) d.CursorPosition.HasValue)

            If document Is Nothing Then
                AssertEx.Fail("The test is missing a $$ in the workspace.")
            End If

            Dim textBuffer = document.GetTextBuffer()
            Dim textView = document.GetTextView()

            Dim peekableItemSource As New PeekableItemSource(textBuffer,
                                                             workspace.GetService(Of IPeekableItemFactory),
                                                             New MockPeekResultFactory(workspace.GetService(Of IPersistentSpanFactory)),
                                                             workspace.GetService(Of IMetadataAsSourceFileService),
                                                             workspace.GetService(Of IWaitIndicator))

            Dim peekableSession As New Mock(Of IPeekSession)(MockBehavior.Strict)
            Dim triggerPoint = New SnapshotPoint(document.GetTextBuffer().CurrentSnapshot, document.CursorPosition.Value)
            peekableSession.Setup(Function(s) s.GetTriggerPoint(It.IsAny(Of ITextSnapshot))).Returns(triggerPoint)

            Dim items As New List(Of IPeekableItem)

            peekableItemSource.AugmentPeekSession(peekableSession.Object, items)
            If Not items.Any Then
                Return Nothing
            End If

            Dim peekResult As New PeekResultCollection(workspace)
            Dim item = items.SingleOrDefault()

            If item IsNot Nothing Then
                Dim resultSource = item.GetOrCreateResultSource(PredefinedPeekRelationships.Definitions.Name)
                resultSource.FindResults(PredefinedPeekRelationships.Definitions.Name,
                                         peekResult,<commit_msg>fixed mock for missing API call<commit_after>﻿' Copyright (c) Microsoft.  All Rights Reserved.  Licensed under the Apache License, Version 2.0.  See License.txt in the project root for license information.

Imports System.IO
Imports System.Threading
Imports Microsoft.CodeAnalysis.Editor.Host
Imports Microsoft.CodeAnalysis.Editor.Implementation.Peek
Imports Microsoft.CodeAnalysis.Editor.Peek
Imports Microsoft.CodeAnalysis.Editor.UnitTests.Workspaces
Imports Microsoft.VisualStudio.Imaging.Interop
Imports Microsoft.VisualStudio.Language.Intellisense
Imports Microsoft.VisualStudio.Text
Imports Moq

Namespace Microsoft.CodeAnalysis.Editor.UnitTests.Peek
    Public Class PeekTests
        <Fact, WorkItem(820706), Trait(Traits.Feature, Traits.Features.Peek)>
        Public Sub InvokeInEmptyFile()
            Dim result = GetPeekResultCollection(<Workspace>
                                                     <Project Language="C#" CommonReferences="true">
                                                         <Document>$$}</Document>
                                                     </Project>
                                                 </Workspace>)

            Assert.Null(result)
        End Sub

        <Fact, WorkItem(827025), Trait(Traits.Feature, Traits.Features.Peek)>
        Public Sub WorksAcrossLanguages()
            Using workspace = TestWorkspaceFactory.CreateWorkspace(<Workspace>
                                                                       <Project Language="C#" AssemblyName="Reference" CommonReferences="true">
                                                                           <Document>public class {|Identifier:TestClass|} { }</Document>
                                                                       </Project>
                                                                       <Project Language="Visual Basic" CommonReferences="true">
                                                                           <ProjectReference>Reference</ProjectReference>
                                                                           <Document>
                                                                                Public Class Blah : Inherits $$TestClass : End Class
                                                                          </Document>
                                                                       </Project>
                                                                   </Workspace>)
                Dim result = GetPeekResultCollection(workspace)

                Assert.Equal(1, result.Items.Count)
                result.AssertNavigatesToIdentifier(index:=0, name:="Identifier")
            End Using
        End Sub

        <Fact, WorkItem(824336), Trait(Traits.Feature, Traits.Features.Peek)>
        Public Sub PeekDefinitionWhenInvokedOnLiteral()
            Using workspace = TestWorkspaceFactory.CreateWorkspace(<Workspace>
                                                                       <Project Language="C#" CommonReferences="true">
                                                                           <Document>class C { string s = $$"Foo"; }</Document>
                                                                       </Project>
                                                                   </Workspace>)
                Dim result = GetPeekResultCollection(workspace)

                Assert.Equal(1, result.Items.Count)
                Assert.Equal($"String [{EditorFeaturesResources.FromMetadata}]", result(0).DisplayInfo.Label)
                Assert.Equal($"String [{EditorFeaturesResources.FromMetadata}]", result(0).DisplayInfo.Title)
                Assert.True(result.GetRemainingIdentifierLineTextOnDisk(index:=0).StartsWith("String", StringComparison.Ordinal))
            End Using
        End Sub

        <Fact, WorkItem(824331), WorkItem(820289), Trait(Traits.Feature, Traits.Features.Peek)>
        Public Sub PeekDefinitionWhenExtensionMethodFromMetadata()
            Using workspace = TestWorkspaceFactory.CreateWorkspace(<Workspace>
                                                                       <Project Language="C#" CommonReferences="true">
                                                                           <Document>
                                                                               using System.Linq;
                                                                               class C { void M() { int[] a; a.$$Distinct(); }</Document>
                                                                       </Project>
                                                                   </Workspace>)
                Dim result = GetPeekResultCollection(workspace)

                Assert.Equal(1, result.Items.Count)
                Assert.Equal($"Enumerable [{EditorFeaturesResources.FromMetadata}]", result(0).DisplayInfo.Label)
                Assert.Equal($"Enumerable [{EditorFeaturesResources.FromMetadata}]", result(0).DisplayInfo.Title)
                Assert.True(result.GetRemainingIdentifierLineTextOnDisk(index:=0).StartsWith("Distinct", StringComparison.Ordinal))
            End Using
        End Sub

        <Fact, WorkItem(819660), Trait(Traits.Feature, Traits.Features.Peek)>
        Public Sub PeekDefinitionFromVisualBasicMetadataAsSource()
            Using workspace = TestWorkspaceFactory.CreateWorkspace(<Workspace>
                                                                       <Project Language="Visual Basic" CommonReferences="true">
                                                                           <Document><![CDATA[<System.$$Serializable()>
Class AA
End Class
</Document>
                                                          ]]></Document>
                                                                       </Project>
                                                                   </Workspace>)
                Dim result = GetPeekResultCollection(workspace)

                Assert.Equal(1, result.Items.Count)
                Assert.Equal($"SerializableAttribute [{EditorFeaturesResources.FromMetadata}]", result(0).DisplayInfo.Label)
                Assert.Equal($"SerializableAttribute [{EditorFeaturesResources.FromMetadata}]", result(0).DisplayInfo.Title)
                Assert.True(result.GetRemainingIdentifierLineTextOnDisk(index:=0).StartsWith("New()", StringComparison.Ordinal)) ' Navigates to constructor
            End Using
        End Sub

        <Fact, WorkItem(819602), Trait(Traits.Feature, Traits.Features.Peek)>
        Public Sub PeekDefinitionOnParamNameXmlDocComment()
            Using workspace = TestWorkspaceFactory.CreateWorkspace(<Workspace>
                                                                       <Project Language="Visual Basic" CommonReferences="true">
                                                                           <Document><![CDATA[
Class C
''' <param name="$$exePath"></param>
Public Sub ddd(ByVal {|Identifier:exePath|} As String)
End Sub
End Class
                                                          ]]></Document>
                                                                       </Project>
                                                                   </Workspace>)
                Dim result = GetPeekResultCollection(workspace)

                Assert.Equal(1, result.Items.Count)
                result.AssertNavigatesToIdentifier(0, "Identifier")
            End Using
        End Sub


        <Fact, WorkItem(820363), Trait(Traits.Feature, Traits.Features.Peek)>
        Public Sub PeekDefinitionOnLinqVariable()
            Using workspace = TestWorkspaceFactory.CreateWorkspace(<Workspace>
                                                                       <Project Language="Visual Basic" CommonReferences="true">
                                                                           <Document><![CDATA[
Module M
    Sub S()
        Dim arr = {3, 4, 5}
        Dim q = From i In arr Select {|Identifier:$$d|} = i.GetType
    End Sub
End Module
                                                          ]]></Document>
                                                                       </Project>
                                                                   </Workspace>)
                Dim result = GetPeekResultCollection(workspace)

                Assert.Equal(1, result.Items.Count)
                result.AssertNavigatesToIdentifier(0, "Identifier")
            End Using
        End Sub


        <Fact>
        <WorkItem(1091211)>
        Public Sub PeekAcrossProjectsInvolvingPortableReferences()
            Dim workspaceDefinition =
<Workspace>
    <Project Language="C#" AssemblyName="CSharpAssembly" CommonReferencesPortable="true">
        <Document>
            namespace N
            {
                public class CSClass
                {
                    public void  {|Identifier:M|}(int i) { }
                }
            }
        </Document>
    </Project>
    <Project Language="Visual Basic" AssemblyName="VBAssembly" CommonReferences="true" CommonReferenceFacadeSystemRuntime="true">
        <ProjectReference>CSharpAssembly</ProjectReference>
        <Document>
            Imports N

            Public Class VBClass
                Sub Test()
                    Dim x As New CSClass()
                    x.M$$(5)
                End Sub
            End Class
        </Document>
    </Project>
</Workspace>

            Using workspace = TestWorkspaceFactory.CreateWorkspace(workspaceDefinition)
                Dim result = GetPeekResultCollection(workspace)

                Assert.Equal(1, result.Items.Count)
                result.AssertNavigatesToIdentifier(0, "Identifier")
            End Using

        End Sub

        Private Function GetPeekResultCollection(workspace As XElement) As PeekResultCollection
            Using testWorkspace = TestWorkspaceFactory.CreateWorkspace(workspace)
                Return GetPeekResultCollection(testWorkspace)
            End Using
        End Function

        Private Function GetPeekResultCollection(workspace As TestWorkspace) As PeekResultCollection
            Dim document = workspace.Documents.FirstOrDefault(Function(d) d.CursorPosition.HasValue)

            If document Is Nothing Then
                AssertEx.Fail("The test is missing a $$ in the workspace.")
            End If

            Dim textBuffer = document.GetTextBuffer()
            Dim textView = document.GetTextView()

            Dim peekableItemSource As New PeekableItemSource(textBuffer,
                                                             workspace.GetService(Of IPeekableItemFactory),
                                                             New MockPeekResultFactory(workspace.GetService(Of IPersistentSpanFactory)),
                                                             workspace.GetService(Of IMetadataAsSourceFileService),
                                                             workspace.GetService(Of IWaitIndicator))

            Dim peekableSession As New Mock(Of IPeekSession)(MockBehavior.Strict)
            Dim triggerPoint = New SnapshotPoint(document.GetTextBuffer().CurrentSnapshot, document.CursorPosition.Value)
            peekableSession.Setup(Function(s) s.GetTriggerPoint(It.IsAny(Of ITextSnapshot))).Returns(triggerPoint)
            peekableSession.SetupGet(Function(s) s.RelationshipName).Returns("IsDefinedBy")

            Dim items As New List(Of IPeekableItem)

            peekableItemSource.AugmentPeekSession(peekableSession.Object, items)
            If Not items.Any Then
                Return Nothing
            End If

            Dim peekResult As New PeekResultCollection(workspace)
            Dim item = items.SingleOrDefault()

            If item IsNot Nothing Then
                Dim resultSource = item.GetOrCreateResultSource(PredefinedPeekRelationships.Definitions.Name)
                resultSource.FindResults(PredefinedPeekRelationships.Definitions.Name,
                                         peekResult,
{'fraction_non_alphanumeric': 0.07373252832685469, 'fraction_numerical': 0.006699197750392854, 'mean_word_length': 1.316157456182358, 'pattern_counts': {'":': 0, '<': 155, '<?xml version=': 0, '>': 155, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 228, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '813021', 'n_tokens_mistral': 4865, 'n_tokens_neox': 4644, 'n_words': 1022}