Esempio n. 1
0
        public async Task RunClangTidyAsync(int aCommandId, CommandUILocation commandUILocation, Document document = null)
        {
            await PrepareCommmandAsync(commandUILocation);

            await Task.Run(() =>
            {
                lock (mutex)
                {
                    try
                    {
                        using var silentFileController = new SilentFileChangerController();
                        using var fileChangerWatcher   = new FileChangerWatcher();

                        var tidySettings = SettingsProvider.TidySettingsModel;

                        if (CommandIds.kTidyFixId == aCommandId || tidySettings.TidyOnSave)
                        {
                            fileChangerWatcher.OnChanged += FileOpener.Open;

                            var dte2 = VsServiceProvider.GetService(typeof(DTE)) as DTE2;
                            string solutionFolderPath = SolutionInfo.IsOpenFolderModeActive() ?
                                                        dte2.Solution.FullName : dte2.Solution.FullName
                                                        .Substring(0, dte2.Solution.FullName.LastIndexOf('\\'));

                            fileChangerWatcher.Run(solutionFolderPath);

                            FilePathCollector fileCollector = new FilePathCollector();
                            var filesPath = fileCollector.Collect(mItemsCollector.Items).ToList();

                            silentFileController.SilentFiles(filesPath);
                            silentFileController.SilentFiles(dte2.Documents);
                        }

                        if (tidySettings.DetectClangTidyFile && !mItemsCollector.IsEmpty)
                        {
                            // Check for .clang-tidy congif file
                            if (FileSystem.SearchAllTopDirectories(mItemsCollector.Items[0].GetPath(), FileSystem.ConfigClangTidyFileName))
                            {
                                tidySettings.UseChecksFrom = ClangTidyUseChecksFrom.TidyFile;
                            }
                            else
                            {
                                tidySettings.UseChecksFrom = ClangTidyUseChecksFrom.PredefinedChecks;
                            }

                            var settingsHandlder = new SettingsHandler();
                            settingsHandlder.SaveSettings();
                        }

                        RunScript(aCommandId, false);
                    }
                    catch (Exception exception)
                    {
                        VsShellUtilities.ShowMessageBox(AsyncPackage, exception.Message, "Error",
                                                        OLEMSGICON.OLEMSGICON_CRITICAL, OLEMSGBUTTON.OLEMSGBUTTON_OK, OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST);
                    }
                }
            });
        }
 public void RunClangFormat(CommandUILocation commandUILocation)
 {
     if (CommandUILocation.ContextMenu == commandUILocation)
     {
         FormatAllSelectedDocuments();
     }
     else
     {
         FormatActiveDocument();
     }
 }
Esempio n. 3
0
 public void RunClangFormat(CommandUILocation commandUILocation)
 {
     if (CommandUILocation.ContextMenu == commandUILocation)
     {
         FormatAllSelectedDocuments();
     }
     else // format command is called from toolbar (CommandUILocation.Toolbar == commandUILocation)
     {
         FormatActiveDocument();
     }
 }
Esempio n. 4
0
        public void RunClangFormat(CommandUILocation commandUILocation)
        {
            if (CommandUILocation.ContextMenu == commandUILocation)
            {
                FormatAllSelectedDocuments();
                return;
            }

            if (CommandUILocation.Toolbar == commandUILocation)
            {
                FormatActiveDocument();
                return;
            }
        }
        private void SetCommandLocation()
        {
            switch (currentCommand)
            {
            case CommandIds.kClangFormatToolbarId:
            case CommandIds.kCompileToolbarId:
            case CommandIds.kTidyToolbarId:
            case CommandIds.kTidyFixToolbarId:
                commandUILocation = CommandUILocation.Toolbar;
                break;

            default:
                commandUILocation = CommandUILocation.ContextMenu;
                break;
            }
        }
Esempio n. 6
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);
            }
        }
Esempio n. 7
0
        public async Task RunClangCompileAsync(int aCommandId, CommandUILocation commandUILocation)
        {
            await PrepareCommmandAsync(commandUILocation);

            await Task.Run(() =>
            {
                try
                {
                    RunScript(aCommandId);
                }
                catch (Exception exception)
                {
                    VsShellUtilities.ShowMessageBox(AsyncPackage, exception.Message, "Error",
                                                    OLEMSGICON.OLEMSGICON_CRITICAL, OLEMSGBUTTON.OLEMSGBUTTON_OK, OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST);
                }
            });
        }
