In pasuk 4, there are two ways to parse. Leimor opens a quote, but does it end at the word hazot or at the word mipanecha? If at hazot, then the mistaken Israelite is only thinking of his own righteousness, but not of the wickedness of the nations. But then, pasuk 5 seems exceptionally redundant, given that most of the idea has already been put forth in pasuk 4.
If at mipanecha, then the Israelite thinks of both his own righteousness and the wickedness of the dispossessed nations. And then the next pasuk (5) conveys partial agreement and partial disagreement: he was wrong about his own righteousness, but right about the nations' wickedness as cause.
This latter option seems to be Rashi's take on the pasuk. For on pasuk 4, he writes:
|Do not say to yourself: “My righteousness and the nations’ evil brought it about.”||אל תאמר בלבבך: צדקתי ורשעת הגוים גרמו:|
such that what you say in your heart includes the nations evil. And on pasuk 5, he writes:
|for the wickedness of these nations: Here [the word] כִּי means“but.” [See Rashi on Deut. 7:17.]||לא בצדקתך וגו' אתה בא לרשת וגו' כי ברשעת הגוים: הרי כי משמש בלשון אלא:|
such that effectively, combined with his earlier comment, he is selecting one of the two reasons put forth. If not for the earlier comment of Rashi, I would have interpreted it as this being the reason only Hashem puts forth, throughout. But now, it is selection from among the two. Indeed, as Siftei Chachamim expands, originally you thought it was these two causes, your righteousness and their sinfulness, but now you know it was just their sinfulness, and so they were cast out and the land made ownerless, and kol hakodem zacha, which was the Israelites.
Indeed, reading with Rashi, in pasuk 5 what replaces the righteousness is Hashem's desire to fulfill his promise to the Avot, so perhaps the idea is the substitution.
And this is essentially what Rashbam says. Ramban (same link) seems to suggest like the former, that all the person speaks of is of his own righteousness, but Ramban is not explicit about it.
Mendelsohnn writes much of this, and then favors the former explanation, that the person is only speaking of his righteousness, because of the change in person: it begins with "my righteousness" and "brought me in", but at the end of the pasuk it states מִפָּנֶיךָ, "before you."
Shadal agrees, writing:
ד ] וכרשעת הגוים האלה : הנכון כדעת רמבמ"ן , לא כרש"י ורשב"ם , שאם כן מפני היה לו לומר . והנה וי"ו וברשעת ענינה התנגדות ודוגמתה (למטה פסוק כ " ט ) והם עמך ונחלתך , וכן : מדוע באתם אלי ואתם שנאתם אותי ( בראשית כ " ו כ " ז ), למה תקראנה לי נעמי וה ' ענה בי ( רות א ' כ " א), איך תאמר אהבתיך ולבך אין אתי ( שופטים ט " ז ט " ו ), מה לך לספר חקי וגו ' ואתה שנאת מוסר ( תהלים ג ' י " ז ).
Thus, if it were part of the quote, or part of the incorrect supposition, it should have said mipanay rather than mipanecha. But associated with this, we have to account for the vav. Rashi has no problem with the vav, for it is continuing the thought of the person, who thinks
What is Rashi to do? What is Rashbam to do? Did they not see that the word was mipanecha, rather than mipanay?!
The answer is straightforward, but we should rightly go to Judaica Press for it. Judaica Press' translation is supposed to be in accordance with Rashi's commentary on the verse, and so we can see how they see fit to translate it:
Do not say to yourself, when the Lord, your God, has repelled them from before you, saying, "Because of my righteousness, the Lord has brought me to possess this land," and [that] because of the wickedness of these nations, the Lord drives them out from before you.
Thus, they certainly do end the quote before the end of the verse. But by placing the implicit word [that] there, they demonstrate how one can read the pasuk without placing the text inside of the quote. Thus, you would also say that it is because of the wickedness of the nations that the Lord drives them out from before you.
Why should Rashi prefer this reading? It is indeed a possible reading, but slightly more awkward. Why is it preferable? What is motivating Rashi? This is something to dwell upon, but perhaps it is just the aforementioned vav.