Example #1
0
        private int GetHeaderLength(TextDocument textDocument, bool skipUsings)
        {
            var headerBlock = ReadTextBlock(textDocument);
            var language    = textDocument.GetCodeLanguage();

            return(FileHeaderHelper.GetHeaderLength(language, headerBlock, skipUsings));
        }
Example #2
0
        /// <summary>
        /// Formats the comment.
        /// </summary>
        public TextPoint Format()
        {
            if (!IsValid)
            {
                throw new InvalidOperationException("Cannot format comment, the comment is not valid.");
            }

            var originalText = _startPoint.GetText(_endPoint);
            var matches      = _commentLineRegex.Matches(originalText).OfType <Match>().ToArray();

            var commentOptions = new CommentOptions
            {
                Prefix = matches.First(m => m.Success).Groups["prefix"].Value ?? string.Empty,
                Regex  = CodeCommentHelper.GetCommentRegex(_document.GetCodeLanguage(), false)
            };

            // Concatenate the comment lines without comment prefixes and see if the resulting bit
            // can be parsed as XML.
            ICommentLine line        = null;
            var          lineTexts   = matches.Select(m => m.Groups["line"].Value).ToArray();
            var          commentText = string.Join(Environment.NewLine, lineTexts);

            if (commentText.Contains('<'))
            {
                try
                {
                    var xml = XElement.Parse($"<doc>{commentText}</doc>");
                    line = new CommentLineXml(xml);
                }
                catch (System.Xml.XmlException)
                {
                    // If XML cannot be parsed, comment will be handled as a normal text comment.
                }
            }

            if (line == null)
            {
                line = new CommentLine(commentText);
            }

            var formatter = new CommentFormatter(
                line,
                _formatterOptions,
                commentOptions);

            if (!formatter.Equals(originalText))
            {
                var cursor = StartPoint.CreateEditPoint();
                cursor.Delete(EndPoint);
                cursor.Insert(formatter.ToString());
                _endPoint = cursor.CreateEditPoint();
            }

            return(EndPoint);
        }
Example #3
0
        internal static HeaderPosition GetFileHeaderPositionFromSettings(TextDocument textDocument)
        {
            switch (textDocument.GetCodeLanguage())
            {
            case CodeLanguage.CSharp:
                return((HeaderPosition)Settings.Default.Cleaning_UpdateFileHeader_HeaderPosition);

            default:
                return(HeaderPosition.DocumentStart);
            }
        }
Example #4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="CodeComment" /> class.
        /// </summary>
        public CodeComment(TextPoint point, FormatterOptions options)
        {
            if (point == null)
            {
                throw new ArgumentNullException(nameof(point));
            }

            _document         = point.Parent;
            _commentLineRegex = CodeCommentHelper.GetCommentRegex(_document.GetCodeLanguage());
            _formatterOptions = options;

            Expand(point);
        }
Example #5
0
        /// <summary>
        /// Initializes a new instance of the <see cref="CodeComment" /> class.
        /// </summary>
        public CodeComment(TextPoint point, int tabSize)
        {
            if (point == null)
            {
                throw new ArgumentNullException(nameof(point));
            }

            _document         = point.Parent;
            _commentLineRegex = CodeCommentHelper.GetCommentRegex(_document.GetCodeLanguage());
            _tabSize          = tabSize;

            Expand(point);
        }
