def __init__(self, editwin, index): self.editwin = editwin self.text = text = editwin.text parser = PyParse.Parser(editwin.indentwidth, editwin.tabwidth) def index2line(index): return int(float(index)) lno = index2line(text.index(index)) if not editwin.context_use_ps1: for context in editwin.num_context_lines: startat = max(lno - context, 1) startatindex = repr(startat) + '.0' stopatindex = '%d.end' % lno parser.set_str(text.get(startatindex, stopatindex) + ' \n') bod = parser.find_good_parse_start(editwin._build_char_in_string_func(startatindex)) if bod is not None or startat == 1: break parser.set_lo(bod or 0) else: r = text.tag_prevrange('console', index) if r: startatindex = r[1] else: startatindex = '1.0' stopatindex = '%d.end' % lno parser.set_str(text.get(startatindex, stopatindex) + ' \n') parser.set_lo(0) self.rawtext = parser.str[:-2] self.stopatindex = stopatindex self.bracketing = parser.get_last_stmt_bracketing() self.isopener = [ i > 0 and self.bracketing[i][1] > self.bracketing[i - 1][1] for i in range(len(self.bracketing)) ] self.set_index(index) return
def __init__(self, editwin, index): "To initialize, analyze the surroundings of the given index." self.editwin = editwin self.text = text = editwin.text self.ext = ext = editwin.ext parser = PyParse.Parser(editwin.indentwidth, editwin.tabwidth) def index2line(index): return int(float(index)) lno = index2line(text.index(index)) if not editwin.context_use_ps1: for context in editwin.num_context_lines: startat = max(lno - context, 1) startatindex = repr(startat) + ".0" stopatindex = "%d.end" % lno # We add the newline because PyParse requires a newline # at end. We add a space so that index won't be at end # of line, so that its status will be the same as the # char before it, if should. parser.set_str(text.get(startatindex, stopatindex) + ' \n') bod = parser.find_good_parse_start( editwin._build_char_in_string_func(startatindex)) if bod is not None or startat == 1: break parser.set_lo(bod or 0) else: r = text.tag_prevrange("console", index) if r: startatindex = r[1] else: startatindex = "1.0" stopatindex = "%d.end" % lno # We add the newline because PyParse requires it. We add a # space so that index won't be at end of line, so that its # status will be the same as the char before it, if should. parser.set_str(text.get(startatindex, stopatindex) + ' \n') parser.set_lo(0) # We want what the parser has, minus the last newline and space. self.rawtext = parser.str[:-2] # Parser.str apparently preserves the statement we are in, so # that stopatindex can be used to synchronize the string with # the text box indices. self.stopatindex = stopatindex self.bracketing = parser.get_last_stmt_bracketing() # find which pairs of bracketing are openers. These always # correspond to a character of rawtext. self.isopener = [ i > 0 and self.bracketing[i][1] > self.bracketing[i - 1][1] for i in range(len(self.bracketing)) ] self.set_index(index)
def newline_and_indent_event(self, event): text = self.text first, last = self.get_selection_indices() text.undo_block_start() try: if first and last: text.delete(first, last) text.mark_set('insert', first) line = text.get('insert linestart', 'insert') i, n = 0, len(line) while i < n and line[i] in ' \t': i = i + 1 if i == n: text.insert('insert linestart', '\n') return 'break' indent = line[:i] i = 0 last_line_of_prompt = sys.ps1.split('\n')[-1] while line and line[-1] in ' \t' and line != last_line_of_prompt: line = line[:-1] i = i + 1 if i: text.delete('insert - %d chars' % i, 'insert') while text.get('insert') in ' \t': text.delete('insert') text.insert('insert', '\n') lno = index2line(text.index('insert')) y = PyParse.Parser(self.indentwidth, self.tabwidth) if not self.context_use_ps1: for context in self.num_context_lines: startat = max(lno - context, 1) startatindex = repr(startat) + '.0' rawtext = text.get(startatindex, 'insert') y.set_str(rawtext) bod = y.find_good_parse_start(self.context_use_ps1, self._build_char_in_string_func(startatindex)) if bod is not None or startat == 1: break y.set_lo(bod or 0) else: r = text.tag_prevrange('console', 'insert') if r: startatindex = r[1] else: startatindex = '1.0' rawtext = text.get(startatindex, 'insert') y.set_str(rawtext) y.set_lo(0) c = y.get_continuation_type() if c != PyParse.C_NONE: if c == PyParse.C_STRING_FIRST_LINE: pass elif c == PyParse.C_STRING_NEXT_LINES: text.insert('insert', indent) elif c == PyParse.C_BRACKET: self.reindent_to(y.compute_bracket_indent()) elif c == PyParse.C_BACKSLASH: if y.get_num_lines_in_stmt() > 1: text.insert('insert', indent) else: self.reindent_to(y.compute_backslash_indent()) return 'break' indent = y.get_base_indent_string() text.insert('insert', indent) if y.is_block_opener(): self.smart_indent_event(event) elif indent and y.is_block_closer(): self.smart_backspace_event(event) return 'break' finally: text.see('insert') text.undo_block_stop() return