Krita Source Code Documentation
Loading...
Searching...
No Matches
krita.sceditor.indenter.PythonCodeIndenter Class Reference
+ Inheritance diagram for krita.sceditor.indenter.PythonCodeIndenter:

Public Member Functions

 __init__ (self, editor, indents=4)
 
 correct_indentation (self, lineno)
 
- Public Member Functions inherited from krita.sceditor.indenter.TextIndenter
 deindent (self, lineno)
 
 entering_new_line (self, lineno)
 
 indent (self, lineno)
 
 insert_tab (self, index)
 

Public Attributes

 line_editor
 
- Public Attributes inherited from krita.sceditor.indenter.TextIndenter
 editor
 
 indents
 
 line_editor
 

Protected Member Functions

 _get_base_indentation (self, lineno)
 
 _get_correct_indentation (self, lineno)
 
 _indents_caused_by_current_stmt (self, current_line)
 
 _indents_caused_by_prev_stmt (self, stmt_range)
 
 _last_non_blank (self, lineno)
 
 _startswith (self, line, tokens)
 
 _strip (self, line)
 
- Protected Member Functions inherited from krita.sceditor.indenter.TextIndenter
 _count_line_indents (self, lineno)
 
 _set_line_indents (self, lineno, indents)
 

Detailed Description

Definition at line 81 of file indenter.py.

Constructor & Destructor Documentation

◆ __init__()

krita.sceditor.indenter.PythonCodeIndenter.__init__ ( self,
editor,
indents = 4 )

Reimplemented from krita.sceditor.indenter.TextIndenter.

Definition at line 83 of file indenter.py.

83 def __init__(self, editor, indents=4):
84 super(PythonCodeIndenter, self).__init__(editor, indents)
85

References krita.sceditor.indenter.PythonCodeIndenter.__init__().

Member Function Documentation

◆ _get_base_indentation()

krita.sceditor.indenter.PythonCodeIndenter._get_base_indentation ( self,
lineno )
protected

Definition at line 106 of file indenter.py.

106 def _get_base_indentation(self, lineno):
107 range_finder = _StatementRangeFinder(
108 self.line_editor, self._last_non_blank(lineno))
109 start = range_finder.get_statement_start()
110 if not range_finder.is_line_continued():
111 changes = self._indents_caused_by_prev_stmt(
112 (start, self._last_non_blank(lineno)))
113 return self._count_line_indents(start) + changes
114 if range_finder.last_open_parens():
115 open_parens = range_finder.last_open_parens()
116 parens_line = self.line_editor.get_line(open_parens[0])
117 if parens_line[open_parens[1] + 1:].strip() == '':
118 if len(range_finder.open_parens) > 1:
119 return range_finder.open_parens[-2][1] + 1
120 else:
121 return self._count_line_indents(start) + self.indents
122 return range_finder.last_open_parens()[1] + 1
123
124 start_line = self.line_editor.get_line(start)
125 if start == lineno - 1:
126 try:
127 equals_index = start_line.index(' = ') + 1
128 if start_line[equals_index + 1:].strip() == '\\':
129 return self._count_line_indents(start) + self.indents
130 return equals_index + 2
131 except ValueError:
132 match = re.search(r'(\b )|(\.)', start_line)
133 if match:
134 return match.start() + 1
135 else:
136 return len(start_line) + 1
137 else:
138 return self._count_line_indents(self._last_non_blank(lineno))
139

◆ _get_correct_indentation()

krita.sceditor.indenter.PythonCodeIndenter._get_correct_indentation ( self,
lineno )
protected

Definition at line 93 of file indenter.py.

93 def _get_correct_indentation(self, lineno):
94 if lineno == 1:
95 return 0
96 new_indent = self._get_base_indentation(lineno)
97
98 prev_lineno = self._last_non_blank(lineno)
99 prev_line = self.line_editor.get_line(prev_lineno)
100 if prev_lineno == lineno or prev_line.strip() == '':
101 new_indent = 0
102 current_line = self.line_editor.get_line(lineno)
103 new_indent += self._indents_caused_by_current_stmt(current_line)
104 return new_indent
105

References krita.sceditor.indenter.PythonCodeIndenter._get_base_indentation(), krita.sceditor.indenter.PythonCodeIndenter._indents_caused_by_current_stmt(), krita.sceditor.indenter.PythonCodeIndenter._last_non_blank(), krita.sceditor.indenter.TextIndenter.line_editor, krita.sceditor.indenter.PythonCodeIndenter.line_editor, and krita.sceditor.widget.RopeEditorWrapper.line_editor().

