This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Kryptanyte's profile picture

Comment Clearing Program

Started by Kryptanyte, 21 November 2013 - 11:52 PM
Kryptanyte #1
Posted 22 November 2013 - 12:52 AM
I don't really know a real useful reason you would want to use this, but it's there if you need it.

The title says it all, this program will clear programs of comments, however it will not clear any multi line comments Aka –[[ Comment in Here ]] (EDIT HERE), Block Comments

Program: http://pastebin.com/2hHydSQt

Again for those who prefer spoiler tags
SpoilertArgs = {…}

function stringSplit(sString)

local chars= {}

for i = 1, #sString do

chars = sString:sub(i, i)

end

return chars

end


function comment(line)

local letters = {}

if line == nil or line == "" then

return "empty", line

end

local letters = stringSplit(line)

if letters == nil or #letters == 0 then

return "empty", line

end

for i = 1, #letters do

if letters == "-" and letters[i+1] == "-" then

if letters[i+2] == "[" and letters[i+3] == "[" then

return "empty", line

elseif letters[i-1] == "]" and letters[i-2] == "]" then

return "empty", line

end

local clearLine = {}

for t = 1, i-1 do

table.insert(clearLine, letters[t])

end

return "clearLine", table.concat(clearLine)

end

end

return "empty", line

end

function lines(str)

local t = {}
local function helper(line) table.insert(t, line) return "" end
helper((str:gsub("(.-)\r?\n", helper)))
return t

end

function clearFile(Path, newPath)

local file = fs.open(Path, "r")
fileTotal = file.readAll()
file.close()
local fileTable = {}
local newFile = {}

local fileTable = lines(fileTotal)


for i = 1,#fileTable do

local result, Q = comment(fileTable)

if result == "empty" then

table.insert(newFile, Q)

elseif result == "clearLine" then

table.insert(newFile, Q)

end

end

local file = fs.open(newPath, "w")

for i = 1, #newFile do

file.writeLine(newFile)

end

file.close()

end

if tArgs[2] == nil then

clearFile(tArgs[1], tArgs[1])

else

clearFile(tArgs[1], tArgs[2])

end

Usage:



ClearC file1 file2	 -- This will clear file1 of comments and place the cleared file in file2

OR

ClearC file1		 -- This will clear file1 of comments and replace it in the same file


Also note this program does not remove cleared lines if they are emptied and there is no text before the comment, so;



function this()

	print("Some text here") -- Comment Here

	-- And Another Comment

end

Will turn into this:

function this()

	print("Some text here")



end


Post any bugs you get when using this program and if you really want me to fix the empty line thing just say, if there's enough interest then I will try and get it to work.
Edited on 22 November 2013 - 05:24 AM
rhyleymaster #2
Posted 22 November 2013 - 01:16 AM
Or we could just use backspace, or Notepad++ to remove them all. But I guess some people feel the urge to remove comments another way.
Cozzimoto #3
Posted 22 November 2013 - 05:39 AM
what about block comments too.


function this()
  print("Some text here")
  --[[ Multi line comment
  For Explaining things
  ON multiple lines
  --]]

end

Edited on 22 November 2013 - 04:43 AM
Kryptanyte #4
Posted 22 November 2013 - 06:23 AM
Yeah, I did say it wouldn't do them. I can add it in if you really want me to.
theoriginalbit #5
Posted 22 November 2013 - 06:23 AM
I think it'd be good to add, quite a lot of people use them. While you're at it what about this block comment?


--[=[
Multi-line comment that
supports use of [[ ]] inside of it.
]=]
Edited on 30 November 2013 - 06:56 PM
Kryptanyte #6
Posted 22 November 2013 - 06:26 AM
While you're at it what about this block comment?


--[=[
Multi-line comment that
supports use of [[ ]] inside of it.
]]

Maybe I'll do the regular block comment first…
DiabolusNeil #7
Posted 22 November 2013 - 02:13 PM
I actually find this useful for myself. I add a lot of comments in my code for myself for developing, but I really don't want other people to see them.

EDIT: Grammatical correction.
Edited on 22 November 2013 - 01:14 PM
D3matt #8
Posted 30 November 2013 - 01:55 PM
Not wanting other people to see your comments is rather sad, tbh. Have you seen some of the comments in commercial program source code? *shudders*
Lyqyd #9
Posted 30 November 2013 - 01:58 PM
I think it'd be good to add, quite a lot of people use them. While you're at it what about this block comment?


--[=[
Multi-line comment that
supports use of [[ ]] inside of it.
]]

That one is invalid, since the closing brackets needs to match the opening brackets.
theoriginalbit #10
Posted 30 November 2013 - 07:56 PM
That one is invalid, since the closing brackets needs to match the opening brackets.
Whoops, I knew it didn't look right. Couldn't put my finger on it. thanks.