Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why my plugin keeps adding an extra line to a script source?

Asked by 2 years ago
Edited 2 years ago

I am working on a plugin that helps you edit and fix your code.

I made a 'replace keyword' system but when a keyword is replaced in another script source, an extra line comes in the source.

local rptext = ui.Frame.Replace.Text
local withtext = ui.Frame.With.Text


local lines = {}

for line in string.gmatch(item.Source, "[^\n]*") do
    if string.find(line,rptext) then
        local modifiedline = string.gsub(line,rptext,withtext)

        table.insert(lines, modifiedline)

    else
        table.insert(lines, line)

    end

end


item.Source = table.concat(lines,'\n')

1 answer

Log in to vote
0
Answered by
imKirda 4491 Moderation Voter Community Moderator
2 years ago
Edited 2 years ago

i was trying to find the cause of the issue for a long time but it's kind of hard, i will make an update if i find out about it. what i could find out was that it seems to match some character after some non-empty newlines which returns nil when put into string.byte.

the solution for your case is to just string.split, there really is no need for gmatch, split the string by newlines:

local rptext = ui.Frame.Replace.Text
local withtext = ui.Frame.With.Text

local lines = string.split(item.Source, "\n")

for i, line in ipairs(lines) do
    lines[i] = string.gsub(line, rptext, withtext)
end

item.Source = table.concat(lines, "\n")

notice there is no reason to check if string.find finds something, gsub won't replace anything if it's not found, you are redundantly doing 2 pattern searches.

now let's get to feedback part, your plugin is absolute trash... like who can't just click Ctrl + H you self appointed programmer ;-;

Ad

Answer this question