locked
Extending JavaScript Intellisense in VS2012 (possibly VS2010?) for promise success-functions RRS feed

  • Question

  • Hi,

    I am trying to extend intellisense by adding a completionItem to a promise success-function variable (which happens to be a known loaded AMD module object - albeit its type is irrelevant to the problem at hand, it could be anything for which intellisense xml comments have been setup).

    At design time, the promise success-functions parameter is naturally unknown to intellisense but at runtime I would know all about the parameter type and contents of course. Therefore I want to add one or more completionItems to it at design time through my own custom intellisense extension.

    Example client-code script:

    /// <reference path="/Scripts/SomeObject.intellisense.js" />

    SomeObject. // Intellisense works here. 

    SomeActionThatReturnsAPromise().then(
            function success(SomeObject) {
                    SomeObject. // Intellisense wanted here too. 
            },
            function failure(errorMessage) {
                    alert(errorMessage);
            }
    );

    Example intellisense file referenced above in client-code script:

    intellisense.logMessage("SomeObject Intellisense has been activated.");

    // SomeObject description enriched with intellisense for Visual Studio.
    var SomeObject = {
        SomeMethod: function (a) {
            /// <summary>SomeObject.SomeMethod function description</summary>
            /// <param  name="a" type="String">A string parameter value</param>
            /// <returns type="String" />
        }
    };

    intellisense.addEventListener('statementcompletion', function (event) {
        intellisense.logMessage("Target object: " + JSON.stringify(event.target));
        intellisense.logMessage("Target name: " + event.targetName);
        intellisense.logMessage("Num completion items: " + event.items.length);
        // Regardless of what intellisense picks up, try to add the function to the completionItems array. This works like a charm!
        event.items.push(
            {
                name: 'SomeMethod',
                kind: 'method',
                parentObject: SomeObject,
                value: SomeObject.SomeMethod,
                comments: intellisense.getFunctionComments(SomeObject.SomeMethod),
                glyph: 'vs:GlyphGroupInterface'  // Just to see if I can change the glyph.
            }
        );

        if (event.targetName === "SomeObject" && event.target === undefined) {
            // This is where I would like to add the completionItem
            // but intellisense does nothing when the items.push code
            // is placed here. I speculate that the event.target === undefined
            // is the reason for the failure to add completionItems / 
            // affect the items array.
            //
            // Intellisense reports the following for the "SomeObject." inside the success-function:
            // "intellisense was unable to determine an accurate completion list".
        }
    });

    I have tried to add a completionItem to the SomeObject function parameter by calling:

    intellisense.addEventListener('statementcompletion', function (event) {
        if (event.targetName === "SomeObject" && event.target === undefined) {
            event.items.push(
                {
                    name: 'SomeMethod',
                    kind: 'method',
                    parentObject: SomeObject,
                    value: SomeObject.SomeMethod,
                    comments: intellisense.getFunctionComments(SomeObject.SomeMethod),
                    glyph: 'vs:GlyphGroupInterface'  // Just to see if I can change the glyph.
                }
            );
            // Nothins happens?
        }
    });

    As can be reproduced by the above scripts in VS2012, the target comes out as undefined. I presume this is the reason for failing to add the completionItem to events.items? 

    Please advice on how to achieve this - thanks!

    Best regards,
    Michael S. Fosgerau

    Denmark


    • Edited by Michael S. Fosgerau Wednesday, May 29, 2013 8:57 PM minor example code layout / formatting change
    Wednesday, May 29, 2013 8:55 PM

All replies

  • Hi,

    Have you seen the article below:

    http://msdn.microsoft.com/en-US/library/vstudio/hh874692.aspx

    I think it is helpful to you.

    Regards,

    Disley


    崖山之後無中國,明亡之後無華夏

    Friday, May 31, 2013 8:19 AM
  • I have.

    And it was not. ;-)

    There is as far as I can tell no way of augmenting Intellisense with completionItems for unknown design time types - the problem being that VS does not see updates to event.items when event.target is undefined. That's my theory at least.

    I think it's either a bug or (worse) intended.

    Thursday, June 20, 2013 11:32 AM