Skip to content

Commit 0c2715d

Browse files
committed
Add outline for HTML!!
1 parent 38f7872 commit 0c2715d

File tree

1 file changed

+36
-8
lines changed

1 file changed

+36
-8
lines changed

modes/html/mode.gd

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
extends TextForgeMode
22

3+
const SELF_CLOSING_TAGS := [
4+
"area", "base", "br", "col", "embed", "hr", "img", "input",
5+
"link", "meta", "param", "source", "track", "wbr"
6+
]
7+
38
var keyword_colors: Dictionary[Color, Array] = {
49
Color.hex(0xffa6a6ff): ["html", "head", "body", "title", "meta", "link", "base", "style", "script"],
510
Color.hex(0xccb3ffff): ["p", "br", "hr", "pre", "blockquote", "code", "kbd"],
@@ -12,10 +17,6 @@ var keyword_colors: Dictionary[Color, Array] = {
1217
Color.hex(0xb3d9f2ff): ["a", "details", "summary", "dialog", "menu", "menuitem"],
1318
Color.hex(0xd9f2ccff): ["href", "src", "alt", "id", "class", "type", "rel", "name", "value", "placeholder", "action", "method", "disabled", "checked", "selected"],
1419
}
15-
var self_closing_tags := [
16-
"area", "base", "br", "col", "embed", "hr", "img", "input",
17-
"link", "meta", "param", "source", "track", "wbr"
18-
]
1920

2021
func _initialize_mode() -> Error:
2122
_initialize_highlighter()
@@ -72,15 +73,42 @@ func _auto_format(text: String) -> String:
7273
func _update_code_completion_options(text: String) -> void:
7374
for color in keyword_colors:
7475
for keyword in keyword_colors[color]:
75-
if keyword in self_closing_tags:
76+
if keyword in SELF_CLOSING_TAGS:
7677
Global.get_editor().add_code_completion_option(CodeEdit.KIND_CLASS, "<" + keyword + "/>", keyword + "/>", color)
7778
else:
7879
Global.get_editor().add_code_completion_option(CodeEdit.KIND_CLASS, "<" + keyword + ">", keyword + ">\n\t\n</" + keyword + ">", color)
7980

8081

81-
# TODO
8282
func _generate_outline(text: String) -> Array:
83-
return Array()
83+
var parser = XMLParser.new()
84+
parser.open_buffer(text.to_utf8_buffer())
85+
86+
var outline := []
87+
var stack := []
88+
89+
while parser.read() != ERR_FILE_EOF:
90+
match parser.get_node_type():
91+
XMLParser.NODE_ELEMENT:
92+
var node_name = parser.get_node_name()
93+
var line = parser.get_current_line()
94+
var node := [node_name, line]
95+
96+
if stack.size() > 0:
97+
stack[-1].append(node)
98+
else:
99+
outline.append(node)
100+
101+
if not parser.is_empty() and not SELF_CLOSING_TAGS.has(node_name):
102+
stack.append(node)
103+
104+
XMLParser.NODE_ELEMENT_END:
105+
if stack.size() > 0:
106+
stack.pop_back()
107+
108+
return outline
109+
110+
111+
84112

85113

86114
# TODO
@@ -160,7 +188,7 @@ func _count_tag_diff(line: String) -> int:
160188
for match in matches:
161189
var closing := match.get_string(1) == "/"
162190
var tag := match.get_string(2).to_lower()
163-
var self_closing := tag in self_closing_tags or match.get_string(0).ends_with("/>")
191+
var self_closing := tag in SELF_CLOSING_TAGS or match.get_string(0).ends_with("/>")
164192
if self_closing:
165193
continue
166194
elif closing:

0 commit comments

Comments
 (0)