Yes, the HolySpirit does depart from people based on their rebelliousness.
However, God does not have evil in His repertoire. He does not create evil - only good. The absence of the HolySpirit leaves a vacuum whilch the evil one fills.
This is what was observed. It was error for the writer to assume God deliberately troubled Saul. Saul^s choice to rebel resulted in his self-inflicted troubles.