<s> from __future__ import unicode_literals <EOL> from prompt_toolkit . document import Document <EOL> from prompt_toolkit . buffer import Buffer , AcceptAction <EOL> from prompt_toolkit . filters import Always <EOL> from pyvim . completion import DocumentCompleter <EOL> from six import string_types <EOL> import os <EOL> import weakref <EOL> __all__ = ( <EOL> '<STR_LIT>' , <EOL> ) <EOL> class EditorBuffer ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , editor , buffer_name , location = None , text = None ) : <EOL> assert isinstance ( buffer_name , string_types ) <EOL> assert location is None or isinstance ( location , string_types ) <EOL> assert text is None or isinstance ( text , string_types ) <EOL> assert not ( location and text ) <EOL> self . _editor_ref = weakref . ref ( editor ) <EOL> self . buffer_name = buffer_name <EOL> self . location = location <EOL> self . encoding = '<STR_LIT:utf-8>' <EOL> self . is_new = True <EOL> if location : <EOL> text = self . _read ( location ) <EOL> else : <EOL> text = text or '<STR_LIT>' <EOL> self . _file_content = text <EOL> self . buffer = Buffer ( <EOL> is_multiline = Always ( ) , <EOL> completer = DocumentCompleter ( editor , self ) , <EOL> initial_document = Document ( text , <NUM_LIT:0> ) , <EOL> accept_action = AcceptAction . IGNORE ) <EOL> self . report_errors = [ ] <EOL> @ property <EOL> def editor ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _editor_ref ( ) <EOL> @ property <EOL> def has_unsaved_changes ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _file_content != self . buffer . text <EOL> def _read ( self , location ) : <EOL> """<STR_LIT>""" <EOL> for io in self . editor . io_backends : <EOL> if io . can_open_location ( location ) : <EOL> exists = io . exists ( location ) <EOL> if exists in ( True , NotImplemented ) : <EOL> self . is_new = False <EOL> try : <EOL> text , self . encoding = io . read ( location ) <EOL> text = text . replace ( '<STR_LIT:\r\n>' , '<STR_LIT:\n>' ) <EOL> if text . endswith ( '<STR_LIT:\n>' ) : <EOL> text = text [ : - <NUM_LIT:1> ] <EOL> except Exception as e : <EOL> self . editor . show_message ( '<STR_LIT>' % ( location , e ) ) <EOL> return '<STR_LIT>' <EOL> else : <EOL> return text <EOL> else : <EOL> self . is_new = True <EOL> return '<STR_LIT>' <EOL> self . editor . show_message ( '<STR_LIT>' % location ) <EOL> return '<STR_LIT>' <EOL> def reload ( self ) : <EOL> """<STR_LIT>""" <EOL> text = self . _read ( self . location ) <EOL> cursor_position = min ( self . buffer . cursor_position , len ( text ) ) <EOL> self . buffer . document = Document ( text , cursor_position ) <EOL> self . _file_content = text <EOL> def write ( self , location = None ) : <EOL> """<STR_LIT>""" <EOL> if location is not None : <EOL> self . location = location <EOL> assert self . location <EOL> for io in self . editor . io_backends : <EOL> if io . can_open_location ( self . location ) : <EOL> break <EOL> else : <EOL> self . editor . show_message ( '<STR_LIT>' % location ) <EOL> try : <EOL> io . write ( self . location , self . buffer . text + '<STR_LIT:\n>' , self . encoding ) <EOL> self . is_new = False <EOL> except Exception as e : <EOL> self . editor . show_message ( '<STR_LIT:%s>' % e ) <EOL> else : <EOL> self . _file_content = self . buffer . text <EOL> def get_display_name ( self , short = False ) : <EOL> """<STR_LIT>""" <EOL> if self . location is None : <EOL> return '<STR_LIT>' <EOL> elif short : <EOL> return os . path . basename ( self . location ) <EOL> else : <EOL> return self . location <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( <EOL> self . __class__ . __name__ , <EOL> self . buffer_name , self . buffer ) </s>
