45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
# File 'lib/textbringer/tree_sitter_adapter.rb', line 45
def custom_highlight(window)
window.instance_variable_set(:@highlight_on, {})
window.instance_variable_set(:@highlight_off, {})
return unless can_highlight?
parser = get_parser
return unless parser
buffer = window.buffer
base_pos = buffer.point_min
buffer_text = buffer.to_s
old_tree = get_cached_tree(buffer, buffer_text)
tree = parser.parse_string(old_tree, buffer_text)
return unless tree
cache_tree(buffer, tree, buffer_text)
if TreeSitterAdapter.debug?
File.open("/tmp/tree_sitter_debug.log", "a") do |f|
f.puts "[#{Time.now}] custom_highlight"
f.puts " base_pos=#{base_pos} buffer.bytesize=#{buffer_text.bytesize}"
f.puts " incremental_parse=#{!old_tree.nil?}"
end
end
highlight_on = {}
highlight_off = {}
node_map = TreeSitter::NodeMaps.for(tree_sitter_language)
visit_node(tree.root_node, node_map) do |node, start_byte, end_byte|
face = node_type_to_face(node.type.to_sym)
next unless face
face = Face[face]
if face
highlight_on[base_pos + start_byte] = face
highlight_off[base_pos + end_byte] = true
if TreeSitterAdapter.debug? && highlight_on.size <= 5
File.open("/tmp/tree_sitter_debug.log", "a") do |f|
f.puts " #{node.type} pos=#{base_pos + start_byte}-#{base_pos + end_byte} face=#{face}"
end
end
end
end
if TreeSitterAdapter.debug?
File.open("/tmp/tree_sitter_debug.log", "a") do |f|
f.puts " total_highlights=#{highlight_on.size}"
end
end
window.instance_variable_set(:@highlight_on, highlight_on)
window.instance_variable_set(:@highlight_off, highlight_off)
end
|