public static void AssertFunctionBoundSnippetElement(SnippetElement snippet, SnippetElement target, string input, string expected)
 {
     Assert.IsType <FunctionBoundElement>(snippet);
     Assert.IsType <SnippetReplaceableTextElement>(target);
     Assert.Equal((snippet as FunctionBoundElement).TargetElement, target);
     Assert.Equal(expected, (snippet as FunctionBoundElement).ConvertText(input));
 }
        private static SnippetElement CreateSnippetElement(SerializationContext context)
        {
            var element = new SnippetElement()
            {
                Code = CreateCodeElement(context)
            };

            if (context.Snippet.Literals.Count > 0)
            {
                element.Declarations = CreateDeclarationsElement(context);
            }

            element.Imports    = CreateImportsElements(context.Snippet.Namespaces);
            element.References = CreateReferenceElements(context.Snippet.AssemblyReferences);

            return(element);
        }
        private static void LoadSnippetElement(SnippetElement element, Snippet snippet)
        {
            if (element.Code != null)
            {
                LoadCodeElement(element.Code, snippet);
            }

            if (element.Declarations != null)
            {
                LoadDeclarationsElement(element.Declarations, snippet);
            }

            if (element.Imports != null)
            {
                LoadImports(element.Imports, snippet);
            }

            if (element.References != null)
            {
                LoadReferences(element.References, snippet);
            }
        }
Exemple #4
0
        static SnippetElement CreateElementForValue(ILanguageService languageService, int caret, int line, int column, Dictionary <string, SnippetReplaceableTextElement> replaceableElements, string val, int offset, string snippetText)
        {
            SnippetReplaceableTextElement srte;
            int equalsSign = val.IndexOf('=');

            if (equalsSign > 0)
            {
                string name = val.Substring(0, equalsSign);
                if (replaceableElements.TryGetValue(name, out srte))
                {
                    if (srte.Text == null)
                    {
                        srte.Text = val.Substring(equalsSign + 1);
                    }
                    return(srte);
                }
            }

            foreach (ISnippetElementProvider provider in SnippetElementProviders)
            {
                SnippetElement element = provider.GetElement(new SnippetInfo(val, snippetText, offset));
                if (element != null)
                {
                    return(element);
                }
            }

            if (replaceableElements.TryGetValue(val, out srte))
            {
                return new SnippetBoundElement {
                           TargetElement = srte
                }
            }
            ;

            Match m = functionPattern.Match(val);

            if (m.Success)
            {
                Func <string, string> f = GetFunction(languageService, m.Groups[1].Value);

                if (f != null)
                {
                    string innerVal = m.Groups[2].Value;

                    if (replaceableElements.TryGetValue(innerVal, out srte))
                    {
                        return new FunctionBoundElement {
                                   TargetElement = srte, Function = f
                        }
                    }
                    ;
                    string result2 = GetValue(languageService, caret, line, column, innerVal);
                    if (result2 != null)
                    {
                        return new SnippetTextElement {
                                   Text = f(result2)
                        }
                    }
                    ;
                    else
                    {
                        return new SnippetTextElement {
                                   Text = f(innerVal)
                        }
                    };
                }
                else if (replaceableElements.TryGetValue("_" + m.Groups[1].Value, out srte))
                {
                    return(new SnippetBoundElement {
                        TargetElement = srte
                    });
                }
                else
                {
                    return(replaceableElements["_" + m.Groups[1].Value] = new SnippetReplaceableTextElement {
                        Text = "_" + m.Groups[1].Value
                    });
                }
            }

            string result = GetValue(languageService, caret, line, column, val);

            if (result != null)
            {
                return new SnippetTextElement {
                           Text = result
                }
            }
            ;
            else if (replaceableElements.TryGetValue(val, out srte))
            {
                return(new SnippetBoundElement {
                    TargetElement = srte
                });
            }
            else
            {
                return(replaceableElements[val] = new SnippetReplaceableTextElement {
                    Text = val
                });                                                                                 // ${unknown} -> replaceable element
            }
        }
 public static void AssertBoundSnippetElement(SnippetElement snippet, SnippetElement target)
 {
     Assert.IsType <SnippetBoundElement>(snippet);
     Assert.IsType <SnippetReplaceableTextElement>(target);
     Assert.Equal((snippet as SnippetBoundElement).TargetElement, target);
 }
 public static void AssertSnippetTextElement <T>(SnippetElement snippet, string text)
 {
     Assert.IsType <T>(snippet);
     Assert.Equal(text, (snippet as SnippetTextElement).Text);
 }
Exemple #7
0
        static SnippetElement CreateElementForValue(ITextEditor context, Dictionary <string, SnippetReplaceableTextElement> replaceableElements, string val, int offset, string snippetText)
        {
            SnippetReplaceableTextElement srte;
            int equalsSign = val.IndexOf('=');

            if (equalsSign > 0)
            {
                string name = val.Substring(0, equalsSign);
                if (replaceableElements.TryGetValue(name, out srte))
                {
                    if (srte.Text == null)
                    {
                        srte.Text = val.Substring(equalsSign + 1);
                    }
                    return(srte);
                }
            }

            foreach (ISnippetElementProvider provider in SnippetManager.Instance.SnippetElementProviders)
            {
                SnippetElement element = provider.GetElement(new SnippetInfo(val, snippetText, offset));
                if (element != null)
                {
                    return(element);
                }
            }

            if (replaceableElements.TryGetValue(val, out srte))
            {
                return new SnippetBoundElement {
                           TargetElement = srte
                }
            }
            ;
            Match m = functionPattern.Match(val);

            if (m.Success)
            {
                Func <string, string> f = GetFunction(context, m.Groups[1].Value);

                if (f != null)
                {
                    string innerVal = m.Groups[2].Value;
                    if (replaceableElements.TryGetValue(innerVal, out srte))
                    {
                        return new FunctionBoundElement {
                                   TargetElement = srte, function = f
                        }
                    }
                    ;
                    string result2 = GetValue(context, innerVal);
                    if (result2 != null)
                    {
                        return new SnippetTextElement {
                                   Text = f(result2)
                        }
                    }
                    ;
                    else
                    {
                        return new SnippetTextElement {
                                   Text = f(innerVal)
                        }
                    };
                }
            }
            string result = GetValue(context, val);

            if (result != null)
            {
                return new SnippetTextElement {
                           Text = result
                }
            }
            ;
            else
            {
                return new SnippetReplaceableTextElement {
                           Text = val
                }
            };                                                                           // ${unknown} -> replaceable element
        }