Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Casting warning when there should be none. #2727

Closed
gesslar opened this issue Jun 21, 2024 · 2 comments
Closed

Casting warning when there should be none. #2727

gesslar opened this issue Jun 21, 2024 · 2 comments

Comments

@gesslar
Copy link

gesslar commented Jun 21, 2024

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
Copy link

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
Copy link
Author

gesslar commented Jun 30, 2024

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

@gesslar gesslar closed this as completed Jun 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants