Exemple #1
0
            public void Process()
            {
                int successCount = 0;

                foreach (string file in this.options.Files)
                {
                    this.Log($"Processing: {file}");

                    if (!options.Ignore)
                    {
                        var extension = Path.GetExtension(file);
                        this.Log($"Extension: {extension}", LogLevel.Debug);

                        if (!extension.Equals(".xaml", StringComparison.OrdinalIgnoreCase))
                        {
                            this.Log("Skipping... Can only process XAML files. Use the --ignore parameter to override.");
                            continue;
                        }
                    }

                    var path = Path.GetFullPath(file);
                    this.Log($"Full Path: {file}", LogLevel.Debug);

                    string   originalContent = null;
                    Encoding encoding        = Encoding.UTF8; // Visual Studio by default uses UTF8
                    using (var reader = new StreamReader(path))
                    {
                        originalContent = reader.ReadToEnd();
                        encoding        = reader.CurrentEncoding;
                        this.Log($"\nOriginal Content:\n\n{originalContent}\n", LogLevel.Insanity);
                    }

                    var formattedOutput = stylerService.ManipulateTreeAndFormatInput(originalContent);
                    this.Log($"\nFormatted Output:\n\n{formattedOutput}\n", LogLevel.Insanity);

                    using (var writer = new StreamWriter(path, false, encoding))
                    {
                        try
                        {
                            writer.Write(formattedOutput);
                            this.Log($"Finished Processing: {file}", LogLevel.Verbose);
                            successCount++;
                        }
                        catch (Exception e)
                        {
                            this.Log("Skipping... Error formatting XAML. Increase log level for more details.");
                            this.Log($"Exception: {e.Message}", LogLevel.Verbose);
                            this.Log($"StackTrace: {e.StackTrace}", LogLevel.Debug);
                        }
                    }
                }

                this.Log($"Processed {successCount} of {this.options.Files.Count} files.", LogLevel.Minimal);
            }
Exemple #2
0
        /// <summary>
        /// Parse input document and verify output against
        /// </summary>
        /// <param name="styler"></param>
        /// <param name="callerMemberName"></param>
        private void DoTest(StylerService styler, [System.Runtime.CompilerServices.CallerMemberName] string callerMemberName = "")
        {
            var testFileBaseName = Path.Combine("TestFiles", callerMemberName);

            // Excercise stylerService using supplied test xaml data
            string actualOutput = styler.ManipulateTreeAndFormatInput(File.ReadAllText(testFileBaseName + ".testxaml"));

            // Write output to ".actual" file for further investigation
            File.WriteAllText(testFileBaseName + ".actual", actualOutput, Encoding.UTF8);

            // Check result
            Assert.That(actualOutput, Is.EqualTo(File.ReadAllText(testFileBaseName + ".expected")));
        }
        private void Execute(Document document)
        {
            if (!this.IsFormatableDocument(document))
            {
                return;
            }

            Properties xamlEditorProps = this.dte.Properties["TextEditor", "XAML"];

            var stylerOptions = GetDialogPage(typeof(PackageOptions)).AutomationObject as IStylerOptions;

            stylerOptions.IndentSize     = Int32.Parse(xamlEditorProps.Item("IndentSize").Value.ToString());
            stylerOptions.IndentWithTabs = (bool)xamlEditorProps.Item("InsertTabs").Value;

            StylerService styler = StylerService.CreateInstance(stylerOptions);

            var textDocument = (TextDocument)document.Object("TextDocument");

            TextPoint currentPoint   = textDocument.Selection.ActivePoint;
            int       originalLine   = currentPoint.Line;
            int       originalOffset = currentPoint.LineCharOffset;

            EditPoint startPoint = textDocument.StartPoint.CreateEditPoint();
            EditPoint endPoint   = textDocument.EndPoint.CreateEditPoint();

            string xamlSource = startPoint.GetText(endPoint);

            xamlSource = styler.ManipulateTreeAndFormatInput(xamlSource);

            startPoint.ReplaceText(endPoint, xamlSource, 0);

            if (originalLine <= textDocument.EndPoint.Line)
            {
                textDocument.Selection.MoveToLineAndOffset(originalLine, originalOffset);
            }
            else
            {
                textDocument.Selection.GotoLine(textDocument.EndPoint.Line);
            }
        }