@Alan-Kilborn
Thank you so much. It works.
If anybody is interested, here is the full python script. It’s NOT python standard, but it really helps me read code.
text=editor.getText()
################################################################################
''' compress operators, removes trailing and following space from operators '''
operators=[ '+', '-', '*', ':', '/' '//', '%', '**', ]
assigners=[ '=', '+=', '-=', '*=', ':=', ]
comparators=[ '==', '!=', '<', '>', '<=', '>=', ]
for character in operators+assigners+comparators:
if ' '+character+' ' in text:
editor.replace( ' '+character+' ', character )
################################################################################
''' padd brackets, adds space to brackets right of open, left of close '''
brackets_open='([{'
brackets_close=')]}'
for index, character in enumerate( text ):
''' searching edge cases '''
if index==0: # first character
trailing_character=" "
if len( text )>1:
following_character=text[ index+1 ]
else:
following_character=" "
elif index==len( text )-1: # last character
trailing_character=text[ index-1 ]
following_character=" "
else:
trailing_character=text[ index-1 ]
following_character=text[ index+1 ]
''' replacing '''
if character in brackets_open and following_character!=' ':
if character=='(':
editor.replace( character, '\('+' ' ) # '(' needs escape '\('
else:
editor.replace( character, character+' ' )
elif character in brackets_close and trailing_character!=' ':
if character=='\)':
editor.replace( character, ' '+'\)' ) # '(' needs escape '\)'
else:
editor.replace( character, ' '+character )
################################################################################