Beispiel #1
0
        /// <summary>
        /// Runs clang-format on the current selection
        /// </summary>
        private void FormatSelection(OptionPageGrid options)
        {
            IWpfTextView view = Vsix.GetCurrentView();

            if (view == null)
            {
                // We're not in a text view.
                return;
            }
            string text   = view.TextBuffer.CurrentSnapshot.GetText();
            int    start  = view.Selection.Start.Position.GetContainingLine().Start.Position;
            int    end    = view.Selection.End.Position.GetContainingLine().End.Position;
            int    length = end - start;

            // clang-format doesn't support formatting a range that starts at the end
            // of the file.
            if (start >= text.Length && text.Length > 0)
            {
                start = text.Length - 1;
            }
            string path     = Vsix.GetDocumentParent(view);
            string filePath = Vsix.GetDocumentPath(view);

            RunClangFormatAndApplyReplacements(text, start, length, path, filePath, options, view);
        }
        private void FormatView(IWpfTextView view, OptionPageGrid options)
        {
            if (view == null)
            {
                // We're not in a text view.
                return;
            }

            string filePath = Vsix.GetDocumentPath(view);
            var    path     = Path.GetDirectoryName(filePath);
            string text     = view.TextBuffer.CurrentSnapshot.GetText();

            RunClangFormatAndApplyReplacements(text, 0, text.Length, path, filePath, options, view);
        }
Beispiel #3
0
        private void FormatView(IWpfTextView view, OptionPageGrid options)
        {
            if (view == null)
            {
                // We're not in a text view.
                return;
            }

            string filePath = Vsix.GetDocumentPath(view);
            var    path     = Path.GetDirectoryName(filePath);

            string text = view.TextBuffer.CurrentSnapshot.GetText();

            if (!text.EndsWith(Environment.NewLine))
            {
                view.TextBuffer.Insert(view.TextBuffer.CurrentSnapshot.Length, Environment.NewLine);
                text += Environment.NewLine;
            }
            byte[] buffer = enc.GetBytes(text);
            RunClangFormatAndApplyReplacements(buffer, 0, buffer.Length, path, filePath, options, view);
        }
Beispiel #4
0
        /// <summary>
        /// Runs clang-format on the current selection
        /// </summary>
        private void FormatSelection(OptionPageGrid options)
        {
            IWpfTextView view = Vsix.GetCurrentView();

            if (view == null)
            {
                // We're not in a text view.
                return;
            }
            string text  = view.TextBuffer.CurrentSnapshot.GetText();
            int    start = view.Selection.Start.Position.GetContainingLine().Start.Position;
            int    end   = view.Selection.End.Position.GetContainingLine().End.Position;

            if (start >= end)
            {
                return;
            }

            // convert the utf16 index to multi bytes index
            start = enc.GetByteCount(text.ToCharArray(), 0, start);
            end   = enc.GetByteCount(text.ToCharArray(), 0, end);
            int length = end - start;

            if (length <= 0)
            {
                return;
            }

            // clang-format doesn't support formatting a range that starts at the end
            // of the file.
            if (start >= text.Length && text.Length > 0)
            {
                start = text.Length - 1;
            }
            string path     = Vsix.GetDocumentParent(view);
            string filePath = Vsix.GetDocumentPath(view);

            byte[] buffer = enc.GetBytes(text);
            RunClangFormatAndApplyReplacements(buffer, start, length, path, filePath, options, view);
        }
Beispiel #5
0
        private void OnBeforeSave(object sender, Document document)
        {
            var options = GetUserOptions();

            if (!options.FormatOnSave)
            {
                return;
            }

            if (!FileHasExtension(document.FullName, options.FormatOnSaveFileExtensions))
            {
                return;
            }

            if (!Vsix.IsDocumentDirty(document))
            {
                return;
            }

            var optionsWithNoFallbackStyle = GetUserOptions().Clone();

            optionsWithNoFallbackStyle.FallbackStyle = "none";
            FormatDocument(document, optionsWithNoFallbackStyle);
        }
Beispiel #6
0
 private void FormatDocument(Document document, OptionPageGrid options)
 {
     FormatView(Vsix.GetDocumentView(document), options);
 }
Beispiel #7
0
 /// <summary>
 /// Runs clang-format on the current document
 /// </summary>
 private void FormatDocument(OptionPageGrid options)
 {
     FormatView(Vsix.GetCurrentView(), options);
 }