예제 #1
0
        private Grid GetGridForInput(SnippetInput input)
        {
            var newInputGrid = new Grid();

            newInputGrid.Visibility = Visibility.Visible;
            newInputGrid.ColumnDefinitions.Add(new ColumnDefinition());
            newInputGrid.ColumnDefinitions.Add(new ColumnDefinition());
            newInputGrid.ColumnDefinitions.Add(new ColumnDefinition());
            newInputGrid.ColumnDefinitions.Add(new ColumnDefinition());
            newInputGrid.ColumnDefinitions.Add(new ColumnDefinition());
            newInputGrid.ColumnDefinitions.Add(new ColumnDefinition());
            newInputGrid.RowDefinitions.Add(new RowDefinition());

            var variableName = new TextBox();

            variableName.Text = input.VariableName;
            newInputGrid.Children.Add(variableName);
            Grid.SetColumn(variableName, 0);

            var friendlyName = new TextBox();

            friendlyName.Text = input.FriendlyName;
            newInputGrid.Children.Add(friendlyName);
            Grid.SetColumn(friendlyName, 1);

            var description = new TextBox();

            description.Text = input.Description;
            newInputGrid.Children.Add(description);
            Grid.SetColumn(description, 2);

            var type = new ComboBox();

            type.DisplayMemberPath = "Value";
            type.SelectedValuePath = "Key";
            var availableTypes = TypeService.GetAvailableTypes().ToDictionary(x => x.ID, x => x.Name);

            type.ItemsSource   = availableTypes;
            type.SelectedValue = input.TypeID;
            newInputGrid.Children.Add(type);
            Grid.SetColumn(type, 3);

            var optional = new CheckBox();

            optional.IsChecked = input.IsOptional;
            newInputGrid.Children.Add(optional);
            Grid.SetColumn(optional, 4);

            var remove = new Button();

            remove.Content = "Remove";
            remove.Click  += (sender, args) =>
            {
                InputStack.Children.Remove(newInputGrid);
            };
            newInputGrid.Children.Add(remove);
            Grid.SetColumn(remove, 5);

            return(newInputGrid);
        }
예제 #2
0
        public void ConvertSpacesToTabsForDeeplyNestedCode()
        {
            var input = new SnippetInput
            {
                Body          = @"public void MethodName()
{
    if (true)
    {
        WriteLine();
    }
}",
                ConvertToTabs = true,
                TabLength     = 2
            };

            var generator = new SnippetGenerator();

            var snippet = generator.GetSnippet(input);

            var lines = Regex.Split(snippet, @"\r?\n");

            Assert.EndsWith("\"public void MethodName()\",", lines[3]);
            Assert.EndsWith("\"{\",", lines[4]);
            Assert.EndsWith("\"\\t\\tif (true)\",", lines[5]);
            Assert.EndsWith("\"\\t\\t{\",", lines[6]);
            Assert.EndsWith("\"\\t\\t\\t\\tWriteLine();\",", lines[7]);
            Assert.EndsWith("\"\\t\\t}\",", lines[8]);
            Assert.EndsWith("\"}\"", lines[9]);
        }
예제 #3
0
        public void GeneratePropertySnippet()
        {
            var input = new SnippetInput
            {
                Name           = "Property",
                Prefix         = "prop",
                Description    = "An automatically implemented property.",
                HasDescription = true,
                Body           = "public ${1:int} ${2:MyProperty} { get; set; }$0"
            };

            var generator = new SnippetGenerator();

            var snippet = generator.GetSnippet(input);

            var lines = Regex.Split(snippet, @"\r?\n");

            Assert.Equal("\"Property\": {", lines[0]);
            Assert.Equal("    \"prefix\": \"prop\",", lines[1]);
            Assert.Equal("    \"body\": [", lines[2]);
            Assert.Equal("        \"public ${1:int} ${2:MyProperty} { get; set; }$0\"", lines[3]);
            Assert.Equal("    ],", lines[4]);
            Assert.Equal("    \"description\": \"An automatically implemented property.\"", lines[5]);
            Assert.Equal("}", lines[6]);
        }
