@@ -390,10 +390,26 @@ func processInternal(cfg config, file *os.File, pairs map[string]map[string]stri
390390 trimmedNextLine = nextLine
391391 }
392392
393- // Only check top-level definitions.
394- // Example: on a line like "func f() {", inFunc is still false here.
395- // Without the explicit func check, the next line could be treated like a top-level definition.
396- if ! inFunc && ! inInterface && ! strings .HasPrefix (trimmedLine , "func " ) && isValidDefinition (trimmedNextLine , & inGroup , & inStruct ) {
393+ // Track whether nextLine is inside a function or interface block.
394+ // We update this first so the first line inside a block is not treated
395+ // as a top-level definition.
396+ if strings .HasPrefix (trimmedLine , "func " ) {
397+ funcSpaces = indentSize
398+ inFunc = true
399+ } else if inFunc && trimmedLine == "}" && funcSpaces == indentSize {
400+ funcSpaces = - 1
401+ inFunc = false
402+ }
403+ if strings .HasSuffix (trimmedLine , "interface {" ) {
404+ interfaceSpaces = indentSize
405+ inInterface = true
406+ } else if inInterface && trimmedLine == "}" && interfaceSpaces == indentSize {
407+ interfaceSpaces = - 1
408+ inInterface = false
409+ }
410+
411+ // Check for new doc links to add on top-level definitions only.
412+ if ! inFunc && ! inInterface && isValidDefinition (trimmedNextLine , & inGroup , & inStruct ) {
397413 // Find the "Exposed As" line in the doc comment
398414 var existingDoclink string
399415 comScanner := bufio .NewScanner (strings .NewReader (commentBlock ))
@@ -453,23 +469,6 @@ func processInternal(cfg config, file *os.File, pairs map[string]map[string]stri
453469 }
454470 }
455471
456- // update inFunc after we actually check for doclinks to allow us to check
457- // a function's definition, without checking anything inside the function
458- if strings .HasPrefix (trimmedLine , "func " ) {
459- funcSpaces = indentSize
460- inFunc = true
461- } else if inFunc && trimmedLine == "}" && funcSpaces == indentSize {
462- funcSpaces = - 1
463- inFunc = false
464- }
465- if strings .HasSuffix (trimmedLine , "interface {" ) {
466- interfaceSpaces = indentSize
467- inInterface = true
468- } else if inInterface && trimmedLine == "}" && interfaceSpaces == indentSize {
469- interfaceSpaces = - 1
470- inInterface = false
471- }
472-
473472 newFile += line + "\n "
474473 }
475474
0 commit comments