Example #6
0
        /// <summary>
        /// Initializes a new instance of the <see cref="CodeComment" /> class.
        /// </summary>
        public CodeComment(TextPoint point, int tabSize)
        {
            if (point == null)
            {
                throw new ArgumentNullException(nameof(point));
            }

            _document = point.Parent;
            _commentLineRegex = CodeCommentHelper.GetCommentRegex(_document.GetCodeLanguage());
            _tabSize = tabSize;

            Expand(point);
        }
        private void ReplaceFileHeader(TextDocument textDocument, string settingsFileHeader)
        {
            var headerBlock         = ReadHeaderBlock(textDocument);
            var currentHeaderLength = FileHeaderHelper.GetHeaderLength(textDocument.GetCodeLanguage(), headerBlock);
            var currentHeader       = headerBlock.Substring(0, currentHeaderLength + 1) + Environment.NewLine;
            var newHeader           = settingsFileHeader + Environment.NewLine;

            if (string.Equals(currentHeader, newHeader))
            {
                return;
            }

            var docStart = textDocument.StartPoint.CreateEditPoint();

            docStart.ReplaceText(currentHeaderLength, newHeader, (int)vsEPReplaceTextOptions.vsEPReplaceTextKeepMarkers);
        }
        /// <summary>
        /// Gets the file header from settings based on the language of the specified document.
        /// </summary>
        /// <param name="textDocument">The text document.</param>
        /// <returns>A file header from settings.</returns>
        private static string GetFileHeaderFromSettings(TextDocument textDocument)
        {
            switch (textDocument.GetCodeLanguage())
            {
            case CodeLanguage.CPlusPlus: return(Settings.Default.Cleaning_UpdateFileHeaderCPlusPlus);

            case CodeLanguage.CSharp: return(Settings.Default.Cleaning_UpdateFileHeaderCSharp);

            case CodeLanguage.CSS: return(Settings.Default.Cleaning_UpdateFileHeaderCSS);

            case CodeLanguage.FSharp: return(Settings.Default.Cleaning_UpdateFileHeaderFSharp);

            case CodeLanguage.HTML: return(Settings.Default.Cleaning_UpdateFileHeaderHTML);

            case CodeLanguage.JavaScript: return(Settings.Default.Cleaning_UpdateFileHeaderJavaScript);

            case CodeLanguage.JSON: return(Settings.Default.Cleaning_UpdateFileHeaderJSON);

            case CodeLanguage.LESS: return(Settings.Default.Cleaning_UpdateFileHeaderLESS);

            case CodeLanguage.PHP: return(Settings.Default.Cleaning_UpdateFileHeaderPHP);

            case CodeLanguage.PowerShell: return(Settings.Default.Cleaning_UpdateFileHeaderPowerShell);

            case CodeLanguage.R: return(Settings.Default.Cleaning_UpdateFileHeaderR);

            case CodeLanguage.SCSS: return(Settings.Default.Cleaning_UpdateFileHeaderSCSS);

            case CodeLanguage.TypeScript: return(Settings.Default.Cleaning_UpdateFileHeaderTypeScript);

            case CodeLanguage.VisualBasic: return(Settings.Default.Cleaning_UpdateFileHeaderVB);

            case CodeLanguage.XAML: return(Settings.Default.Cleaning_UpdateFileHeaderXAML);

            case CodeLanguage.XML: return(Settings.Default.Cleaning_UpdateFileHeaderXML);

            default: return(null);
            }
        }
Example #9
0
        /// <summary>
        /// Removes the trailing newline from the end of the specified text document.
        /// </summary>
        /// <param name="textDocument">The text document to cleanup.</param>
        internal void RemoveEOFTrailingNewLine(TextDocument textDocument)
        {
            if (!Settings.Default.Cleaning_RemoveEndOfFileTrailingNewLine)
            {
                return;
            }

            EditPoint cursor = textDocument.EndPoint.CreateEditPoint();

            if (cursor.AtEndOfDocument && cursor.AtStartOfLine && cursor.AtEndOfLine)
            {
                // Make an exception for C++ resource files to work-around known EOF issue: http://connect.microsoft.com/VisualStudio/feedback/details/173903/resource-compiler-returns-a-rc1004-unexpected-eof-found-error#details
                if (textDocument.GetCodeLanguage() == CodeLanguage.CPlusPlus &&
                    (textDocument.Parent.FullName.EndsWith(".h") || textDocument.Parent.FullName.EndsWith(".rc2")))
                {
                    return;
                }

                var backCursor = cursor.CreateEditPoint();
                backCursor.CharLeft();
                backCursor.Delete(cursor);
            }
        }
