Skip to content

Commit

Permalink
Merge pull request #6 from yogesh19k/main
Browse files Browse the repository at this point in the history
Added support for multi-line comments
  • Loading branch information
mainak55512 authored Aug 25, 2024
2 parents e42b53b + 2249c3b commit 50c4378
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
31 changes: 31 additions & 0 deletions comments.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,34 @@ var comment_map map[string]string = map[string]string{
"clj": ";;",
"lisp": ";",
}

/* Multi Line comments Map, Key: extension name , value:[Opening symbol]:[Closing symbol] */
var multi_comment_map map[string]string = map[string]string{
"go": "/*:*/",
"c": "/*:*/",
"cpp": "/*:*/",
"js": "/*:*/",
"ts": "/*:*/",
"jsx": "/*:*/",
"tsx": "/*:*/",
"java": "/*:*/",
"rs": "/*:*/",
"swift": "/*:*/",
"kt": "/*:*/",
"php": "/*:*/",
"m": "/*:*/",
"groovy": "/*:*/",
"cs": "/*:*/",
"scala": "/*:*/",
"zig": "/*:*/",
"gleam": "/*:*/",
"py": `""":"""`,
"sh": ": ':'",
"pl": "/*:*/",
"jl": "#=:=#",
"lua": "--[[:]]--",
"hs": "/*:*/",
"sql": "/*:*/",
"cbl": "/*:*/",
"erl": "=begin:=cut",
}
25 changes: 24 additions & 1 deletion utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,37 @@ func countLines(file_name string, ext string) (int32, int32, int32, int32) {
var code int32 = 0
var gap int32 = 0
var comments int32 = 0
var inside_multi_line_comment bool =false
var multi_comment_str_open string=""
var multi_comment_str_close string=""
comment_str, exists := comment_map[ext]
multi_comment_str_pair,multi_exists :=multi_comment_map[ext]
//multi_comment_str_pair will have the opening and closing symbols ' : ' separated
if multi_exists{
multi_comment_str_open=strings.Split(multi_comment_str_pair,":")[0]
multi_comment_str_close=strings.Split(multi_comment_str_pair,":")[1]
}
for {
content, _, err := reader.ReadLine()
content_str := string(content[:])
if err != nil {
break
}
if content_str == "" {
//Checks if [Opening symbol] is present at staring of the line
if multi_exists && strings.HasPrefix(strings.TrimSpace(content_str),multi_comment_str_open){
inside_multi_line_comment=true
}
//Checks if [Closing symbol] is present at staring or at the end of the line
if multi_exists &&
strings.HasPrefix(strings.TrimSpace(content_str),multi_comment_str_close) ||
strings.HasSuffix(strings.TrimSpace(content_str),multi_comment_str_close){
inside_multi_line_comment=false
comments++
}
//Moved the inside_multi_line_comment to top condition as it has priority over other cases
if inside_multi_line_comment{
comments++
}else if content_str == "" {
gap++
} else if exists == true && strings.HasPrefix(strings.TrimSpace(content_str), comment_str) {
comments++
Expand Down

0 comments on commit 50c4378

Please sign in to comment.