Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error format doc comment "Ctrl+K, Ctrl+D" formats incorrect comment when the comment in the same line of bracket {, },(, #68760

Open
MauNguyenVan opened this issue Jun 24, 2023 · 5 comments
Assignees
Labels
Milestone

Comments

@MauNguyenVan
Copy link

MauNguyenVan commented Jun 24, 2023

Version Used: VisualStudio 2022 17.6.4

Steps to Reproduce:
Paste this code in VisualStudio, then press Format document (Ctrl+K, Ctrl+D).

namespace ConsoleApp.Classes
{/// <summary>
    /// Error `format doc comment "Ctrl+K, Ctrl+D"` do not format correct comment when the comment in the same line of bracket `{` or `}`
    ///
    /// </summary>
    internal class Class1
    {
        public void Foo()
        {               ///do not format correct comment when the comment in the same line of bracket
            ///Also do not auto add '///' when user enter new line when curent line comnent in the same line of bracket
            ///Err

            DoAction(               /*apple*/
                "banana",
                "orange",
                "coconut",
                "grape",
                "mango");
            while (true)
            {               // do som thing
                Console.WriteLine("");
                break;
            }
        }           /// <summary>
        /// Error `format doc comment "Ctrl+K, Ctrl+D"` do not format correct comment when the comment in the same line of bracket `{` or `}`\
        ///
        /// </summary>
        /// <param name="paras"></param>

        public void DoAction(params string[] paras)
        {/*
              *  Error `format doc comment "Ctrl+K, Ctrl+D"` do not format correct comment when the comment in the same line of bracket `{` or `}`
              */
            Console.WriteLine("");
        }
    }
}

Expected Behavior:

namespace ConsoleApp.Classes
{
    /// <summary>
    /// Error `format doc comment "Ctrl+K, Ctrl+D"` do not format correct comment when the comment in the same line of bracket `{` or `}`
    ///
    /// </summary>
    internal class Class1
    {
        public void Foo()
        {
            ///do not format correct comment when the comment in the same line of bracket
            ///Also do not auto add '///' when user enter new line when curent line comnent in the same line of bracket
            ///Err
            DoAction(/*apple*/
                "banana",
                "orange",
                "coconut",
                "grape",
                "mango");
            while (true)
            {
                // do som thing
                Console.WriteLine("");
                break;
            }
        }

        /// <summary>
        /// Error `format doc comment "Ctrl+K, Ctrl+D"` do not format correct comment when the comment in the same line of bracket `{` or `}`\
        ///
        /// </summary>
        /// <param name="paras"></param>
        public void DoAction(params string[] paras)
        {
            /*
            *  Error `format doc comment "Ctrl+K, Ctrl+D"` do not format correct comment when the comment in the same line of bracket `{` or `}`
            */
            Console.WriteLine("");
        }
    }
}

Actual Behavior:

namespace ConsoleApp.Classes
{/// <summary>
 /// Error `format doc comment "Ctrl+K, Ctrl+D"` do not format correct comment when the comment in the same line of bracket `{` or `}`
 ///
 /// </summary>
    internal class Class1
    {
        public void Foo()
        {               ///do not format correct comment when the comment in the same line of bracket
                        ///Also do not auto add '///' when user enter new line when curent line comnent in the same line of bracket
                        ///Err

            DoAction(               /*apple*/
                "banana",
                "orange",
                "coconut",
                "grape",
                "mango");
            while (true)
            {               // do som thing
                Console.WriteLine("");
                break;
            }
        }           /// <summary>
                    /// Error `format doc comment "Ctrl+K, Ctrl+D"` do not format correct comment when the comment in the same line of bracket `{` or `}`\
                    ///
                    /// </summary>
                    /// <param name="paras"></param>

        public void DoAction(params string[] paras)
        {/*
              *  Error `format doc comment "Ctrl+K, Ctrl+D"` do not format correct comment when the comment in the same line of bracket `{` or `}`
              */
            Console.WriteLine("");
        }
    }
}

Video demo:

VS2022_17_6_4-ErrorFormatDoc

@dotnet-issue-labeler dotnet-issue-labeler bot added Area-IDE untriaged Issues and PRs which have not yet been triaged by a lead labels Jun 24, 2023
@CyrusNajmabadi
Copy link
Member

Doc comments can only be leading trivia. So it's likely the tree is not in a good state for the formatter to work with. Would likely need special casing to handle this.

@arunchndr arunchndr removed the untriaged Issues and PRs which have not yet been triaged by a lead label Aug 3, 2023
@arunchndr arunchndr added this to the Backlog milestone Aug 3, 2023
@sharwell
Copy link
Member

sharwell commented Aug 7, 2023

@CyrusNajmabadi The tree would always be in a consistent state for the formatter after a paste operation. It can only enter a bad state through the SyntaxFactory APIs, but the paste operation would involve direct text manipulation and go back through the parser.

@CyrusNajmabadi
Copy link
Member

@CyrusNajmabadi The tree would always be in a consistent state for the formatter after a paste operation.

I didn't mean that it's an invalidly constructed tree. Just a tree that is not in a good state for the formatter. For example, the formatter wound would need to deal with the very weird case of doc comments spread over trailing and leading trivia.

@sharwell
Copy link
Member

sharwell commented Aug 7, 2023

For example, the formatter wound would need to deal with the very weird case of doc comments spread over trailing and leading trivia.

All of the documentation comment is still parsed as leading trivia. The line with } does not include a trailing newline trivia.

@CyrusNajmabadi
Copy link
Member

OH, that's fascinating and even more bizarre :) I guess then the formatter could see these tokens without any appropraite end of line and add such a thing. I would be ok with that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants