{"id":18515,"date":"2018-07-17T09:00:10","date_gmt":"2018-07-17T16:00:10","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/visualstudio\/?p=18515"},"modified":"2019-02-14T15:23:39","modified_gmt":"2019-02-14T23:23:39","slug":"visual-studio-intellicode-inferring-coding-conventions-for-consistent-code","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/visualstudio\/visual-studio-intellicode-inferring-coding-conventions-for-consistent-code\/","title":{"rendered":"Visual Studio IntelliCode now infers coding conventions for consistent code"},"content":{"rendered":"<p>Following the <a href=\"https:\/\/go.microsoft.com\/fwlink\/?linkid=872708\">initial announcement<\/a> of Visual Studio IntelliCode at Build 2018, we\u2019re excited to report that the <a href=\"https:\/\/go.microsoft.com\/fwlink\/?linkid=872707\">Visual Studio IntelliCode Extension<\/a> has been updated to enable coding convention inference for C#, to help you and your team achieve more readable and consistent code. If you&#8217;re new to the Intellicode extension, it already provides AI-assisted IntelliSense suggestions, which you can read about <a href=\"https:\/\/go.microsoft.com\/fwlink\/?linkid=872708\">in the initial announcement<\/a>. If you already have the extension installed, you may have automatically received this update. If not, you can get started now by <a href=\"https:\/\/go.microsoft.com\/fwlink\/?linkid=872707\">downloading the extension<\/a>.<\/p>\n<h2>Inferring code style and formatting conventions for your codebase<\/h2>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2018\/07\/Inferring-code-style-and-formatting-conventions-for-your-codebase.png\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" class=\"aligncenter\" title=\"Inferring code style and formatting conventions for your codebase\" src=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2018\/07\/Inferring-code-style-and-formatting-conventions-for-your-codebase.png\" alt=\"Inferring code style and formatting conventions for your codebase\" \/><\/a><\/p>\n<p>Consistency is important for code maintainability and readability; in fact, a recent <a href=\"https:\/\/www.microsoft.com\/en-us\/research\/wp-content\/uploads\/2016\/05\/MS-Code-Review-Tech-Report-MSR-TR-2016-27.pdf\">Microsoft Research study<\/a> in this area showed that 18 percent of pull request review comments were related to coding conventions, styles,\u00a0and naming issues.<\/p>\n<p>With this update, the <strong>IntelliCode<\/strong> extension can now generate an .<a href=\"https:\/\/docs.microsoft.com\/en-us\/visualstudio\/ide\/create-portable-custom-editor-options\">editorconfig file<\/a>\u00a0on-demand that best matches the coding conventions used in your codebase; no more long discussions with your team about the best convention to use! Once generated, this file will help you maintain consistency in your team\u2019s codebase. Fixing up formatting issues is quick and easy with the Quick Action lightbulb menu (Ctrl+. or Alt+Enter) or with the new configurable Format Document (Ctrl+K, D) in <a href=\"https:\/\/visualstudio.microsoft.com\/vs\/preview\/\">Visual Studio 2017 version 15.8<\/a> Preview 3 and higher.<\/p>\n<h2>How to use .editorconfig inference<\/h2>\n<p>IntelliCode creates the inferred .<a href=\"https:\/\/docs.microsoft.com\/en-us\/visualstudio\/ide\/create-portable-custom-editor-options\">editorconfig file<\/a> by scanning the code files below your chosen location to create the best matching set of conventions. Once it\u2019s there, you will see any coding convention violations related to style show up in the error list as Messages as illustrated below:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2018\/07\/Coding-convention-violations-related-to-style-show-up-in-the-error-list-as-Messages.png\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" class=\"aligncenter\" title=\"Coding convention violations related to style show up in the error list as Messages\" src=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2018\/07\/Coding-convention-violations-related-to-style-show-up-in-the-error-list-as-Messages.png\" alt=\"Coding convention violations related to style show up in the error list as Messages\" width=\"699\" height=\"249\" \/>&#8220;\/&gt;<\/a><\/p>\n<p>The style violations are set up to default to \u201cMessage\u201d severity \u2013 this is shown by the \u201c:suggestion\u201d suffix on each rule line in the .editorconfig file .You can tweak the file if you want a different level of enforcement, like warning or error.\u00a0 Note that formatting violations do not show up in the Error List, \u00a0but can still be fixed easily \u2013 see our \u201chow to\u201d below.<\/p>\n<p>You can invoke EditorConfig generation via the Solution Explorer. Simply right-click on the solution, project, or folder level folder where you want the conventions to apply, select \u201cadd\u201d, and then \u201cEditorConfig file (IntelliCode)\u201d. For projects you can also select \u201cadd\u201d then \u201cnew item\u201d then \u201cEditorConfig file (IntelliCode)\u201d. Note that EditorConfig files apply enforcement hierarchically to all files below it in the filesystem \u201ccone\u201d. For instance, if you want a set of conventions that apply to your whole solution, place your .editorconfig in the solution directory. If you have subfolders or projects that have different conventions, you can create a new EditorConfig for those subfolders\/projects to override the conventions from the higher level.<\/p>\n<h2>How to fix up your code to conform to the new conventions<\/h2>\n<p>There are a couple of ways to clean up your code to conform to the new conventions, depending on your goals.<\/p>\n<ol>\n<li><b>Using Quick Actions Lightbulb Menu (Ctrl+. or Alt+Enter)<\/b>\nFor style issues, lightbulbs will appear (along with error list entries), to allow you to auto-fix any inconsistencies. You can choose the scope of the fix in the lightbulb \u2013 instance, document, project or solution.<\/li>\n<\/ol>\n<ol start=\"2\">\n<li><b>Using Format Document (Ctrl+K,D)<\/b>\nFor both format and style issues, if you are using Visual Studio 2017 15.8 Preview 3 or higher, you\u2019ll be able to use the extended \u201cFormat Document\u201d command to perform additional code cleanup for the current document. This will fix up the code in that document to conform to the conventions you just created, allowing you to adopt a \u201cclean up as you touch each file\u201d approach to getting consistent. Note that this additional capability in format document only covers a fixed set of issues, which you can change in <strong>Tools &gt; Options &gt; Text Editor &gt; C# &gt; Code Style &gt; Formatting &gt; General &gt; Format Document Settings (Experiment)<\/strong>:<a href=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2018\/07\/Format-Document-Options-Extension-For-Conventions.png\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" class=\"aligncenter\" title=\"Format Document Options Extension For Conventions\" src=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2018\/07\/Format-Document-Options-Extension-For-Conventions.png\" alt=\"Format Document Options Extension For Conventions\" width=\"361\" height=\"362\" \/><\/a><\/li>\n<\/ol>\n<p>Notice that the default settings do not fix some style conventions. You can opt in to these via tools options (e.g. Apply implicit\/explicit type preferences will run style rules related to the use of var in your code).<\/p>\n<h2>Things to be aware of when using EditorConfig inference<\/h2>\n<p>There are a couple of known issues to be aware of when using EditorConfig inference:<\/p>\n<ol>\n<li><b>You need to close and reopen open files to see changes to the conventions apply in 15.8 Preview 2 and earlier<\/b>\nIn Visual Studio 2017 versions prior to 15.8 Preview 3, you will need to close and re-open all open documents to see the conventions in the EditorConfig file you create take effect. This is fixed in the 15.8 Preview 3 release.<\/li>\n<\/ol>\n<ol start=\"2\">\n<li><b>Creating through the File-New Item dialog adds a \u201c1\u201d To the filename<\/b>\nIf you use the File-New Item dialog route to create your EditorConfig, in Visual Studio versions prior to 15.8 Preview 4, you\u2019ll need to manually remove the superfluous \u201c1\u201d Prefixing the EditorConfig file name in the dialog. This is fixed in the 15.8 Preview 4 release.<\/li>\n<\/ol>\n<p>Thanks for all your interest in IntelliCode. We\u2019re excited to hear your feedback and improve IntelliCode as we make more progress. Use the extension\u2019s <a href=\"https:\/\/marketplace.visualstudio.com\/items?itemName=VisualStudioExptTeam.VSIntelliCode#review-details\">review page<\/a> to make suggestions; if you find any problems with the extension, please use the built-in Visual Studio <a href=\"https:\/\/docs.microsoft.com\/en-us\/visualstudio\/ide\/how-to-report-a-problem-with-visual-studio-2017\">\u201cReport a Problem\u201d<\/a> option, and mention <strong>IntelliCode<\/strong> in your report \u2013 reporting this way helps us gather helpful information to resolve the issue.<\/p>\n<h2>Get Involved<\/h2>\n<p>As we expand capabilities to more scenarios and other languages, we\u2019ll announce a limited preview of <strong>IntelliCode<\/strong>.<\/p>\n<p><a href=\"https:\/\/go.microsoft.com\/fwlink\/?linkid=872706\">Sign up<\/a> to become an insider to keep up with the project and join the waitlist for the private preview.<\/p>\n<p>Thanks!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn how to use the Visual Studio IntelliCode Extension to enable coding convention inference for C#, to help you and your team achieve more readable and consistent code.<\/p>\n","protected":false},"author":587,"featured_media":18818,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[155],"tags":[1383,467,156],"class_list":["post-18515","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-visual-studio","tag-c","tag-intellicode","tag-visual-studio-2017"],"acf":[],"blog_post_summary":"<p>Learn how to use the Visual Studio IntelliCode Extension to enable coding convention inference for C#, to help you and your team achieve more readable and consistent code.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/posts\/18515","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/users\/587"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/comments?post=18515"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/posts\/18515\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/media\/18818"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/media?parent=18515"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/categories?post=18515"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/tags?post=18515"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}