Skip to content

Commit

Permalink
Fix string literal cutting logic across Go and Java outputs
Browse files Browse the repository at this point in the history
Adjust trimming logic to maintain quotes accurately when cutting long string literals in both Go and Java outputs.
  • Loading branch information
spachava753 committed Oct 10, 2024
1 parent c41bb82 commit b8ba1a4
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
10 changes: 6 additions & 4 deletions codemap/go_output.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
})
}
Expand Down
4 changes: 2 additions & 2 deletions codemap/java_output.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
}
Expand Down

0 comments on commit b8ba1a4

Please sign in to comment.