summaryrefslogtreecommitdiff
path: root/script/core/rangeformatting.lua
blob: 7c3953e6b1137b636f2113e799aa96ae922a2786 (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
local files = require("files")
local log = require("log")
local converter = require("proto.converter")

return function(uri, range, options)
    local state = files.getState(uri)
    if not state then
        return
    end
    local suc, codeFormat = pcall(require, "code_format")
    if not suc then
        return
    end
    local text = state.originText
    local status, formattedText, startLine, endLine = codeFormat.range_format(
        uri, text, range.start.line, range["end"].line, options)

    if not status then
        if formattedText ~= nil then
            log.error(formattedText)
        end

        return
    end

    return {
        {
            start = converter.unpackPosition(state, { line = startLine, character = 0 }),
            finish = converter.unpackPosition(state, { line = endLine + 1, character = 0 }),
            text = formattedText,
        }
    }
end