Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Make minor effeciency changes to regex usages #1133

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions pythonx/jedi_vim.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import os
import sys
from shlex import split as shsplit
import string
from contextlib import contextmanager
from pathlib import Path
try:
Expand All @@ -26,6 +27,10 @@
else:
ELLIPSIS = u"…"

IDENTIFIER_CHARS = set(string.letters + string.digits + "_")
RIGHTMOST_WORD_PATTERN = re.compile(r"\w+$")
SIGNATURE_QUOTES_PATTERN = re.compile(r'''\\*["']+''')
RIGHTMOST_SIGNATURE_QUOTES_PATTERN = re.compile(r'''\\*["']+$''')

try:
# Somehow sys.prefix is set in combination with VIM and virtualenvs.
Expand Down Expand Up @@ -339,7 +344,7 @@ def completions():
if vim.eval('a:findstart') == '1':
count = 0
for char in reversed(vim.current.line[:column]):
if not re.match(r'[\w\d]', char):
if char not in IDENTIFIER_CHARS:
break
count += 1
vim.command('return %i' % (column - count))
Expand Down Expand Up @@ -898,13 +903,12 @@ def show_call_signatures(signatures=()):

# Check the replace stuff for strings, to append them
# (don't want to break the syntax)
regex_quotes = r'''\\*["']+'''
# `add` are all the quotation marks.
# join them with a space to avoid producing '''
add = ' '.join(re.findall(regex_quotes, replace))
add = ' '.join(SIGNATURE_QUOTES_PATTERN.findall(replace))
# search backwards
if add and replace[0] in ['"', "'"]:
a = re.search(regex_quotes + '$', prefix)
a = RIGHTMOST_SIGNATURE_QUOTES_PATTERN.search(prefix)
add = ('' if a is None else a.group(0)) + add

tup = '%s, %s' % (len(add), replace)
Expand Down Expand Up @@ -1016,7 +1020,7 @@ def rename(delete_word=True):
else:
vim_command('normal! yiwel')

if re.match(r'\w+$', line[cursor[1]:]):
if RIGHTMOST_WORD_PATTERN.match(line[cursor[1]:]):
# In case the deleted word is at the end of the line we need to
# move the cursor to the end.
vim_command('startinsert!')
Expand Down
Loading