Skip to content

Instantly share code, notes, and snippets.

@disnet
Created August 7, 2013 07:06
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save disnet/6171843 to your computer and use it in GitHub Desktop.
Save disnet/6171843 to your computer and use it in GitHub Desktop.
thinking about sweet.js and code completion

Let's say you want to implement TypeScript with macros:

function foo(str: string) {
    return str.toUpperCase()
    //        ^
    // want to provide completion here
}

So we define the function macro:

macro function {
    // for simplicity, only match a single param + type
    case { 
        $name ($param : $type) { $body ... } 
    } => {
        // (1) get the list of possible completions for the type
        completions = getCompletions($type)

        // (2) record in the completion service that 
        //     each identifier $param in $name has 
        //     those completions
        setCompletions($name, $param, completions)

        // expand to the appropriate code
        return #{
            function $name ($param) { $body ... }
        }
    }
}

Exactly how you implement getCompletions and setCompletions depends on how you want to provide the service. The basic idea though is that during expansion you have the information you need to map identifier names in their appropriate scope (the function body) to their completions. You just have to record that somewhere and feed that to the editor.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment