@@ -44,7 +44,7 @@ private IpynbNotebookParser(PythonInputFile inputFile) {
44
44
}
45
45
46
46
private final PythonInputFile inputFile ;
47
- private StringBuilder aggregatedSource = new StringBuilder ();
47
+ private final StringBuilder aggregatedSource = new StringBuilder ();
48
48
49
49
// Keys are the aggregated source line number
50
50
private final Map <Integer , IPythonLocation > locationMap = new HashMap <>();
@@ -76,7 +76,7 @@ private void processCodeCell(JsonParser jParser) throws IOException {
76
76
JsonToken jsonToken = jParser .nextToken ();
77
77
if (JsonToken .FIELD_NAME .equals (jsonToken ) && "source" .equals (jParser .currentName ())) {
78
78
jsonToken = jParser .nextToken ();
79
- if (parseSourceArray (jParser , jsonToken )) {
79
+ if (parseSourceArray (jParser , jsonToken ) || parseSourceMultilineString ( jParser , jsonToken ) ) {
80
80
break ;
81
81
} else {
82
82
throw new IllegalStateException ("Unexpected token: " + jsonToken );
@@ -91,16 +91,38 @@ private boolean parseSourceArray(JsonParser jParser, JsonToken jsonToken) throws
91
91
}
92
92
while (jParser .nextToken () != JsonToken .END_ARRAY ) {
93
93
String sourceLine = jParser .getValueAsString ();
94
- JsonLocation tokenLocation = jParser .currentTokenLocation ();
94
+ addLineToSource (sourceLine , jParser .currentTokenLocation ());
95
+ }
96
+ // Account for the last cell delimiter
97
+ addDelimiterToSource ();
98
+ return true ;
99
+ }
100
+
101
+ private boolean parseSourceMultilineString (JsonParser jParser , JsonToken jsonToken ) throws IOException {
102
+ if (jsonToken != JsonToken .VALUE_STRING ) {
103
+ return false ;
104
+ }
105
+ String sourceLine = jParser .getValueAsString ();
106
+ JsonLocation tokenLocation = jParser .currentTokenLocation ();
95
107
96
- aggregatedSource . append ( sourceLine );
97
- locationMap . put ( aggregatedSourceLine , new IPythonLocation ( tokenLocation . getLineNr (), tokenLocation . getColumnNr ()) );
98
- aggregatedSourceLine ++ ;
108
+ for ( String line : sourceLine . lines (). toList ()) {
109
+ aggregatedSource . append ( line );
110
+ addLineToSource ( " \n " , tokenLocation ) ;
99
111
}
100
112
// Account for the last cell delimiter
113
+ addDelimiterToSource ();
114
+ return true ;
115
+ }
116
+
117
+ private void addLineToSource (String sourceLine , JsonLocation tokenLocation ) {
118
+ aggregatedSource .append (sourceLine );
119
+ locationMap .put (aggregatedSourceLine , new IPythonLocation (tokenLocation .getLineNr (), tokenLocation .getColumnNr ()));
120
+ aggregatedSourceLine ++;
121
+ }
122
+
123
+ private void addDelimiterToSource () {
101
124
aggregatedSource .append (SONAR_PYTHON_NOTEBOOK_CELL_DELIMITER );
102
125
aggregatedSourceLine ++;
103
- return true ;
104
126
}
105
127
106
128
public record ParseResult (PythonInputFile inputFile , String aggregatedSource , Map <Integer , IPythonLocation > locationMap ) {
0 commit comments