Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

(WIP) Changes to improve data.table completion #20

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
9 changes: 6 additions & 3 deletions pythonx/ncm_r.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,6 @@ def get_matches(self, word, pkg=None, pipe=None, data=None):
:returns: list of ncm matches
"""

self.get_all_obj_matches()
obj_m = self._obj_matches

if pipe or data:
Expand Down Expand Up @@ -281,8 +280,12 @@ def on_complete(self, ctx):
if isinquot and func and not re.search('(library|require|data)', func):
return

self.get_all_obj_matches()
pipe = rlang.get_pipe(cur_buffer, lnum, col)
data = rlang.get_df_inside_brackets(ctx['typed'])
data = rlang.get_df_inside_brackets(ctx['typed'], obj_m=self._obj_matches)

if pipe and data == '.':
data = pipe

self._info('word: "{}", func: "{}", pkg: {}, pipe: {}, data: {}'.format(
word, func, pkg, pipe, data))
Expand All @@ -297,7 +300,7 @@ def on_complete(self, ctx):

matches = self.get_matches(word, pkg=pkg)

self.complete(ctx, ctx['startccol'], matches)
self.complete(ctx, ctx['startccol'], matches, refresh=1)


SOURCE = Source(vim)
Expand Down
25 changes: 22 additions & 3 deletions pythonx/rlang.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,16 +159,35 @@ def get_option(typed=''):
return None


def get_df_inside_brackets(typed=''):
def get_df_inside_brackets(typed='', obj_m=[]):
"""Return df name when cursor is inside brackets"""

if not typed:
return ''

df_brackets = re.compile(r'(\w+)\[[^\[\]]*,[^\[\]]*$')
df_brackets = re.compile(r'([\w.]+)\[[^\[\]]*$')

df_match = df_brackets.search(typed)

if df_match:
return df_match.group(1)
full_match = df_match.group(0)
word = df_match.group(1)

if ',' in full_match:
return word

# Check if the object is a data.table or '.' (indicating it's referring
# to a data.frame or data.table being used in a pipe), if so then
# return it
if word == '.':
return word

try:
match_struct = [m.get('struct', '') for m in obj_m if m.get('word', '') == word][0]
except IndexError:
match_struct = ''

if match_struct == 'data.table':
return word

return ''
17 changes: 17 additions & 0 deletions test/test_ncmr.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,23 @@ def feedkeys(keys):
feedkeys(['$i'])
TEST.ask()

# ==== DATA.TABLE ==== #
send_rcmd("library('data.table'); sleep_dt <- as.data.table(sleep)")
TEST = TestCase('Is ncm-R suggesting median?',
['sleep_dt[, medi'])
NVIM.feedkeys('A')
TEST.ask()

TEST = TestCase('Is ncm-R suggesting the sleep variable "extra"?',
['sleep_dt[ext'])
NVIM.feedkeys('A')
TEST.ask()

TEST = TestCase('Is ncm-R suggesting the sleep variable "extra"?',
['sleep_dt %>%', ' .[ext'])
NVIM.feedkeys(DOWN + 'A')
TEST.ask()

# ==== IT'S OVER ==== #
TEST = TestCase(r'Testing is over \o/')
TEST.ask()