Exemple #1
0
            private (SnippetReplaceableTextElement Header, Snippet Innner) GetInnnerSnipetts(IEnumerable <string> strs)
            {
                var conv = string.Join("\n", strs);

                var pos = new SnippetReplaceableTextElement {
                    Text = "0"
                };
                var map = new Dictionary <string, Func <SnippetBoundElement> >
                {
                    ["p"] = () => new PrevMoveStrGeneretor()
                    {
                        TargetElement = pos
                    },
                    ["n"] = () => new NextMoveStrGeneretor()
                    {
                        TargetElement = pos
                    },
                };
                var ret = new Snippet();

                foreach (var item in ConvertToElement(conv, map))
                {
                    ret.Elements.Add(item);
                }
                return(Header : pos, Innner : ret);
            }
Exemple #2
0
        private MoveSnippet()
        {
            var pos = new SnippetReplaceableTextElement {
                Text = ">"
            };

            _snippet = new Snippet
            {
                Elements =
                {
                    new SnippetTextElement()
                    {
                        Text = "[-"
                    },
                    pos,
                    new SnippetTextElement()
                    {
                        Text = "+"
                    },
                    new PointerMoveSnippet.PointerMoveSnipetGenerator()
                    {
                        TargetElement = pos
                    },
                    new SnippetTextElement()
                    {
                        Text = "]"
                    },
                }
            };
        }
Exemple #3
0
        public static Snippet CreateAvalonEditSnippet(RunbookModelProxy runbook, string snippetText)
        {
            if (snippetText == null)
            {
                throw new ArgumentNullException("text");
            }

            var replaceableElements = new Dictionary <string, SnippetReplaceableTextElement>(StringComparer.OrdinalIgnoreCase);

            foreach (Match m in pattern.Matches(snippetText))
            {
                string val        = m.Groups[1].Value;
                int    equalsSign = val.IndexOf('=');
                if (equalsSign > 0)
                {
                    string name = val.Substring(0, equalsSign);
                    replaceableElements[name] = new SnippetReplaceableTextElement();
                }
            }

            Snippet snippet = new Snippet();
            int     pos     = 0;

            foreach (Match m in pattern.Matches(snippetText))
            {
                if (pos < m.Index)
                {
                    snippet.Elements.Add(new SnippetTextElement {
                        Text = snippetText.Substring(pos, m.Index - pos)
                    });
                    pos = m.Index;
                }

                snippet.Elements.Add(CreateElementForValue(runbook, replaceableElements, m.Groups[1].Value, m.Index, snippetText));
                pos = m.Index + m.Length;
            }

            if (pos < snippetText.Length)
            {
                snippet.Elements.Add(new SnippetTextElement {
                    Text = snippetText.Substring(pos)
                });
            }

            if (!snippet.Elements.Any(e => e is SnippetCaretElement))
            {
                var obj   = snippet.Elements.FirstOrDefault(e2 => e2 is SnippetSelectionElement);
                int index = snippet.Elements.IndexOf(obj);

                if (index > -1)
                {
                    snippet.Elements.Insert(index + 1, new SnippetCaretElement());
                }
            }

            return(snippet);
        }
Exemple #4
0
        public static Snippet Parse(ILanguageService languageService, int caret, int line, int column, string snippetText)
        {
            if (snippetText == null)
            {
                throw new ArgumentNullException("text");
            }
            var replaceableElements = new Dictionary <string, SnippetReplaceableTextElement>(StringComparer.OrdinalIgnoreCase);

            foreach (Match m in pattern.Matches(snippetText))
            {
                string val        = m.Groups[1].Value;
                int    equalsSign = val.IndexOf('=');
                if (equalsSign > 0)
                {
                    string name = val.Substring(0, equalsSign);
                    replaceableElements[name] = new SnippetReplaceableTextElement();
                }
            }

            var snippet = new Snippet();
            int pos     = 0;

            foreach (Match m in pattern.Matches(snippetText))
            {
                if (pos < m.Index)
                {
                    snippet.Elements.Add(new SnippetTextElement {
                        Text = snippetText.Substring(pos, m.Index - pos)
                    });
                    pos = m.Index;
                }

                snippet.Elements.Add(CreateElementForValue(languageService, caret, line, column, replaceableElements, m.Groups[1].Value, m.Index, snippetText));

                pos = m.Index + m.Length;
            }
            if (pos < snippetText.Length)
            {
                snippet.Elements.Add(new SnippetTextElement {
                    Text = snippetText.Substring(pos)
                });
            }
            if (!snippet.Elements.Any(e => e is SnippetCaretElement))
            {
                int index = snippet.Elements.FindIndex(e2 => e2 is SnippetSelectionElement);
                if (index > -1)
                {
                    snippet.Elements.Insert(index + 1, new SnippetCaretElement());
                }
            }
            return(snippet);
        }