Esempio n. 8
0
        public async Task RunClangTidyAsync(int aCommandId, CommandUILocation commandUILocation)
        {
            await PrepareCommmandAsync(commandUILocation);

            await Task.Run(() =>
            {
                try
                {
                    using (var silentFileController = new SilentFileChangerController())
                    {
                        using (var fileChangerWatcher = new FileChangerWatcher())
                        {
                            SettingsProvider settingsProvider = new SettingsProvider();
                            TidySettingsModel tidySettings    = settingsProvider.GetTidySettingsModel();

                            if (CommandIds.kTidyFixId == aCommandId || tidySettings.TidyOnSave)
                            {
                                fileChangerWatcher.OnChanged += FileOpener.Open;

                                var dte2 = VsServiceProvider.GetService(typeof(DTE)) as DTE2;
                                string solutionFolderPath = SolutionInfo.IsOpenFolderModeActive() ?
                                                            dte2.Solution.FullName : dte2.Solution.FullName
                                                            .Substring(0, dte2.Solution.FullName.LastIndexOf('\\'));

                                fileChangerWatcher.Run(solutionFolderPath);

                                FilePathCollector fileCollector = new FilePathCollector();
                                var filesPath = fileCollector.Collect(mItemsCollector.Items).ToList();

                                silentFileController.SilentFiles(filesPath);
                                silentFileController.SilentFiles(dte2.Documents);
                            }
                            RunScript(aCommandId);
                        }
                    }
                }
                catch (Exception exception)
                {
                    VsShellUtilities.ShowMessageBox(AsyncPackage, exception.Message, "Error",
                                                    OLEMSGICON.OLEMSGICON_CRITICAL, OLEMSGBUTTON.OLEMSGBUTTON_OK, OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST);
                }
            });
        }
Esempio n. 9
0
        public void RunClangFormat(CommandUILocation commandUILocation)
        {
            if (clearOutput == false)
            {
                OnFormatFile(new FormatCommandEventArgs()
                {
                    CanFormat = true, Clear = clearOutput
                });
            }

            clearOutput = true;

            if (CommandUILocation.ContextMenu == commandUILocation)
            {
                FormatAllSelectedDocuments();
            }
            else
            {
                FormatActiveDocument();
            }
        }
        public async Task LaunchCommandAsync(int aCommandId, CommandUILocation aCommandUILocation)
        {
            switch (aCommandId)
            {
            case CommandIds.kSettingsId:
            {
                SettingsCommand.Instance.ShowSettings();
                break;
            }

            case CommandIds.kStopClang:
            {
                await StopCommand.Instance.RunStopClangCommandAsync();

                break;
            }

            case CommandIds.kClangFormat:
            {
                FormatCommand.Instance.RunClangFormat(aCommandUILocation);
                OnAfterFormatCommand();
                break;
            }

            case CommandIds.kClangFormatToolbarId:
            {
                FormatCommand.Instance.RunClangFormat(aCommandUILocation);
                OnAfterFormatCommand();
                break;
            }

            case CommandIds.kCompileId:
            {
                OnBeforeClangCommand(CommandIds.kCompileId);
                await CompileCommand.Instance.RunClangCompileAsync(CommandIds.kCompileId, aCommandUILocation);

                OnAfterClangCommand();
                break;
            }

            case CommandIds.kCompileToolbarId:
            {
                OnBeforeClangCommand(CommandIds.kCompileId);
                await CompileCommand.Instance.RunClangCompileAsync(CommandIds.kCompileId, aCommandUILocation);

                OnAfterClangCommand();
                break;
            }

            case CommandIds.kTidyId:
            {
                OnBeforeClangCommand(CommandIds.kTidyId);
                await TidyCommand.Instance.RunClangTidyAsync(CommandIds.kTidyId, aCommandUILocation);

                OnAfterClangCommand();
                break;
            }

            case CommandIds.kTidyToolbarId:
            {
                OnBeforeClangCommand(CommandIds.kTidyId);
                await TidyCommand.Instance.RunClangTidyAsync(CommandIds.kTidyId, aCommandUILocation);

                OnAfterClangCommand();
                break;
            }

            case CommandIds.kTidyFixId:
            {
                OnBeforeClangCommand(CommandIds.kTidyFixId);
                await TidyCommand.Instance.RunClangTidyAsync(CommandIds.kTidyFixId, aCommandUILocation);

                OnAfterClangCommand();
                break;
            }

            case CommandIds.kTidyFixToolbarId:
            {
                OnBeforeClangCommand(CommandIds.kTidyFixId);
                await TidyCommand.Instance.RunClangTidyAsync(CommandIds.kTidyFixId, aCommandUILocation);

                OnAfterClangCommand();
                break;
            }

            case CommandIds.kITidyExportConfigId:
            {
                TidyConfigCommand.Instance.ExportConfig();
                break;
            }

            case CommandIds.kIgnoreFormatId:
            {
                IgnoreFormatCommand.Instance.RunIgnoreFormatCommand(CommandIds.kIgnoreFormatId);
                break;
            }

            case CommandIds.kIgnoreCompileId:
            {
                IgnoreCompileCommand.Instance.RunIgnoreCompileCommand(CommandIds.kIgnoreCompileId);
                break;
            }

            case CommandIds.kLogoutId:
            {
                Logout.Instance.LogoutUser();
                break;
            }

            default:
                break;
            }
        }
