In this post, I want to color code II Samuel 7 according to P. Kyle McCarter's division of the sources in his Anchor Bible commentary. The code is as follows:
Green will be for the primitive document, which McCarter dates to the time of David and Solomon. The thesis of this document is that David wanted to build God a house, and God responded in turn that he'll build David a house, an everlasting dynasty.
Blue will be for the prophetic source, which is skeptical about the monarchy and opposes the building of a temple. I'm not sure right now when McCarter dates it, but he thinks that the prophetic writer edited the primitive document.
Red will be for the Deuteronomist, who thinks God's name (not God himself) dwells in the temple and stresses that Solomon will be the one who will build it, since "rest" in the land will exist under Solomon, not David (and rest must precede centralization, according to Deuteronomy 12). My impression is that, for McCarter, the Deuteronomist adds his additions to the primitive document+the prophetic additions.
The translation is the New Revised Standard Version.
2 Samuel 7:1 Now when the king was settled in his house, and the LORD had given him rest from all his enemies around him,
2 the king said to the prophet Nathan, "See now, I am living in a house of cedar, but the ark of God stays in a tent."
3 Nathan said to the king, "Go, do all that you have in mind; for the LORD is with you."
4 But that same night the word of the LORD came to Nathan:
5 Go and tell my servant David: Thus says the LORD: Are you the one to build me a house to live in?
6 I have not lived in a house since the day I brought up the people of Israel from Egypt to this day, but I have been moving about in a tent and a tabernacle.
7 Wherever I have moved about among all the people of Israel, did I ever speak a word with any of the tribal leaders of Israel, whom I commanded to shepherd my people Israel, saying, "Why have you not built me a house of cedar?"
8 Now therefore thus you shall say to my servant David: Thus says the LORD of hosts: I took you from the pasture, from following the sheep to be prince over my people Israel;
9 and I have been with you wherever you went, and have cut off all your enemies from before you; and I will make for you a great name, like the name of the great ones of the earth.
10 And I will appoint a place for my people Israel and will plant them, so that they may live in their own place, and be disturbed no more; and evildoers shall afflict them no more, as formerly,
11 from the time that I appointed judges over my people Israel; and I will give you rest from all your enemies. Moreover the LORD declares to you that the LORD will make you a house.
12 When your days are fulfilled and you lie down with your ancestors, I will raise up your offspring after you, who shall come forth from your body, and I will establish his kingdom.
13 He shall build a house for my name, and I will establish the throne of his kingdom forever.
14 I will be a father to him, and he shall be a son to me. When he commits iniquity, I will punish him with a rod such as mortals use, with blows inflicted by human beings.
15 But I will not take my steadfast love from him, as I took it from Saul, whom I put away from before you.
16 Your house and your kingdom shall be made sure forever before me; your throne shall be established forever.
17 In accordance with all these words and with all this vision, Nathan spoke to David.
18 Then King David went in and sat before the LORD, and said, "Who am I, O Lord GOD, and what is my house, that you have brought me thus far?
19 And yet this was a small thing in your eyes, O Lord GOD; you have spoken also of your servant's house for a great while to come. May this be instruction for the people, O Lord GOD!
20 And what more can David say to you? For you know your servant, O Lord GOD!
21 Because of your promise, and according to your own heart, you have wrought all this greatness, so that your servant may know it.
22 Therefore you are great, O LORD God; for there is no one like you, and there is no God besides you, according to all that we have heard with our ears.
23 Who is like your people, like Israel? Is there another nation on earth whose God went to redeem it as a people, and to make a name for himself, doing great and awesome things for them, by driving out before his people nations and their gods?
24 And you established your people Israel for yourself to be your people forever; and you, O LORD, became their God.
25 And now, O LORD God, as for the word that you have spoken concerning your servant and concerning his house, confirm it forever; do as you have promised.
26 Thus your name will be magnified forever in the saying, 'The LORD of hosts is God over Israel'; and the house of your servant David will be established before you.
27 For you, O LORD of hosts, the God of Israel, have made this revelation to your servant, saying, 'I will build you a house'; therefore your servant has found courage to pray this prayer to you.
28 And now, O Lord GOD, you are God, and your words are true, and you have promised this good thing to your servant;
29 now therefore may it please you to bless the house of your servant, so that it may continue forever before you; for you, O Lord GOD, have spoken, and with your blessing shall the house of your servant be blessed forever."
Personally, I'm not sure if the division absolutely has to be this way. But I do sympathize with McCarter's claim that pro- and anti-temple voices co-exist in this chapter, and also that the Deuteronomist applies a chapter that's mostly about David's everlasting dynasty to Solomon's construction of the temple.