11package annotation
22
33import (
4- "strings"
4+ "strings"
55)
66
77// Normalizer strips language-specific comment syntax from raw documentation blocks.
@@ -11,7 +11,7 @@ type Normalizer struct{}
1111// NewNormalizer creates a Normalizer.
1212// @intent provide a reusable comment normalizer for annotation extraction
1313func NewNormalizer () * Normalizer {
14- return & Normalizer {}
14+ return & Normalizer {}
1515}
1616
1717// Normalize removes comment delimiters and line prefixes from raw comment text.
@@ -21,81 +21,88 @@ func NewNormalizer() *Normalizer {
2121// @see annotation.stripBlockDelimiters
2222// @see annotation.stripLinePrefix
2323func (n * Normalizer ) Normalize (text string , language string ) string {
24- if text == "" {
25- return ""
26- }
24+ if text == "" {
25+ return ""
26+ }
2727
28- text = stripBlockDelimiters (text , language )
28+ text = stripBlockDelimiters (text , language )
2929
30- lines := strings .Split (text , "\n " )
31- var result []string
30+ lines := strings .Split (text , "\n " )
31+ var result []string
3232
33- for _ , line := range lines {
34- stripped := stripLinePrefix (line , language )
35- stripped = strings .TrimRight (stripped , " \t " )
36- if stripped != "" || len (result ) > 0 {
37- result = append (result , stripped )
38- }
39- }
33+ for _ , line := range lines {
34+ stripped := stripLinePrefix (line , language )
35+ stripped = strings .TrimRight (stripped , " \t " )
36+ if stripped != "" || len (result ) > 0 {
37+ result = append (result , stripped )
38+ }
39+ }
4040
41- for len (result ) > 0 && result [len (result )- 1 ] == "" {
42- result = result [:len (result )- 1 ]
43- }
41+ for len (result ) > 0 && result [len (result )- 1 ] == "" {
42+ result = result [:len (result )- 1 ]
43+ }
4444
45- return strings .Join (result , "\n " )
45+ return strings .Join (result , "\n " )
4646}
4747
4848// stripBlockDelimiters removes outer block-comment wrappers for the given language.
4949// @intent keep only the inner documentation payload from block-style comments
5050func stripBlockDelimiters (text string , language string ) string {
51- switch language {
52- case "go" , "java" , "cpp" , "c" , "csharp" , "typescript" , "javascript" , "kotlin" , "swift" , "scala" :
53- if strings .HasPrefix (text , "/**" ) {
54- text = strings .TrimPrefix (text , "/**" )
55- text = strings .TrimSuffix (strings .TrimSpace (text ), "*/" )
56- } else if strings .HasPrefix (text , "/*" ) {
57- text = strings .TrimPrefix (text , "/*" )
58- text = strings .TrimSuffix (strings .TrimSpace (text ), "*/" )
59- }
60- case "python" :
61- if strings .HasPrefix (text , `"""` ) && strings .HasSuffix (text , `"""` ) {
62- text = strings .TrimPrefix (text , `"""` )
63- text = strings .TrimSuffix (text , `"""` )
64- } else if strings .HasPrefix (text , "'''" ) && strings .HasSuffix (text , "'''" ) {
65- text = strings .TrimPrefix (text , "'''" )
66- text = strings .TrimSuffix (text , "'''" )
67- }
68- }
69- return text
51+ switch language {
52+ case "go" , "java" , "cpp" , "c" , "csharp" , "typescript" , "javascript" , "kotlin" , "swift" , "scala" :
53+ if strings .HasPrefix (text , "/**" ) {
54+ text = strings .TrimPrefix (text , "/**" )
55+ text = strings .TrimSuffix (strings .TrimSpace (text ), "*/" )
56+ } else if strings .HasPrefix (text , "/*" ) {
57+ text = strings .TrimPrefix (text , "/*" )
58+ text = strings .TrimSuffix (strings .TrimSpace (text ), "*/" )
59+ }
60+ case "python" :
61+ if strings .HasPrefix (text , `"""` ) && strings .HasSuffix (text , `"""` ) {
62+ text = strings .TrimPrefix (text , `"""` )
63+ text = strings .TrimSuffix (text , `"""` )
64+ } else if strings .HasPrefix (text , "'''" ) && strings .HasSuffix (text , "'''" ) {
65+ text = strings .TrimPrefix (text , "'''" )
66+ text = strings .TrimSuffix (text , "'''" )
67+ }
68+ }
69+ return text
7070}
7171
7272// stripLinePrefix removes one line-level comment marker from a comment line.
7373// @intent normalize individual documentation lines across language comment syntaxes
7474func stripLinePrefix (line string , language string ) string {
75- trimmed := strings .TrimLeft (line , " \t " )
75+ trimmed := strings .TrimLeft (line , " \t " )
7676
77- switch language {
78- case "go" , "cpp" , "c" , "csharp" , "typescript" , "javascript" , "kotlin" , "swift" , "scala" , "java" :
79- if strings .HasPrefix (trimmed , "// " ) {
80- return trimmed [3 :]
81- }
82- if strings .HasPrefix (trimmed , "//" ) {
83- return trimmed [2 :]
84- }
85- if strings .HasPrefix (trimmed , "* " ) {
86- return trimmed [2 :]
87- }
88- if trimmed == "*" {
89- return ""
90- }
91- case "python" , "ruby" :
92- if strings .HasPrefix (trimmed , "# " ) {
93- return trimmed [2 :]
94- }
95- if strings .HasPrefix (trimmed , "#" ) {
96- return trimmed [1 :]
97- }
98- }
77+ switch language {
78+ case "go" , "cpp" , "c" , "csharp" , "typescript" , "javascript" , "kotlin" , "swift" , "scala" , "java" :
79+ if strings .HasPrefix (trimmed , "// " ) {
80+ return trimmed [3 :]
81+ }
82+ if strings .HasPrefix (trimmed , "//" ) {
83+ return trimmed [2 :]
84+ }
85+ if strings .HasPrefix (trimmed , "* " ) {
86+ return trimmed [2 :]
87+ }
88+ if trimmed == "*" {
89+ return ""
90+ }
91+ case "python" , "ruby" :
92+ if strings .HasPrefix (trimmed , "# " ) {
93+ return trimmed [2 :]
94+ }
95+ if strings .HasPrefix (trimmed , "#" ) {
96+ return trimmed [1 :]
97+ }
98+ case "lua" :
99+ if strings .HasPrefix (trimmed , "-- " ) {
100+ return trimmed [3 :]
101+ }
102+ if strings .HasPrefix (trimmed , "--" ) {
103+ return trimmed [2 :]
104+ }
105+ }
99106
100- return trimmed
107+ return trimmed
101108}
0 commit comments