The logic here is copy/pasted from IdentifierCompleter. Don't copy/paste methods, create good abstractions so that the same method can be reused. Find a good way of reusing this logic. Copy/pasting logic is pretty much always the worst choice available.