Example #10
0
 /// <summary>
 /// Gets the file header from settings based on the language of the specified document.
 /// </summary>
 /// <param name="textDocument">The text document.</param>
 /// <returns>A file header from settings.</returns>
 private static string GetFileHeaderFromSettings(TextDocument textDocument)
 {
     switch (textDocument.GetCodeLanguage())
     {
         case CodeLanguage.CPlusPlus: return Settings.Default.Cleaning_UpdateFileHeaderCPlusPlus;
         case CodeLanguage.CSharp: return Settings.Default.Cleaning_UpdateFileHeaderCSharp;
         case CodeLanguage.CSS: return Settings.Default.Cleaning_UpdateFileHeaderCSS;
         case CodeLanguage.FSharp: return Settings.Default.Cleaning_UpdateFileHeaderFSharp;
         case CodeLanguage.HTML: return Settings.Default.Cleaning_UpdateFileHeaderHTML;
         case CodeLanguage.JavaScript: return Settings.Default.Cleaning_UpdateFileHeaderJavaScript;
         case CodeLanguage.JSON: return Settings.Default.Cleaning_UpdateFileHeaderJSON;
         case CodeLanguage.LESS: return Settings.Default.Cleaning_UpdateFileHeaderLESS;
         case CodeLanguage.PHP: return Settings.Default.Cleaning_UpdateFileHeaderPHP;
         case CodeLanguage.PowerShell: return Settings.Default.Cleaning_UpdateFileHeaderPowerShell;
         case CodeLanguage.R: return Settings.Default.Cleaning_UpdateFileHeaderR;
         case CodeLanguage.SCSS: return Settings.Default.Cleaning_UpdateFileHeaderSCSS;
         case CodeLanguage.TypeScript: return Settings.Default.Cleaning_UpdateFileHeaderTypeScript;
         case CodeLanguage.VisualBasic: return Settings.Default.Cleaning_UpdateFileHeaderVB;
         case CodeLanguage.XAML: return Settings.Default.Cleaning_UpdateFileHeaderXAML;
         case CodeLanguage.XML: return Settings.Default.Cleaning_UpdateFileHeaderXML;
         default: return null;
     }
 }
Example #11
0
 /// <summary>
 /// Get the comment prefix (regex) for the given document's language.
 /// </summary>
 /// <param name="document">The document.</param>
 /// <returns>The comment prefix regex, without trailing spaces.</returns>
 internal static string GetCommentPrefix(TextDocument document)
 {
     return GetCommentPrefixForLanguage(document.GetCodeLanguage());
 }
Example #12
0
 /// <summary>
 /// Get the comment prefix (regex) for the given document's language.
 /// </summary>
 /// <param name="document">The document.</param>
 /// <returns>The comment prefix regex, without trailing spaces.</returns>
 internal static string GetCommentPrefix(TextDocument document)
 {
     return(GetCommentPrefixForLanguage(document.GetCodeLanguage()));
 }
Example #13
0
        /// <summary>
        /// Removes the trailing newline from the end of the specified text document.
        /// </summary>
        /// <param name="textDocument">The text document to cleanup.</param>
        internal void RemoveEOFTrailingNewLine(TextDocument textDocument)
        {
            if (!Settings.Default.Cleaning_RemoveEndOfFileTrailingNewLine) return;

            EditPoint cursor = textDocument.EndPoint.CreateEditPoint();

            if (cursor.AtEndOfDocument && cursor.AtStartOfLine && cursor.AtEndOfLine)
            {
                // Make an exception for C++ resource files to work-around known EOF issue: http://connect.microsoft.com/VisualStudio/feedback/details/173903/resource-compiler-returns-a-rc1004-unexpected-eof-found-error#details
                if (textDocument.GetCodeLanguage() == CodeLanguage.CPlusPlus &&
                    (textDocument.Parent.FullName.EndsWith(".h") || textDocument.Parent.FullName.EndsWith(".rc2")))
                {
                    return;
                }

                var backCursor = cursor.CreateEditPoint();
                backCursor.CharLeft();
                backCursor.Delete(cursor);
            }
        }