News

Visual Studio Dev Requests Code Copying with Proper Indentation, Mads K. Delivers

Microsoft's Visual Studio extension guru Mads Kristensen is at it again, taking a developer request and turning it into a handy tool, which he has put out there for experimentation and feedback.

This time he addressed an August 2020 post in Microsoft's Developer Community site for bug reports, feature requests and so on. Under the title "Easy copy code without indentation," the item from Thomas Ardal reads (unedited):

If often need to copy code from within Visual Studio and paste it into a blog post, some documentation, etc. When copying from indented code files (like a method in a class in a C# file) you get the spaces (or tabs) from the beginning of each line copied as well. Cleaning up these extra tabs after paste is boring work. To avoid doing that, I often mark the code I want copied and shift+tab the code until it is left aligned in the file and make a copy. Then ctrl+z to snap it back into place. It would be nice with a special shortcut, button, or something else that copies code so that it looks good pasted.

Yesterday (Oct. 25), Kristensen announced his solution, the Copy Nice extension in the Visual Studio Marketplace.

Copy Nice
[Click on image for larger view.] Copy Nice (source: Microsoft).

"You want to share some code you've written with a colleague, so you select it in the editor and hit Ctrl+C to copy it," he said in a blog post. "As you paste it in Outlook/Slack/Teams, you realize that the indentation levels are inconsistent due to your original selection. You must now either go back to Visual Studio and do a box selection and copy that, or manually fix the indentation issue in Outlook. You know this, yet you make this mistake almost every time. Sounds familiar?"

As an experiment, he worked up Copy Nice to remove unwanted indentation when code is copied from the editor, which required dealing with the thorny question of how the tool can be used for just unwanted indentations as opposed to wanted indentations. In addition to a code copy including indentation that needs to be manually stripped out later, another type of problem typically occurs when a developer neglects to include leading indentation when selecting text, instead pointing the cursor to the start of the text instead of capturing that indentation character to the left. That fails to capture indentation that may be wanted.

Kristensen indicated that annoying code copying indentation problems are commonplace, noting that multiple developers have requested a feature to address them, in addition to the August 2020 request from Ardal. Indeed, his post has garnered 68 votes and 13 comments.

Code Copying Indentation Hassle in Animated Action
[Click on image for larger, animated GiF view.] Code Copying Indentation Hassle in Animated Action (source: Thomas Ardal).

The tool itself, released on Oct. 12 and updated yesterday, was nearing 440 installs at the time of this writing and earned an average 4.8 rating (0-5 scale) from six developers who reviewed it. One said: "Simple, yet effective. Works as advertised. This is especially useful when doing code reviews or pasting code into markdown format or in MS Teams. This fixes that super annoying removing formatted space. Lovely!"

Kristensen, who has authored some 185 extensions in the VS Marketplace, is now seeking more feedback on Copy Nice.

"If this feature is interesting to you, please install the Copy Nice extension and take it for a ride. Make sure to share any ideas and bugs on the GitHub issue tracker, and feel free to send pull requests too. The feature request ticket is a suitable place to share your comments and thoughts, so make sure to vote and comment there as well."

Kristensen's new Copy Nice tool essentially duplicates functionality in his Tweaks extension, he indicated.

About the Author

David Ramel is an editor and writer at Converge 360.

comments powered by Disqus

Featured

  • Compare New GitHub Copilot Free Plan for Visual Studio/VS Code to Paid Plans

    The free plan restricts the number of completions, chat requests and access to AI models, being suitable for occasional users and small projects.

  • Diving Deep into .NET MAUI

    Ever since someone figured out that fiddling bits results in source code, developers have sought one codebase for all types of apps on all platforms, with Microsoft's latest attempt to further that effort being .NET MAUI.

  • Copilot AI Boosts Abound in New VS Code v1.96

    Microsoft improved on its new "Copilot Edit" functionality in the latest release of Visual Studio Code, v1.96, its open-source based code editor that has become the most popular in the world according to many surveys.

  • AdaBoost Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the AdaBoost.R2 algorithm for regression problems (where the goal is to predict a single numeric value). The implementation follows the original source research paper closely, so you can use it as a guide for customization for specific scenarios.

  • Versioning and Documenting ASP.NET Core Services

    Building an API with ASP.NET Core is only half the job. If your API is going to live more than one release cycle, you're going to need to version it. If you have other people building clients for it, you're going to need to document it.

Subscribe on YouTube