nsshell::CurrentThreadHandler method autocomplete (public)

 <instance of nsshell::CurrentThreadHandler[i]> autocomplete arg \
    kernel channel

Defined in /usr/local/ns/tcl/nsshell/shell.tcl

Try to autocomplete the value provided in "$arg". This method returns a dict with the "status" and a "result" consisting of potential completions.

Parameters:
arg
kernel
channel

Partial Call Graph (max 5 caller/called nodes):
%3

Testcases:
No testcase defined.
Source code:
ns_log notice "===== Autocomplete ===== arg <$arg>"
set result {}
set type ""
set words [split $arg " "]
set lastWord [lindex $words end]
# Santiphap: Variable autocomplete if last word start with $
if { [string match {$*} $lastWord] } {
    ns_log notice "Autocomplete variable: $arg"
    set type variables
    # Santiphap: Get var prefix without $
    set var_prefix [string range $lastWord 1 end]
    # Santiphap: Get matched variable
    set var_result [:completion_elements "info vars $var_prefix*" $kernel $channel]
    # Santiphap: Get matched namespace
    set namespace_parent [join [lreplace [split $var_prefix :] end-1 end] ::]
    set namespace_pattern [lindex [split $var_prefix :] end]
    set namespace_result [:completion_elements  "namespace children ${namespace_parent}:: $namespace_pattern*"  $kernel $channel]
    #
    # Prepend dollar "$" to variable names for the result
    #
    set var_result       [lmap v $var_result {set _ "\$$v"}]
    set namespace_result [lmap v $namespace_result {set _ "\$${v}::"}]
    set result [join [lsort -unique [concat $var_result $namespace_result]]]
} else {
    # Santiphap: Get last word of the last command
    set sub_arg [string trimleft [lindex [split $arg "\["] end]]
    #ns_log notice "===== arg <$arg> sub_arg <$sub_arg>"
    # Santiphap: Command autocomplete
    set words [split $sub_arg " "]
    if { [llength $words] == 1} {
        #
        # A single word was provided, try to autocomplete
        # it as a command.
        #
        set type commands
        ns_log notice "===== Autocomplete single word command: <$sub_arg>"

        set providedNs [namespace qualifiers $sub_arg]
        set checkNs [expr {$providedNs eq "" ? "::" : $providedNs}]

        lappend cmds  {*}[:completion_elements [list info commands $sub_arg*] $kernel $channel]  {*}[:completion_elements [list namespace children $checkNs $sub_arg*] $kernel $channel]

        set result [lsort -unique $cmds]

    } elseif { [llength $words] eq 2} {

        #
        # Two words
        #
        lassign $words main sub

        if {$main in {set unset}} {
            set result [:completion_elements "info vars $sub*" $kernel $channel]
            set type variables
            #
            # We could add here as well namespace
            # completion for namespaced vars.
            #

        } else {
            set type subcommands
            ns_log notice "Autocomplete subcommand: <$sub_arg>"

            try {
                #
                # Get matched class/object methods. We use
                # here the nsf primitive "lookupmethods",
                # since this works for NX and XOTcl.
                #
                :completion_elements  [list $main ::nsf::methods::object::info::lookupmethods $sub*]  $kernel $channel
            } on error {errorMsg} {
                ns_log notice "lookupmethods failed: $errorMsg"
                set methods {}
            } on ok {result} {
                set methods $result
            }
            #
            # In case, we found no XOTcl/NX methods,
            # check, if there are Tcl/NaviServer commands
            # with subcommands. In order to avoid
            # collateral damage, we do this just for
            # asserted commands.
            #
            if {[llength $methods] == 0} {
                ns_log notice "NO methods for <$sub*>"
                if { $main in {

                    array binary chan clock encoding file info
                    namespace package string trace

                    ns_asynclogfile ns_chan ns_conn
                    ns_connchan ns_crypto::eckey ns_driver
                    ns_hmac ns_http ns_ictl ns_info ns_job
                    ns_logctl ns_md ns_server ns_set ns_thread
                    ns_urlspace ns_writer nsv_array

                }} {
                    try {
                        $main ""
                    } on error {errMsg} {
                        if {[regexp {: must be (.*)$} $errMsg . subcmds]} {
                            regsub -all ", or " $subcmds "" subcmds
                            set methods [lmap c [split $subcmds ,] {
                                set m [string trim $c]
                                if {![string match $sub$m]} continue
                                set m
                            }]
                        }
                    }
                }
            }
            # Sort & unique
            #ns_log notice "RESULT for <$sub*> -> [concat $methods]"
            set result [lsort -unique [concat $methods]]
        }
    }
}
if {[namespace exists $kernel]} {
    #
    # Clean up after the _eval commands
    #
    namespace delete $kernel
}

# Santiphap: Return autocomplete option type and list
return [list status autocomplete result [concat $type $result]]
XQL Not present:
Generic, PostgreSQL, Oracle
[ hide source ] | [ make this the default ]
Show another procedure: