Add tabcompletion support

Add proper tabcompletion support. Most work is code cleanup and adding unittests.
The `use strict` and `use warnings` pragmas have package scope. The lines above add them to the `main` package, but `query` doesn't use them. Add them here, too.