Exemple #1
0
        private void FormatDocument(Document aDocument, ClangFormatOptionsView aOptions)
        {
            mClangFormatView = aOptions;
            mDocument        = aDocument;

            RunClangFormat(new object(), new EventArgs());
        }
Exemple #2
0
        public static ClangFormatOptionsView GetClangFormatOptionsViewFromFile()
        {
            ClangFormatOptionsView clangFormatOptionsView = new ClangFormatOptionsView();

            clangFormatOptionsView.LoadSettingsFromStorage();
            return(clangFormatOptionsView);
        }
Exemple #3
0
        public void FormatDocument(Document aDocument, ClangFormatOptionsView aOptions, CommandUILocation commandUILocation)
        {
            mClangFormatView = aOptions;
            mDocument        = aDocument;

            RunClangFormat(commandUILocation);
        }
Exemple #4
0
        public void FormatDocument(Document aDocument, ClangFormatOptionsView aOptions)
        {
            mClangFormatView = aOptions;
            mDocument        = aDocument;

            RunClangFormat();
        }
Exemple #5
0
        private string GetClangFormatParameters(ClangFormatOptionsView aClangFormatView)
        {
            if (true == string.IsNullOrWhiteSpace(aClangFormatView.Style))
            {
                return(string.Empty);
            }

            return(aClangFormatView.Style);
        }
        private void Initialize(string ignoreFiles)
        {
            ThreadHelper.ThrowIfNotOnUIThread();
            mDte = (DTE2)ServiceProvider.GlobalProvider.GetService(typeof(DTE));
            SettingsTestUtility.ResetClangFormatOptionsView();

            mIgnoreFormatCommand         = IgnoreFormatCommand.Instance;
            mFormatOptions               = SettingsProvider.ClangFormatSettings;
            mFormatOptions.FilesToIgnore = ignoreFiles;
        }
Exemple #7
0
        public ClangFormatOptionsUserControl(ClangFormatOptionsView clangFormatOptions)
        {
            InitializeComponent();

            wpfPropGrid.SelectedObject = clangFormatOptions;

            Loaded += delegate
            {
                UIElementsActivator.Activate(HwndSource.FromVisual(this) as HwndSource);
            };
        }
Exemple #8
0
        public async Task ClangFormatOptionsView_NotNullAsync()
        {
            //Arrange
            await UnitTestUtility.LoadPackageAsync();

            //Act
            ClangFormatOptionsView clangFormatSettings = SettingsProvider.ClangFormatSettings;

            //Assert
            Assert.NotNull(clangFormatSettings);
        }
        private List <string> RemoveDuplicateFiles(List <string> documentsToIgnore, ClangFormatOptionsView settings)
        {
            List <string> trimmedDocumentToIgnore = new List <string>();

            foreach (var item in documentsToIgnore)
            {
                if (!settings.FilesToIgnore.Contains(item))
                {
                    trimmedDocumentToIgnore.Add(item);
                }
            }
            return(trimmedDocumentToIgnore);
        }
Exemple #10
0
        public async Task FormatStyle_ChangeValue_CompareViewToFileAsync()
        {
            await UnitTestUtility.LoadPackageAsync();

            SettingsTestUtility.ResetClangFormatOptionsView();
            ClangFormatOptionsView clangFormatSettings = SettingsProvider.ClangFormatSettings;

            clangFormatSettings.Style = ClangFormatStyle.Mozilla;
            SettingsTestUtility.SaveFormatOptions(clangFormatSettings);
            ClangFormatOptionsView clangFormatSettingsFromFile = SettingsTestUtility.GetClangFormatOptionsViewFromFile();

            Assert.Equal(clangFormatSettings.Style, clangFormatSettingsFromFile.Style);
        }
Exemple #11
0
        public async Task AssumeFilename_ChangeValue_CompareViewToFileAsync()
        {
            await UnitTestUtility.LoadPackageAsync();

            SettingsTestUtility.ResetClangFormatOptionsView();
            ClangFormatOptionsView clangFormatSettings = SettingsProvider.ClangFormatSettings;

            clangFormatSettings.AssumeFilename = "Test";
            SettingsTestUtility.SaveFormatOptions(clangFormatSettings);
            ClangFormatOptionsView clangFormatSettingsFromFile = SettingsTestUtility.GetClangFormatOptionsViewFromFile();

            Assert.Equal(clangFormatSettings.AssumeFilename, clangFormatSettingsFromFile.AssumeFilename);
        }
Exemple #12
0
        public async Task FileExtensions_ChangeValue_CompareViewToFileAsync()
        {
            await UnitTestUtility.LoadPackageAsync();

            SettingsTestUtility.ResetClangFormatOptionsView();
            ClangFormatOptionsView clangFormatSettings = SettingsProvider.ClangFormatSettings;

            clangFormatSettings.FileExtensions = ".cpp";
            SettingsTestUtility.SaveFormatOptions(clangFormatSettings);
            ClangFormatOptionsView clangFormatSettingsFromFile = SettingsTestUtility.GetClangFormatOptionsViewFromFile();

            Assert.Equal(clangFormatSettings.FileExtensions, clangFormatSettingsFromFile.FileExtensions);
        }
Exemple #13
0
 /// <summary>
 /// Instance constructor
 /// </summary>
 public GenericScriptBuilder(ClangGeneralOptionsView aGeneralOptions, ClangTidyOptionsView aTidyOptions, ClangTidyPredefinedChecksOptionsView aTidyChecks,
                             ClangTidyCustomChecksOptionsView aTidyCustomChecks, ClangFormatOptionsView aClangFormatView, string aVsEdition, string aVsVersion, bool aTidyFixFlag = false)
 {
     mGeneralOptions         = aGeneralOptions;
     mTidyOptions            = aTidyOptions;
     mTidyChecks             = aTidyChecks;
     mTidyCustomChecks       = aTidyCustomChecks;
     mClangFormatView        = aClangFormatView;
     mVsEdition              = aVsEdition;
     mVsVersion              = aVsVersion;
     mTidyFixFlag            = aTidyFixFlag;
     mUseClangTidyConfigFile = false;
 }
Exemple #14
0
        public async Task CustomExecutable_ChangeValue_CompareViewToFileAsync()
        {
            await UnitTestUtility.LoadPackageAsync();

            SettingsTestUtility.ResetClangFormatOptionsView();
            ClangFormatOptionsView clangFormatSettings = SettingsProvider.ClangFormatSettings;

            clangFormatSettings.ClangFormatPath.Enable = true;
            clangFormatSettings.ClangFormatPath.Value  = @"D:\Test.exe";
            SettingsTestUtility.SaveFormatOptions(clangFormatSettings);
            ClangFormatOptionsView clangFormatSettingsFromFile = SettingsTestUtility.GetClangFormatOptionsViewFromFile();

            Assert.Equal(clangFormatSettings.ClangFormatPath.Enable, clangFormatSettingsFromFile.ClangFormatPath.Enable);
            Assert.Equal(clangFormatSettings.ClangFormatPath.Value, clangFormatSettingsFromFile.ClangFormatPath.Value);
        }
Exemple #15
0
        private void FormatAllSelectedDocuments()
        {
            foreach (var item in CollectSelectedItems(true))
            {
                var document = (item.GetObject() as ProjectItem).Document;

                if (null == document)
                {
                    document = DocumentsHandler.GetActiveDocument();
                }

                mClangFormatView = SettingsProvider.GetSettingsPage(typeof(ClangFormatOptionsView)) as ClangFormatOptionsView;
                mDocument        = document;

                RunClangFormat();
            }
        }
Exemple #16
0
        private void FormatAllSelectedDocuments(CommandUILocation commandUILocation)
        {
            foreach (var item in CollectItems(true, ScriptConstants.kAcceptedFileExtensions, commandUILocation))
            {
                var document = (item.GetObject() as ProjectItem).Document;

                if (null == document)
                {
                    document = DocumentsHandler.GetActiveDocument();
                }

                mClangFormatView = SettingsProvider.ClangFormatSettings;
                mDocument        = document;

                RunClangFormat(commandUILocation);
            }
        }
        private void FormatAllSelectedDocuments()
        {
            foreach (var item in CollectSelectedItems(true))
            {
                var document = (item.GetObject() as ProjectItem).Document;

                if (null == document)
                {
                    document = DocumentsHandler.GetActiveDocument();
                }

                mClangFormatView = GetUserOptions();
                mDocument        = document;

                RunClangFormat(new object(), new EventArgs());
            }
        }
