summaryrefslogtreecommitdiff
path: root/script/core/diagnostics/count-down-loop.lua
blob: 9bc4b273a8ceca98c9e61411b5812b8de25bc31c (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
local files    = require "files"
local guide    = require "parser.guide"
local lang     = require 'language'

return function (uri, callback)
    local state = files.getState(uri)
    local text  = files.getText(uri)
    if not state or not text then
        return
    end

    guide.eachSourceType(state.ast, 'loop', function (source)
        local maxNumer = source.max and tonumber(source.max[1])
        if maxNumer ~= 1 then
            return
        end
        local minNumber = source.init and tonumber(source.init[1])
        if minNumber and minNumber <= 1 then
            return
        end
        if not source.step then
            callback {
                start   = source.init.start,
                finish  = source.max.finish,
                message = lang.script('DIAG_COUNT_DOWN_LOOP'
                    , ('%s, %s'):format(text:sub(
                        guide.positionToOffset(state, source.init.start),
                        guide.positionToOffset(state, source.max.finish)
                    ), '-1')
                )
            }
        else
            local stepNumber = tonumber(source.step[1])
            if stepNumber and stepNumber > 0 then
                callback {
                    start   = source.init.start,
                    finish  = source.step.finish,
                    message = lang.script('DIAG_COUNT_DOWN_LOOP'
                        , ('%s, -%s'):format(text:sub(
                            guide.positionToOffset(state, source.init.start),
                            guide.positionToOffset(state, source.max.finish)
                        ), source.step[1])
                    )
                }
            end
        end
    end)
end