File: lydocument.py

package info (click to toggle)
frescobaldi 3.0.0~git20161001.0.eec60717%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 19,792 kB
  • ctags: 5,843
  • sloc: python: 37,853; sh: 180; makefile: 69
file content (252 lines) | stat: -rw-r--r-- 9,129 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
# This file is part of the Frescobaldi project, http://www.frescobaldi.org/
#
# Copyright (c) 2013 - 2014 by Wilbert Berendsen
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
# See http://www.gnu.org/licenses/ for more information.

"""
Document

Provides a ly.document.Document api for a QTextDocument (or: more specifically
a Frescobaldi document.Document).

This can be used to perform operations from the ly module on a loaded
Frescobaldi document.

You don't need to save a Document instance. Just create it and use it, then 
discard it.

"""

from __future__ import absolute_import

from PyQt5.QtGui import QTextCursor

import ly.document
import tokeniter
import highlighter


def cursor(cursor, select_all=False):
    """Return a Cursor for the specified QTextCursor.
    
    The ly Cursor is instantiated with a Document proxying for the
    original cursors document.
    
    So you can call all operations in the ly module and they will work on a
    Frescobaldi document (which is a subclass of QTextDocument).
    
    If select_all is True, the ly Cursor selects the whole document if the 
    original cursor has no selection.
    
    """
    if not select_all or cursor.hasSelection():
        start, end = cursor.selectionStart(), cursor.selectionEnd()
    else:
        start, end = 0, None
    return Cursor(Document(cursor.document()), start, end)
    

class Cursor(ly.document.Cursor):
    """A ly.document.Cursor with an extra cursor() method."""
    def cursor(self):
        """Return a QTextCursor with the same selection."""
        c = QTextCursor(self.document.document)
        c.movePosition(QTextCursor.End) if self.end is None else c.setPosition(self.end)
        c.setPosition(self.start, QTextCursor.KeepAnchor)
        return c


class Document(ly.document.DocumentBase):
    """Document proxies a loaded Frescobaldi document (QTextDocument).
    
    This is used to let the tools in the ly module operate on Frescobaldi
    documents.
    
    Creating a Document is very fast, you do not need to save it. When 
    applying the changes, Document starts an editblock, so that the 
    operations appears as one undo-item.
    
    It is recommended to not nest calls to QTextCursor.beginEditBlock(), as 
    the highlighter is not called to update the tokens until the last 
    endEditBlock() is called.
    
    Therefore Document provides a simple mechanism for combining several 
    change operations via the combine_undo attribute.
    
    If combine_undo is None (the default), the first time changes are applied
    QTextCursor.beginEditBlock() will be called, but subsequent times 
    QTextCursor.joinPreviousEditBlock() will be used. So the highlighter 
    updates the tokens between the operations, but they will appear as one 
    undo-item.
    
    If you want to combine the very first operation already with an earlier 
    change, set combine_undo to True before the changes are applied (e.g. 
    before entering or exiting the context).
    
    If you do not want to combine operations into a single undo-item at all,
    set combine_undo to False.
    
    (Of course you can nest calls to QTextCursor.beginEditBlock(), but in 
    that case the tokens will not be updated between your operations. If 
    your operations do not depend on the tokens, it is no problem 
    whatsoever. The tokens *are* updated after the last call to 
    QTextCursor.endEditBlock().)
    
    """
    
    def __init__(self, document):
        self._d = document
        super(Document, self).__init__()
        self.combine_undo = None
    
    def __len__(self):
        """Return the number of blocks"""
        return self._d.blockCount()
    
    def __getitem__(self, index):
        """Return the block at the specified index."""
        return self._d.findBlockByNumber(index)
        
    @property
    def document(self):
        """Return the QTextDocument we were instantiated with."""
        return self._d
    
    @property
    def filename(self):
        """Return the document's local filename, if any."""
        return self.document.url().toLocalFile()
    
    def plaintext(self):
        """The document contents as a plain text string."""
        return self._d.toPlainText()

    def setplaintext(self, text):
        """Sets the document contents to the text string."""
        self._d.setPlainText(text)

    def size(self):
        """Return the number of characters in the document."""
        return self._d.characterCount()

    def block(self, position):
        """Return the text block at the specified character position.
        
        The text block itself has no methods, but it can be used as an
        argument to other methods of this class.
        
        (Blocks do have to support the '==' operator.)
        
        """
        return self._d.findBlock(position)
    
    def index(self, block):
        """Return the linenumber of the block (starting with 0)."""
        return block.blockNumber()
         
    def position(self, block):
        """Return the position of the specified block."""
        return block.position()

    def text(self, block):
        """Return the text of the specified block."""
        return block.text()
    
    def next_block(self, block):
        """Return the next block, which may be invalid."""
        return block.next()
    
    def previous_block(self, block):
        """Return the previous block, which may be invalid."""
        return block.previous()
    
    def isvalid(self, block):
        """Return True if the block is a valid block."""
        return block.isValid()
    
    def apply_changes(self):
        """Apply the changes and update the tokens."""
        c = QTextCursor(self._d)
        # record a sensible position for undo
        c.setPosition(self._changes_list[-1][0])
        c.joinPreviousEditBlock() if self.combine_undo else c.beginEditBlock()
        try:
            for start, end, text in self._changes_list:
                c.movePosition(QTextCursor.End) if end is None else c.setPosition(end)
                c.setPosition(start, QTextCursor.KeepAnchor)
                c.insertText(text)
        finally:
            c.endEditBlock()
            if self.combine_undo is None:
                self.combine_undo = True
        
    def tokens(self, block):
        """Return the tuple of tokens of the specified block."""
        return tokeniter.tokens(block)
        
    def initial_state(self):
        """Return the state at the beginning of the document."""
        return highlighter.highlighter(self._d).initialState()
        
    def state(self, block):
        """Return the state at the start of the specified block."""
        return tokeniter.state(block)
            
    def state_end(self, block):
        """Return the state at the end of the specified block."""
        return tokeniter.state_end(block)


class Runner(ly.document.Runner):
    """A Runner that adds a cursor() method, returning a QTextCursor."""
    def cursor(self, start=0, end=None):
        """Returns a QTextCursor for the last token.
        
        If start is given the cursor will start at position start in the token
        (from the beginning of the token). Start defaults to 0.
        If end is given, the cursor will end at that position in the token (from
        the beginning of the token). End defaults to the length of the token.
        
        """
        if end is None:
            end = len(self.token())
        c = QTextCursor(self.document.document)
        c.setPosition(self.position() + start)
        c.setPosition(self.position() + end, QTextCursor.KeepAnchor)
        return c


class Source(ly.document.Source):
    """A Source that adds a cursor() method, returning a QTextCursor."""
    def cursor(self, token, start=0, end=None):
        """Returns a QTextCursor for the specified token.
        
        If start is given the cursor will start at position start in the token
        (from the beginning of the token). Start defaults to 0.
        If end is given, the cursor will end at that position in the token (from
        the beginning of the token). End defaults to the length of the token.
        
        """
        if end is None:
            end = len(token)
        c = QTextCursor(self.document.document)
        pos = self.position(token)
        c.setPosition(pos + start)
        c.setPosition(pos + end, QTextCursor.KeepAnchor)
        return c