Beispiel #1
0
        private string FormatEndOfFile(IWpfTextView aView, out string aDirPath, out string aFilePath)
        {
            aFilePath = Vsix.GetDocumentPath(aView);
            aDirPath  = Path.GetDirectoryName(aFilePath);

            var text    = aView.TextBuffer.CurrentSnapshot.GetText();
            var newline = text.Contains(Environment.NewLine) ? Environment.NewLine : "\n";

            if (!text.EndsWith(newline))
            {
                aView.TextBuffer.Insert(aView.TextBuffer.CurrentSnapshot.Length, newline);
                text += newline;
            }

            return(text);
        }
Beispiel #2
0
        private void ExecuteFormatCommand()
        {
            try
            {
                if (ValidExecution(out IWpfTextView view) == false)
                {
                    return;
                }

                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);
                }
                else
                {
                    // format the end of the file for format document
                    text = FormatEndOfFile(view, filePath, out dirPath);
                }

                string output = RunFormatProcess(dirPath, filePath, text, startPosition, length);

                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);
            }
        }
        private void ExecuteFormatCommand()
        {
            try
            {
                if (ValidExecution(out IWpfTextView view) == false)
                {
                    return;
                }

                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);
                }
                else
                {
                    // format the end of the file for format document
                    text = FormatEndOfFile(view, filePath, out dirPath);
                }

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

                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);
            }

            OnFormatFile(new FormatCommandEventArgs()
            {
                CanFormat = true
            });
        }
Beispiel #4
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;
            }
        }
Beispiel #5
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);
            }
        }