Editor
One editing context — a connection, a pane, whatever owns a caret.
Its file is created once and rewritten per query, never replaced. A new file means a new dangling file module, and every module analyze touches outlives close (see there). So a file per keystroke retains a PSI tree per keystroke; one file per editor retains one, for as long as the editor lives — which is why an editor is worth holding onto rather than making one per request.
Functions
code with a caret at offset, ready to analyze. A placeholder identifier is spliced in at the caret first, because completion runs on source that does not parse: foo. has no selector, so the parser yields no dotted expression and the receiver cannot be reached. IntelliJ splices one in for the same reason, and reusing its name means anything that special-cases it still behaves.
Swaps the file's whole tree for one parsed from text. The file object survives, so its module — and with it the analysis cache key — stays the one this editor already had. The modification stamp moves, which is what tells the session on that key to be rebuilt instead of reused stale.