Add tabcompletion support

Add proper tabcompletion support. Most work is code cleanup and adding unittests.
I certainly don't insist... But reading the code quickly, I don't find the name very meaningful... May be just a comment can clarify what it does... I let you decide!