protected override bool OnApply()
        {
            var firstNonBlankNonCommentLine =
                editorConfigFile.Preamble.Lines
                .Where(line => !(line is EditorConfigBlankLine))
                .Where(line => !(line is EditorConfigCommentLine))
                .FirstOrDefault();

            var lastNonBlankLine =
                editorConfigFile.Preamble.Lines
                .Where(line => !(line is EditorConfigBlankLine))
                .LastOrDefault();

            var lineToInsertAt =
                firstNonBlankNonCommentLine != null
                    ? firstNonBlankNonCommentLine.LineNumber
                : lastNonBlankLine != null
                    ? lastNonBlankLine.LineNumber + 1
                : 1;

            editorConfigFile.Edit(lines => {
                lines.Insert(lineToInsertAt - 1, "root = true");
            });

            FileExtensions.RewriteAllLines(editorConfigPath, editorConfigFile.LinesOfText);

            return(true);
        }
Exemplo n.º 2
0
        public void AddSectionWorks()
        {
            EditorConfigFile editorConfigFile = PrepareTest("addsection", out var file, out var workingFile);

            IniSectionData iniSectionData = new IniSectionData("*.cs")
            {
                new IniCommentData("TEST ADDED SECTION"),
                new IniPropertyData("testProperty", "testValue"),
                new IniEmptyLine(),
                new IniCommentData("ANOTHER COMMENT"),
                new IniPropertyData("anotherProperty", "anotherValue")
            };

            var sectionLength = iniSectionData.Length;

            using (var editContext = editorConfigFile.Edit(new EditorConfigFileOptions {
                EndSectionWithBlankLineOrComment = false
            }))
            {
                editContext.AddSection(iniSectionData);

                editContext.SaveChanges();
            }

            editorConfigFile.Sections.Count.Should().Be(2);

            // Confirm the file is one line longer
            string[] fileText   = File.ReadAllLines(file);
            var      fileLength = fileText.Length;

            string[] workingFileText   = File.ReadAllLines(workingFile);
            var      workingFileLength = workingFileText.Length;

            workingFileLength.Should().Be(fileLength + sectionLength);
        }
Exemplo n.º 3
0
        protected override bool OnApply()
        {
            var insertIndex =
                section.Lines
                .Where(line => !(line is EditorConfigBlankLine))
                .Select(line => line.Index + 1)
                .LastOrDefault();

            var isBlankLineRequired =
                insertIndex < editorConfigFile.Lines.Count - 1 &&
                !(editorConfigFile.Lines[insertIndex] is EditorConfigBlankLine);

            var linesToInsert = new List <string>();

            linesToInsert.Add($"{key} = {value}");

            if (isBlankLineRequired)
            {
                linesToInsert.Insert(0, "");
            }

            editorConfigFile.Edit(lines => {
                lines.InsertRange(insertIndex, linesToInsert);
            });

            FileExtensions.RewriteAllLines(editorConfigPath, editorConfigFile.LinesOfText);

            return(true);
        }
Exemplo n.º 4
0
        public void RemoveWorks()
        {
            EditorConfigFile editorConfigFile = PrepareTest("remove", out var file, out var workingFile);

            const int TestSection = 0;
            var       sectionName = editorConfigFile.Sections[TestSection].Name;

            // Find aftercomment property line
            editorConfigFile.TryGetProperty("aftercomment", editorConfigFile.Sections[TestSection], out var afterProp).Should().BeTrue();
            var lineNumber = afterProp !.LineNumber;

            // Remove beforecommment
            editorConfigFile.TryGetProperty("beforecomment", editorConfigFile.Sections[TestSection], out var prop).Should().BeTrue();
            using (var editContext = editorConfigFile.Edit(new EditorConfigFileOptions {
                EndSectionWithBlankLineOrComment = false
            }))
            {
                editContext.Sections[sectionName].Remove(prop !).Should().BeTrue();

                editContext.SaveChanges();
            }

            editorConfigFile.TryGetProperty("beforecomment", editorConfigFile.Sections[TestSection], out _).Should().BeFalse();

            // Confirm aftercomment has moved up a line
            editorConfigFile.TryGetProperty("aftercomment", editorConfigFile.Sections[TestSection], out var updatedProperty).Should().BeTrue();
            updatedProperty !.LineNumber.Should().Be(lineNumber - 1);

            // Confirm the file is one line shorter
            var fileLength        = File.ReadAllLines(file).Length;
            var workingFileLength = File.ReadAllLines(workingFile).Length;

            workingFileLength.Should().Be(fileLength - 1);
        }
