@@ -93,6 +93,13 @@ def _return_document_handler(cli, buffer):
93
93
AcceptAction .IGNORE = AcceptAction (handler = None )
94
94
95
95
96
+ class ValidationState (object ):
97
+ " The validation state of a buffer. This is set after the validation. "
98
+ VALID = 'VALID'
99
+ INVALID = 'INVALID'
100
+ UNKNOWN = 'UNKNOWN'
101
+
102
+
96
103
class CompletionState (object ):
97
104
"""
98
105
Immutable class that contains a completion state.
@@ -270,6 +277,7 @@ def reset(self, initial_document=None, append_to_history=False):
270
277
271
278
# `ValidationError` instance. (Will be set when the input is wrong.)
272
279
self .validation_error = None
280
+ self .validation_state = ValidationState .UNKNOWN
273
281
274
282
# State of the selection.
275
283
self .selection_state = None
@@ -397,6 +405,7 @@ def working_index(self, value):
397
405
def _text_changed (self ):
398
406
# Remove any validation errors and complete state.
399
407
self .validation_error = None
408
+ self .validation_state = ValidationState .UNKNOWN
400
409
self .complete_state = None
401
410
self .yank_nth_arg_state = None
402
411
self .document_before_paste = None
@@ -410,6 +419,7 @@ def _text_changed(self):
410
419
def _cursor_position_changed (self ):
411
420
# Remove any validation errors and complete state.
412
421
self .validation_error = None
422
+ self .validation_state = ValidationState .UNKNOWN
413
423
self .complete_state = None
414
424
self .yank_nth_arg_state = None
415
425
self .document_before_paste = None
@@ -1075,7 +1085,10 @@ def validate(self):
1075
1085
"""
1076
1086
Returns `True` if valid.
1077
1087
"""
1078
- self .validation_error = None
1088
+ # Don't call the validator again, if it was already called for the
1089
+ # current input.
1090
+ if self .validation_state != ValidationState .UNKNOWN :
1091
+ return self .validation_state == ValidationState .VALID
1079
1092
1080
1093
# Validate first. If not valid, set validation exception.
1081
1094
if self .validator :
@@ -1086,9 +1099,12 @@ def validate(self):
1086
1099
cursor_position = e .cursor_position
1087
1100
self .cursor_position = min (max (0 , cursor_position ), len (self .text ))
1088
1101
1102
+ self .validation_state = ValidationState .INVALID
1089
1103
self .validation_error = e
1090
1104
return False
1091
1105
1106
+ self .validation_state = ValidationState .VALID
1107
+ self .validation_error = None
1092
1108
return True
1093
1109
1094
1110
def append_to_history (self ):
0 commit comments