5

The Microsoft link here lists three methods that we can use.

But how do we get the current namespace? I see there is a similar question, but the answer to that is using Macros, which doesnt solve this specific question.

The NameSpace() to do something like this:

<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <CodeSnippet Format="1.0.0">
        <Header>
            .
            .
        </Header>
        <Snippet>
            <Declarations>
                <Literal>
                    <ID>namespace</ID> 
                    <Function>NameSpace()</Function>                     
                </Literal>
            </Declarations>
            <Code Language="csharp">
                <![CDATA[
                    $namespace$
                ]]>
            </Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>
2
  • So are you writing an addin or what? Commented Oct 4, 2012 at 15:07
  • yes, basically adding more snippets in to make coding faster in languages other than C#. Commented Oct 4, 2012 at 15:12

1 Answer 1

6

Found out that it can be done and it brings into picture the classes ExpansionProvider & ExpansionFunction

For the above snippet, I had to do something as such:

internal class NameSpaceExpansionFunction : ExpansionFunction
    {
        public NameSpaceExpansionFunction(ExpansionProvider provider)
            : base(provider)
        {
        }

        public override string GetCurrentValue()
        {
           //get namespace
           return namespace;
        }
    }

And the LanguageService tells the snippet file where to look for definition of the function:

public class MyLanguageService : LanguageService
    {
        public override ExpansionFunction CreateExpansionFunction(ExpansionProvider provider,
                                                                  string functionName)
        {
            ExpansionFunction function = null;
            if (String.Compare(functionName, "NameSpace", true) == 0)
            {
                function = new NameSpaceExpansionFunction(provider);
            }
            return function;
        }
    }

This turned out to be more like a tutorial question hence I have provided the links above. Should be helpful. Worked for me :)

Sign up to request clarification or add additional context in comments.

6 Comments

So where do NamespaceExpansionFunction and MyLanguageService go?
MyLanguageService should go in your VSPackage project. The following link explains more on VSPackage development: msdn.microsoft.com/en-us/library/dn705845.aspx
What is your entire LanguageService Class look like? There are lots of other abstract methods required to be overloaded. Also the GetCurrentValue() method doesn't appear to be complete. What is namespace? Why is there now SemiColon after it?
Hi, You will need to go through VSPackage tutorial to understand how LanguageService class should look like. You would need to override methods as per your requirement. The GetCurrentValue above is an example not implementation. We cannot run it. It just shows that we can use "some logic" to get namespace during snippet insertion. Check MSDN for ExpansionFunction documentation for more info on it.
It is from my understanding, that you create this if you're creating a new language, but you can't say, use it in C#, is this correct?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.