From b8ba1a44c42d65598f5e6f2baf5e7ba694117e2f Mon Sep 17 00:00:00 2001 From: Shashank Pachava Date: Thu, 10 Oct 2024 14:48:34 -0400 Subject: [PATCH] Fix string literal cutting logic across Go and Java outputs Adjust trimming logic to maintain quotes accurately when cutting long string literals in both Go and Java outputs. --- codemap/go_output.go | 10 ++++++---- codemap/java_output.go | 4 ++-- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/codemap/go_output.go b/codemap/go_output.go index 23112ad..eebdf09 100644 --- a/codemap/go_output.go +++ b/codemap/go_output.go @@ -106,7 +106,7 @@ func generateGoFileOutput(src []byte, maxLiteralLen int) (string, error) { for _, capture := range match.Captures { start := capture.Node.StartByte() end := capture.Node.EndByte() - content := src[start:end] + content := string(src[start:end]) // Check if the string literal is within a function or method body inBody := false @@ -117,10 +117,12 @@ func generateGoFileOutput(src []byte, maxLiteralLen int) (string, error) { } } - if !inBody && len(content)-2 > maxLiteralLen { // -2 for the quotes + str := strings.Trim(content, "\"`") + quoteLen := (len(content) - len(str)) / 2 + if !inBody && len(str) > maxLiteralLen { cutRanges = append(cutRanges, cutRange{ - start: start + uint(maxLiteralLen) + 1, // +1 to keep the starting quote - end: end - 1, // -1 to keep the closing quote + start: start + uint(maxLiteralLen) + uint(quoteLen), // +quoteLen to keep the starting quotes + end: end - uint(quoteLen), // -quoteLen to keep the closing quotes addEllipsis: true, }) } diff --git a/codemap/java_output.go b/codemap/java_output.go index 6b23653..2482c2f 100644 --- a/codemap/java_output.go +++ b/codemap/java_output.go @@ -119,8 +119,8 @@ func generateJavaFileOutput(src []byte, maxLiteralLen int) (string, error) { quoteLen := (len(content) - len(str)) / 2 if !inBody && len(str) > maxLiteralLen { cutRanges = append(cutRanges, cutRange{ - start: start + uint(maxLiteralLen) + uint(quoteLen), // +1 to keep the starting quote - end: end - uint(quoteLen), // -quoteLen to keep the closing quote + start: start + uint(maxLiteralLen) + uint(quoteLen), // +quoteLen to keep the starting quotes + end: end - uint(quoteLen), // -quoteLen to keep the closing quotes addEllipsis: true, }) }