◆ _indents_caused_by_current_stmt()

krita.sceditor.indenter.PythonCodeIndenter._indents_caused_by_current_stmt ( self,
current_line )
protected

Definition at line 167 of file indenter.py.

167 def _indents_caused_by_current_stmt(self, current_line):
168 new_indent = 0
169 if self._strip(current_line) == 'else:':
170 new_indent -= self.indents
171 if self._strip(current_line) == 'finally:':
172 new_indent -= self.indents
173 if self._startswith(current_line, ('elif',)):
174 new_indent -= self.indents
175 if self._startswith(current_line, ('except',)) and \
176 self._strip(current_line).endswith(':'):
177 new_indent -= self.indents
178 return new_indent
179

References krita.sceditor.indenter.PythonCodeIndenter._startswith(), krita.sceditor.indenter.PythonCodeIndenter._strip(), and krita.sceditor.indenter.TextIndenter.indents.

◆ _indents_caused_by_prev_stmt()

krita.sceditor.indenter.PythonCodeIndenter._indents_caused_by_prev_stmt ( self,
stmt_range )
protected

Definition at line 140 of file indenter.py.

140 def _indents_caused_by_prev_stmt(self, stmt_range):
141 first_line = self.line_editor.get_line(stmt_range[0])
142 last_line = self.line_editor.get_line(stmt_range[1])
143 new_indent = 0
144 if self._strip(last_line).endswith(':'):
145 new_indent += self.indents
146 if self._startswith(first_line, ('return', 'raise', 'pass',
147 'break', 'continue')):
148 new_indent -= self.indents
149 return new_indent
150

References krita.sceditor.indenter.PythonCodeIndenter._startswith(), krita.sceditor.indenter.PythonCodeIndenter._strip(), krita.sceditor.indenter.TextIndenter.indents, krita.sceditor.indenter.TextIndenter.line_editor, krita.sceditor.indenter.PythonCodeIndenter.line_editor, and krita.sceditor.widget.RopeEditorWrapper.line_editor().

◆ _last_non_blank()

krita.sceditor.indenter.PythonCodeIndenter._last_non_blank ( self,
lineno )
protected

Definition at line 86 of file indenter.py.

86 def _last_non_blank(self, lineno):
87 current_line = lineno - 1
88 while current_line != 1 and \
89 self.line_editor.get_line(current_line).strip() == '':
90 current_line -= 1
91 return current_line
92

References krita.sceditor.indenter.TextIndenter.line_editor, krita.sceditor.indenter.PythonCodeIndenter.line_editor, and krita.sceditor.widget.RopeEditorWrapper.line_editor().

◆ _startswith()

krita.sceditor.indenter.PythonCodeIndenter._startswith ( self,
line,
tokens )
protected

Definition at line 151 of file indenter.py.

151 def _startswith(self, line, tokens):
152 line = self._strip(line)
153 for token in tokens:
154 if line == token or line.startswith(token + ' '):
155 return True
156

References krita.sceditor.indenter.PythonCodeIndenter._strip().

◆ _strip()

krita.sceditor.indenter.PythonCodeIndenter._strip ( self,
line )
protected

Definition at line 157 of file indenter.py.

157 def _strip(self, line):
158 try:
159 numsign = line.rindex('#')
160 comment = line[numsign:]
161 if '\'' not in comment and '\"' not in comment:
162 line = line[:numsign]
163 except ValueError:
164 pass
165 return line.strip()
166

◆ correct_indentation()

krita.sceditor.indenter.PythonCodeIndenter.correct_indentation ( self,
lineno )
Correct the indentation of the line containing the given index

Reimplemented from krita.sceditor.indenter.TextIndenter.

Definition at line 180 of file indenter.py.

180 def correct_indentation(self, lineno):
181 """Correct the indentation of the line containing the given index"""
182 self._set_line_indents(lineno, self._get_correct_indentation(lineno))
183
184

References krita.sceditor.indenter.PythonCodeIndenter._get_correct_indentation(), and krita.sceditor.indenter.TextIndenter._set_line_indents().

Member Data Documentation

◆ line_editor

krita.sceditor.indenter.PythonCodeIndenter.line_editor

Definition at line 108 of file indenter.py.


The documentation for this class was generated from the following file: