completion_env

Autocomplete helper: determine context of the token to be autocompleted

Adapted from the stata_kernel version (omitting mata-specific stuff).

Note the corner cases discussed here, but the commented out test cases below are not yet handled:


source

CompletionEnv

def CompletionEnv():

ends_in_a_string_literal tests:

ends_in_a_comment tests:


source

Env

def Env(
    *args, **kwds
):

Enum where members are also (and must be) ints


source

CompletionEnv.get_env

def get_env(
    code:str, # Right-truncated to cursor position
    r2chars:str, # The two characters immediately after `code`, used to accurately determine rcomp
    sc_delimiter
)->Tuple[__main__.Env, int, str, str]:

Returns completions environment

Returns

env : Env
pos : int Where the completions start. This is set to the start of the word to be completed. out_chunk : str Word to match. rcomp : str How to finish the completion (defaulting to nothing): locals: ’ globals (if start with ${): } scalars: ) scalars (if start with `): )’

test_eq(
    test_instance.get_env("`", "", None)[0:3],
    (Env.LOCAL, 1, ""))
test_eq(
    test_instance.get_env("*%b", "", None)[0:2],
    (Env.MAGIC, 1))
test_eq(
    test_instance.get_env("gen time_tc = clock(datea", "", None)[0:2],
    (Env.GENERAL, 20))
test_eq(
    test_instance.get_env("list var1-v", "", None)[0:2],
    (Env.GENERAL, 10))