Skip to content

Commit 43a5236

Browse files
committedDec 14, 2016
Added Buffer.validation_state attribute. + Don't call the validator again as long as the input didn't change.
1 parent 28bdee1 commit 43a5236

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed
 

‎prompt_toolkit/buffer.py

+17-1
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,13 @@ def _return_document_handler(cli, buffer):
9393
AcceptAction.IGNORE = AcceptAction(handler=None)
9494

9595

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+
96103
class CompletionState(object):
97104
"""
98105
Immutable class that contains a completion state.
@@ -270,6 +277,7 @@ def reset(self, initial_document=None, append_to_history=False):
270277

271278
# `ValidationError` instance. (Will be set when the input is wrong.)
272279
self.validation_error = None
280+
self.validation_state = ValidationState.UNKNOWN
273281

274282
# State of the selection.
275283
self.selection_state = None
@@ -397,6 +405,7 @@ def working_index(self, value):
397405
def _text_changed(self):
398406
# Remove any validation errors and complete state.
399407
self.validation_error = None
408+
self.validation_state = ValidationState.UNKNOWN
400409
self.complete_state = None
401410
self.yank_nth_arg_state = None
402411
self.document_before_paste = None
@@ -410,6 +419,7 @@ def _text_changed(self):
410419
def _cursor_position_changed(self):
411420
# Remove any validation errors and complete state.
412421
self.validation_error = None
422+
self.validation_state = ValidationState.UNKNOWN
413423
self.complete_state = None
414424
self.yank_nth_arg_state = None
415425
self.document_before_paste = None
@@ -1075,7 +1085,10 @@ def validate(self):
10751085
"""
10761086
Returns `True` if valid.
10771087
"""
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
10791092

10801093
# Validate first. If not valid, set validation exception.
10811094
if self.validator:
@@ -1086,9 +1099,12 @@ def validate(self):
10861099
cursor_position = e.cursor_position
10871100
self.cursor_position = min(max(0, cursor_position), len(self.text))
10881101

1102+
self.validation_state = ValidationState.INVALID
10891103
self.validation_error = e
10901104
return False
10911105

1106+
self.validation_state = ValidationState.VALID
1107+
self.validation_error = None
10921108
return True
10931109

10941110
def append_to_history(self):

0 commit comments

Comments
 (0)
Please sign in to comment.