Esempio n. 11
0
        public async Task LaunchCommandAsync(int aCommandId, CommandUILocation aCommandUILocation)
        {
            switch (aCommandId)
            {
            case CommandIds.kSettingsId:
            {
                await SettingsCommand.Instance.ShowSettingsAsync();

                break;
            }

            case CommandIds.kStopClang:
            {
                await StopCommand.Instance.RunStopClangCommandAsync(false);

                break;
            }

            case CommandIds.kClangFormat:
            {
                clearOutputOnFormat = true;
                FormatCommand.Instance.RunClangFormat(aCommandUILocation);
                break;
            }

            case CommandIds.kClangFormatToolbarId:
            {
                clearOutputOnFormat = true;
                FormatCommand.Instance.RunClangFormat(aCommandUILocation);
                break;
            }

            case CommandIds.kCompileId:
            {
                await StopBackgroundRunnersAsync();

                OnBeforeClangCommand(CommandIds.kCompileId);

                await CompileCommand.Instance.RunClangCompileAsync(CommandIds.kCompileId, aCommandUILocation);

                OnAfterClangCommand();
                break;
            }

            case CommandIds.kCompileToolbarId:
            {
                await StopBackgroundRunnersAsync();

                OnBeforeClangCommand(CommandIds.kCompileId);

                await CompileCommand.Instance.RunClangCompileAsync(CommandIds.kCompileId, aCommandUILocation);

                OnAfterClangCommand();
                break;
            }

            case CommandIds.kTidyId:
            {
                await StopBackgroundRunnersAsync();

                OnBeforeClangCommand(CommandIds.kTidyId);

                await TidyCommand.Instance.RunClangTidyAsync(CommandIds.kTidyId, aCommandUILocation);

                OnAfterClangCommand();
                break;
            }

            case CommandIds.kTidyToolbarId:
            {
                await StopBackgroundRunnersAsync();

                OnBeforeClangCommand(CommandIds.kTidyId);

                await TidyCommand.Instance.RunClangTidyAsync(CommandIds.kTidyId, aCommandUILocation);

                OnAfterClangCommand();
                break;
            }

            case CommandIds.kTidyFixId:
            {
                await StopBackgroundRunnersAsync();

                OnBeforeClangCommand(CommandIds.kTidyFixId);

                await TidyCommand.Instance.RunClangTidyAsync(CommandIds.kTidyFixId, aCommandUILocation);

                OnAfterClangCommand();
                break;
            }

            case CommandIds.kTidyFixToolbarId:
            {
                await StopBackgroundRunnersAsync();

                OnBeforeClangCommand(CommandIds.kTidyFixId);

                await TidyCommand.Instance.RunClangTidyAsync(CommandIds.kTidyFixId, aCommandUILocation);

                OnAfterClangCommand();
                break;
            }

            case CommandIds.kIgnoreFormatId:
            {
                IgnoreFormatCommand.Instance.RunIgnoreFormatCommand();
                break;
            }

            case CommandIds.kIgnoreCompileId:
            {
                IgnoreCompileCommand.Instance.RunIgnoreCompileCommand();
                break;
            }

            case CommandIds.kJsonCompilationDatabase:
            {
                await StopBackgroundRunnersAsync();

                OnBeforeClangCommand(CommandIds.kJsonCompilationDatabase);

                await JsonCompilationDatabaseCommand.Instance.ExportAsync();

                OnAfterClangCommand();
                break;
            }

            default:
                break;
            }
        }
Esempio n. 12
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);
            }
        }
Esempio n. 13
0
        public void FormatDocument(Document aDocument, ClangFormatOptionsView aOptions, CommandUILocation commandUILocation)
        {
            mClangFormatView = aOptions;
            mDocument        = aDocument;

            RunClangFormat(commandUILocation);
        }