Beispiel #1
0
        private void ExecuteFromMenu(object sender, EventArgs e)
        {
            ThreadHelper.ThrowIfNotOnUIThread();
            var dte = (DTE)Package.GetGlobalService(typeof(DTE));

            if (dte.ActiveDocument == null)
            {
                return;
            }
            var activeDocument = (TextDocument)dte.ActiveDocument.Object();

            if (activeDocument == null)
            {
                return;
            }
            var formatter = new TSQLFormatter.Model.Formatter();

            if (String.IsNullOrWhiteSpace(activeDocument.Selection.Text))
            {
                var formattedText = formatter.GetFormattedText(activeDocument.GetText());
                activeDocument.SetText(formattedText);
            }
            else
            {
                var formattedText = formatter.GetFormattedText(activeDocument.Selection.Text);
                activeDocument.Selection.Delete();
                activeDocument.Selection.Insert(formattedText);
            }
        }
Beispiel #2
0
        private void ExecuteFromFileContext(object sender, EventArgs e)
        {
            ThreadHelper.ThrowIfNotOnUIThread();
            var formatter            = new TSQLFormatter.Model.Formatter();
            var dte                  = (DTE)Package.GetGlobalService(typeof(DTE));
            var selectedProjectItems = dte.SelectedItems.GetSelectedProjectItems().ToList();

            foreach (var projectItem in selectedProjectItems)
            {
                if (projectItem.Document != null)
                {
                    var textDocument  = (TextDocument)projectItem.Document.Object();
                    var formattedText = formatter.GetFormattedText(textDocument.GetText());
                    textDocument.SetText(formattedText);
                }
                else
                {
                    var fileName      = projectItem.FileNames[1];
                    var fileText      = File.ReadAllText(fileName);
                    var formattedText = formatter.GetFormattedText(fileText);
                    File.WriteAllText(fileName, formattedText);
                }
            }
        }