Phuket2
Jun 07, 2016 - 19:51
A silly example for editor.annotate_line. It's does a search on the current file and setting an annotation. This case is just the occurrence. Editor has a built in search, but just an example.
import editor, clipboard, console, io
def get_file_text():
file_name = editor.get_path()
with io.open(file_name, encoding = 'utf-8') as file:
return file.read()
def find_lines(text, search_text):
lst = []
for ln_no, ln in enumerate(text.splitlines()):
if search_text in ln:
lst.append(ln_no + 1)
return lst
def mark_lines(lns, s_text):
for i, ln in enumerate(lns):
editor.annotate_line(ln, text = str(i + 1), style = 'success', expanded= False)
if __name__ == '__main__':
s_text = console.input_alert('Enter text to search for')
if not s_text:
exit()
file_text = get_file_text()
ln_list = find_lines(file_text, s_text)
mark_lines(ln_list, s_text)
