Godot-TextEditor/addons/text_editor/TE_Search.gd

164 lines
4.5 KiB
GDScript3
Raw Normal View History

2021-10-18 21:20:19 +02:00
tool
extends "res://addons/text_editor/TE_RichTextLabel.gd"
2021-10-24 17:27:27 +02:00
onready var line_edit:LineEdit = get_parent().get_node("c/le")
onready var all_toggle:CheckBox = get_parent().get_node("c/all")
onready var case_toggle:CheckBox = get_parent().get_node("c/case")
2021-10-18 21:20:19 +02:00
var console_urls:Array = []
var last_search:String = ""
func _ready():
var _e
_e = line_edit.connect("text_entered", self, "_text_entered")
2021-10-24 17:27:27 +02:00
# fix fonts
2021-10-18 21:20:19 +02:00
line_edit.add_font_override("font", editor.FONT_R)
2021-10-24 17:27:27 +02:00
all_toggle.add_font_override("font", editor.FONT_R)
case_toggle.add_font_override("font", editor.FONT_R)
2021-10-18 21:20:19 +02:00
2021-11-27 11:35:18 +01:00
func select():
line_edit.grab_focus()
line_edit.grab_click_focus()
line_edit.select_all()
2021-10-18 21:20:19 +02:00
func _clicked(args):
match args[0]:
"goto":
2021-10-24 17:27:27 +02:00
var tab:TextEdit = editor.open_file(args[1])
2021-10-18 21:20:19 +02:00
editor.select_file(args[1])
2021-10-24 17:27:27 +02:00
yield(get_tree(), "idle_frame")
# goto line
2021-11-27 11:35:18 +01:00
var hfrom = int(args[2])
var line = int(args[3])
tab.goto_line(hfrom)
tab.goto_line(line, false)
2021-10-24 17:27:27 +02:00
# select area
2021-11-27 11:35:18 +01:00
var from = int(args[4])
var lenn = int(args[5])
2021-10-24 17:27:27 +02:00
tab.select(line, from, line, from + lenn)
2021-11-27 11:35:18 +01:00
tab.cursor_set_line(line)
tab.cursor_set_column(from)
2021-10-18 21:20:19 +02:00
func _text_entered(search_for:String):
last_search = search_for
clear()
var found:Dictionary = _search(search_for)
var bbcode:PoolStringArray = PoolStringArray()
var fpaths = found.keys()
for k in len(fpaths):
if k != 0:
bbcode.append("")
var file_path:String = fpaths[k]
var l = clr("%s/%s " % [k+1, len(fpaths)], Color.orange) + clr(file_path.get_file(), Color.yellow)
2021-10-24 17:27:27 +02:00
l = meta(l, ["goto", file_path, 0, 0, 0], file_path)
2021-10-18 21:20:19 +02:00
bbcode.append(l)
2021-11-27 11:35:18 +01:00
var all_found = found[file_path]
for j in len(all_found):
2021-10-18 21:20:19 +02:00
var result = found[file_path][j]
var got_lines = result[0]
2021-11-27 11:35:18 +01:00
var highlight_from = result[1]
var line_index = result[2]
var char_index = result[3]
2021-10-18 21:20:19 +02:00
2021-11-27 11:35:18 +01:00
bbcode.append(clr(" %s/%s" % [j+1, len(all_found)], Color.orange))
2021-10-18 21:20:19 +02:00
for i in len(got_lines):
l = ""
2021-11-27 11:35:18 +01:00
var highlight = got_lines[i][0]
var lindex = got_lines[i][1]
var ltext = got_lines[i][2]
2021-10-18 21:20:19 +02:00
2021-11-27 11:35:18 +01:00
if highlight:
2021-12-19 16:43:24 +01:00
# var line:String = ltext
# var head:String = line.substr(0, char_index)
# var midd:String = line.substr(char_index, len(search_for))
# var tail:String = line.substr(char_index+len(search_for))
# head = clr(head, Color.white.darkened(.25))
# midd = b(clr(midd, Color.white))
# tail = clr(tail, Color.white.darkened(.25))
var h = TE_Util.highlight(ltext, char_index, len(search_for), Color.white.darkened(.25), Color.white)
l = "\t" + clr(str(lindex) + ": ", Color.white.darkened(.25)) + h
2021-10-18 21:20:19 +02:00
2021-11-27 11:35:18 +01:00
else:
l = "\t" + clr(str(lindex) + ": ", Color.white.darkened(.65)) + clr(ltext, Color.white.darkened(.5))
2021-10-18 21:20:19 +02:00
if l:
2021-11-27 11:35:18 +01:00
l = meta(l, ["goto", file_path, highlight_from, line_index, char_index, len(search_for)])
2021-10-18 21:20:19 +02:00
bbcode.append(l)
set_bbcode(bbcode.join("\n"))
2021-12-19 16:43:24 +01:00
2021-10-18 21:20:19 +02:00
# get a list of files containging lines
func _search(search_for:String) -> Dictionary:
2021-10-24 17:27:27 +02:00
var out = {}
var search_for_l:String
if case_toggle.pressed:
search_for_l = search_for
else:
search_for_l = search_for.to_lower()
var paths:Array
# search all
if all_toggle.pressed:
paths = editor.file_paths
# only search selected
else:
var sel = editor.get_selected_file()
if not sel:
var err_msg = "no file open to search"
editor.console.err(err_msg)
push_error(err_msg)
return out
else:
paths = [sel]
2021-11-27 11:35:18 +01:00
2021-10-24 17:27:27 +02:00
for path in paths:
2021-10-18 21:20:19 +02:00
var lines = TE_Util.load_text(path).split("\n")
for line_index in len(lines):
2021-10-24 17:27:27 +02:00
var line:String = lines[line_index]
# make lowercase, if case doesn't matter
if not case_toggle.pressed:
line = line.to_lower()
2021-10-18 21:20:19 +02:00
# find index where result is found
var char_index:int = line.find(search_for_l)
if char_index != -1:
2021-10-24 17:27:27 +02:00
if not path in out:
out[path] = []
2021-11-27 11:35:18 +01:00
var preview_lines = [[true, line_index, lines[line_index]]]
2021-10-18 21:20:19 +02:00
var highlight_from:int = line_index
2021-10-24 17:27:27 +02:00
2021-11-27 11:35:18 +01:00
# previous few lines before a blank
for i in range(1, 3):
if line_index-i >= 0:
if not lines[line_index-i].strip_edges():
break
highlight_from = line_index-i
preview_lines.push_front([false, line_index-i, lines[line_index-i]])
# next few lines before a blank
for i in range(1, 3):
if line_index+i < len(lines):
if not lines[line_index+i].strip_edges():
break
preview_lines.push_back([false, line_index+i, lines[line_index+i]])
2021-10-24 17:27:27 +02:00
2021-10-18 21:20:19 +02:00
# lines, index in file, index in line
2021-11-27 11:35:18 +01:00
out[path].append([preview_lines, highlight_from, line_index, char_index])
2021-10-24 17:27:27 +02:00
return out