You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

36 lines
1.1 KiB

import re
from Npp import editor
snake = re.compile('([a-z\\d]+_)+[a-z_\\d]+')
camel = re.compile('((?<![A-Z])[a-zA-Z][a-z]*|\\d+)+')
camel_split = re.compile('((?<![A-Z])[a-zA-Z][a-z]*|\\d+)')
def toggle(text,pascal = False):
if snake.match(text):
groups = text.split('_')
ret_s = groups[0]
if pascal:
ret_s = ret_s.capitalize()
for group in groups[1:]:
ret_s += group.capitalize()
elif camel.match(text):
groups = camel_split.findall(text)
ret_s = '_'.join(group.lower() for group in groups)
return ret_s
if __name__ == "__main__":
start = editor.getSelectionStart()
content = editor.getSelText()
pascal = 'P256' in content
replace_all = 'R256' in content
if pascal:
content = content.replace('P256','')
if replace_all:
content = content.replace('R256','')
editor.replaceSel(content)
start += 2
result = toggle(content,pascal)
if replace_all:
editor.replace(content,result)
else:
editor.replaceSel(result)
editor.setSelectionStart(start)
editor.setSelectionEnd(start + len(result))