Add test for CodeCompletionMode

also fix a few bugs and cleanup the code