Fix OutOfBound line number issue

This fix handles scenarios where ChatGPT incorrectly returns a line
number that exceeds the actual number of lines in the code.
Additionally, log clarity is enhanced by omitting the exception body
previously logged when the code range is not accurately retrieved.

Change-Id: I31a3e8329b7daea8cf7fd08ff9c55dd1aa087d56
Signed-off-by: Patrizio <patrizio.gelosi@amarulasolutions.com>
diff --git a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/common/client/patch/code/InlineCode.java b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/common/client/patch/code/InlineCode.java
index ae41d44..06de7ee 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/common/client/patch/code/InlineCode.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/common/client/patch/code/InlineCode.java
@@ -33,7 +33,7 @@
             return joinWithNewLine(codeByRange);
         }
         else {
-            return getLine(commentProperty);
+            return getLineFromLineNumber(commentProperty.getLine());
         }
     }
 
@@ -51,7 +51,10 @@
     }
 
     private String getLineSlice(int line_num) {
-        String line = newContent.get(line_num);
+        String line = getLineFromLineNumber(line_num);
+        if (line == null) {
+            throw new RuntimeException("Error retrieving line number from content");
+        }
         try {
             if (line_num == range.endLine) {
                 line = line.substring(0, range.endCharacter);
@@ -61,19 +64,27 @@
             }
         }
         catch (StringIndexOutOfBoundsException e) {
-            log.info("Could not extract a slice from line \"{}\". The whole line is returned", line, e);
+            log.info("Could not extract a slice from line \"{}\". The whole line is returned", line);
         }
         return line;
     }
 
-    private String getLine(GerritComment commentProperty) {
+    private String getLineFromLineNumber(int line_num) {
+        String line = null;
         try {
-            return newContent.get(commentProperty.getLine());
+            line = newContent.get(line_num);
         }
         catch (IndexOutOfBoundsException e) {
-            log.warn("Could not extract line #{} from the code", commentProperty.getLine(), e);
-            return null;
+            // If the line number returned by ChatGPT exceeds the actual number of lines, return the last line
+            int lastLine = newContent.size() - 1;
+            if (line_num > lastLine) {
+                line = newContent.get(lastLine);
+            }
+            else {
+                log.warn("Could not extract line #{} from the code", line_num);
+            }
         }
+        return line;
     }
 
 }