31
31
32
32
public class IpynbNotebookParser {
33
33
34
- public static final String SONAR_PYTHON_NOTEBOOK_CELL_DELIMITER = "#SONAR_PYTHON_NOTEBOOK_CELL_DELIMITER\n " ;
34
+ public static final String SONAR_PYTHON_NOTEBOOK_CELL_DELIMITER = "#SONAR_PYTHON_NOTEBOOK_CELL_DELIMITER" ;
35
35
36
36
public static GeneratedIPythonFile parseNotebook (PythonInputFile inputFile ) {
37
37
try {
@@ -50,7 +50,9 @@ private IpynbNotebookParser(PythonInputFile inputFile) {
50
50
51
51
// Keys are the aggregated source line number
52
52
private final Map <Integer , IPythonLocation > locationMap = new HashMap <>();
53
- private int aggregatedSourceLine = 1 ;
53
+ private int aggregatedSourceLine = 0 ;
54
+ private int lastPythonLine = 0 ;
55
+ private boolean isFirstCell = true ;
54
56
55
57
public GeneratedIPythonFile parseNotebook () throws IOException {
56
58
String content = inputFile .wrappedFile ().contents ();
@@ -68,12 +70,15 @@ public GeneratedIPythonFile parseNotebook() throws IOException {
68
70
}
69
71
}
70
72
}
73
+ // Account for EOF token
74
+ addDefaultLocation (lastPythonLine , jParser .currentTokenLocation ());
71
75
}
72
76
73
77
return new GeneratedIPythonFile (inputFile .wrappedFile (), aggregatedSource .toString (), locationMap );
74
78
}
75
79
76
80
private void processCodeCell (JsonParser jParser ) throws IOException {
81
+
77
82
while (!jParser .isClosed ()) {
78
83
JsonToken jsonToken = jParser .nextToken ();
79
84
if (JsonToken .FIELD_NAME .equals (jsonToken ) && "source" .equals (jParser .currentName ())) {
@@ -87,26 +92,38 @@ private void processCodeCell(JsonParser jParser) throws IOException {
87
92
}
88
93
}
89
94
95
+ private void appendNewLineAfterPreviousCellDelimiter () {
96
+ if (!isFirstCell ) {
97
+ aggregatedSource .append ("\n " );
98
+ } else {
99
+ isFirstCell = false ;
100
+ }
101
+ }
102
+
90
103
private boolean parseSourceArray (JsonParser jParser , JsonToken jsonToken ) throws IOException {
91
104
if (jsonToken != JsonToken .START_ARRAY ) {
92
105
return false ;
93
106
}
107
+ appendNewLineAfterPreviousCellDelimiter ();
108
+ JsonLocation tokenLocation = jParser .currentTokenLocation ();
94
109
while (jParser .nextToken () != JsonToken .END_ARRAY ) {
95
110
String sourceLine = jParser .getValueAsString ();
96
- var tokenLocation = jParser .currentTokenLocation ();
111
+ tokenLocation = jParser .currentTokenLocation ();
97
112
var countEscapedChar = countEscapeCharacters (sourceLine , new LinkedHashMap <>(), tokenLocation .getColumnNr ());
98
113
addLineToSource (sourceLine , tokenLocation , countEscapedChar );
99
114
}
100
115
aggregatedSource .append ("\n " );
101
116
// Account for the last cell delimiter
102
- addDelimiterToSource ();
117
+ addDelimiterToSource (tokenLocation );
118
+ lastPythonLine = aggregatedSourceLine ;
103
119
return true ;
104
120
}
105
121
106
122
private boolean parseSourceMultilineString (JsonParser jParser , JsonToken jsonToken ) throws IOException {
107
123
if (jsonToken != JsonToken .VALUE_STRING ) {
108
124
return false ;
109
125
}
126
+ appendNewLineAfterPreviousCellDelimiter ();
110
127
String sourceLine = jParser .getValueAsString ();
111
128
JsonLocation tokenLocation = jParser .currentTokenLocation ();
112
129
var previousLen = 0 ;
@@ -122,7 +139,8 @@ private boolean parseSourceMultilineString(JsonParser jParser, JsonToken jsonTok
122
139
previousExtraChars = currentCount ;
123
140
}
124
141
// Account for the last cell delimiter
125
- addDelimiterToSource ();
142
+ addDelimiterToSource (tokenLocation );
143
+ lastPythonLine = aggregatedSourceLine ;
126
144
return true ;
127
145
}
128
146
@@ -132,13 +150,18 @@ private void addLineToSource(String sourceLine, JsonLocation tokenLocation, Map<
132
150
133
151
private void addLineToSource (String sourceLine , IPythonLocation location ) {
134
152
aggregatedSource .append (sourceLine );
135
- locationMap .put (aggregatedSourceLine , location );
136
153
aggregatedSourceLine ++;
154
+ locationMap .put (aggregatedSourceLine , location );
137
155
}
138
156
139
- private void addDelimiterToSource () {
157
+ private void addDelimiterToSource (JsonLocation tokenLocation ) {
140
158
aggregatedSource .append (SONAR_PYTHON_NOTEBOOK_CELL_DELIMITER );
141
159
aggregatedSourceLine ++;
160
+ addDefaultLocation (aggregatedSourceLine , tokenLocation );
161
+ }
162
+
163
+ private void addDefaultLocation (int line , JsonLocation tokenLocation ) {
164
+ locationMap .putIfAbsent (line , new IPythonLocation (tokenLocation .getLineNr (), tokenLocation .getColumnNr (), Map .of (-1 , 0 )));
142
165
}
143
166
144
167
private static Map <Integer , Integer > countEscapeCharacters (String sourceLine , Map <Integer , Integer > colMap , int colOffSet ) {
0 commit comments