File: matcher.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 (205 lines) | stat: -rw-r--r-- 7,352 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
# This file is part of the Frescobaldi project, http://www.frescobaldi.org/
#
# Copyright (c) 2008 - 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.

"""
Highlights matching tokens such as { and }, << and >> etc.
"""


import weakref

from PyQt5.QtWidgets import QAction

import app
import plugin
import ly.lex
import lydocument
import viewhighlighter
import actioncollection
import actioncollectionmanager


class AbstractMatcher(object):
    def __init__(self, view=None):
        """Initialize with an optional View. (Does not keep a reference.)"""
        self._view = lambda: None
        if view:
            self.setView(view)
        app.settingsChanged.connect(self.updateSettings)
        self.updateSettings()

    def updateSettings(self):
        from PyQt5.QtCore import QSettings
        s = QSettings()
        s.beginGroup("editor_highlighting")
        self._match_duration = s.value("match", 1, int) * 1000

    def setView(self, view):
        """Set the current View (to monitor for cursor position changes)."""
        old = self._view()
        if old:
            old.cursorPositionChanged.disconnect(self.showMatches)
        if view:
            self._view = weakref.ref(view)
            view.cursorPositionChanged.connect(self.showMatches)
        else:
            self._view = lambda: None
    
    def view(self):
        """Return the current View."""
        return self._view()
    
    def highlighter(self):
        """Implement to return an ArbitraryHighlighter for the current View."""
        pass
    
    def showMatches(self):
        """Highlights matching tokens if the view's cursor is at such a token."""
        cursors = matches(self.view().textCursor(), self.view())
        if cursors:
            self.highlighter().highlight("match", cursors, 2, self._match_duration)
        else:
            self.highlighter().clear("match")


class Matcher(AbstractMatcher, plugin.MainWindowPlugin):
    """One Matcher automatically handling the current View."""
    def __init__(self, mainwindow):
        super(Matcher, self).__init__()
        ac = self.actionCollection = Actions()
        actioncollectionmanager.manager(mainwindow).addActionCollection(ac)
        ac.view_matching_pair.triggered.connect(self.moveto_match)
        ac.view_matching_pair_select.triggered.connect(self.select_match)
        mainwindow.currentViewChanged.connect(self.setView)

        view = mainwindow.currentView()
        if view:
            self.setView(view)
        
    def highlighter(self):
        return viewhighlighter.highlighter(self.view())

    def moveto_match(self):
        """Jump to the matching token."""
        self.goto_match(False)
        
    def select_match(self):
        """Select from the current to the matching token."""
        self.goto_match(True)
        
    def goto_match(self, select=False):
        """Jump to the matching token, selecting the text if select is True."""
        cursor = self.view().textCursor()
        cursors = matches(cursor)
        if len(cursors) < 2:
            return
        if select:
            if cursors[0] < cursors[1]:
                anchor, pos = cursors[0].selectionStart(), cursors[1].selectionEnd()
            else:
                anchor, pos = cursors[0].selectionEnd(), cursors[1].selectionStart()
            cursor.setPosition(anchor)
            cursor.setPosition(pos, cursor.KeepAnchor)
        else:
            cursor.setPosition(cursors[1].selectionStart())
        self.view().setTextCursor(cursor)


class Actions(actioncollection.ActionCollection):
    name = "matchingpair"
    def createActions(self, parent):
        self.view_matching_pair = QAction(parent)
        self.view_matching_pair_select = QAction(parent)
    
    def translateUI(self):
        self.view_matching_pair.setText(_("Matching Pai&r"))
        self.view_matching_pair_select.setText(_("&Select Matching Pair"))


def matches(cursor, view=None):
    """Return a list of zero to two cursors specifying matching tokens.
    
    If the list is empty, the cursor was not at a MatchStart/MatchEnd token,
    if the list only contains one cursor the matching token could not be found,
    if the list contains two cursors, the first is the token the cursor was at,
    and the second is the matching token.
    
    If view is given, only the visible part of the document is searched.
    
    """
    block = cursor.block()
    column = cursor.position() - block.position()
    tokens = lydocument.Runner(lydocument.Document(cursor.document()))
    tokens.move_to_block(block)
    
    if view is not None:
        first_block = view.firstVisibleBlock()
        bottom = view.contentOffset().y() + view.viewport().height()
        pred_forward = lambda: view.blockBoundingGeometry(tokens.block).top() <= bottom
        pred_backward = lambda: tokens.block >= first_block
    else:
        pred_forward = lambda: True
        pred_backward = lambda: True
    
    source = None
    for token in tokens.forward_line():
        if token.pos <= column <= token.end:
            if isinstance(token, ly.lex.MatchStart):
                match, other = ly.lex.MatchStart, ly.lex.MatchEnd
                def source_gen():
                    while pred_forward():
                        for t in tokens.forward_line():
                            yield t
                        if not tokens.next_block():
                            break
                source = source_gen()
                break
            elif isinstance(token, ly.lex.MatchEnd):
                match, other = ly.lex.MatchEnd, ly.lex.MatchStart
                def source_gen():
                    while pred_backward():
                        for t in tokens.backward_line():
                            yield t
                        if not tokens.previous_block():
                            break
                source = source_gen()
                break
        elif token.pos > column:
            break
    cursors = []
    if source:
        # we've found a matcher item
        cursors.append(tokens.cursor())
        nest = 0
        for token2 in source:
            if isinstance(token2, other) and token2.matchname == token.matchname:
                if nest == 0:
                    # we've found the matching item!
                    cursors.append(tokens.cursor())
                    break
                else:
                    nest -= 1
            elif isinstance(token2, match) and token2.matchname == token.matchname:
                nest += 1
    return cursors



app.mainwindowCreated.connect(Matcher.instance)