Exemple #5
0
        public static Snippet CreateAvalonEditSnippet(string snippetText)
        {
            if (snippetText == null)
            {
                throw new ArgumentNullException(nameof(snippetText));
            }
            var replaceableElements = new Dictionary <string, SnippetReplaceableTextElement>(StringComparer.OrdinalIgnoreCase);

            foreach (var match in _pattern.Matches(snippetText).OfType <Match>())
            {
                var val        = match.Groups[1].Value;
                var equalsSign = val.IndexOf('=');
                if (equalsSign > 0)
                {
                    var name = val.Substring(0, equalsSign);
                    replaceableElements[name] = new SnippetReplaceableTextElement();
                }
            }
            var snippet = new Snippet();
            var pos     = 0;

            foreach (var match in _pattern.Matches(snippetText).OfType <Match>())
            {
                if (pos < match.Index)
                {
                    snippet.Elements.Add(new SnippetTextElement {
                        Text = snippetText.Substring(pos, match.Index - pos)
                    });
                }
                snippet.Elements.Add(CreateElementForValue(replaceableElements, match.Groups[1].Value, match.Index, snippetText));
                pos = match.Index + match.Length;
            }
            if (pos < snippetText.Length)
            {
                snippet.Elements.Add(new SnippetTextElement {
                    Text = snippetText.Substring(pos)
                });
            }
            if (!snippet.Elements.Any(e => e is SnippetCaretElement))
            {
                var element = snippet.Elements.Select((s, i) => new { s, i }).FirstOrDefault(s => s.s is SnippetSelectionElement);
                var index   = element?.i ?? -1;
                if (index > -1)
                {
                    snippet.Elements.Insert(index + 1, new SnippetCaretElement());
                }
            }
            return(snippet);
        }
        private void  InsertSnippet()
        {
            var caption = new SnippetReplaceableTextElement {
                Text = "caption"
            };
            var label = new SnippetReplaceableTextElement {
                Text = "label"
            };
            Snippet ss      = new Snippet();
            Snippet snippet = new Snippet
            {
                //\begin{figure}[htbp]\centering $0\caption{${1:caption}}\label{${2:$(unless yas/modified-p (reftex-label nil 'dont-insert))}}\end{figure}
                Elements =
                {
                    new SnippetTextElement            {
                        Text = @"\begin{figure}[htbp]" + Environment.NewLine + @"\centering" + Environment.NewLine + @"\caption{"
                    },
                    new SnippetReplaceableTextElement {
                        Text = "caption"
                    },
                    //caption,
                    //new SnippetReplaceableTextElement { Text = "caption" },
                    //caption,
                    new SnippetTextElement            {
                        Text = "}" + Environment.NewLine + @"\label{"
                    },
                    //new SnippetBoundElement { TargetElement = label },
                    //label,
                    new SnippetReplaceableTextElement {
                        Text = "label"
                    },
                    new SnippetTextElement            {
                        Text = @"}" + Environment.NewLine + @"\end{figure}"
                    }
                }
            };

            snippet.Insert(textEditor.TextArea);
        }
        public PointerMoveSnippet()
        {
            var pos = new SnippetReplaceableTextElement {
                Text = ">"
            };

            _snippet = new Snippet
            {
                Elements =
                {
                    pos,
                    new SnippetTextElement()
                    {
                        Text = " "
                    },
                    new PointerMoveSnipetGenerator()
                    {
                        TargetElement = pos
                    },
                }
            };
        }
        public RegionSnippet()
        {
            var text = new SnippetReplaceableTextElement {
                Text = "MyRegion"
            };

            _snippet = new Snippet
            {
                Elements =
                {
                    new SnippetTextElement()
                    {
                        Text = "#region "
                    },
                    text,
                    new SnippetTextElement()
                    {
                        Text = "\n#endregion"
                    },
                }
            };
        }
