summaryrefslogtreecommitdiff
path: root/qutebrowser/completion/completer.py
blob: b7d49a2741005a88471598d26e2d78b198a99798 (plain)
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
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:

# Copyright 2014-2021 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
#
# This file is part of qutebrowser.
#
# qutebrowser 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 3 of the License, or
# (at your option) any later version.
#
# qutebrowser 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 qutebrowser.  If not, see <https://www.gnu.org/licenses/>.

"""Completer attached to a CompletionView."""

import dataclasses
from typing import TYPE_CHECKING

from PyQt5.QtCore import pyqtSlot, QObject, QTimer

from qutebrowser.config import config
from qutebrowser.commands import parser, cmdexc
from qutebrowser.misc import objects, split
from qutebrowser.utils import log, utils, debug, objreg
from qutebrowser.completion.models import miscmodels
from qutebrowser.completion import completionwidget
if TYPE_CHECKING:
    from qutebrowser.browser import browsertab


@dataclasses.dataclass
class CompletionInfo:

    """Context passed into all completion functions."""

    config: config.Config
    keyconf: config.KeyConfig  # pylint: disable=used-before-assignment
    win_id: int
    cur_tab: 'browsertab.AbstractTab'


class Completer(QObject):

    """Completer which manages completions in a CompletionView.

    Attributes:
        _cmd: The statusbar Command object this completer belongs to.
        _win_id: The id of the window that owns this object.
        _timer: The timer used to trigger the completion update.
        _last_cursor_pos: The old cursor position so we avoid double completion
                          updates.
        _last_text: The old command text so we avoid double completion updates.
        _last_before_cursor: The prior value of before_cursor.
    """

    def __init__(self, *, cmd, win_id, parent=None):
        super().__init__(parent)
        self._cmd = cmd
        self._win_id = win_id
        self._timer = QTimer()
        self._timer.setSingleShot(True)
        self._timer.setInterval(0)
        self._timer.timeout.connect(self._update_completion)
        self._last_cursor_pos = -1
        self._last_text = None
        self._last_before_cursor = None
        self._cmd.update_completion.connect(self.schedule_completion_update)

    def __repr__(self):
        return utils.get_repr(self)

    def _completion(self) -> completionwidget.CompletionView:
        """Convenience method to get the current completion."""
        completion = self.parent()
        assert isinstance(completion, completionwidget.CompletionView)
        return completion

    def _model(self):
        """Convenience method to get the current completion model."""
        return self._completion().model()

    def _get_new_completion(self, before_cursor, under_cursor):
        """Get the completion function based on the current command text.

        Args:
            before_cursor: The command chunks before the cursor.
            under_cursor: The command chunk under the cursor.

        Return:
            A completion model.
        """
        if '--' in before_cursor or under_cursor.startswith('-'):
            # cursor on a flag or after an explicit split (--)
            return None
        log.completion.debug("Before removing flags: {}".format(before_cursor))
        if not before_cursor:
            # '|' or 'set|'
            log.completion.debug('Starting command completion')
            return miscmodels.command
        try:
            cmd = objects.commands[before_cursor[0]]
        except KeyError:
            log.completion.debug("No completion for unknown command: {}"
                                 .format(before_cursor[0]))
            return None

        before_cursor = [x for x in before_cursor if not x.startswith('-')]
        log.completion.debug("After removing flags: {}".format(before_cursor))
        argpos = len(before_cursor) - 1
        try:
            func = cmd.get_pos_arg_info(argpos).completion
        except IndexError:
            log.completion.debug("No completion in position {}".format(argpos))
            return None
        return func

    def _quote(self, s):
        """Quote s if it needs quoting for the commandline.

        Note we don't use shlex.quote because that quotes a lot of shell
        metachars we don't need to have quoted.
        """
        if not s:
            return "''"
        elif any(c in s for c in ' "\'\t\n\\'):
            # use single quotes, and put single quotes into double quotes
            # the string $'b is then quoted as '$'"'"'b'
            return "'" + s.replace("'", "'\"'\"'") + "'"
        else:
            return s

    def _partition(self):
        """Divide the commandline text into chunks around the cursor position.

        Return:
            ([parts_before_cursor], 'part_under_cursor', [parts_after_cursor])
        """
        text = self._cmd.text()[len(self._cmd.prefix()):]
        if not text or not text.strip():
            # Only ":", empty part under the cursor with nothing before/after
            return [], '', []

        try:
            parse_result = parser.CommandParser().parse(text, keep=True)
        except cmdexc.NoSuchCommandError:
            cmdline = split.split(text, keep=True)
        else:
            cmdline = parse_result.cmdline

        parts = [x for x in cmdline if x]
        pos = self._cmd.cursorPosition() - len(self._cmd.prefix())
        pos = min(pos, len(text))  # Qt treats 2-byte UTF-16 chars as 2 chars
        log.completion.debug(f'partitioning {parts} around position {pos}')
        for i, part in enumerate(parts):
            pos -= len(part)
            if pos <= 0:
                if part[pos-1:pos+1].isspace():
                    # cursor is in a space between two existing words
                    parts.insert(i, '')
                prefix = [x.strip() for x in parts[:i]]
                center = parts[i].strip()
                # strip trailing whitespace included as a separate token
                postfix = [x.strip() for x in parts[i+1:] if not x.isspace()]
                log.completion.debug(f"partitioned: {prefix} '{center}' {postfix}")
                return prefix, center, postfix

        raise utils.Unreachable(f"Not all parts consumed: {parts}")

    @pyqtSlot(str)
    def on_selection_changed(self, text):
        """Change the completed part if a new item was selected.

        Called from the views selectionChanged method.

        Args:
            text: Newly selected text.
        """
        if text is None:
            return
        before, center, after = self._partition()
        log.completion.debug("Changing {} to '{}'".format(center, text))
        try:
            maxsplit = objects.commands[before[0]].maxsplit
        except (KeyError, IndexError):
            maxsplit = None
        if maxsplit is None:
            text = self._quote(text)
        model = self._model()
        if model.count() == 1 and config.val.completion.quick:
            # If we only have one item, we want to apply it immediately and go
            # on to the next part, unless we are quick-completing the part
            # after maxsplit, so that we don't keep offering completions
            # (see issue #1519)
            if maxsplit is not None and maxsplit < len(before):
                self._change_completed_part(text, before, after)
            else:
                self._change_completed_part(text, before, after,
                                            immediate=True)
        else:
            self._change_completed_part(text, before, after)

    @pyqtSlot()
    def schedule_completion_update(self):
        """Schedule updating/enabling completion.

        For performance reasons we don't want to block here, instead we do this
        in the background.

        We delay the update only if we've already input some text and ignore
        updates if the text is shorter than completion.min_chars (unless we're
        hitting backspace in which case updates won't be ignored).
        """
        _cmd, _sep, rest = self._cmd.text().partition(' ')
        input_length = len(rest)
        if (0 < input_length < config.val.completion.min_chars and
                self._cmd.cursorPosition() > self._last_cursor_pos):
            log.completion.debug("Ignoring update because the length of "
                                 "the text is less than completion.min_chars.")
        elif (self._cmd.cursorPosition() == self._last_cursor_pos and
              self._cmd.text() == self._last_text):
            log.completion.debug("Ignoring update because there were no "
                                 "changes.")
        else:
            log.completion.debug("Scheduling completion update.")
            start_delay = config.val.completion.delay if self._last_text else 0
            self._timer.start(start_delay)
        self._last_cursor_pos = self._cmd.cursorPosition()
        self._last_text = self._cmd.text()

    @pyqtSlot()
    def _update_completion(self):
        """Check if completions are available and activate them."""
        completion = self._completion()

        if self._cmd.prefix() != ':':
            # This is a search or gibberish, so we don't need to complete
            # anything (yet)
            # FIXME complete searches
            # https://github.com/qutebrowser/qutebrowser/issues/32
            completion.set_model(None)
            self._last_before_cursor = None
            return

        before_cursor, pattern, after_cursor = self._partition()

        log.completion.debug("Updating completion: {} {} {}".format(
            before_cursor, pattern, after_cursor))

        pattern = pattern.strip("'\"")
        func = self._get_new_completion(before_cursor, pattern)

        if func is None:
            log.completion.debug('Clearing completion')
            completion.set_model(None)
            self._last_before_cursor = None
            return

        if before_cursor == self._last_before_cursor:
            # If the part before the cursor didn't change since the last
            # completion, we only need to filter existing matches without
            # having to regenerate completion results.
            completion.set_pattern(pattern)
            return

        self._last_before_cursor = before_cursor

        args = (x for x in before_cursor[1:] if not x.startswith('-'))
        cur_tab = objreg.get('tab', scope='tab', window=self._win_id,
                             tab='current')

        with debug.log_time(log.completion, 'Starting {} completion'
                            .format(func.__name__)):
            info = CompletionInfo(config=config.instance,
                                  keyconf=config.key_instance,
                                  win_id=self._win_id,
                                  cur_tab=cur_tab)
            model = func(*args, info=info)
        with debug.log_time(log.completion, 'Set completion model'):
            completion.set_model(model)

        completion.set_pattern(pattern)

    def _change_completed_part(self, newtext, before, after, immediate=False):
        """Change the part we're currently completing in the commandline.

        Args:
            text: The text to set (string) for the token under the cursor.
            before: Commandline tokens before the token under the cursor.
            after: Commandline tokens after the token under the cursor.
            immediate: True if the text should be completed immediately
                       including a trailing space and we shouldn't continue
                       completing the current item.
        """
        text = self._cmd.prefix() + ' '.join(before + [newtext])
        pos = len(text) + (1 if immediate else 0)
        if after:
            text += ' ' + ' '.join(after)
        elif immediate:
            # pad with a space if quick-completing the last entry
            text += ' '
        log.completion.debug("setting text = '{}', pos = {}".format(text, pos))

        # generally, we don't want to let self._cmd emit cursorPositionChanged,
        # because that'll schedule a completion update. That happens when
        # tabbing through the completions, and we want to change the command
        # text but we also want to keep the original completion list for the
        # command the user manually entered. The exception is when we're
        # immediately completing, in which case we *do* want to update the
        # completion view so that we can start completing the next part
        if not immediate:
            self._cmd.blockSignals(True)

        self._cmd.setText(text)
        self._cmd.setCursorPosition(pos)
        self._cmd.setFocus()

        self._cmd.blockSignals(False)
        self._cmd.show_cmd.emit()