Add tabcompletion support

Add proper tabcompletion support. Most work is code cleanup and adding unittests.