File: indenter.py

package info (click to toggle)
orange3 3.40.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 15,908 kB
  • sloc: python: 162,745; ansic: 622; makefile: 322; sh: 93; cpp: 77
file content (530 lines) | stat: -rw-r--r-- 18,334 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
"""
Adapted from a code editor component created
for Enki editor as replacement for QScintilla.
Copyright (C) 2020  Andrei Kopats

Originally licensed under the terms of GNU Lesser General Public License
as published by the Free Software Foundation, version 2.1 of the license.
This is compatible with Orange3's GPL-3.0 license.
"""
from AnyQt.QtGui import QTextCursor

# pylint: disable=pointless-string-statement

MAX_SEARCH_OFFSET_LINES = 128


class Indenter:
    """Qutepart functionality, related to indentation

    Public attributes:
        width           Indent width
        useTabs         Indent uses Tabs (instead of spaces)
    """
    _DEFAULT_INDENT_WIDTH = 4
    _DEFAULT_INDENT_USE_TABS = False

    def __init__(self, qpart):
        self._qpart = qpart

        self.width = self._DEFAULT_INDENT_WIDTH
        self.useTabs = self._DEFAULT_INDENT_USE_TABS

        self._smartIndenter = IndentAlgPython(qpart, self)

    def text(self):
        """Get indent text as \t or string of spaces
        """
        if self.useTabs:
            return '\t'
        else:
            return ' ' * self.width

    def triggerCharacters(self):
        """Trigger characters for smart indentation"""
        return self._smartIndenter.TRIGGER_CHARACTERS

    def autoIndentBlock(self, block, char='\n'):
        """Indent block after Enter pressed or trigger character typed
        """
        currentText = block.text()
        spaceAtStartLen = len(currentText) - len(currentText.lstrip())
        currentIndent = currentText[:spaceAtStartLen]
        indent = self._smartIndenter.computeIndent(block, char)
        if indent is not None and indent != currentIndent:
            self._qpart.replaceText(block.position(), spaceAtStartLen, indent)

    def onChangeSelectedBlocksIndent(self, increase, withSpace=False):
        """Tab or Space pressed and few blocks are selected, or Shift+Tab pressed
        Insert or remove text from the beginning of blocks
        """

        def blockIndentation(block):
            text = block.text()
            return text[:len(text) - len(text.lstrip())]

        def cursorAtSpaceEnd(block):
            cursor = QTextCursor(block)
            cursor.setPosition(block.position() + len(blockIndentation(block)))
            return cursor

        def indentBlock(block):
            cursor = cursorAtSpaceEnd(block)
            cursor.insertText(' ' if withSpace else self.text())

        def spacesCount(text):
            return len(text) - len(text.rstrip(' '))

        def unIndentBlock(block):
            currentIndent = blockIndentation(block)

            if currentIndent.endswith('\t'):
                charsToRemove = 1
            elif withSpace:
                charsToRemove = 1 if currentIndent else 0
            else:
                if self.useTabs:
                    charsToRemove = min(spacesCount(currentIndent), self.width)
                else:  # spaces
                    if currentIndent.endswith(self.text()):  # remove indent level
                        charsToRemove = self.width
                    else:  # remove all spaces
                        charsToRemove = min(spacesCount(currentIndent), self.width)

            if charsToRemove:
                cursor = cursorAtSpaceEnd(block)
                cursor.setPosition(cursor.position() - charsToRemove, QTextCursor.KeepAnchor)
                cursor.removeSelectedText()

        cursor = self._qpart.textCursor()

        startBlock = self._qpart.document().findBlock(cursor.selectionStart())
        endBlock = self._qpart.document().findBlock(cursor.selectionEnd())
        if (cursor.selectionStart() != cursor.selectionEnd() and
                endBlock.position() == cursor.selectionEnd() and
                endBlock.previous().isValid()):
            # do not indent not selected line if indenting multiple lines
            endBlock = endBlock.previous()

        indentFunc = indentBlock if increase else unIndentBlock

        if startBlock != endBlock:  # indent multiply lines
            stopBlock = endBlock.next()

            block = startBlock

            with self._qpart:
                while block != stopBlock:
                    indentFunc(block)
                    block = block.next()

            newCursor = QTextCursor(startBlock)
            newCursor.setPosition(endBlock.position() + len(endBlock.text()),
                                  QTextCursor.KeepAnchor)
            self._qpart.setTextCursor(newCursor)
        else:  # indent 1 line
            indentFunc(startBlock)

    def onShortcutIndentAfterCursor(self):
        """Tab pressed and no selection. Insert text after cursor
        """
        cursor = self._qpart.textCursor()

        def insertIndent():
            if self.useTabs:
                cursor.insertText('\t')
            else:  # indent to integer count of indents from line start
                charsToInsert = self.width - (len(self._qpart.textBeforeCursor()) % self.width)
                cursor.insertText(' ' * charsToInsert)

        if cursor.positionInBlock() == 0:  # if no any indent - indent smartly
            block = cursor.block()
            self.autoIndentBlock(block, '')

            # if no smart indentation - just insert one indent
            if self._qpart.textBeforeCursor() == '':
                insertIndent()
        else:
            insertIndent()

    def onShortcutUnindentWithBackspace(self):
        """Backspace pressed, unindent
        """
        assert self._qpart.textBeforeCursor().endswith(self.text())

        charsToRemove = len(self._qpart.textBeforeCursor()) % len(self.text())
        if charsToRemove == 0:
            charsToRemove = len(self.text())

        cursor = self._qpart.textCursor()
        cursor.setPosition(cursor.position() - charsToRemove, QTextCursor.KeepAnchor)
        cursor.removeSelectedText()

    def onAutoIndentTriggered(self):
        """Indent current line or selected lines
        """
        cursor = self._qpart.textCursor()

        startBlock = self._qpart.document().findBlock(cursor.selectionStart())
        endBlock = self._qpart.document().findBlock(cursor.selectionEnd())

        if startBlock != endBlock:  # indent multiply lines
            stopBlock = endBlock.next()

            block = startBlock

            with self._qpart:
                while block != stopBlock:
                    self.autoIndentBlock(block, '')
                    block = block.next()
        else:  # indent 1 line
            self.autoIndentBlock(startBlock, '')


