orbitalquark / textadept

Textadept is a fast, minimalist, and remarkably extensible cross-platform text editor for programmers.
https://orbitalquark.github.io/textadept
MIT License
649 stars 38 forks source link

lua interpeter scope not working properly #534

Closed YahYahDev closed 4 months ago

YahYahDev commented 4 months ago

i was working on making a module and was trying to use a if condition in a for loop and it could not modify a table from outside the if statement. i did quite alot of testing and it is not the libs im using and the bug persists even if the variables are global or not, im on ta 12.3.

local parse = require("ctf.Parse")
    os.execute("clear")
    local dirpath = parse.GetAllBlock(buffer.filename, "^", "/") -- Gets path minus filename

    for i = 1, #dirpath -1 do -- Reconstructs path from choped buffer.filename
        dirpath[1] = str.Merge(dirpath[1].."/", dirpath[i + 1]) 
    end

    local ctags = io.popen("ctags -f - -R "..dirpath[1].."/".." --kinds-C=t") -- Use ctags to parse a projects directory
    local tags = ctags:read("*a")
    ctags:close()

    print(tags)
    types = {}

    content = parse.GetLines(tags)

    for i = 1, #content do
        -- Bug if statement?
        if string.find(content[i], "typedef") then
            types[i] = parse.GetBlock(content[i], "^", "%s") -- attempt to update state of table
        end
        str.PrintArray(types)
    end
    str.PrintArray(types)

    events.connect(events.LEXER_LOADED, function(name)
        if name == 'ansi_c' then buffer.lexer:set_word_list(lexer.TYPE, types, true) end -- Update word list to include custom types
    end)
YahYahDev commented 4 months ago

UPDATE: updated to 12.4 and it still does not work

orbitalquark commented 4 months ago

It's not clear to me what is failing. Could you give an example of what you are seeing vs. what you are expecting?

YahYahDev commented 4 months ago

so basicly im trying to insert values into the "types" table but for some reason it is not applying when i attempt to append it in an "if block" i am unsure of what the cause is but ive tested quite a bit with it and when you make a "print" statement in the "if block" for the "types" table it prints it but as soon as you attempt to "print" outside of the "if block" all changes made are reversed and it reverts to the default table state. i might just be being stupid but i have around 3-4 years of scripting in lua and have never had this issue before and its boggling my mind.

YahYahDev commented 4 months ago

updated the code example to show which if statement was having issues.

YahYahDev commented 4 months ago

i think a better example of what im looking at is this

    types = {}
    content = parse.GetLines(tags)

    for i = 1, #content do
        if string.find(content[i], "typedef") then
            types[i] = parse.GetBlock(content[i], "^", "%s")
            print(types[1]) -- prints what is expected
        end
    end
    print(types[1]) -- prints nil
orbitalquark commented 4 months ago

In your original code example, types will likely be a table with "holes" (in Lua-speak). That is, there will be some iterations of i where types[i] will not be set due to conditional failure. I suspect str.PrintArray() uses the # operator, which has undefined behavior if the table has holes in it. lexer:set_word_list() will also fail for tables with holes in them.

Instead of types[i] = ..., how about using types[#types + 1] = ...?

YahYahDev commented 4 months ago

that works! i had no idea that the # operator didnt give you the number of non nil elements gues you learn somthin new every day xD.