Exemple #18
0
        private void FormatAllSelectedDocuments()
        {
            foreach (var item in CollectSelectedItems())
            {
                if (!(item.GetObject() is ProjectItem))
                {
                    return; // the selected file is not a project item
                }
                var document = (item.GetObject() as ProjectItem).Document;
                if (null == document)
                {
                    continue;
                }

                mClangFormatView = GetUserOptions();
                mDocument        = document;

                RunClangFormat(new object(), new EventArgs());
            }
        }
Exemple #19
0
        public void ConstructParameters(ClangGeneralOptionsView aGeneralOptions, ClangTidyOptionsView aTidyOptions, ClangTidyChecksOptionsView aTidyChecks,
                                        ClangTidyCustomChecksOptionsView aTidyCustomChecks, ClangFormatOptionsView aClangFormatView, DTE2 aDTEObj, string aVsEdition, string aVsVersion)
        {
            mParameters = GetGeneralParameters(aGeneralOptions);
            mParameters = null != aTidyOptions ?
                          $"{mParameters} {GetTidyParameters(aTidyOptions, aTidyChecks, aTidyCustomChecks)}" : $"{mParameters} {ScriptConstants.kParallel}";

            if (null != aClangFormatView && null != aTidyOptions && true == aTidyOptions.Fix && true == aTidyOptions.FormatAfterTidy)
            {
                mParameters = $"{mParameters} {ScriptConstants.kClangFormatStyle} {GetClangFormatParameters(aClangFormatView)}";
            }

            mParameters = $"{mParameters} {ScriptConstants.kVsVersion} {aVsVersion} {ScriptConstants.kVsEdition} {aVsEdition}";
        }
Exemple #20
0
 public static void SaveFormatOptions(ClangFormatOptionsView clangFormatOptionsView)
 {
     clangFormatOptionsView.SaveSettingsToStorage();
 }
Exemple #21
0
        public void RunClangFormat(CommandUILocation commandUILocation)
        {
            try
            {
                if (mClangFormatView == null)
                {
                    FormatAllSelectedDocuments(commandUILocation);
                    return;
                }

                if (ScriptConstants.kCMakeConfigFile == mDocument.Name.ToLower())
                {
                    return;
                }

                var view = Vsix.GetDocumentView(mDocument);
                if (view == null)
                {
                    return;
                }

                StatusBarHandler.Status("Clang-Format started...", 1, vsStatusAnimation.vsStatusAnimationBuild, 1);

                System.Diagnostics.Process process;
                var dirPath  = string.Empty;
                var filePath = Vsix.GetDocumentPath(view);
                var text     = view.TextBuffer.CurrentSnapshot.GetText();

                var startPosition = 0;
                var length        = text.Length;

                if (false == view.Selection.StreamSelectionSpan.IsEmpty)
                {
                    // get the necessary elements for format selection
                    FindStartPositionAndLengthOfSelectedText(view, text, out startPosition, out length);
                    dirPath          = Vsix.GetDocumentParent(view);
                    mClangFormatView = SettingsProvider.ClangFormatSettings;
                }
                else
                {
                    // format the end of the file for format document
                    text = FormatEndOfFile(view, filePath, out dirPath);
                }

                process = CreateProcess(text, startPosition, length, dirPath, filePath, mClangFormatView);

                try
                {
                    process.Start();
                }
                catch (Exception exception)
                {
                    throw new Exception(
                              $"Cannot execute {process.StartInfo.FileName}.\n{exception.Message}.");
                }

                process.StandardInput.Write(text);
                process.StandardInput.Close();

                var output = process.StandardOutput.ReadToEnd();
                process.WaitForExit();

                if (0 != process.ExitCode)
                {
                    throw new Exception(process.StandardError.ReadToEnd());
                }

                ApplyClangFormat(output, view);
            }
            catch (Exception exception)
            {
                VsShellUtilities.ShowMessageBox(AsyncPackage, exception.Message, "Error while running clang-format",
                                                OLEMSGICON.OLEMSGICON_INFO, OLEMSGBUTTON.OLEMSGBUTTON_OK, OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST);
            }
            finally
            {
                mDocument        = null;
                mClangFormatView = null;
                StatusBarHandler.Status("Clang-Format finished", 0, vsStatusAnimation.vsStatusAnimationBuild, 0);
            }
        }
