Skip to content

Commit

Permalink
Added Buffer.validation_state attribute. + Don't call the validator a…
Browse files Browse the repository at this point in the history
…gain as long as the input didn't change.
  • Loading branch information
jonathanslenders committed Dec 14, 2016
1 parent 28bdee1 commit 43a5236
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion prompt_toolkit/buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,13 @@ def _return_document_handler(cli, buffer):
AcceptAction.IGNORE = AcceptAction(handler=None)


class ValidationState(object):
" The validation state of a buffer. This is set after the validation. "
VALID = 'VALID'
INVALID = 'INVALID'
UNKNOWN = 'UNKNOWN'


class CompletionState(object):
"""
Immutable class that contains a completion state.
Expand Down Expand Up @@ -270,6 +277,7 @@ def reset(self, initial_document=None, append_to_history=False):

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

# State of the selection.
self.selection_state = None
Expand Down Expand Up @@ -397,6 +405,7 @@ def working_index(self, value):
def _text_changed(self):
# Remove any validation errors and complete state.
self.validation_error = None
self.validation_state = ValidationState.UNKNOWN
self.complete_state = None
self.yank_nth_arg_state = None
self.document_before_paste = None
Expand All @@ -410,6 +419,7 @@ def _text_changed(self):
def _cursor_position_changed(self):
# Remove any validation errors and complete state.
self.validation_error = None
self.validation_state = ValidationState.UNKNOWN
self.complete_state = None
self.yank_nth_arg_state = None
self.document_before_paste = None
Expand Down Expand Up @@ -1075,7 +1085,10 @@ def validate(self):
"""
Returns `True` if valid.
"""
self.validation_error = None
# Don't call the validator again, if it was already called for the
# current input.
if self.validation_state != ValidationState.UNKNOWN:
return self.validation_state == ValidationState.VALID

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

self.validation_state = ValidationState.INVALID
self.validation_error = e
return False

self.validation_state = ValidationState.VALID
self.validation_error = None
return True

def append_to_history(self):
Expand Down

0 comments on commit 43a5236

Please sign in to comment.