julia-vscode / DocumentFormat.jl
1 10
function readindent(io)
2 20
    indent = 0
3 70
    while !eof(io) && (p = Base.peek(io); p == 0x20 || p == 0x09)
4 70
        p == 0x20 && (indent += 1)
5 70
        p == 0x09 && (indent += 4)
6 70
        read(io, UInt8)
7
    end
8 70
    return indent
9
end
10

11 20
function get_lines(s::String)
12 70
    io = IOBuffer(s)
13 70
    lines = Tuple{Int,Int}[(0, readindent(io))] # startbyte, indent
14 70
    while !eof(io)
15 60
        c = read(io, Char)
16 70
        if c == '\r' && Base.peek(io) == 0x0a
17 0
            c = read(io, Char)
18 0
            push!(lines, (position(io), readindent(io)))
19 70
        elseif c == '\n'
20 70
            push!(lines, (position(io), readindent(io)))
21
        end
22
    end
23 70
    first(last(lines)) != lastindex(s) && push!(lines, (lastindex(s), 0))
24 70
    lines
25
end
26

27

28
function line_of(offset, lines)
29 70
    offset > first(last(lines)) && error()
30 70
    offset == 0 && return 1
31 70
    for i = 1:length(lines) - 1
32 70
        if lines[i][1] < offset <= lines[i + 1][1]
33 70
            return i
34
        end
35
    end
36 0
    return length(lines)
37
end
38

39 20
function issameline(offset1, offset2, lines)
40 70
    line_of(offset1, lines) == line_of(offset2, lines)
41
end
42

43 0
function get_expr(x, offset, pos=0)
44 0
    if pos > offset
45 0
        return nothing
46
    end
47 0
    if length(x) > 0
48 0
        for a in x
49 0
            if pos < offset <= (pos + a.fullspan)
50 0
                return get_expr(a, offset, pos)
51
            end
52 0
            pos += a.fullspan
53
        end
54 0
    elseif pos == 0
55 0
        return x
56 0
    elseif (pos < offset <= (pos + x.fullspan))
57 0
        return x
58
    end
59
end

Read our documentation on viewing source code .

Loading