Add tabcompletion support

Add proper tabcompletion support. Most work is code cleanup and adding unittests.
OTOH, it's not really your code. That change is probably out of the scope of this PR.