Argument completions for user-defined functions

There is an undocumented file in the installation directory named specialArgFunctions.tr:

NotebookOpen @ FileNameJoin @
  { $InstallationDirectory, "SystemFiles", "FrontEnd", "SystemResources"
  , "FunctionalFrequency", "specialArgFunctions.tr"
  }

This file describes in detail how to attach completion actions to each parameter of listed functions. For example, it contains the entry:

"Import"->{2, "ImportFormats_Names.trie"},

and explains that 2 specifies absolute pathname completion for the first argument, and that the second argument should be completed from a compiled list found in the file ImportFormats_Names.trie in the same directory.

So, we can achieve the desired goal by adding the following entry for readCSV:

"readCSV"->{2},

filename completion screenshot

The rules use symbol names unqualified by context. Thus, they apply equally well for symbols in any context. In fact, experimentation shows that the parameters of qualified symbols are not completed, even for the shipped rules (e.g. try completing System`Import["c:\\).

As usual, the undocumented nature of this feature means that it could change at any time.


Before you start using this, be aware that this is all undocumented and unsupported functionality. It may change at any time, it may crash Mathematica, and it may bite you when you least expect it. I wouldn't be surprised if it turned out that incorrect use messes up the internal state of the front end. Use at your own risk.

I tried the examples I show here in M10.0.2 and M11.0.1. I assume it also works in inbetween versions.


We can add custom completion to our own functions this way:

addCompletion = FE`Evaluate[FEPrivate`AddSpecialArgCompletion[#]] &

addCompletion["readCSV" -> {2}]

addCompletion takes an argument of the form "functionName" -> {a1, a2, ...}. "functionName" is a string containing the name of the function to use completion for. a1, a2, ... are argument specifiers that control how auto-completion is done for the first, second, etc. arguments.

Argument specifiers can be:

  • A numeric code with the following meanings:

    Normal argument     0
    AbsoluteFilename    2
    RelativeFilename    3
    Color               4
    PackageName         7
    DirectoryName       8
    InterpreterType     9
    
  • A list of strings. Each string will be a possible completion for that argument.

  • A string containing a keyword referring to an internal list:

    "Style"
    "ScreenStyleEnvironment"
    "PrintingStyleEnvironment"
    "MenuListStyles"
    "MenuListScreenStyleEnvironments"
    "MenuListPrintingStyleEnvironments"
    "codingNoteFontCom"
    "ExternalDataCharacterEncoding"
    "MenuListPalettesMenu"
    "MenuListStyleDefinitions"
    "ConvertersPath"
    "MenuListDisplayAsFormatTypes"
    "MenuListConvertFormatTypes"
    "MenuListCellTags"
    "MenuListWindows"
    "MenuListHelpWindows"
    "MenuListPackageWindows"
    "MenuListPaletteWindows"
    "MenuListPlayerWindows"
    "MenuListStylesheetWindows"
    "MenuListTextWindows"
    "MenuListFonts"
    "MenuListGlobalEvaluators"
    "MenuListNotebookEvaluators"
    "MenuListStartEvaluators"
    "MenuListQuitEvaluators"
    "MenuListNotebooksMenu"
    
  • Some other forms for argument specifiers are described in specialArgFunctions.tr, see References below


Example

A custom myImport function could be set up like this:

addCompletion["myImport" -> {2, $ImportFormats}]

enter image description here


References:

  • https://github.com/WolframResearch/GitLink/blob/master/GitLink/Kernel/Completions.wl

  • The contents of FindFile["NDSolve`FEM`"] (I checked it in version 11.0.1).

  • The contents of FileNameJoin[{$InstallationDirectory, "SystemFiles", "Components", "AutoCompletionData", "Main", "specialArgFunctions.tr"}] (I checked it in version 11.0.1)

  • 2015 Wolfram Technology Conference talk by John Fultz


This can be accomplished with the AddCodeCompletion function in the function repository.

Evaluate the following piece of code:

AddCodeCompletion = ResourceFunction["AddCodeCompletion"]
AddCodeCompletion["readCSV"]["AbsoluteFileName"]

readCSV will now auto-complete absolute file paths. AddCodeCompletion can be used with relative filenames as well.

Note: AddCodeCompletion function uses FEPrivate`AddSpecialArgCompletion just like Szabolcs' answer to this question.