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
|
from qtpy import QtCore
from qtpy import QtWidgets
from qtpy.QtCore import Qt
from qtpy.QtCore import Signal
from ..i18n import N_
from ..utils import Group
from .. import cmds
from .. import core
from .. import hotkeys
from .. import utils
from .. import qtutils
from ..qtutils import get
from .standard import Dialog
from .text import HintedLineEdit
from .text import VimHintedPlainTextEdit
from .text import VimTextBrowser
from . import defs
def grep(context):
"""Prompt and use 'git grep' to find the content."""
widget = new_grep(context, parent=qtutils.active_window())
widget.show()
return widget
def new_grep(context, text=None, parent=None):
"""Construct a new Grep dialog"""
widget = Grep(context, parent=parent)
if text:
widget.search_for(text)
return widget
def parse_grep_line(line):
"""Parse a grep result line into (filename, line_number, content)"""
try:
filename, line_number, contents = line.split(':', 2)
result = (filename, line_number, contents)
except ValueError:
result = None
return result
def goto_grep(context, line):
"""Called when Search -> Grep's right-click 'goto' action."""
parsed_line = parse_grep_line(line)
if parsed_line:
filename, line_number, _ = parsed_line
cmds.do(
cmds.Edit,
context,
[filename],
line_number=line_number,
background_editor=True,
)
class GrepThread(QtCore.QThread):
"""Gather `git grep` results in a background thread"""
result = Signal(object, object, object)
def __init__(self, context, parent):
QtCore.QThread.__init__(self, parent)
self.context = context
self.query = None
self.shell = False
self.regexp_mode = '--basic-regexp'
def run(self):
if self.query is None:
return
git = self.context.git
query = self.query
if self.shell:
args = utils.shell_split(query)
else:
args = [query]
status, out, err = git.grep(self.regexp_mode, n=True, _readonly=True, *args)
if query == self.query:
self.result.emit(status, out, err)
else:
self.run()
class Grep(Dialog):
"""A dialog for searching content using `git grep`"""
def __init__(self, context, parent=None):
Dialog.__init__(self, parent)
self.context = context
self.grep_result = ''
self.setWindowTitle(N_('Search'))
if parent is not None:
self.setWindowModality(Qt.WindowModal)
self.edit_action = qtutils.add_action(self, N_('Edit'), self.edit, hotkeys.EDIT)
self.refresh_action = qtutils.add_action(
self, N_('Refresh'), self.search, *hotkeys.REFRESH_HOTKEYS
)
self.input_label = QtWidgets.QLabel('git grep')
self.input_label.setFont(qtutils.diff_font(context))
self.input_txt = HintedLineEdit(
context, N_('command-line arguments'), parent=self
)
self.regexp_combo = combo = QtWidgets.QComboBox()
combo.setToolTip(N_('Choose the "git grep" regular expression mode'))
items = [N_('Basic Regexp'), N_('Extended Regexp'), N_('Fixed String')]
combo.addItems(items)
combo.setCurrentIndex(0)
combo.setEditable(False)
tooltip0 = N_('Search using a POSIX basic regular expression')
tooltip1 = N_('Search using a POSIX extended regular expression')
tooltip2 = N_('Search for a fixed string')
combo.setItemData(0, tooltip0, Qt.ToolTipRole)
combo.setItemData(1, tooltip1, Qt.ToolTipRole)
combo.setItemData(2, tooltip2, Qt.ToolTipRole)
combo.setItemData(0, '--basic-regexp', Qt.UserRole)
combo.setItemData(1, '--extended-regexp', Qt.UserRole)
combo.setItemData(2, '--fixed-strings', Qt.UserRole)
self.result_txt = GrepTextView(context, N_('grep result...'), self)
self.preview_txt = PreviewTextView(context, self)
self.preview_txt.setFocusProxy(self.result_txt)
self.edit_button = qtutils.edit_button(default=True)
qtutils.button_action(self.edit_button, self.edit_action)
self.refresh_button = qtutils.refresh_button()
qtutils.button_action(self.refresh_button, self.refresh_action)
text = N_('Shell arguments')
tooltip = N_(
'Parse arguments using a shell.\n'
'Queries with spaces will require "double quotes".'
)
self.shell_checkbox = qtutils.checkbox(
text=text, tooltip=tooltip, checked=False
)
self.close_button = qtutils.close_button()
self.refresh_group = Group(self.refresh_action, self.refresh_button)
self.refresh_group.setEnabled(False)
self.edit_group = Group(self.edit_action, self.edit_button)
self.edit_group.setEnabled(False)
self.input_layout = qtutils.hbox(
defs.no_margin,
defs.button_spacing,
self.input_label,
self.input_txt,
self.regexp_combo,
)
self.bottom_layout = qtutils.hbox(
defs.no_margin,
defs.button_spacing,
self.refresh_button,
self.shell_checkbox,
qtutils.STRETCH,
self.close_button,
self.edit_button,
)
self.splitter = qtutils.splitter(Qt.Vertical, self.result_txt, self.preview_txt)
self.mainlayout = qtutils.vbox(
defs.margin,
defs.no_spacing,
self.input_layout,
self.splitter,
self.bottom_layout,
)
self.setLayout(self.mainlayout)
thread = self.worker_thread = GrepThread(context, self)
thread.result.connect(self.process_result, type=Qt.QueuedConnection)
self.input_txt.textChanged.connect(lambda s: self.search())
self.regexp_combo.currentIndexChanged.connect(lambda x: self.search())
self.result_txt.leave.connect(self.input_txt.setFocus)
self.result_txt.cursorPositionChanged.connect(self.update_preview)
qtutils.add_action(
self.input_txt,
'Focus Results',
self.focus_results,
hotkeys.DOWN,
*hotkeys.ACCEPT
)
qtutils.add_action(self, 'Focus Input', self.focus_input, hotkeys.FOCUS)
qtutils.connect_toggle(self.shell_checkbox, lambda x: self.search())
qtutils.connect_button(self.close_button, self.close)
qtutils.add_close_action(self)
self.init_size(parent=parent)
def focus_input(self):
"""Focus the grep input field and select the text"""
self.input_txt.setFocus()
self.input_txt.selectAll()
def focus_results(self):
"""Give focus to the results window"""
self.result_txt.setFocus()
def regexp_mode(self):
"""Return the selected grep regex mode"""
idx = self.regexp_combo.currentIndex()
return self.regexp_combo.itemData(idx, Qt.UserRole)
def search(self):
"""Initiate a search by starting the GrepThread"""
self.edit_group.setEnabled(False)
self.refresh_group.setEnabled(False)
query = get(self.input_txt)
if len(query) < 2:
self.result_txt.clear()
self.preview_txt.clear()
return
self.worker_thread.query = query
self.worker_thread.shell = get(self.shell_checkbox)
self.worker_thread.regexp_mode = self.regexp_mode()
self.worker_thread.start()
def search_for(self, txt):
"""Set the initial value of the input text"""
self.input_txt.set_value(txt)
def text_scroll(self):
"""Return the scrollbar value for the results window"""
scrollbar = self.result_txt.verticalScrollBar()
if scrollbar:
return get(scrollbar)
return None
def set_text_scroll(self, scroll):
"""Set the scrollbar value for the results window"""
scrollbar = self.result_txt.verticalScrollBar()
if scrollbar and scroll is not None:
scrollbar.setValue(scroll)
def text_offset(self):
"""Return the cursor's offset within the result text"""
return self.result_txt.textCursor().position()
def set_text_offset(self, offset):
"""Set the text cursor from an offset"""
cursor = self.result_txt.textCursor()
cursor.setPosition(offset)
self.result_txt.setTextCursor(cursor)
def process_result(self, status, out, err):
"""Apply the results from grep to the widgets"""
if status == 0:
value = out + err
elif out + err:
value = 'git grep: ' + out + err
else:
value = ''
# save scrollbar and text cursor
scroll = self.text_scroll()
offset = min(len(value), self.text_offset())
self.grep_result = value
self.result_txt.set_value(value)
# restore
self.set_text_scroll(scroll)
self.set_text_offset(offset)
enabled = status == 0
self.edit_group.setEnabled(enabled)
self.refresh_group.setEnabled(True)
if not value:
self.preview_txt.clear()
def update_preview(self):
"""Update the file preview window"""
parsed_line = parse_grep_line(self.result_txt.selected_line())
if parsed_line:
filename, line_number, _ = parsed_line
self.preview_txt.preview(filename, line_number)
def edit(self):
"""Launch an editor on the currently selected line"""
goto_grep(self.context, self.result_txt.selected_line())
def export_state(self):
"""Export persistent settings"""
state = super().export_state()
state['sizes'] = get(self.splitter)
return state
def apply_state(self, state):
"""Apply persistent settings"""
result = super().apply_state(state)
try:
self.splitter.setSizes(state['sizes'])
except (AttributeError, KeyError, ValueError, TypeError):
result = False
return result
class GrepTextView(VimHintedPlainTextEdit):
"""A text view with hotkeys for launching editors"""
def __init__(self, context, hint, parent):
VimHintedPlainTextEdit.__init__(self, context, hint, parent=parent)
self.context = context
self.goto_action = qtutils.add_action(self, 'Launch Editor', self.edit)
self.goto_action.setShortcut(hotkeys.EDIT)
self.menu_actions.append(self.goto_action)
def edit(self):
goto_grep(self.context, self.selected_line())
class PreviewTask(qtutils.Task):
"""Asynchronous task for loading file content"""
def __init__(self, filename, line_number):
qtutils.Task.__init__(self)
self.content = ''
self.filename = filename
self.line_number = line_number
def task(self):
try:
self.content = core.read(self.filename, errors='ignore')
except OSError:
pass
return (self.filename, self.content, self.line_number)
class PreviewTextView(VimTextBrowser):
"""Preview window for file contents"""
def __init__(self, context, parent):
VimTextBrowser.__init__(self, context, parent)
self.filename = None
self.content = None
self.runtask = qtutils.RunTask(parent=self)
def preview(self, filename, line_number):
"""Preview a file at the specified line number"""
if filename != self.filename:
request = PreviewTask(filename, line_number)
self.runtask.start(request, finish=self.show_preview)
else:
self.scroll_to_line(line_number)
def clear(self):
self.filename = ''
self.content = ''
super().clear()
def show_preview(self, task):
"""Show the results of the asynchronous file read"""
filename = task.filename
content = task.content
line_number = task.line_number
if filename != self.filename:
self.filename = filename
self.content = content
self.set_value(content)
self.scroll_to_line(line_number)
def scroll_to_line(self, line_number):
"""Scroll to the specified line number"""
try:
line_num = int(line_number) - 1
except ValueError:
return
self.numbers.set_highlighted(line_num)
cursor = self.textCursor()
cursor.setPosition(0)
self.setTextCursor(cursor)
self.ensureCursorVisible()
cursor.movePosition(cursor.Down, cursor.MoveAnchor, line_num)
cursor.movePosition(cursor.EndOfLine, cursor.KeepAnchor)
self.setTextCursor(cursor)
self.ensureCursorVisible()
scrollbar = self.verticalScrollBar()
step = scrollbar.pageStep() // 2
position = scrollbar.sliderPosition() + step
scrollbar.setSliderPosition(position)
self.ensureCursorVisible()
|