-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathdocument.jl
164 lines (146 loc) · 5.81 KB
/
document.jl
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
struct FormatSkip
startline::Int
endline::Int
startoffset::Int
endoffset::Int
end
struct Document
srcfile::JuliaSyntax.SourceFile
format_skips::Vector{FormatSkip}
# Line where there's a noindent comment. "#! format: noindent". This is checked during
# the print stage to know if the contents of the block (recursive) should not be indented.
noindent_blocks::Vector{Int}
comments::Dict{Int,Tuple{Int,String}}
ends_on_nl::Bool
end
function Document(text::AbstractString)
format_skips = FormatSkip[]
noindent_blocks = Int[]
stack = Tuple{Int,Int}[]
comments = Dict{Int,Tuple{Int,String}}()
srcfile = JuliaSyntax.SourceFile(text)
tokens = JuliaSyntax.tokenize(text)
ends_on_nl = kind(tokens[end]) === K"NewlineWs"
for (i, t) in enumerate(tokens)
if kind(t) === K"Comment"
ws = 0
if i > 1 &&
(kind(tokens[i-1]) === K"Whitespace" || kind(tokens[i-1]) === K"NewlineWs")
pt = tokens[i-1]
prevval = try
srcfile.code[first(pt.range):last(pt.range)]
catch e
if isa(e, StringIndexError)
srcfile.code[first(pt.range):prevind(text, last(pt.range))]
else
rethrow(e)
end
end
idx = findlast(c -> c == '\n', prevval)
if idx === nothing
(idx = 1)
end
ws = count(c -> c == ' ', prevval[idx:end])
end
startline = JuliaSyntax.source_line(srcfile, first(t.range))
endline = JuliaSyntax.source_line(srcfile, last(t.range))
val = try
srcfile.code[first(t.range):last(t.range)]
catch
srcfile.code[first(t.range):prevind(srcfile.code, last(t.range))]
end
if startline == endline && startswith(val, "#=") && endswith(val, "=#")
elseif startline == endline
if ws > 0
comments[startline] = (ws, val)
else
comments[startline] = (ws, val)
end
else
# multiline comment
idx = findfirst(x -> x == '\n', val)::Int + 1
fc2 = findfirst(c -> !isspace(c), val[idx:end])
lr = JuliaSyntax.source_line_range(srcfile, first(t.range))
lineoffset = (first(t.range) - lr[1]) + 1
ws2 = fc2 === nothing || lineoffset < fc2 ? ws : max(ws, fc2 - 1)
line = startline
cs = ""
for (i, c) in enumerate(val)
cs *= c
if c == '\n'
fc = findfirst(c -> !isspace(c), cs)
if fc === nothing
# comment is all whitespace
comments[line] = (ws, cs[end:end])
else
idx = min(fc, ws + 1)
comments[line] = (ws, cs[idx:end])
end
line += 1
ws = ws2
cs = ""
end
end
# last comment
idx = min(findfirst(c -> !isspace(c), cs)::Int, ws + 1)
comments[line] = (ws, cs[idx:end])
end
if length(stack) == 0 && occursin(r"^#!\s*format\s*:\s*off\s*$", val)
# There should not be more than 1
# "off" tag on the stack at a time.
offset = first(t.range)
r = JuliaSyntax.source_line_range(srcfile, offset)
line = JuliaSyntax.source_line(srcfile, offset)
push!(stack, (line, first(r)))
elseif length(stack) > 0 && occursin(r"^#!\s*format\s*:\s*on\s*$", val)
# If "#! format: off" has not been seen
# "#! format: on" is treated as a normal comment.
offset = last(t.range)
r = JuliaSyntax.source_line_range(srcfile, offset)
line = JuliaSyntax.source_line(srcfile, offset)
v = pop!(stack)
push!(format_skips, FormatSkip(v[1], line, v[2], last(r)))
end
if occursin(r"^#!\s*format\s*:\s*noindent\s*$", val)
line = JuliaSyntax.source_line(srcfile, first(t.range))
push!(noindent_blocks, line)
end
elseif kind(t) === K"ErrorEofMultiComment"
l = JuliaSyntax.source_line(srcfile, first(t.range))
throw(
ErrorException(
"""Unable to format. Multi-line comment on line $l is not closed.""",
),
)
end
end
# If there is a SINGLE "#! format: off" tag
# do not format from the "off" tag onwards.
if length(stack) == 1 && length(format_skips) == 0
# -1 signifies everything afterwards "#! format: off"
# will not formatted.
v = pop!(stack)
line = numlines(srcfile)
r = JuliaSyntax.source_line_range(srcfile, srcfile.line_starts[line])
push!(format_skips, FormatSkip(v[1], line, v[2], last(r)))
end
return Document(srcfile, format_skips, noindent_blocks, comments, ends_on_nl)
end
function has_noindent_block(d::Document, r::Tuple{Int,Int})
for b in d.noindent_blocks
if b in r
return true
end
end
return false
end
numlines(sf::JuliaSyntax.SourceFile) = length(sf.line_starts) - 1
numlines(d::Document) = numlines(d.srcfile)
function getsrcval(d::Document, r::UnitRange{T}) where {T<:Union{Int,UInt}}
code = JuliaSyntax.sourcetext(d.srcfile)
try
code[r]
catch
code[first(r):prevind(code, last(r))]
end
end