Exemplo n.º 5
0
        public void RemoveSectionWorks()
        {
            EditorConfigFile editorConfigFile = PrepareTest("removesection", out var file, out var workingFile);

            const int TestSection = 0;

            // Find number of lines in section
            var sectionLength = editorConfigFile.Sections[TestSection].Length;
            var sectionName   = editorConfigFile.Sections[TestSection].Name;

            using (var editContext = editorConfigFile.Edit())
            {
                editContext.Sections.Remove(sectionName);

                editContext.SaveChanges();
            }

            editorConfigFile.Sections.Count.Should().Be(0);

            // Confirm the file is shorter by the number of removed lines
            var fileLength        = File.ReadAllLines(file).Length;
            var workingFileLength = File.ReadAllLines(workingFile).Length;

            // The final line would have been an empty line, which gets removed
            workingFileLength.Should().Be(fileLength - sectionLength - 1);
        }
        protected override bool OnApply()
        {
            var sectionToInsertAfter =
                isSectionFirst ?
                editorConfigFile.Sections.First() :
                editorConfigFile.Sections.Last();

            var insertIndex =
                sectionToInsertAfter.Lines
                .Select(l => l.Index + 1)
                .LastOrDefault();

            var isBlankLineRequired =
                insertIndex > 0 &&
                !(editorConfigFile.Lines[insertIndex - 1] is EditorConfigBlankLine);

            var linesToInsert = new List <string>();

            if (isBlankLineRequired)
            {
                linesToInsert.Add("");
            }

            linesToInsert.Add(
                $"[{name}]",
                $"");

            editorConfigFile.Edit(lines => {
                lines.InsertRange(insertIndex, linesToInsert);
            });

            FileExtensions.RewriteAllLines(editorConfigPath, editorConfigFile.LinesOfText);

            return(true);
        }
Exemplo n.º 7
0
        public void NotSavingMakesNoChanges()
        {
            EditorConfigFile editorConfigFile = PrepareTest("nochange", out string file, out string workingFile);

            const int    TestSection       = 0;
            const string TestPropertyKey   = "testProperty";
            const string TestPropertyValue = "testValue";

            var iniProperty = new IniPropertyData(TestPropertyKey, TestPropertyValue);

            var sectionName = editorConfigFile.Sections[TestSection].Name;

            using (var editContext = editorConfigFile.Edit())
            {
                editContext.Sections[sectionName].Add(iniProperty);
            }

            var originalText = File.ReadAllText(file);
            var copyText     = File.ReadAllText(workingFile);

            originalText.Should().Be(copyText);
        }
Exemplo n.º 8
0
        public void AddWorks()
        {
            EditorConfigFile editorConfigFile = PrepareTest("add", out var file, out var workingFile);

            const int    TestSection       = 0;
            const string TestPropertyKey   = "testProperty";
            const string TestPropertyValue = "testValue";

            var section     = editorConfigFile.Sections[TestSection];
            var sectionName = section.Name;

            // Find aftercomment property line
            editorConfigFile.TryGetProperty("aftercomment", section, out var prop).Should().BeTrue();
            var lineNumber = prop !.LineNumber;

            var iniProperty = new IniPropertyData(TestPropertyKey, TestPropertyValue);

            using (var editContext = editorConfigFile.Edit(new EditorConfigFileOptions {
                EndSectionWithBlankLineOrComment = false
            }))
            {
                editContext.Sections[sectionName].Add(iniProperty);

                editContext.SaveChanges();
            }

            editorConfigFile.TryGetProperty(TestPropertyKey, editorConfigFile.Sections[TestSection], out var updatedProperty).Should().BeTrue();
            updatedProperty !.Data.Value.Should().Be(TestPropertyValue);

            // Use the original value rather than re-reading the property. This ensures that the file is otherwise unchanged
            updatedProperty !.LineNumber.Should().Be(lineNumber + 1);

            // Confirm the file is one line longer
            var fileLength        = File.ReadAllLines(file).Length;
            var workingFileLength = File.ReadAllLines(workingFile).Length;

            workingFileLength.Should().Be(fileLength + 1);
        }