Yes you can do this easily with a trigger, just sets the event to "when a unit is attacked", check with conditions if the attacking unit is a long range unit (you'll have to set manually what you call "long range" , eg 800.00) or invisible (you have to use "Or" condition and fill the 2 conditions with what I've said).
Then you just have to add one action ordering the attacked unit to stop.