summaryrefslogtreecommitdiff
path: root/server-beta/src/proto/interface.lua
blob: 6af0c9964a628ab530dffa71b3d5e2e58fefe207 (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
local guide = require 'parser.guide'

local m = {}


--- 获取 position 对应的光标位置
---@param lines table
---@param text string
---@param position position
---@return integer
function m.offset(lines, text, position)
    local row    = position.line
    local start  = guide.lineRange(lines, row)
    local offset = utf8.offset(text, position.character + 1, start)
    return offset
end

--- 将光标位置转化为 position
---@alias position table
---@param lines table
---@param text string
---@param offset integer
---@return position
function m.position(lines, text, offset)
    local row, col = guide.positionOf(lines, offset)
    local start    = guide.lineRange(lines, row)
    local ucol     = utf8.len(text, start + 1, start + col, true)
    return {
        line      = row,
        character = ucol,
    }
end

--- 将2个光标位置转化为 range
---@alias range table
---@param lines table
---@param text string
---@param offset1 integer
---@param offset2 integer
function m.range(lines, text, offset1, offset2)
    return {
        start   = m.position(lines, text, offset1),
        ['end'] = m.position(lines, text, offset2),
    }
end

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

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

return m