11local CommentExtractor = {}
22
3- --- Extracts comments with their associated code context
4- -- @param lines table The lines of the Lua file as a table of strings.
5- -- @return table A table containing comment blocks with context.
6- function CommentExtractor .extractComments (lines )
3+ --- Extracts comment blocks that belong together
4+ function CommentExtractor .extractBlocks (lines )
75 local blocks = {}
8- local currentCommentBlock = {}
9- local i = 1
6+ local currentBlock = {
7+ comments = {},
8+ codeContext = nil
9+ }
1010
11+ local i = 1
1112 while i <= # lines do
12- local line = lines [i ]:match (" ^%s*(.*)" ) -- Trim leading whitespace
13+ local line = lines [i ]
14+ local trimmed = line :match (" ^%s*(.-)%s*$" )
1315
1416 -- Check if this is a comment line
15- if line :find (" ^%-%-%-" ) or line :find (" ^%-%-" ) then
16- table.insert (currentCommentBlock , line )
17- elseif # currentCommentBlock > 0 then
18- -- We have accumulated comments, check if next non-empty line is code
19- local codeContext = nil
20- local j = i
21-
22- -- Skip empty lines to find the actual code
23- while j <= # lines and lines [j ]:match (" ^%s*$" ) do
24- j = j + 1
17+ if trimmed :match (" ^%-%-%-" ) or trimmed :match (" ^%-%-" ) then
18+ table.insert (currentBlock .comments , trimmed )
19+ elseif # currentBlock .comments > 0 then
20+ -- We have comments, now look for the code that follows
21+ local codeLineIndex = CommentExtractor .findNextCodeLine (lines , i )
22+ if codeLineIndex then
23+ local codeLine = lines [codeLineIndex ]:match (" ^%s*(.-)%s*$" )
24+ currentBlock .codeContext = CommentExtractor .analyzeCode (codeLine , codeLineIndex )
2525 end
2626
27- if j <= # lines then
28- local codeLine = lines [j ]:match (" ^%s*(.*)" )
29- -- Check if it's a function, class, property, etc.
30- if codeLine :find (" ^function" ) or
31- codeLine :find (" ^local function" ) or
32- codeLine :find (" ^local%s+%w+%s*=" ) or
33- codeLine :find (" ^%w+%.%w+" ) then
34- codeContext = {
35- type = CommentExtractor .getCodeType (codeLine ),
36- name = CommentExtractor .extractName (codeLine ),
37- line = codeLine ,
38- lineNumber = j
39- }
40- end
41- end
42-
43- -- Add the comment block with its context
44- table.insert (blocks , {
45- comments = currentCommentBlock ,
46- context = codeContext
47- })
48-
49- currentCommentBlock = {}
27+ -- Save this block and start a new one
28+ table.insert (blocks , currentBlock )
29+ currentBlock = {comments = {}, codeContext = nil }
5030 end
5131
5232 i = i + 1
5333 end
5434
55- -- Handle any remaining comments
56- if # currentCommentBlock > 0 then
57- table.insert (blocks , {
58- comments = currentCommentBlock ,
59- context = nil
60- })
35+ -- Handle remaining comments
36+ if # currentBlock .comments > 0 then
37+ table.insert (blocks , currentBlock )
6138 end
6239
6340 return blocks
6441end
6542
66- --- Determines the type of code (function, class, property, etc.)
67- function CommentExtractor .getCodeType (codeLine )
68- if codeLine :find (" ^function" ) or codeLine :find (" ^local function" ) then
69- return " function"
70- elseif codeLine :find (" ^local%s+%w+%s*=%s*setmetatable" ) then
71- return " class"
72- elseif codeLine :find (" ^local%s+%w+%s*=" ) then
73- return " variable"
74- elseif codeLine :find (" ^%w+%.defineProperty" ) then
75- return " property_definition"
76- else
77- return " unknown"
43+ --- Find the next non-empty code line
44+ function CommentExtractor .findNextCodeLine (lines , startIndex )
45+ for i = startIndex , # lines do
46+ local trimmed = lines [i ]:match (" ^%s*(.-)%s*$" )
47+ if trimmed ~= " " and not trimmed :match (" ^%-%-" ) then
48+ return i
49+ end
7850 end
51+ return nil
7952end
8053
81- --- Extracts the name from a code line
82- function CommentExtractor .extractName (codeLine )
54+ --- Analyze what kind of code this is
55+ function CommentExtractor .analyzeCode (codeLine , lineNumber )
8356 -- Function patterns
84- local funcName = codeLine :match (" ^function%s+([%w%.%:]+)" )
85- if funcName then return funcName end
57+ if codeLine :match (" ^function%s+([%w%.%:]+)" ) then
58+ local name = codeLine :match (" ^function%s+([%w%.%:]+)" )
59+ return {type = " function" , name = name , line = codeLine , lineNumber = lineNumber }
60+ end
8661
87- local localFuncName = codeLine :match (" ^local%s+function%s+([%w%.%:]+)" )
88- if localFuncName then return localFuncName end
62+ if codeLine :match (" ^local%s+function%s+([%w%.%:]+)" ) then
63+ local name = codeLine :match (" ^local%s+function%s+([%w%.%:]+)" )
64+ return {type = " function" , name = name , line = codeLine , lineNumber = lineNumber }
65+ end
8966
90- -- Variable/class patterns
91- local varName = codeLine :match (" ^local%s+([%w_]+)%s*=" )
92- if varName then return varName end
67+ -- Class/variable patterns
68+ if codeLine :match (" ^local%s+([%w_]+)%s*=%s*setmetatable" ) then
69+ local name = codeLine :match (" ^local%s+([%w_]+)%s*=" )
70+ return {type = " class" , name = name , line = codeLine , lineNumber = lineNumber }
71+ end
9372
94- -- Method patterns
95- local methodName = codeLine :match (" ^([%w%.%:]+)%s*=" )
96- if methodName then return methodName end
73+ if codeLine :match (" ^local%s+([%w_]+)%s*=" ) then
74+ local name = codeLine :match (" ^local%s+([%w_]+)%s*=" )
75+ return {type = " variable" , name = name , line = codeLine , lineNumber = lineNumber }
76+ end
9777
98- return " unknown"
78+ return { type = " unknown" , name = " unknown " , line = codeLine , lineNumber = lineNumber }
9979end
10080
10181return CommentExtractor
0 commit comments