summaryrefslogtreecommitdiff
path: root/script/proto/converter.lua
blob: d761042cb1de9a1b5db2c74eeb7cbc15a5665c17 (plain)
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
local guide   = require 'parser.guide'
local files   = require 'files'
local encoder = require 'encoder'

local offsetEncoding = 'utf16'

local m = {}

---@alias position {line: integer, character: integer}

---@param row integer
---@param col integer
---@return position
function m.position(row, col)
    return {
        line      = row,
        character = col,
    }
end

---@param state parser.state
---@param pos integer
---@return position
local function rawPackPosition(state, pos)
    local row, col = guide.rowColOf(pos)
    if col > 0 then
        local text  = state.lua
        if state and text then
            local lineOffset = state.lines[row]
            if lineOffset then
                local start = lineOffset
                local finish = lineOffset + col - 1
                if start <= #text and finish <= #text then
                    col = encoder.len(offsetEncoding, text, lineOffset, lineOffset + col - 1)
                end
            else
                col = 0
            end
        end
    end
    return {
        line      = row,
        character = col,
    }
end

---@param state parser.state
---@param pos integer
---@return position
local function diffedPackPosition(state, pos)
    local offset       = guide.positionToOffset(state, pos)
    local originOffset = files.diffedOffsetBack(state, offset)
    local originPos    = guide.offsetToPositionByLines(state.originLines, originOffset)
    local row, col     = guide.rowColOf(originPos)
    if col > 0 then
        local text = state.originText
        if text then
            local lineOffset  = state.originLines[row]
            local finalOffset = math.min(lineOffset + col - 1, #text + 1)
            col = encoder.len(offsetEncoding, text, lineOffset, finalOffset)
        end
    end
    return {
        line      = row,
        character = col,
    }
end

---@param state parser.state
---@param pos integer
---@return position
function m.packPosition(state, pos)
    if files.hasDiffed(state) then
        return diffedPackPosition(state, pos)
    else
        return rawPackPosition(state, pos)
    end
end

---@param state parser.state
---@param position position
---@return integer
local function rawUnpackPosition(state, position)
    local row, col = position.line, position.character
    if col > 0 then
        local text  = state.lua
        if state and text then
            local lineOffset = state.lines[row]
            local textOffset = encoder.offset(offsetEncoding, text, col + 1, lineOffset)
            if textOffset and lineOffset then
                col = textOffset - lineOffset
            end
        end
    end
    local pos = guide.positionOf(row, col)
    return pos
end

---@param state parser.state
---@param position position
---@return integer
local function diffedUnpackPosition(state, position)
    local row, col = position.line, position.character
    if col > 0 then
        local lineOffset = state.originLines[row]
        if lineOffset then
            local textOffset = encoder.offset(offsetEncoding, state.originText, col + 1, lineOffset)
            if textOffset and lineOffset then
                col = textOffset - lineOffset
            end
        end
    end
    local originPos    = guide.positionOf(row, col)
    local originOffset = guide.positionToOffsetByLines(state.originLines, originPos)
    local offset       = files.diffedOffset(state, originOffset)
    local pos          = guide.offsetToPosition(state, offset)
    return pos
end

---@param state    parser.state
---@param position position
---@return integer
function m.unpackPosition(state, position)
    if files.hasDiffed(state) then
        return diffedUnpackPosition(state, position)
    else
        return rawUnpackPosition(state, position)
    end
end

---@alias range {start: position, end: position}

---@param state  parser.state
---@param start  integer
---@param finish integer
---@return range
function m.packRange(state, start, finish)
    local range = {
        start   = m.packPosition(state, start),
        ['end'] = m.packPosition(state, finish),
    }
    return range
end

---@param start position
---@param finish position
---@return range
function m.range(start, finish)
    return {
        start   = start,
        ['end'] = finish,
    }
end

---@param state parser.state
---@param range range
---@return integer start
---@return integer finish
function m.unpackRange(state, range)
    local start  = m.unpackPosition(state, range.start)
    local finish = m.unpackPosition(state, range['end'])
    return start, finish
end

---@alias location {uri: uri, range: range}

---@param uri string
---@param range range
---@return location
function m.location(uri, range)
    return {
        uri   = uri,
        range = range,
    }
end

---@alias locationLink {targetUri:uri, targetRange: range, targetSelectionRange: range, originSelectionRange: range}

---@param uri string
---@param range range
---@param selection range
---@param origin range
---@return locationLink
function m.locationLink(uri, range, selection, origin)
    return {
        targetUri            = uri,
        targetRange          = range,
        targetSelectionRange = selection,
        originSelectionRange = origin,
    }
end

---@alias textEdit {range: range, newText: string}

---@param range   range
---@param newtext string
---@return textEdit
function m.textEdit(range, newtext)
    return {
        range   = range,
        newText = newtext,
    }
end

function m.setOffsetEncoding(encoding)
    offsetEncoding = encoding:lower():gsub('%-', '')
end

return m