summaryrefslogtreecommitdiff
path: root/server-beta/src/core/diagnostics/trailing-space.lua
blob: ef7fbef140c28a67f61b7e4a44208cbe1986b0fd (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
local files = require 'files'
local lang  = require 'language'
local guide = require 'parser.guide'

local function isInString(ast, offset)
    local result = false
    guide.eachSourceType(ast, 'string', function (source)
        if offset >= source.start and offset <= source.finish then
            result = true
        end
    end)
    return result
end

return function (uri, callback)
    local ast   = files.getAst(uri)
    if not ast then
        return
    end
    local lines = files.getLines(uri)
    local text  = files.getText(uri)

    for i = 1, #lines do
        local start  = lines[i].start + 1
        local finish = lines[i].finish - 1
        local line = text:sub(start, finish)
        if line:find '^[ \t]+[\r\n]*$' then
            local offset = guide.offsetOf(lines, i-1, start-1)
            if isInString(ast.ast, offset) then
                goto NEXT_LINE
            end
            callback {
                start   = start,
                finish  = finish,
                message = lang.script.DIAG_LINE_ONLY_SPACE,
            }
            goto NEXT_LINE
        end

        local pos = line:find '[ \t]+[\r\n]*$'
        if pos then
            start = start + pos - 1
            local offset = guide.offsetOf(lines, i-1, start-1)
            if isInString(ast.ast, offset) then
                goto NEXT_LINE
            end
            callback {
                start   = start,
                finish  = finish,
                message = lang.script.DIAG_LINE_POST_SPACE,
            }
            goto NEXT_LINE
        end

        ::NEXT_LINE::
    end
end