@@ -148,6 +148,26 @@ def to_comment(self, correct_y: int) -> Comment:
148
148
comment .text = comment .text .replace ("<" , "`" ).replace (">" , "`" )
149
149
return comment
150
150
151
+ def to_code (self , correct_y : int ) -> str :
152
+ """
153
+ Writes the code with regard to the correct node class.
154
+ :param correct_y: the index of the correct node class.
155
+ :return: code string
156
+ """
157
+ if correct_y == CLASS_INDEX [CLS_NOOP ]:
158
+ return ""
159
+ if correct_y == CLASS_INDEX [CLS_NOOP ]:
160
+ comment .text = "format: %s at column %d should be removed" % (
161
+ CLASSES [self .y ], self .start .col )
162
+ elif self .y == CLASS_INDEX [CLS_NOOP ]:
163
+ comment .text = "format: %s should be inserted at column %d" % (
164
+ CLASSES [correct_y ], self .start .col )
165
+ else :
166
+ comment .text = "format: replace %s with %s at column %d" % (
167
+ CLASSES [self .y ], CLASSES [correct_y ], self .start .col )
168
+ comment .text = comment .text .replace ("<" , "`" ).replace (">" , "`" )
169
+ return comment
170
+
151
171
152
172
CLS_SPACE = "<space>"
153
173
CLS_SPACE_INC = "<+space>"
@@ -159,6 +179,18 @@ def to_comment(self, correct_y: int) -> Comment:
159
179
CLS_SINGLE_QUOTE = "'"
160
180
CLS_DOUBLE_QUOTE = '"'
161
181
CLS_NOOP = "<noop>"
182
+ CLASSES_REPR = {
183
+ CLS_SPACE : " " ,
184
+ CLS_TAB : "\t " ,
185
+ CLS_NEWLINE : "\n " ,
186
+ CLS_SPACE_INC : "<nan>" ,
187
+ CLS_SPACE_DEC : "<nan>" ,
188
+ CLS_TAB_INC : "<nan>" ,
189
+ CLS_TAB_DEC : "<nan>" ,
190
+ CLS_SINGLE_QUOTE : "'" ,
191
+ CLS_DOUBLE_QUOTE : '"' ,
192
+ CLS_NOOP : "" ,
193
+ }
162
194
CLASSES = (CLS_SPACE , CLS_TAB , CLS_NEWLINE , CLS_SPACE_INC , CLS_SPACE_DEC ,
163
195
CLS_TAB_INC , CLS_TAB_DEC , CLS_SINGLE_QUOTE , CLS_DOUBLE_QUOTE , CLS_NOOP )
164
196
CLASS_INDEX = {cls : i for i , cls in enumerate (CLASSES )}
@@ -639,6 +671,8 @@ def _parse_file(self, contents: str, root: bblfsh.Node, path: str) -> \
639
671
parser = self .tokens .PARSER
640
672
searchsorted = numpy .searchsorted
641
673
for node in node_tokens :
674
+ if node .start_position .offset < pos :
675
+ continue
642
676
if node .start_position .offset > pos :
643
677
sumlen = 0
644
678
diff = contents [pos :node .start_position .offset ]
0 commit comments