Repository: dotnet/roslyn
File: src/LanguageServer/Protocol/Handler/ProjectContext/GetTextDocumentWithContextHandler.cs (C#)

Code before:
        var documentIds = context.Solution.GetDocumentIds(request.TextDocument.DocumentUri);

        if (!documentIds.Any())
        {
            return null;
        }

        var contexts = new List<VSProjectContext>();

        foreach (var documentId in documentIds)
        {
            var project = context.Solution.GetRequiredProject(documentId.ProjectId);
            var projectContext = ProtocolConversions.ProjectToProjectContext(project);
            contexts.Add(projectContext);
        }

        // If the document is open, it doesn't matter which DocumentId we pass to GetDocumentIdInCurrentContext since
        // all the documents are linked at that point, so we can just pass the first arbitrarily. If the document is closed
        // GetDocumentIdInCurrentContext will just return the same ID back, which means we're going to pick the first
        // ID in GetDocumentIdsWithFilePath, but there's really nothing we can do since we don't have contexts for
        // close documents anyways.
        var openDocumentId = documentIds.First();
        var currentContextDocumentId = context.Workspace.GetDocumentIdInCurrentContext(openDocumentId);

        // Create a key that uniquely identifies this set of contexts. We use this to track the user's preferred context
        // on the client side.
        var keyString = string.Join(";", contexts.Select(c => c.Label).OrderBy(l => l));
        var keyHash = XxHash128.Hash(Encoding.Unicode.GetBytes(keyString));
        var key = Convert.ToBase64String(keyHash);

        return new VSProjectContextList
        {
            Key = key,
            ProjectContexts = [.. contexts],
            DefaultIndex = documentIds.IndexOf(d => d == currentContextDocumentId)
        };
    }
}


Review comment (suggestion):
```suggestion
        // Create a key that uniquely identifies this set of contexts. The client side stores the preferred context
        // against this key on the client side so we can restore the active context across different documents.
```

Code after:
        var documentIds = context.Solution.GetDocumentIds(request.TextDocument.DocumentUri);

        if (!documentIds.Any())
        {
            return null;
        }

        var contexts = new List<VSProjectContext>();

        foreach (var documentId in documentIds)
        {
            var project = context.Solution.GetRequiredProject(documentId.ProjectId);
            var projectContext = ProtocolConversions.ProjectToProjectContext(project);
            contexts.Add(projectContext);
        }

        // If the document is open, it doesn't matter which DocumentId we pass to GetDocumentIdInCurrentContext since
        // all the documents are linked at that point, so we can just pass the first arbitrarily. If the document is closed
        // GetDocumentIdInCurrentContext will just return the same ID back, which means we're going to pick the first
        // ID in GetDocumentIdsWithFilePath, but there's really nothing we can do since we don't have contexts for
        // close documents anyways.
        var openDocumentId = documentIds.First();
        var currentContextDocumentId = context.Workspace.GetDocumentIdInCurrentContext(openDocumentId);

        // Create a key that uniquely identifies this set of contexts. The client side stores the preferred context
        // against this key on the client side so we can restore the active context across different documents.
        var keyString = string.Join(";", contexts.Select(c => c.Label).OrderBy(l => l));
        var keyHash = XxHash128.Hash(Encoding.Unicode.GetBytes(keyString));
        var key = Convert.ToBase64String(keyHash);

        return new VSProjectContextList
        {
            Key = key,
            ProjectContexts = [.. contexts],
            DefaultIndex = documentIds.IndexOf(d => d == currentContextDocumentId)
        };
    }
}