예제 #4
0
        private Grid GetRunInputGridForInput(SnippetInput input)
        {
            var newInputGrid = new Grid();

            newInputGrid.Visibility = Visibility.Visible;
            newInputGrid.ColumnDefinitions.Add(new ColumnDefinition());
            newInputGrid.ColumnDefinitions.Add(new ColumnDefinition());
            newInputGrid.ColumnDefinitions.Add(new ColumnDefinition());
            newInputGrid.RowDefinitions.Add(new RowDefinition());

            var friendlyName = new Label();

            friendlyName.Content = input.FriendlyName;
            newInputGrid.Children.Add(friendlyName);
            Grid.SetColumn(friendlyName, 0);

            var type = TypeService.GetAvailableTypes().First(x => x.ID == input.TypeID);

            if (!type.HasMultipleValues)
            {
                var friendlyNameInput = new TextBox();
                newInputGrid.Children.Add(friendlyNameInput);
                Grid.SetColumn(friendlyNameInput, 1);
            }
            else
            {
                var listDropDown = new ComboBox();
                if (type.ListValues.Count == 1)
                {
                    listDropDown.ItemsSource = type.ListValues.First().Value;
                }
                else
                {
                    var items = new List <Item>();
                    foreach (var listSection in type.ListValues)
                    {
                        foreach (var element in listSection.Value)
                        {
                            items.Add(new Item(element, listSection.Key));
                        }
                    }

                    var listCollection = new ListCollectionView(items);
                    listCollection.GroupDescriptions.Add(new PropertyGroupDescription("Category"));
                    listDropDown.ItemsSource = listCollection;
                }
                newInputGrid.Children.Add(listDropDown);
                Grid.SetColumn(listDropDown, 1);
            }

            var id = new Label();

            id.Content    = input.TypeID;
            id.Visibility = Visibility.Collapsed;
            newInputGrid.Children.Add(id);
            Grid.SetColumn(id, 2);

            return(newInputGrid);
        }
예제 #5
0
        public void EscapeDollarSigns()
        {
            var input = new SnippetInput {
                Body = "$name = ${1:name};"
            };

            var snippet = new SnippetGenerator().GetSnippet(input);

            var lines = Regex.Split(snippet, @"\r?\n");

            Assert.Equal("        \"\\\\$name = ${1:name};\"", lines[3]);
        }
예제 #6
0
        public void PreserveSpacesNotUsedForIndentation()
        {
            var input = new SnippetInput
            {
                Body          = "    // SOME    TEXT",
                TabLength     = 4,
                ConvertToTabs = true
            };

            var snippet = new SnippetGenerator().GetSnippet(input);

            var lines = Regex.Split(snippet, @"\r?\n");

            Assert.EndsWith("\"\\t// SOME    TEXT\"", lines[3]);
        }
        private Dictionary <string, SnippetDetails> ShapeSnippet(SnippetInput input)
        {
            var snippetDetails = new SnippetDetails
            {
                Prefix = input.Prefix ?? string.Empty,
                Body   = input.Body?.Any() == true
                    ? EscapeDollarSigns(
                    EnsureRequestedIndentation(input.Body, input.ConvertToTabs, input.TabLength)).GetLines()
                    : new List <string>
                {
                    string.Empty
                },
                Description = input.HasDescription ? input.Description ?? string.Empty : null
            };

            return(new Dictionary <string, SnippetDetails>
            {
                { input.Name ?? string.Empty, snippetDetails }
            });
        }
        public string GetSnippet(SnippetInput input)
        {
            var shape = ShapeSnippet(input);

            using var stringWriter = new StringWriter();
            using var jsonWriter   = new JsonTextWriter(stringWriter)
                  {
                      Formatting  = Formatting.Indented,
                      Indentation = Indentation
                  };

            var serilizer = new JsonSerializer()
            {
                NullValueHandling = NullValueHandling.Ignore
            };

            serilizer.Serialize(jsonWriter, shape);

            var json = stringWriter.ToString();

            return(GetJsonBody(json));
        }
예제 #9
0
        public void GenerateMultilineSnippet()
        {
            var input = new SnippetInput
            {
                Name   = "Property",
                Prefix = "prop",
                Body   = "public ${1:int} ${2:MyProperty} { get; set; }$0" + Environment.NewLine + "// Second Line"
            };

            var generator = new SnippetGenerator();

            var snippet = generator.GetSnippet(input);

            var lines = Regex.Split(snippet, @"\r?\n");

            Assert.Equal("\"Property\": {", lines[0]);
            Assert.Equal("    \"prefix\": \"prop\",", lines[1]);
            Assert.Equal("    \"body\": [", lines[2]);
            Assert.Equal("        \"public ${1:int} ${2:MyProperty} { get; set; }$0\",", lines[3]);
            Assert.Equal("        \"// Second Line\"", lines[4]);
            Assert.Equal("    ]", lines[5]);
            Assert.Equal("}", lines[6]);
        }
예제 #10
0
        public void ConvertSpacesToTabs()
        {
            var input = new SnippetInput
            {
                Name          = "Property",
                Prefix        = "prop",
                Body          = "    public ${1:int} ${2:MyProperty} { get; set; }$0",
                ConvertToTabs = true,
                TabLength     = 4
            };

            var generator = new SnippetGenerator();

            var snippet = generator.GetSnippet(input);

            var lines = Regex.Split(snippet, @"\r?\n");

            Assert.Equal("\"Property\": {", lines[0]);
            Assert.Equal("    \"prefix\": \"prop\",", lines[1]);
            Assert.Equal("    \"body\": [", lines[2]);
            Assert.Equal("        \"\\tpublic ${1:int} ${2:MyProperty} { get; set; }$0\"", lines[3]);
            Assert.Equal("    ]", lines[4]);
            Assert.Equal("}", lines[5]);
        }