Exemple #22
0
        private System.Diagnostics.Process CreateProcess(string aText, int aOffset, int aLength, string aPath, string aFilePath, ClangFormatOptionsView aClangFormatView)
        {
            string vsixPath = Path.GetDirectoryName(
                typeof(RunClangPowerToolsPackage).Assembly.Location);

            System.Diagnostics.Process process = new System.Diagnostics.Process();
            process.StartInfo.UseShellExecute        = false;
            process.StartInfo.CreateNoWindow         = true;
            process.StartInfo.RedirectStandardInput  = true;
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.RedirectStandardError  = true;
            process.StartInfo.FileName = Path.Combine(vsixPath, ScriptConstants.kClangFormat);

            process.StartInfo.Arguments = " -offset " + aOffset +
                                          " -length " + aLength +
                                          " -output-replacements-xml " +
                                          $" {ScriptConstants.kStyle} \"{aClangFormatView.Style}\"" +
                                          $" {ScriptConstants.kFallbackStyle} \"{aClangFormatView.FallbackStyle}\"";

            var assumeFilename = aClangFormatView.AssumeFilename;

            if (string.IsNullOrEmpty(assumeFilename))
            {
                assumeFilename = aFilePath;
            }
            if (!string.IsNullOrEmpty(assumeFilename))
            {
                process.StartInfo.Arguments += $" -assume-filename \"{assumeFilename}\"";
            }

            if (null != aPath)
            {
                process.StartInfo.WorkingDirectory = aPath;
            }

            return(process);
        }
Exemple #23
0
        /// <summary>
        /// This function is the callback used to execute the command when the menu item is clicked.
        /// See the constructor to see how the menu item is associated with this function using
        /// OleMenuCommandService service and MenuCommand class.
        /// </summary>
        /// <param name="sender">Event sender.</param>
        /// <param name="e">Event args.</param>
        private void RunClangFormat(object sender, EventArgs e)
        {
            try
            {
                if (null == mClangFormatView)
                {
                    FormatAllSelectedDocuments();
                    return;
                }

                var view = Vsix.GetDocumentView(mDocument);
                if (view == null)
                {
                    return;
                }

                System.Diagnostics.Process process;
                var dirPath  = string.Empty;
                var filePath = Vsix.GetDocumentPath(view);
                var text     = view.TextBuffer.CurrentSnapshot.GetText();

                var startPosition = 0;
                var length        = text.Length;

                if (false == view.Selection.StreamSelectionSpan.IsEmpty)
                {
                    // get the necessary elements for format selection
                    FindStartPositionAndLengthOfSelectedText(view, text, out startPosition, out length);
                    dirPath          = Vsix.GetDocumentParent(view);
                    mClangFormatView = GetUserOptions();
                }
                else
                {
                    // format the end of the file for format document
                    text = FormatEndOfFile(view, filePath, out dirPath);
                }

                process = CreateProcess(text, startPosition, length, dirPath, filePath, mClangFormatView);

                try
                {
                    process.Start();
                }
                catch (Exception exception)
                {
                    throw new Exception(
                              $"Cannot execute {process.StartInfo.FileName}.\n{exception.Message}.");
                }

                process.StandardInput.Write(text);
                process.StandardInput.Close();

                var output = process.StandardOutput.ReadToEnd();
                process.WaitForExit();

                if (0 != process.ExitCode)
                {
                    throw new Exception(process.StandardError.ReadToEnd());
                }

                ApplyClangFormat(output, view);
            }
            catch (Exception exception)
            {
                VsShellUtilities.ShowMessageBox(Package, exception.Message, "Error while running clang-format",
                                                OLEMSGICON.OLEMSGICON_INFO, OLEMSGBUTTON.OLEMSGBUTTON_OK, OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST);
            }
            finally
            {
                mDocument        = null;
                mClangFormatView = null;
            }
        }