Exemple #9
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
            }
        }
Exemple #10
0
        private static void ParseCodeLine(string line, SnippetContainerElement root, IDictionary <string, SnippetReplaceableTextElement> symbols)
        {
            var matchResult = ReplaceableTokenPattern.Matches(line);
            var matches     = matchResult.Cast <Match>().Where(m => m.Success).OrderBy(m => m.Index).ToArray();

            if (matchResult.Count > 0)
            {
                int lastIndex = 0;
                foreach (var match in matches)
                {
                    var    group = match.Groups["token"];
                    string token = group.Value;

                    if (lastIndex < group.Index)
                    {
                        root.Elements.Add(new SnippetTextElement {
                            Text = line.Substring(lastIndex, group.Index - lastIndex)
                        });
                    }

                    switch (token)
                    {
                    case "%END%":
                    {
                        // %END% is a special case token that describes where to place the
                        // cursor after completion of snippet insertion.
                        root.Elements.Add(new SnippetCaretElement());
                        break;
                    }

                    case "%SELECTION%":
                    {
                        // %SELECTION% is a special case token that describes where to place the
                        // text that was selected before snippet insertion.  This is useful for
                        // snippets that "surround" other text.
                        int tabCount = line.Substring(0, group.Index).Count(c => c == '\t');
                        root.Elements.Add(new SnippetSelectionElement {
                                Indentation = tabCount
                            });
                        break;
                    }

                    default:
                    {
                        SnippetReplaceableTextElement symbol;
                        if (symbols.TryGetValue(token, out symbol))
                        {
                            root.Elements.Add(new SnippetBoundElement {
                                    TargetElement = symbol
                                });
                        }
                        else
                        {
                            symbol = new SnippetReplaceableTextElement {
                                Text = token.Trim('%')
                            };
                            symbols[token] = symbol;

                            root.Elements.Add(symbol);
                        }
                        break;
                    }
                    }

                    lastIndex = group.Index + group.Length;
                }

                if (lastIndex != line.Length)
                {
                    root.Elements.Add(new SnippetTextElement {
                        Text = line.Substring(lastIndex, line.Length - lastIndex) + Environment.NewLine
                    });
                }
                else
                {
                    root.Elements.Add(new SnippetTextElement {
                        Text = Environment.NewLine
                    });
                }
            }
            else
            {
                root.Elements.Add(new SnippetTextElement {
                    Text = line + Environment.NewLine
                });
            }
        }
        public void Complete(TextArea textArea, ISegment completionSegment, EventArgs insertionRequestEventArgs)
        {
            // Clear down the first lot.
            textArea.Document.Replace(completionSegment.Offset - 1, completionSegment.Length + 1, "");

            var loopCounter = new SnippetReplaceableTextElement();

            Snippet snippet = new Snippet
            {
                Elements =
                {
                    new SnippetTextElement {
                        Text = $"{this.Text}("
                    },
                }
            };

            if (_parameterTypes != null)
            {
                bool addComma = false;

                foreach (var type in _parameterTypes)
                {
                    if (addComma)
                    {
                        snippet.Elements.Add(new SnippetTextElement {
                            Text = ", "
                        });
                    }

                    string prefix = "";
                    string text   = "a";
                    string suffix = "";

                    switch (type)
                    {
                    case VariableType.Number:
                        text = "0";
                        break;

                    case VariableType.String:
                        prefix = suffix = "'";
                        break;

                    case VariableType.Date:
                        prefix = suffix = "#";
                        text   = $"{DateTime.Now}";
                        break;

                    case VariableType.Boolean:
                        text = "true";
                        break;

                    case VariableType.Null:
                        text = "null";
                        break;
                    }

                    if (!string.IsNullOrWhiteSpace(prefix))
                    {
                        snippet.Elements.Add(new SnippetTextElement {
                            Text = prefix
                        });
                    }

                    snippet.Elements.Add(new SnippetReplaceableTextElement {
                        Text = text
                    });

                    if (!string.IsNullOrWhiteSpace(suffix))
                    {
                        snippet.Elements.Add(new SnippetTextElement {
                            Text = suffix
                        });
                    }

                    addComma = true;
                }
            }

            snippet.Elements.Add(new SnippetTextElement {
                Text = ")"
            });

            snippet.Insert(textArea);
        }
        public Snippets()
        {
            this.snippets = new Dictionary <string, Snippet>(StringComparer.OrdinalIgnoreCase);

            var artifact = new SnippetReplaceableTextElement {
                Text = "$a"
            };
            var method = new SnippetReplaceableTextElement {
                Text = "$method"
            };
            var topLevelNodeName = new SnippetReplaceableTextElement {
                Text = "Results"
            };
            var resultNodeName = new SnippetReplaceableTextElement {
                Text = "Result"
            };

            Snippet basicSnippet = new Snippet
            {
                Elements =
                {
                    new SnippetTextElement            {
                        Text = "(: Put description here. It will appear in the tooltip :)\n"
                    },
                    new SnippetTextElement            {
                        Text = "<"
                    }, new SnippetBoundElement        {
                        TargetElement = topLevelNodeName
                    }, new SnippetTextElement         {
                        Text = ">\n"
                    },
                    new SnippetTextElement            {
                        Text = "{\n"
                    },
                    new SnippetTextElement            {
                        Text = "  for "
                    }, new SnippetBoundElement        {
                        TargetElement = artifact
                    }, new SnippetTextElement         {
                        Text = " in /(Class | Table | Form | Query)\n"
                    },
                    new SnippetReplaceableTextElement {
                        Text = "  where true()\n"
                    },
                    new SnippetTextElement            {
                        Text = "  order by "
                    }, new SnippetBoundElement        {
                        TargetElement = artifact
                    }, new SnippetReplaceableTextElement{
                        Text = "/@Name\n"
                    },
                    new SnippetCaretElement           {
                    },
                    new SnippetTextElement            {
                        Text = "  return "
                    }, new SnippetTextElement         {
                        Text = "<"
                    }, new SnippetBoundElement        {
                        TargetElement = resultNodeName
                    },
                    new SnippetTextElement            {
                        Text = " Artifact='{"
                    }, new SnippetBoundElement        {
                        TargetElement = artifact
                    }, new SnippetTextElement         {
                        Text = "/@Artifact}'\n"
                    },
                    new SnippetTextElement            {
                        Text = "    StartLine='{"
                    }, new SnippetBoundElement        {
                        TargetElement = artifact
                    }, new SnippetTextElement         {
                        Text = "/@StartLine}'"
                    },
                    new SnippetTextElement            {
                        Text = " StartCol='{"
                    }, new SnippetBoundElement        {
                        TargetElement = artifact
                    }, new SnippetTextElement         {
                        Text = "/@StartCol}'"
                    },
                    new SnippetTextElement            {
                        Text = " EndLine='{"
                    }, new SnippetBoundElement        {
                        TargetElement = artifact
                    }, new SnippetTextElement         {
                        Text = "/@EndLine}'"
                    },
                    new SnippetTextElement            {
                        Text = " EndCol='{"
                    }, new SnippetBoundElement        {
                        TargetElement = artifact
                    }, new SnippetTextElement         {
                        Text = "/@EndCol}'"
                    },
                    new SnippetTextElement            {
                        Text = "/>\n"
                    },
                    new SnippetTextElement            {
                        Text = "}\n"
                    },
                    new SnippetTextElement            {
                        Text = "</"
                    }, new SnippetBoundElement        {
                        TargetElement = topLevelNodeName
                    }, new SnippetTextElement         {
                        Text = ">\n"
                    },
                }
            };

            Snippet ruleSnippet = new Snippet
            {
                Elements =
                {
                    new SnippetTextElement            {
                        Text = "(: Put description here. It will appear in the tooltip :)\n"
                    },
                    new SnippetTextElement            {
                        Text = "<Diagnostics>\n"
                    },

                    new SnippetTextElement            {
                        Text = "{\n"
                    },
                    new SnippetTextElement            {
                        Text = "  for "
                    }, new SnippetBoundElement        {
                        TargetElement = artifact
                    }, new SnippetTextElement         {
                        Text = " in /(Class | Table | Form | Query)\n"
                    },
                    new SnippetTextElement            {
                        Text = "  for "
                    }, new SnippetBoundElement        {
                        TargetElement = method
                    }, new SnippetTextElement         {
                        Text = " in "
                    }, new SnippetBoundElement        {
                        TargetElement = artifact
                    }, new SnippetTextElement         {
                        Text = "//Method\n"
                    },
                    new SnippetReplaceableTextElement {
                        Text = "  where true() \n"
                    },
                    new SnippetTextElement            {
                        Text = "  order by "
                    }, new SnippetBoundElement        {
                        TargetElement = artifact
                    }, new SnippetReplaceableTextElement{
                        Text = "/@Name\n"
                    },
                    new SnippetTextElement            {
                        Text = "  let $typeNamePair := fn:tokenize("
                    }, new SnippetBoundElement        {
                        TargetElement = artifact
                    }, new SnippetTextElement         {
                        Text = "/@Artifact, ':')\n"
                    },
                    new SnippetCaretElement           {
                    },
                    new SnippetTextElement            {
                        Text = "  return <Diagnostic>\n"
                    },
                    new SnippetTextElement            {
                        Text = "     <Moniker>"
                    }, new SnippetReplaceableTextElement{
                        Text = "Name"
                    }, new SnippetTextElement         {
                        Text = "</Moniker>\n"
                    },
                    new SnippetTextElement            {
                        Text = "     <Severity>"
                    }, new SnippetReplaceableTextElement{
                        Text = "Error, Warning"
                    }, new SnippetTextElement         {
                        Text = "</Severity>\n"
                    },
                    new SnippetTextElement            {
                        Text = "     <Path>"
                    }, new SnippetTextElement         {
                        Text = "dynamics://{$typeNamePair[1]}/{$typeNamePair[2]}/Method/{string("
                    }, new SnippetBoundElement        {
                        TargetElement = method
                    }, new SnippetTextElement         {
                        Text = "/@Name)}"
                    }, new SnippetTextElement         {
                        Text = "</Path>\n"
                    },
                    new SnippetTextElement            {
                        Text = "     <Message>"
                    }, new SnippetReplaceableTextElement{
                        Text = "This is the rule message"
                    }, new SnippetTextElement         {
                        Text = "</Message>\n"
                    },
                    new SnippetTextElement            {
                        Text = "     <DiagnosticType>AppChecker</DiagnosticType>\n"
                    },

                    new SnippetTextElement            {
                        Text = "     <Line>{string("
                    }, new SnippetBoundElement        {
                        TargetElement = artifact
                    }, new SnippetTextElement         {
                        Text = "/@StartLine)}</Line>\n"
                    },
                    new SnippetTextElement            {
                        Text = "     <Column>{string("
                    }, new SnippetBoundElement        {
                        TargetElement = artifact
                    }, new SnippetTextElement         {
                        Text = "/@StartCol)}</Column>\n"
                    },
                    new SnippetTextElement            {
                        Text = "     <EndLine>{string("
                    }, new SnippetBoundElement        {
                        TargetElement = artifact
                    }, new SnippetTextElement         {
                        Text = "/@EndLine)}</EndLine>\n"
                    },
                    new SnippetTextElement            {
                        Text = "     <EndColumn>{string("
                    }, new SnippetBoundElement        {
                        TargetElement = artifact
                    }, new SnippetTextElement         {
                        Text = "/@EndCol)}</EndColumn>\n"
                    },
                    new SnippetTextElement            {
                        Text = "  </Diagnostic>\n"
                    },
                    new SnippetTextElement            {
                        Text = "}\n"
                    },
                    new SnippetTextElement            {
                        Text = "</Diagnostics>\n"
                    },
                }
            };

            Snippet dbInfoSnippet = new Snippet
            {
                Elements =
                {
                    new SnippetTextElement {
                        Text = "(: Show the statistics of the database given its name  :)\n"
                    },
                    new SnippetTextElement {
                        Text = "(: Note: This will consume resources for big databases :)\n"
                    },
                    new SnippetTextElement {
                        Text = "(: if the database is not already open.                :)\n"
                    },
                    new SnippetTextElement {
                        Text = "db:info($database)"
                    }
                }
            };

            Snippet dbListSnippet = new Snippet
            {
                Elements =
                {
                    new SnippetTextElement {
                        Text = "(: Show the databases  :)\n"
                    },
                    new SnippetTextElement {
                        Text = "db:list()"
                    }
                }
            };

            this.snippets.Add("Basic", basicSnippet);
            this.snippets.Add("Appchecker rule", ruleSnippet);
            this.snippets.Add("Database info", dbInfoSnippet);
            this.snippets.Add("Database list", dbListSnippet);
        }