completions

Autocomplete functionality

Adapted from the stata_kernel version.


source

CompletionsManager

 CompletionsManager (stata_session:nbstata.stata_session.StataSession)

source

CompletionsManager.get_globals

 CompletionsManager.get_globals ()

source

CompletionsManager.get_file_paths

 CompletionsManager.get_file_paths (chunk)

Get file paths based on chunk Args: chunk (str): chunk of text after last space. Doesn’t include string punctuation characters Returns: (List[str]): folders and files at that location

from nbstata.config import launch_stata
from nbstata.stata_more import run_sfi
launch_stata(splash=False)
test_stata = StataSession()
test_instance = CompletionsManager(test_stata)
test_instance.get_file_paths("0")
['00_misc_utils.ipynb',
 '01_config.ipynb',
 '02_stata.ipynb',
 '03_stata_more.ipynb',
 '04_code_utils.ipynb',
 '05_noecho.ipynb',
 '06_pandas.ipynb',
 '07_browse.ipynb',
 '08_stata_session.ipynb',
 '09_magics.ipynb']
run_sfi('global in_path "../nbstata"')
test_instance.stata_session.refresh_suggestions()
test_instance.get_file_paths("$in_path/com")
['$in_path/completion_env.py', '$in_path/completions.py']

source

CompletionsManager.get

 CompletionsManager.get (starts, env, rcomp)

Return environment-aware completions list.


source

CompletionsManager.do

 CompletionsManager.do (code, cursor_pos)
from fastcore.test import test_eq
def completions_test_setup(code):
    global test_instance
    run_sfi("clear all")
    run_sfi(code)
    test_instance.stata_session.clear_suggestions()
    

def _complete(code, cursor_pos):
    _, _, matches = test_instance.do(code, cursor_pos)
    return matches
completions_test_setup("gen var1 = 1")
code = "list va"
cursor_pos = 7

test_eq(
    test_instance.env_helper.get_env(
        code[:cursor_pos], code[cursor_pos:(cursor_pos + 2)],
        False),
    (0, 5, 'va', ''),
) 
test_eq(
    _complete(code, cursor_pos),
    ['var1'],
)
completions_test_setup('local test_local "test value"')
test_eq(
    _complete("list `t", 7),
    ["test_local'"],
)
run_sfi('local test_local ""')
completions_test_setup('global test_global "test value"')
test_eq(
    _complete("list ${tes}", 10),
    ['test_global'],
)