LuaLS / lua-language-server

A language server that offers Lua language support - programmed in Lua
https://luals.github.io
MIT License
3.29k stars 305 forks source link

Casting warning when there should be none. #2727

Closed gesslar closed 3 months ago

gesslar commented 3 months ago

How are you using the lua-language-server?

Visual Studio Code Extension (sumneko.lua)

Which OS are you using?

Windows

What is the issue affecting?

Type Checking

Expected Behaviour

I am expecting it to not warn me about a casting issue when there is none.

Actual Behaviour

I am getting a casting warning on a variable that should not be an issue.

[{
    "resource": "/D:/git/ThreshCopy/src/scripts/ThreshCopy/Copy_Collapsed.lua",
    "owner": "_generated_diagnostic_collection_name_#0",
    "code": "cast-local-type",
    "severity": 4,
    "message": "This variable is defined as type `string`. Cannot convert its type to `string|nil`.\n- `nil` cannot match `string`\n- Type `nil` cannot match `string`",
    "source": "Lua Diagnostics.",
    "startLineNumber": 56,
    "startColumn": 3,
    "endLineNumber": 56,
    "endColumn": 18
}]

Reproduction steps

While it is true that getSelection() can return nil, I do accommodate for it with my or statement

parsed = parsed .. (getSelection(window) or "")

This is where it is squiggling image

Additional Notes

Here is everything that is relevant that should help.

local ThreshCopy = {}

function ThreshCopy:trim(s)
    return s:match("^%s*(.-)%s*$")
end

function ThreshCopy:getSelectedText(window, startCol, startRow, endCol, endRow)
    -- Check whether there's an actual selection
    if startCol == endCol and startRow == endRow then return "" end
    local parsed = ""
    -- Loop through each symbol within the range
    for lineNum = startRow, endRow do
        local cStart = lineNum == startRow and startCol or 0
        moveCursor(window, cStart, lineNum)
        local cEnd = lineNum == endRow and endCol or #getCurrentLine() - 1
        selectSection(window, cStart, cEnd - cStart + 1)
        parsed = parsed .. (getSelection(window) or "")
        if lineNum ~= endRow then parsed = parsed .. "\n" end
    end
    return parsed
end

ThreshCopy.handler = function(event, menu, ...)
    local text = ThreshCopy:getSelectedText(...)
    -- Split the text into lines, trim each line, and handle blank lines separately
    local lines = {}
    for line in text:gmatch("([^\n]*)\n?") do
        if line == "" then
            table.insert(lines, "")
        else
            table.insert(lines, ThreshCopy:trim(line))
        end
    end

    -- Join lines, preserving empty lines as blank lines
    local withoutNewLines = ""
    local previousLineEmpty = false
    for _, line in ipairs(lines) do
        if line == "" then
            withoutNewLines = withoutNewLines .. "\n\n"
            previousLineEmpty = true
        else
            if #withoutNewLines > 0 and not previousLineEmpty then
                withoutNewLines = withoutNewLines .. " "
            end
            withoutNewLines = withoutNewLines .. line
            previousLineEmpty = false
        end
    end

    -- Remove any trailing newlines
    withoutNewLines = withoutNewLines:gsub("%s*\n*$", "")

    setClipboardText(withoutNewLines)
end

Log File

No response

nospam2998 commented 3 months ago

Could you @gesslar perhaps please provide a minimized example?

I believe there is a larger chance to actually resolve this issue if reducing the code only to the few lines to illustrate the problem you are having. With half-a-thousand open tickets, monotonously increasing, one wonders what the purpose of filing an obviously non-actionable ticket on this project is.

gesslar commented 3 months ago

If, as you say, nothing can be done. Then nothing can be done. Good day.