-
Notifications
You must be signed in to change notification settings - Fork 183
/
Copy patherb_document.rb
138 lines (120 loc) · 3.2 KB
/
erb_document.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# typed: strict
# frozen_string_literal: true
module RubyLsp
class ERBDocument < Document
extend T::Sig
extend T::Generic
ParseResultType = type_member { { fixed: Prism::ParseResult } }
sig { override.returns(ParseResultType) }
def parse
return @parse_result unless @needs_parsing
@needs_parsing = false
scanner = ERBScanner.new(@source)
scanner.scan
@parse_result = Prism.parse(scanner.ruby)
end
sig { override.returns(T::Boolean) }
def syntax_error?
@parse_result.failure?
end
sig { override.returns(LanguageId) }
def language_id
LanguageId::ERB
end
sig do
params(
position: T::Hash[Symbol, T.untyped],
node_types: T::Array[T.class_of(Prism::Node)],
).returns(NodeContext)
end
def locate_node(position, node_types: [])
RubyDocument.locate(@parse_result.value, create_scanner.find_char_position(position), node_types: node_types)
end
class ERBScanner
extend T::Sig
sig { returns(String) }
attr_reader :ruby, :html
sig { params(source: String).void }
def initialize(source)
@source = source
@html = T.let(+"", String)
@ruby = T.let(+"", String)
@current_pos = T.let(0, Integer)
@inside_ruby = T.let(false, T::Boolean)
end
sig { void }
def scan
while @current_pos < @source.length
scan_char
@current_pos += 1
end
end
private
sig { void }
def scan_char
char = @source[@current_pos]
case char
when "<"
if next_char == "%"
@inside_ruby = true
@current_pos += 1
push_char(" ")
if next_char == "=" && @source[@current_pos + 2] == "="
@current_pos += 2
push_char(" ")
elsif next_char == "=" || next_char == "-"
@current_pos += 1
push_char(" ")
end
else
push_char(T.must(char))
end
when "-"
if @inside_ruby && next_char == "%" &&
@source[@current_pos + 2] == ">"
@current_pos += 2
push_char(" ")
@inside_ruby = false
else
push_char(T.must(char))
end
when "%"
if @inside_ruby && next_char == ">"
@inside_ruby = false
@current_pos += 1
push_char(" ")
else
push_char(T.must(char))
end
when "\r"
@ruby << char
@html << char
if next_char == "\n"
@ruby << next_char
@html << next_char
@current_pos += 1
end
when "\n"
@ruby << char
@html << char
else
push_char(T.must(char))
end
end
sig { params(char: String).void }
def push_char(char)
if @inside_ruby
@ruby << char
@html << " " * char.length
else
@ruby << " " * char.length
@html << char
end
end
sig { returns(String) }
def next_char
@source[@current_pos + 1] || ""
end
end
end
end