-
Notifications
You must be signed in to change notification settings - Fork 8
/
parinfer.py
1427 lines (1199 loc) · 50.7 KB
/
parinfer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
## Parinfer.py - a Parinfer implementation in Python
## v3.12.0
## https://github.com/oakmac/parinfer.py
##
## More information about Parinfer can be found here:
## http://shaunlebron.github.io/parinfer/
##
## Copyright (c) 2015, 2020, Chris Oakman and other contributors
## Released under the ISC license
## https://github.com/oakmac/parinfer.py/blob/master/LICENSE.md
import re
import sys
#-------------------------------------------------------------------------------
# Constants
#-------------------------------------------------------------------------------
INDENT_MODE = 'INDENT_MODE'
PAREN_MODE = 'PAREN_MODE'
BACKSLASH = '\\'
BLANK_SPACE = ' '
DOUBLE_SPACE = ' '
DOUBLE_QUOTE = '"'
NEWLINE = '\n'
TAB = '\t'
LINE_ENDING_REGEX = re.compile(r"\r?\n")
CLOSE_PARENS = frozenset(['}', ')', ']'])
OPEN_PARENS = frozenset(['{', '(', '['])
WHITESPACE = frozenset([NEWLINE, BLANK_SPACE, TAB])
MATCH_PAREN = {
'{': '}',
'}': '{',
'[': ']',
']': '[',
'(': ')',
')': '(',
}
# toggle this to check the asserts during development
RUN_ASSERTS = False
#-------------------------------------------------------------------------------
# Options Structure
#-------------------------------------------------------------------------------
def transformChange(change):
if not change:
return None
newLines = re.split(LINE_ENDING_REGEX, change['newText'])
oldLines = re.split(LINE_ENDING_REGEX, change['oldText'])
# single line case:
# (defn foo| [])
# ^ newEndX, newEndLineNo
# +++
# multi line case:
# (defn foo
# ++++
# "docstring."
# ++++++++++++++++
# |[])
# ++^ newEndX, newEndLineNo
lastOldLineLen = len(oldLines[-1])
lastNewLineLen = len(newLines[-1])
oldEndX = (change['x'] if len(oldLines) == 1 else 0) + lastOldLineLen
newEndX = (change['x'] if len(newLines) == 1 else 0) + lastNewLineLen
newEndLineNo = change['lineNo'] + (len(newLines)-1)
return {
'x': change['x'],
'lineNo': change['lineNo'],
'oldText': change['oldText'],
'newText': change['newText'],
'oldEndX': oldEndX,
'newEndX': newEndX,
'newEndLineNo': newEndLineNo,
'lookupLineNo': newEndLineNo,
'lookupX': newEndX
}
def transformChanges(changes):
if len(changes) == 0:
return None
lines = {}
for change in changes:
change = transformChange(change)
# print("change:",change['lookupLineNo'])
if change['lookupLineNo'] not in lines:
line = lines[change['lookupLineNo']] = {}
else:
line = lines[change['lookupLineNo']]
line[change['lookupX']] = change
return lines
#-------------------------------------------------------------------------------
# Result Structure
#-------------------------------------------------------------------------------
# This represents the running result. As we scan through each character
# of a given text, we mutate this structure to update the state of our
# system.
class Clamped(object):
__slots__ = ('startX', 'endX', 'openers')
def __init__(self):
self.startX = None # startX before paren trail was clamped
self.endX = None # endX before paren trail was clamped
self.openers = [] # openers that were cut out after paren trail was clamped
class ParenTrail(object):
__slots__ = ('lineNo', 'startX', 'endX', 'openers', 'clamped')
def __init__(self):
self.lineNo = None # [integer] - line number of the last parsed paren trail
self.startX = None # [integer] - x position of first paren in this range
self.endX = None # [integer] - x position after the last paren in this range
self.openers = [] # [array of stack elements] - corresponding open-paren for each close-paren in this range
self.clamped = Clamped()
def initialParenTrail():
return ParenTrail()
class Result:
"""Returns a dictionary of the initial state."""
__slots__ = (
'mode', 'smart',
'origText', 'origCursorX', 'origCursorLine',
'inputLines',
'inputLineNo', 'inputX',
'lines', 'lineNo', 'ch', 'x', 'indentX',
'parenStack',
'tabStops', 'parenTrail',
'parenTrails',
'returnParens', 'parens',
'cursorX', 'cursorLine', 'prevCursorX', 'prevCursorLine',
'selectionStartLine',
'changes',
'isInCode', 'isEscaping', 'isEscaped', 'isInStr', 'isInComment',
'commentX',
'quoteDanger', 'trackingIndent', 'skipChar', 'success', 'partialResult',
'forceBalance', 'maxIndent', 'indentDelta', 'trackingArgTabStop',
'error',
'errorPosCache',
'comment')
def __str__(self):
return ('Result {' + 'mode: ' + str(self.mode) + '\n\t'
'smart: ' + str(self.smart) + '\n\t'
'origText: ' + str(self.origText) + '\n\t'
'origCursorX: ' + str(self.origCursorX) + '\n\t'
'origCursorLine: ' + str(self.origCursorLine) + '\n\t'
'inputLines: ' + str(self.inputLines) + '\n\t'
'inputLineNo: ' + str(self.inputLineNo) + '\n\t'
'inputX: ' + str(self.inputX) + '\n\t'
'lines: ' + str(self.lines) + '\n\t'
'lineNo: ' + str(self.lineNo) + '\n\t'
'ch: ' + str(self.ch) + '\n\t'
'x: ' + str(self.x) + '\n\t'
'indentX: ' + str(self.indentX) + '\n\t'
'parenStack: ' + str(self.parenStack) + '\n\t'
'tabStops: ' + str(self.tabStops) + '\n\t'
'parenTrail: ' + str(self.parenTrail) + '\n\t'
'parenTrails: ' + str(self.parenTrails) + '\n\t'
'returnParens: ' + str(self.returnParens) + '\n\t'
'parens: ' + str(self.parens) + '\n\t'
'cursorX: ' + str(self.cursorX) + '\n\t'
'cursorLine: ' + str(self.cursorLine) + '\n\t'
'prevCursorX: ' + str(self.prevCursorX) + '\n\t'
'prevCursorLine: ' + str(self.prevCursorLine) + '\n\t'
'selectionStartLine: ' + str(self.selectionStartLine) + '\n\t'
'changes: ' + str(self.changes) + '\n\t'
'isInCode: ' + str(self.isInCode) + '\n\t'
'isEscaping: ' + str(self.isEscaping) + '\n\t'
'isEscaped: ' + str(self.isEscaped) + '\n\t'
'isInStr: ' + str(self.isInStr) + '\n\t'
'isInComment: ' + str(self.isInComment) + '\n\t'
'commentX: ' + str(self.commentX) + '\n\t'
'quoteDanger: ' + str(self.quoteDanger) + '\n\t'
'trackingIndent: ' + str(self.trackingIndent) + '\n\t'
'skipChar: ' + str(self.skipChar) + '\n\t'
'success: ' + str(self.success) + '\n\t'
'partialResult: ' + str(self.partialResult) + '\n\t'
'forceBalance: ' + str(self.forceBalance) + '\n\t'
'maxIndent: ' + str(self.maxIndent) + '\n\t'
'indentDelta: ' + str(self.indentDelta) + '\n\t'
'trackingArgTabStop: ' + str(self.trackingArgTabStop) + '\n\t'
'error: ' + str(self.error) + '\n\t'
'errorPosCache: ' + str(self.errorPosCache) + '\n\t'
'comment: ' + str(self.comment) + '\n\t}')
def __init__(self, text, options, mode, smart):
"""Constructs a dictionary of the initial state."""
super(Result, self).__init__()
self.mode = mode # [enum] - current processing mode (INDENT_MODE or PAREN_MODE)
self.smart = smart # [boolean] - smart mode attempts special user-friendly behavior
self.origText = text # [string] - original text
self.origCursorX = None # [integer] - original cursorX option
self.origCursorLine = None # [integer] - original cursorLine option
# [string array] - input lines that we process line-by-line char-by-char
self.inputLines = re.split(LINE_ENDING_REGEX, text)
self.inputLineNo = -1 # [integer] - the current input line number
self.inputX = -1 # [integer] - the current input x position of the current character (ch)
self.lines = [] # [string array] - output lines (with corrected parens or indentation)
self.lineNo = -1 # [integer] - output line number we are on
self.ch = '' # [string] - character we are processing (can be changed to indicate a replacement)
self.x = 0 # [integer] - output x position of the current character (ch)
self.indentX = None # [integer] - x position of the indentation point if present
self.parenStack = [] # We track where we are in the Lisp tree by keeping a stack (array) of open-parens.
# Stack elements are objects containing keys {ch, x, lineNo, indentDelta}
# whose values are the same as those described here in this result structure.
self.tabStops = [] # In Indent Mode, it is useful for editors to snap a line's indentation
# to certain critical points. Thus, we have a `tabStops` array of objects containing
# keys {ch, x, lineNo, argX}, which is just the state of the `parenStack` at the cursor line.
self.parenTrail = initialParenTrail() # the range of parens at the end of a line
self.parenTrails = [] # [array of {lineNo, startX, endX}] - all non-empty parenTrails to be returned
self.returnParens = False # [boolean] - determines if we return `parens` described below
self.parens = [] # [array of {lineNo, x, closer, children}] - paren tree if `returnParens` is h
self.cursorX = None # [integer] - x position of the cursor
self.cursorLine = None # [integer] - line number of the cursor
self.prevCursorX = None # [integer] - x position of the previous cursor
self.prevCursorLine = None # [integer] - line number of the previous cursor
self.selectionStartLine = None # [integer] - line number of the current selection starting point
self.changes = None # [object] - mapping change.key to a change object (please see `transformChange` for object structure)
self.isInCode = True # [boolean] - indicates if we are currently in "code space" (not string or comment)
self.isEscaping = False # [boolean] - indicates if the next character will be escaped (e.g. `\c`). This may be inside string comment or code.
self.isEscaped = False # [boolean] - indicates if the current character is escaped (e.g. `\c`). This may be inside string comment or code.
self.isInStr = False # [boolean] - indicates if we are currently inside a string
self.isInComment = False # [boolean] - indicates if we are currently inside a comment
self.commentX = None # [integer] - x position of the start of comment on current line (if any)
self.quoteDanger = False # [boolean] - indicates if quotes are imbalanced inside of a comment (dangerous)
self.trackingIndent = False # [boolean] - are we looking for the indentation point of the current line?
self.skipChar = False # [boolean] - should we skip the processing of the current character?
self.success = False # [boolean] - was the input properly formatted enough to create a valid result?
self.partialResult = False # [boolean] - should we return a partial result when an error occurs?
self.forceBalance = False # [boolean] - should indent mode aggressively enforce paren balance?
self.maxIndent = sys.maxsize # [integer] - maximum allowed indentation of subsequent lines in Paren Mode
self.indentDelta = 0 # [integer] - how far indentation was shifted by Paren Mode
# (preserves relative indentation of nested expressions)
self.trackingArgTabStop = None # [string] - enum to track how close we are to the first-arg tabStop in a list
# For example a tabStop occurs at `bar` below:
#
# ` (foo bar`
# 00011112222000 <-- state after processing char (enums below)
#
# 0 None => not searching
# 1 'space' => searching for next space
# 2 'arg' => searching for arg
#
# (We create the tabStop when the change from 2->0 happens.)
#
self.comment = ';' # [string] default to semicolon as comment character
self.error = { # if 'success' is False, return this error to the user
'name': None, # [string] - Parinfer's unique name for this error
'message': None, # [string] - error message to display
'lineNo': None, # [integer] - line number of error
'x': None, # [integer] - start x position of error
'extra': {
'name': None,
'lineNo': None,
'x': None
}
}
self.errorPosCache = {} # [object] - maps error name to a potential error position
if isinstance(options, dict):
if 'cursorX' in options:
self.cursorX = options['cursorX']
self.origCursorX = options['cursorX']
if 'cursorLine' in options:
self.cursorLine = options['cursorLine']
self.origCursorLine = options['cursorLine']
if 'prevCursorX' in options:
self.prevCursorX = options['prevCursorX']
if 'prevCursorLine' in options:
self.prevCursorLine = options['prevCursorLine']
if 'selectionStartLine' in options:
self.selectionStartLine = options['selectionStartLine']
if 'changes' in options:
self.changes = transformChanges(options['changes'])
if 'partialResult' in options:
self.partialResult = options['partialResult']
if 'forceBalance' in options:
self.forceBalance = options['forceBalance']
if 'returnParens' in options:
self.returnParens = options['returnParens']
if 'comment' in options:
self.comment = options['comment']
#-------------------------------------------------------------------------------
# Possible Errors
#-------------------------------------------------------------------------------
# `result.error.name` is set to any of these
ERROR_QUOTE_DANGER = "quote-danger"
ERROR_EOL_BACKSLASH = "eol-backslash"
ERROR_UNCLOSED_QUOTE = "unclosed-quote"
ERROR_UNCLOSED_PAREN = "unclosed-paren"
ERROR_UNMATCHED_CLOSE_PAREN = "unmatched-close-paren"
ERROR_UNMATCHED_OPEN_PAREN = "unmatched-open-paren"
ERROR_LEADING_CLOSE_PAREN = "leading-close-paren"
ERROR_UNHANDLED = "unhandled"
errorMessages = {}
errorMessages[ERROR_QUOTE_DANGER] = "Quotes must balanced inside comment blocks."
errorMessages[ERROR_EOL_BACKSLASH] = "Line cannot end in a hanging backslash."
errorMessages[ERROR_UNCLOSED_QUOTE] = "String is missing a closing quote."
errorMessages[ERROR_UNCLOSED_PAREN] = "Unclosed open-paren."
errorMessages[ERROR_UNMATCHED_CLOSE_PAREN] = "Unmatched close-paren."
errorMessages[ERROR_UNMATCHED_OPEN_PAREN] = "Unmatched open-paren."
errorMessages[ERROR_LEADING_CLOSE_PAREN] = "Line cannot lead with a close-paren."
errorMessages[ERROR_UNHANDLED] = "Unhandled error."
def cacheErrorPos(result, errorName):
e = {
'lineNo': result.lineNo,
'x': result.x,
'inputLineNo': result.inputLineNo,
'inputX': result.inputX
}
result.errorPosCache[errorName] = e
return e
class ParinferError(Exception):
pass
def error(result, name):
cache = result.errorPosCache.get(name, {})
resultLineNo = result.LineNo if result.partialResult else result.inputLineNo
resultX = result.x if result.partialResult else result.inputX
keyLineNo = 'lineNo' if result.partialResult else 'inputLineNo'
keyX = 'x' if result.partialResult else 'inputX'
e = {
'parinferError': True,
'name': name,
'message': errorMessages[name],
'lineNo': cache[keyLineNo] if cache else resultLineNo,
'x': cache[keyX] if cache else resultX
}
opener = peek(result.parenStack, 0)
if name == ERROR_UNMATCHED_CLOSE_PAREN:
# extra error info for locating the open-paren that it should've matched
if ERROR_UNMATCHED_OPEN_PAREN in result.errorPosCache:
cache = result.errorPosCache[ERROR_UNMATCHED_OPEN_PAREN]
if cache or opener:
if opener:
openerLineNo = opener.LineNo if result.partialResult else opener.inputLineNo
openerX = opener.x if result.partialResult else opener.inputX
e['extra'] = {
'name': ERROR_UNMATCHED_OPEN_PAREN,
'lineNo': cache[keyLineNo] if cache else openerLineNo,
'x': cache[keyX] if cache else openerX
}
elif name == ERROR_UNCLOSED_PAREN:
openerLineNo = opener.LineNo if result.partialResult else opener.inputLineNo
openerX = opener.x if result.partialResult else opener.inputX
e['lineNo'] = openerLineNo
e['x'] = openerX
return ParinferError(e)
#-------------------------------------------------------------------------------
# String Operations
#-------------------------------------------------------------------------------
def replaceWithinString(orig, start, end, replace):
return orig[:start] + replace + orig[end:]
if RUN_ASSERTS:
assert replaceWithinString('aaa', 0, 2, '') == 'a'
assert replaceWithinString('aaa', 0, 1, 'b') == 'baa'
assert replaceWithinString('aaa', 0, 2, 'b') == 'ba'
def getLineEnding(text):
# NOTE: We assume that if the CR char "\r" is used anywhere,
# then we should use CRLF line-endings after every line.
i = text.find("\r")
if i != -1:
return "\r\n"
return "\n"
#-------------------------------------------------------------------------------
# Line Operations
#-------------------------------------------------------------------------------
def isCursorAffected(result, start, end):
if result.cursorX == start and result.cursorX == end:
return result.cursorX == 0
return result.cursorX >= end
def shiftCursorOnEdit(result, lineNo, start, end, replace):
oldLength = end - start
newLength = len(replace)
dx = newLength - oldLength
if (dx != 0 and
result.cursorLine == lineNo and
result.cursorX is not None and
isCursorAffected(result, start, end)):
result.cursorX += dx
def replaceWithinLine(result, lineNo, start, end, replace):
line = result.lines[lineNo]
newLine = replaceWithinString(line, start, end, replace)
result.lines[lineNo] = newLine
shiftCursorOnEdit(result, lineNo, start, end, replace)
def insertWithinLine(result, lineNo, idx, insert):
replaceWithinLine(result, lineNo, idx, idx, insert)
def initLine(result):
result.x = 0
result.lineNo += 1
# reset line-specific state
result.indentX = None
result.commentX = None
result.indentDelta = 0
if ERROR_UNMATCHED_CLOSE_PAREN in result.errorPosCache:
del result.errorPosCache[ERROR_UNMATCHED_CLOSE_PAREN]
if ERROR_UNMATCHED_OPEN_PAREN in result.errorPosCache:
del result.errorPosCache[ERROR_UNMATCHED_OPEN_PAREN]
if ERROR_LEADING_CLOSE_PAREN in result.errorPosCache:
del result.errorPosCache[ERROR_LEADING_CLOSE_PAREN]
result.trackingArgTabStop = None
result.trackingIndent = not result.isInStr
# if the current character has changed, commit its change to the current line.
# def commitChar(result, origCh):
# ch = result.ch
# if origCh != ch:
# replaceWithinLine(result, result.lineNo, result.x, result.x + len(origCh), ch)
# result.indentDelta -= (len(origCh) - len(ch))
# result.x += len(ch)
#-------------------------------------------------------------------------------
# Misc Utils
#-------------------------------------------------------------------------------
def clamp(val, minN, maxN):
return max(minN, min(val, maxN))
# if RUN_ASSERTS:
# assert clamp(1, 3, 5) == 3
# assert clamp(9, 3, 5) == 5
# assert clamp(1, 3, None) == 3
# assert clamp(5, 3, None) == 5
# assert clamp(1, None, 5) == 1
# assert clamp(9, None, 5) == 5
# assert clamp(1, None, None) == 1
def peek(arr, idxFromBack):
# maxIdx = len(arr) - 1
# if idxFromBack > maxIdx:
# return None
# return arr[maxIdx - idxFromBack]
try:
return arr[-1 - idxFromBack]
except IndexError:
return None
if RUN_ASSERTS:
assert peek(['a'], 0) == 'a'
assert peek(['a'], 1) is None
assert peek(['a', 'b', 'c'], 0) == 'c'
assert peek(['a', 'b', 'c'], 1) == 'b'
assert peek(['a', 'b', 'c'], 5) is None
assert peek([], 0) is None
assert peek([], 1) is None
#-------------------------------------------------------------------------------
# Questions about characters
#-------------------------------------------------------------------------------
def isValidCloseParen(parenStack, ch):
if len(parenStack) == 0:
return False
return peek(parenStack, 0).ch == MATCH_PAREN[ch]
# def isWhitespace(result):
# return not result.isEscaped and result.ch in WHITESPACE
# can this be the last code character of a list?
def isClosable(result):
ch = result.ch
closer = ch in CLOSE_PARENS and not result.isEscaped
# closer = ch in ('}', ')', ']') and not result.isEscaped
# return result.isInCode and not isWhitespace(result) and ch != '' and not closer
return result.isInCode and (result.isEscaped or ch not in WHITESPACE) and ch != '' and not closer
# return result.isInCode and not ch in (BLANK_SPACE, DOUBLE_SPACE) and ch != '' and not closer
#-------------------------------------------------------------------------------
# Advanced operations on characters
#-------------------------------------------------------------------------------
def checkCursorHolding(result):
opener = peek(result.parenStack, 0)
parent = peek(result.parenStack, 1)
holdMinX = parent.x+1 if parent else 0
holdMaxX = opener.x
holding = (
result.cursorLine == opener.lineNo and
holdMinX <= result.cursorX and result.cursorX <= holdMaxX
)
shouldCheckPrev = not result.changes and result.prevCursorLine is not None
if shouldCheckPrev:
prevHolding = (
result.prevCursorLine == opener.lineNo and
holdMinX <= result.prevCursorX and result.prevCursorX <= holdMaxX
)
if prevHolding and not holding:
raise ParinferError({'releaseCursorHold': True})
return holding
def trackArgTabStop(result, state):
if state == 'space':
# if result.isInCode and isWhitespace(result):
if result.isInCode and not result.isEscaped and result.ch in WHITESPACE:
result.trackingArgTabStop = 'arg'
elif state == 'arg':
# if not isWhitespace(result):
if result.isEscaped or result.ch not in WHITESPACE:
opener = peek(result.parenStack, 0)
opener.argX = result.x
result.trackingArgTabStop = None
#-------------------------------------------------------------------------------
# Literal character events
#-------------------------------------------------------------------------------
class Opener(object):
__slots__ = ('self', 'inputLineNo', 'inputX', 'lineNo', 'x', 'ch', 'indentDelta',
'maxChildIndent', 'argX', 'children', 'closer')
def __init__(self, inputLineNo, inputX, lineNo, x, ch, indentDelta, maxChildIndent):
super(Opener, self).__init__()
self.inputLineNo = inputLineNo
self.inputX = inputX
self.lineNo = lineNo
self.x = x
self.ch = ch
self.indentDelta = indentDelta
self.maxChildIndent = maxChildIndent
self.argX = None
self.children = None
self.closer = None
def __str__(self):
return ("{ inputLineNo: " + str(self.inputLineNo)
+ "\n inputX: " + str(self.inputX)
+ "\n lineNo: " + str(self.lineNo)
+ "\n x: " + str(self.x)
+ "\n ch: " + str(self.ch)
+ "\n indentDelta: " + str(self.indentDelta)
+ "\n maxChildIndent: " + str(self.maxChildIndent)
+ "}")
def onOpenParen(result):
if result.isInCode:
opener = Opener(
result.inputLineNo,
result.inputX,
result.lineNo,
result.x,
result.ch,
result.indentDelta,
sys.maxsize,
)
if result.returnParens:
opener.children = []
opener.closer = {
'lineNo': None,
'x': None,
'ch': ''
}
parent = peek(result.parenStack, 0)
parent = parent.children if parent else result.parens
parent.append(opener)
result.parenStack.append(opener)
result.trackingArgTabStop = 'space'
def setCloser(opener, lineNo, x, ch):
opener.closer['lineNo'] = lineNo
opener.closer['x'] = x
opener.closer['ch'] = ch
def onMatchedCloseParen(result):
opener = peek(result.parenStack, 0)
if result.returnParens:
setCloser(opener, result.lineNo, result.x, result.ch)
result.parenTrail.endX = result.x + 1
result.parenTrail.openers.append(opener)
if result.mode == INDENT_MODE and result.smart and checkCursorHolding(result):
origStartX = result.parenTrail.startX
origEndX = result.parenTrail.endX
origOpeners = result.parenTrail.openers
resetParenTrail(result, result.lineNo, result.x+1)
result.parenTrail.clamped.startX = origStartX
result.parenTrail.clamped.endX = origEndX
result.parenTrail.clamped.openers = origOpeners
result.parenStack.pop()
result.trackingArgTabStop = None
def onUnmatchedCloseParen(result):
if result.mode == PAREN_MODE:
trail = result.parenTrail
inLeadingParenTrail = trail.lineNo == result.lineNo and trail.startX == result.indentX
canRemove = result.smart and inLeadingParenTrail
if not canRemove:
raise error(result, ERROR_UNMATCHED_CLOSE_PAREN)
elif result.mode == INDENT_MODE and (
ERROR_UNMATCHED_CLOSE_PAREN not in result.errorPosCache):
cacheErrorPos(result, ERROR_UNMATCHED_CLOSE_PAREN)
opener = peek(result.parenStack, 0)
if opener:
e = cacheErrorPos(result, ERROR_UNMATCHED_OPEN_PAREN)
e['inputLineNo'] = opener.inputLineNo
e['inputX'] = opener.inputX
result.ch = ''
def onCloseParen(result):
if result.isInCode:
if isValidCloseParen(result.parenStack, result.ch):
onMatchedCloseParen(result)
else:
onUnmatchedCloseParen(result)
def onTab(result):
if result.isInCode:
result.ch = DOUBLE_SPACE
def onComment(result):
if result.isInCode:
result.isInComment = True
result.commentX = result.x
result.trackingArgTabStop = None
def onNewline(result):
result.isInComment = False
result.ch = ''
def onQuote(result):
if result.isInStr:
result.isInStr = False
elif result.isInComment:
result.quoteDanger = not result.quoteDanger
if result.quoteDanger:
cacheErrorPos(result, ERROR_QUOTE_DANGER)
else:
result.isInStr = True
cacheErrorPos(result, ERROR_UNCLOSED_QUOTE)
def onBackslash(result):
result.isEscaping = True
def afterBackslash(result):
result.isEscaping = False
result.isEscaped = True
if result.ch == NEWLINE:
if result.isInCode:
raise error(result, ERROR_EOL_BACKSLASH)
onNewline(result)
#-------------------------------------------------------------------------------
# Character dispatch
#-------------------------------------------------------------------------------
CHAR_DISPATCH = {
'(': onOpenParen,
'{': onOpenParen,
'[': onOpenParen,
')': onCloseParen,
'}': onCloseParen,
']': onCloseParen,
BACKSLASH: onBackslash,
TAB: onTab,
NEWLINE: onNewline,
DOUBLE_QUOTE: onQuote,
}
def onChar(result):
result.isEscaped = False
if result.isEscaping:
afterBackslash(result)
elif result.ch == result.comment:
onComment(result)
else:
dispatch = CHAR_DISPATCH.get(result.ch, None)
if dispatch is not None:
dispatch(result)
# ch = result.ch
result.isInCode = not result.isInComment and not result.isInStr
# can this be the last code character of a list?
# def isClosable(result):
# ch = result.ch
# closer = ch in CLOSE_PARENS and not result.isEscaped
# closer = ch in ('}', ')', ']') and not result.isEscaped
# closable = result.isInCode and not (not result.isEscaped and result.ch in WHITESPACE) and ch != '' and not (ch in CLOSE_PARENS and not result.isEscaped)
# return result.isInCode and not ch in (BLANK_SPACE, DOUBLE_SPACE) and ch != '' and not closer
# if closable:
if isClosable(result):
resetParenTrail(result, result.lineNo, result.x+len(result.ch))
state = result.trackingArgTabStop
if state:
trackArgTabStop(result, state)
#-------------------------------------------------------------------------------
# Cursor defs
#-------------------------------------------------------------------------------
def isCursorLeftOf(cursorX, cursorLine, x, lineNo):
return (
cursorLine == lineNo and
x is not None and
cursorX is not None and
cursorX <= x # inclusive since (cursorX = x) implies (x-1 < cursor < x)
)
def isCursorRightOf(cursorX, cursorLine, x, lineNo):
return (
cursorLine == lineNo and
x is not None and
cursorX is not None and
cursorX > x
)
def isCursorInComment(result, cursorX, cursorLine):
return isCursorRightOf(cursorX, cursorLine, result.commentX, result.lineNo)
def handleChangeDelta(result):
if result.changes and (result.smart or result.mode == PAREN_MODE):
if result.inputLineNo in result.changes:
line = result.changes[result.inputLineNo]
if result.inputX in line:
change = line[result.inputX]
result.indentDelta += (change['newEndX'] - change['oldEndX'])
#-------------------------------------------------------------------------------
# Paren Trail defs
#-------------------------------------------------------------------------------
def resetParenTrail(result, lineNo, x):
result.parenTrail.lineNo = lineNo
result.parenTrail.startX = x
result.parenTrail.endX = x
result.parenTrail.openers = []
result.parenTrail.clamped.startX = None
result.parenTrail.clamped.endX = None
result.parenTrail.clamped.openers = []
def isCursorClampingParenTrail(result, cursorX, cursorLine):
return (
isCursorRightOf(cursorX, cursorLine, result.parenTrail.startX, result.lineNo) and
not isCursorInComment(result, cursorX, cursorLine)
)
# INDENT MODE: allow the cursor to clamp the paren trail
def clampParenTrailToCursor(result):
startX = result.parenTrail.startX
endX = result.parenTrail.endX
clamping = isCursorClampingParenTrail(result, result.cursorX, result.cursorLine)
if clamping:
newStartX = max(startX, result.cursorX)
newEndX = max(endX, result.cursorX)
line = result.lines[result.lineNo]
removeCount = 0
for i in range(startX, newStartX):
if line[i] in CLOSE_PARENS:
removeCount += 1
openers = result.parenTrail.openers
result.parenTrail.openers = openers[removeCount:]
result.parenTrail.startX = newStartX
result.parenTrail.endX = newEndX
result.parenTrail.clamped.openers = openers[0:removeCount]
result.parenTrail.clamped.startX = startX
result.parenTrail.clamped.endX = endX
# INDENT MODE: pops the paren trail from the stack
def popParenTrail(result):
startX = result.parenTrail.startX
endX = result.parenTrail.endX
if startX == endX:
return
openers = result.parenTrail.openers
while len(openers) != 0:
result.parenStack.append(openers.pop())
# Determine which open-paren (if any) on the parenStack should be considered
# the direct parent of the current line (given its indentation point).
# This allows Smart Mode to simulate Paren Mode's structure-preserving
# behavior by adding its `opener.indentDelta` to the current line's indentation.
# (care must be taken to prevent redundant indentation correction, detailed below)
def getParentOpenerIndex(result, indentX):
i = 0
# for i in range(len(result.parenStack)):
parenStackLen = len(result.parenStack)
while i < parenStackLen:
# idx = i
opener = peek(result.parenStack, i)
currOutside = (opener.x < indentX)
prevIndentX = indentX - result.indentDelta
prevOutside = (opener.x - opener.indentDelta < prevIndentX)
isParent = False
if prevOutside and currOutside:
isParent = True
elif not prevOutside and not currOutside:
isParent = False
elif prevOutside and not currOutside:
# POSSIBLE FRAGMENTATION
# (foo --\
# +--- FRAGMENT `(foo bar)` => `(foo) bar`
# bar) --/
# 1. PREVENT FRAGMENTATION
# ```in
# (foo
# ++
# bar
# ```
# ```out
# (foo
# bar
# ```
if result.indentDelta == 0:
isParent = True
# 2. ALLOW FRAGMENTATION
# ```in
# (foo
# bar
# --
# ```
# ```out
# (foo)
# bar
# ```
elif opener.indentDelta == 0:
isParent = False
else:
# TODO: identify legitimate cases where both are nonzero
# allow the fragmentation by default
isParent = False
# TODO: should we throw to exit instead? either of:
# 1. give up, just `throw error(...)`
# 2. fallback to paren mode to preserve structure
elif not prevOutside and currOutside:
# POSSIBLE ADOPTION
# (foo) --\
# +--- ADOPT `(foo) bar` => `(foo bar)`
# bar --/
nextOpener = peek(result.parenStack, i+1)
# 1. DISALLOW ADOPTION
# ```in
# (foo
# --
# (bar)
# --
# baz)
# ```
# ```out
# (foo
# (bar)
# baz)
# ```
# OR
# ```in
# (foo
# --
# (bar)
# -
# baz)
# ```
# ```out
# (foo
# (bar)
# baz)
# ```
if nextOpener and nextOpener.indentDelta <= opener.indentDelta:
# we can only disallow adoption if nextOpener.indentDelta will actually
# prevent the indentX from being in the opener's threshold.
if indentX + nextOpener.indentDelta > opener.x:
isParent = True
else:
isParent = False
# 2. ALLOW ADOPTION
# ```in
# (foo
# (bar)
# --
# baz)
# ```
# ```out
# (foo
# (bar
# baz))
# ```
# OR
# ```in
# (foo
# -
# (bar)
# --
# baz)
# ```
# ```out
# (foo
# (bar)
# baz)
# ```
elif nextOpener and nextOpener.indentDelta > opener.indentDelta:
isParent = True
# 3. ALLOW ADOPTION
# ```in
# (foo)
# --
# bar
# ```
# ```out
# (foo
# bar)
# ```
# OR
# ```in
# (foo)
# bar
# ++
# ```
# ```out