class IndentAlgBase:
    """Base class for indenters
    """
    TRIGGER_CHARACTERS = ""  # indenter is called, when user types Enter of one of trigger chars

    def __init__(self, qpart, indenter):
        self._qpart = qpart
        self._indenter = indenter

    def indentBlock(self, block):
        """Indent the block
        """
        self._setBlockIndent(block, self.computeIndent(block, ''))

    def computeIndent(self, block, char):
        """Compute indent for the block.
        Basic alorightm, which knows nothing about programming languages
        May be used by child classes
        """
        prevBlockText = block.previous().text()  # invalid block returns empty text
        if char == '\n' and \
                prevBlockText.strip() == '':  # continue indentation, if no text
            return self._prevBlockIndent(block)
        else:  # be smart
            return self.computeSmartIndent(block, char)

    def computeSmartIndent(self, block, char):
        """Compute smart indent.
        Block is current block.
        Char is typed character. \n or one of trigger chars
        Return indentation text, or None, if indentation shall not be modified

        Implementation might return self._prevNonEmptyBlockIndent(), if doesn't have
        any ideas, how to indent text better
        """
        raise NotImplementedError()

    def _qpartIndent(self):
        """Return text previous block, which is non empty (contains something, except spaces)
        Return '', if not found
        """
        return self._indenter.text()

    def _increaseIndent(self, indent):
        """Add 1 indentation level
        """
        return indent + self._qpartIndent()

    def _decreaseIndent(self, indent):
        """Remove 1 indentation level
        """
        if indent.endswith(self._qpartIndent()):
            return indent[:-len(self._qpartIndent())]
        else:  # oops, strange indentation, just return previous indent
            return indent

    def _makeIndentFromWidth(self, width):
        """Make indent text with specified with.
        Contains width count of spaces, or tabs and spaces
        """
        if self._indenter.useTabs:
            tabCount, spaceCount = divmod(width, self._indenter.width)
            return ('\t' * tabCount) + (' ' * spaceCount)
        else:
            return ' ' * width

    def _makeIndentAsColumn(self, block, column, offset=0):
        """ Make indent equal to column indent.
        Shiftted by offset
        """
        blockText = block.text()
        textBeforeColumn = blockText[:column]
        tabCount = textBeforeColumn.count('\t')

        visibleColumn = column + (tabCount * (self._indenter.width - 1))
        return self._makeIndentFromWidth(visibleColumn + offset)

    def _setBlockIndent(self, block, indent):
        """Set blocks indent. Modify text in qpart
        """
        currentIndent = self._blockIndent(block)
        self._qpart.replaceText((block.blockNumber(), 0), len(currentIndent), indent)

    @staticmethod
    def iterateBlocksFrom(block):
        """Generator, which iterates QTextBlocks from block until the End of a document
        But, yields not more than MAX_SEARCH_OFFSET_LINES
        """
        count = 0
        while block.isValid() and count < MAX_SEARCH_OFFSET_LINES:
            yield block
            block = block.next()
            count += 1

    @staticmethod
    def iterateBlocksBackFrom(block):
        """Generator, which iterates QTextBlocks from block until the Start of a document
        But, yields not more than MAX_SEARCH_OFFSET_LINES
        """
        count = 0
        while block.isValid() and count < MAX_SEARCH_OFFSET_LINES:
            yield block
            block = block.previous()
            count += 1

    @classmethod
    def iterateCharsBackwardFrom(cls, block, column):
        if column is not None:
            text = block.text()[:column]
            for index, char in enumerate(reversed(text)):
                yield block, len(text) - index - 1, char
            block = block.previous()

        for b in cls.iterateBlocksBackFrom(block):
            for index, char in enumerate(reversed(b.text())):
                yield b, len(b.text()) - index - 1, char

    def findBracketBackward(self, block, column, bracket):
        """Search for a needle and return (block, column)
        Raise ValueError, if not found
        """
        if bracket in ('(', ')'):
            opening = '('
            closing = ')'
        elif bracket in ('[', ']'):
            opening = '['
            closing = ']'
        elif bracket in ('{', '}'):
            opening = '{'
            closing = '}'
        else:
            raise AssertionError('Invalid bracket "%s"' % bracket)

        depth = 1
        for foundBlock, foundColumn, char in self.iterateCharsBackwardFrom(block, column):
            if not self._qpart.isComment(foundBlock.blockNumber(), foundColumn):
                if char == opening:
                    depth = depth - 1
                elif char == closing:
                    depth = depth + 1

                if depth == 0:
                    return foundBlock, foundColumn
        raise ValueError('Not found')

    def findAnyBracketBackward(self, block, column):
        """Search for a needle and return (block, column)
        Raise ValueError, if not found

        NOTE this methods ignores strings and comments
        """
        depth = {'()': 1,
                 '[]': 1,
                 '{}': 1
                 }

        for foundBlock, foundColumn, char in self.iterateCharsBackwardFrom(block, column):
            if self._qpart.isCode(foundBlock.blockNumber(), foundColumn):
                for brackets in depth:
                    opening, closing = brackets
                    if char == opening:
                        depth[brackets] -= 1
                        if depth[brackets] == 0:
                            return foundBlock, foundColumn
                    elif char == closing:
                        depth[brackets] += 1
        raise ValueError('Not found')

    @staticmethod
    def _lastNonSpaceChar(block):
        textStripped = block.text().rstrip()
        if textStripped:
            return textStripped[-1]
        else:
            return ''

    @staticmethod
    def _firstNonSpaceChar(block):
        textStripped = block.text().lstrip()
        if textStripped:
            return textStripped[0]
        else:
            return ''

    @staticmethod
    def _firstNonSpaceColumn(text):
        return len(text) - len(text.lstrip())

    @staticmethod
    def _lastNonSpaceColumn(text):
        return len(text.rstrip())

    @classmethod
    def _lineIndent(cls, text):
        return text[:cls._firstNonSpaceColumn(text)]

    @classmethod
    def _blockIndent(cls, block):
        if block.isValid():
            return cls._lineIndent(block.text())
        else:
            return ''

    @classmethod
    def _prevBlockIndent(cls, block):
        prevBlock = block.previous()

        if not block.isValid():
            return ''

        return cls._lineIndent(prevBlock.text())

    @classmethod
    def _prevNonEmptyBlockIndent(cls, block):
        return cls._blockIndent(cls._prevNonEmptyBlock(block))

    @staticmethod
    def _prevNonEmptyBlock(block):
        if not block.isValid():
            return block

        block = block.previous()
        while block.isValid() and \
                len(block.text().strip()) == 0:
            block = block.previous()
        return block

    @staticmethod
    def _nextNonEmptyBlock(block):
        if not block.isValid():
            return block

        block = block.next()
        while block.isValid() and \
                len(block.text().strip()) == 0:
            block = block.next()
        return block

    @staticmethod
    def _nextNonSpaceColumn(block, column):
        """Returns the column with a non-whitespace characters
        starting at the given cursor position and searching forwards.
        """
        textAfter = block.text()[column:]
        if textAfter.strip():
            spaceLen = len(textAfter) - len(textAfter.lstrip())
            return column + spaceLen
        else:
            return -1


class IndentAlgPython(IndentAlgBase):
    """Indenter for Python language.
    """

    def _computeSmartIndent(self, block, column):
        """Compute smart indent for case when cursor is on (block, column)
        """
        lineStripped = block.text()[:column].strip()  # empty text from invalid block is ok
        spaceLen = len(block.text()) - len(block.text().lstrip())

        """Move initial search position to bracket start, if bracket was closed
        l = [1,
             2]|
        """
        if lineStripped and \
                lineStripped[-1] in ')]}':
            try:
                backward = self.findBracketBackward(block, spaceLen + len(lineStripped) - 1,
                                                    lineStripped[-1])
                foundBlock, foundColumn = backward
            except ValueError:
                pass
            else:
                return self._computeSmartIndent(foundBlock, foundColumn)

        """Unindent if hanging indentation finished
        func(a,
             another_func(a,
                          b),|
        """
        if len(lineStripped) > 1 and \
                lineStripped[-1] == ',' and \
                lineStripped[-2] in ')]}':

            try:
                foundBlock, foundColumn = self.findBracketBackward(block,
                                                                   len(block.text()[
                                                                       :column].rstrip()) - 2,
                                                                   lineStripped[-2])
            except ValueError:
                pass
            else:
                return self._computeSmartIndent(foundBlock, foundColumn)

        """Check hanging indentation
        call_func(x,
                  y,
                  z
        But
        call_func(x,
            y,
            z
        """
        try:
            foundBlock, foundColumn = self.findAnyBracketBackward(block,
                                                                  column)
        except ValueError:
            pass
        else:
            # indent this way only line, which contains 'y', not 'z'
            if foundBlock.blockNumber() == block.blockNumber():
                return self._makeIndentAsColumn(foundBlock, foundColumn + 1)

        # finally, a raise, pass, and continue should unindent
        if lineStripped in ('continue', 'break', 'pass', 'raise', 'return') or \
                lineStripped.startswith('raise ') or \
                lineStripped.startswith('return '):
            return self._decreaseIndent(self._blockIndent(block))

        """
        for:

        func(a,
             b):
        """
        if lineStripped.endswith(':'):
            newColumn = spaceLen + len(lineStripped) - 1
            prevIndent = self._computeSmartIndent(block, newColumn)
            return self._increaseIndent(prevIndent)

        """ Generally, when a brace is on its own at the end of a regular line
        (i.e a data structure is being started), indent is wanted.
        For example:
        dictionary = {
            'foo': 'bar',
        }
        """
        if lineStripped.endswith('{['):
            return self._increaseIndent(self._blockIndent(block))

        return self._blockIndent(block)

    def computeSmartIndent(self, block, char):
        block = self._prevNonEmptyBlock(block)
        column = len(block.text())
        return self._computeSmartIndent(block, column)