Giter Club home page Giter Club logo

sharpen's Introduction

Sharpen

Sharpen is a Visual Studio extension that intelligently modernizes your existing C# codebase. It makes your code more expressive, more readable, more robust, and more performant, in one word - sharper.

Twitter Visual Studio Marketplace Version AppVeyor GitHub release License

TL; DR

Sharpen identifies places in your code that will benefit from introducing new C# language features. It helps you to learn new C# features quicker and critically refactor your existing code to modern C#.

Sharpen in action

Benefits at a Glance

Sharpen helps you to:

Faster Learning of New C# Features

Learn new C# features faster by applying them to your existing codebase.

Sharpen points you to places in real-life production code - your production code! - where you can learn and apply modern C#. This is a far cry from simplified and artificial code samples often used to explain new C# features. Sharpen allows you to learn and understand modern C# in the context of your own code.

Sharpen provides you with meaningful recommendations and considerations for modernizing your code. It relates those recommendations and considerations to particular C# versions and their language features. Recommendations and considerations supported at the moment are listed below, sorted by the language version. Each new version of Sharpen brings additional recommendations and considerations.

C# 8.0

C# 8.0 recommendations and considerations

C# 7.1

C# 7.1 recommendations and considerations

C# 7.0

C# 7.0 recommendations and considerations

C# 6.0

C# 6.0 recommendations and considerations

C# 5.0

C# 5.0 recommendations and considerations

C# 3.0

C# 3.0 recommendations and considerations

Modernizing and Refactoring Code

Modernize your C# code by refactoring it via a single mouse click.

Sharpen will have a preview window that shows how the code looks now and how it will look after the proposed change is applied. That way, you will clearly understand the difference and, at the same time, visually grasp what the language feature is about. You will have the option to accept the proposed change and automatically refactor that piece of code.

Preview is not implemented at the moment. Here is a rough mockup of that upcoming functionality.

Sharpen preview window

Refactoring will not be limited to the preview window only. It will be possible on different scales. E.g. in a single place in code, or in a single file, or a whole project, or solution. Sharpen will give you full control over the scope and nature of the refactorings. Also, it will automatically refactor only direct recommendations. Those are considered as always safe to apply.

This mockup shows applying suggested changes for a specific language feature within all files of a solution.

Applying Sharpen's recommendations

Until Sharpen built-in refactorings are implemented, you can use the code fixes that come with Visual Studio for some of the recommendations. They are not available for all Sharpen recommendations, but for some, they are, so make sure to take a look if they might exist.

Refactoring code by using Visual Studio code fixes

High-Quality Documentation on C# Language at your Fingerprints

Learn C# without leaving Visual Studio by using built-in, high-quality documentation.

Sharpen will bring high-quality C# documentation right at your fingerprints. It will provide its own extensive built-in documentation that explains C# language features in-depth. It will also link to the official MSDN documentation and curated high-quality online blog posts and videos that bring additional insights into particular topics.

Sharpen aims to become a one-stop-shop for everything you ever wanted to know about the design and evolution of C#.

In its current version, Sharpen links C# releases and language features to their official MSDN documentation. Extensive built-in documentation will be available in upcoming versions of Sharpen. (To open the documentation in an external browser, press the CTRL key while clicking on the link.)

High-Quality Documentation on C# Language at your Fingerprints

Critical Approach to New C# Features

Understand the potential drawbacks of new C# features before using them.

Sharpen imposes a critical view on C# features. It does that by distinguishing between recommendations and considerations. Recommendations are safe and recommended to apply. They will result in a more expressive, more readable, more robust, or more performant code. Recommendations always start with call-for-action verbs like "use," "apply," or "replace." Considerations, on the other hand, ask you to critically assess a particular language feature's usage in a given context. Considerations usually start with the verb "consider."

Sharpen's recommendations and considerations

At the moment, Sharpen's considerations do not provide any explanations. In the upcoming versions, every consideration will come with an explanation. This explanation, together with additional examples, will provide you with enough information to decide if the language feature should be used in that particular case.

An explanation for a Sharpen consideration

Improving Design and Architecture of a Broader Codebase

Recognize potential improvements and redesign and rearchitecture your code on a broader scope.

Sharpen is equipped with intelligent heuristics that recognizes potential code improvements on a broader scope. For example, the "Consider awaiting equivalent asynchronous method and yielding IAsyncEnumerable" recognizes places where asynchronous streams might be a better design choice than returning an IEnumerable<T>. Accepting such consideration impacts and improves the code on a broader scope. It means rearchitecting and redesigning a larger portion of the codebase rather than simply refactoring an isolated piece of code.

Consider awaiting equivalent asynchronous method and yielding IAsyncEnumerable

Other examples would be considering:

  • to rearchitecture legacy asynchronous programming patterns like, e.g. IAsyncResult pattern, to the Task-based Asynchronous Pattern and async and await.
  • to rearchitecture mutable Data Transfer Objects to C# 9.0 records.

Consistent Usage of C# Features

Use new C# features consistently over your entire codebase.

Future versions of Sharpen will allow you to configure which recommendations are relevant for you and refine the recommendations using recommendation-specific settings. Storing of such configurations will be possible on different levels, e.g., on a project, or on a solution, or on a machine level, to name just a few possibilities. It will be possible to share Sharpen's configuration among programmers working on the same project by checking it into the source code repository.

Note that defining and storing configurations is not implemented at the moment.

Installing Sharpen

You can install Sharpen directly from Visual Studio by using Extensions dialog or you can download it from the Visual Studio Marketplace.

Installing Sharpen from Visual Studio

Running Code Analysis

To run Sharpen analysis on a whole solution, go to the "Tools -> Sharpen -> Analyze Solution".
To run an analysis on the currently edited file, use the "Analyze with Sharpen" context menu option on the file context menu.
To run an analysis on an arbitrary scope, use the "Analyze with Sharpen" context menu option after selecting nodes in the Solution Explorer.

Running Sharpen code analysis

Contributing

Sharpen is a volunteer effort. Covering the whole C# evolution is a humongous task that cannot be carried out by a few people. So pitch in and join the development! :-) Before contributing, please make sure to read the contribution guidelines.

Release Notes

All notable changes to the Sharpen extension are documented in the changelog.

License

Sharpen is licensed under the MIT license.

sharpen's People

Contributors

afrlan avatar ironcev avatar shankyjain7243 avatar tom-englert avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

sharpen's Issues

Consecutive analysis runs fully block Visual Studio

The first analysis run on a solution does not block Visual Studio and the progress bar is displayed. Every consecutive analysis run fully blocks Visual Studio until the analysis is finished. The progress bar is not displayed either.

This issue is originally reported by Robert Kurtanjek during the Endgame for v0.5.0.

[C# 3.0: Implicitly Typed Local Variables] Use var keyword in variable declaration with object creation

C# Suggestion definition

C# Suggestion definition ย 
C# Language Versions 3.0
C# Feature Implicitly Typed Local Variables
C# Suggestion Use var keyword in variable declaration with object creation

Description

The suggestion suggests to use var keyword only if the variable is initialized with object creation (new keyword). In other words, suggestion suggests to use var keyword only if the type is directly obvious from the declaration itself.

Usage Examples

  • int i = new System.Int32();

  • List<int> list = new List<int>(10000);

  • List<int> list = new System.Collections.Generic.List<int>(10000);

  • System.Collections.Generic.List<int> list = new List<int>(10000);

  • MyClass myObject = new MyClass(a, b, c);

  • MyClass myObject = new MyClass { A = a, B = b, C = c};

  • using (StreamReader file = new StreamReader("C:\\myfile.txt")) {...}

Non-usage Examples

When the explicit type and the type in the object creation is not exactly the same the suggestion should not appear.

  • IEnumerable<int> list = new List<int>(10000);

  • long l = new int();

Corner Cases

A type named var in the scope

If there is a type named var in the scope, the suggestion should not appear. Because "If a type named var is in scope, then the var keyword will resolve to that type name and will not be treated as part of an implicitly typed local variable declaration."
For more info see: http://thehumbleprogrammer.com/the-variform-var/
This corner case can be ignored in the first implementation since it is very unlikely to happen.

Multiple implicitly-typed variables cannot be initialized in the same statement

E. g. this is valid:

int a = 1, b = 2, c = 3;

but this is not:

var a = 1, b = 2, c = 3;

This corner case must be supported in the first implementation. This means the suggestion must not appear in a case like this:

  • List<int> first = new List<int>(), second = new List<int>();

References

Acceptance Criteria

  • Smoke tests are written and passing for all:
    • Usage Examples
    • Non-usage Examples
    • Corner Cases

Opening Items in the list causes delay

  • Expanding items in the item list causes lagging.It takes some time to expand in some cases.The number of items were more than some close or more than 1000s (Eg: 6377,13092,857 etc..) .
  • Few times white screen is seen for a while on repeated clicking.
    Once it opens up,the behaviour is fine

image

dependency alert: Visual Studio MPF 15.0 - already installed MPF 16.0

Hi I tried installing into VS2019 but was giving me a dependency alert: Visual Studio MPF 15.0, this is despite the fact I already have Visual Studio MPF 16.0 which I checked in the C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\Common7\IDE\CommonExtensions\Platform\Visual Studio MPF directory.

Is there reason why it wont accept v16.0? Is it hard coded to use v15.0?

Add About Dialog

The About Dialog should roughly look like this:

image

The dialog cannot change its window size. Pick up a size that is not too small and not too big and can fit on a 14" laptop screen ;-)

About Tab

If there is an internet connection fetch the data from GitHub and VS Marketplace and display them. If not or if any error happens during fetching do not display anything.

This might be useful:

Version History Tab

The Version History tab lists the versions on one side and when a version is selected the details for that version are displayed on the left:

image

To render the details use WPF Documents.

The whole content of the Version History tab should be generated on the fly based on the content of the Changelog file.

This might be useful:

Process.Start(url) doesn't work in .Net Core 3.0

While developing About dialog i came to the following issue:

I didn't pay much attention to Target framework when starting on the project and came to an interesting issue. Process.Start(url) in .Net Core 3.0 doesn't work (by design, it seems https://github.com/dotnet/corefx/issues/33714)

Process.Start(url) is used in Sharpen tool when opening links using CTRL key modifier.

Currently, this is not a bug but it could be overlooked if project's target framework changes.

C# 10 and VS 2022 support?

This is great extension, much easier to use than laggy ReSharper! Will there be support for Visual Studio 2022 and C# 10 features?

Provide information to users if a C# suggestion cannot be applied because of e.g. a lower language version used in project

E.g. if the project sets the C# language version to 5.0 in its settings, the suggestions for e.g. version 7.0 will also be provided in the Sharpen Results window. We need a mechanism to provide visual information to users that the particular suggestion cannot be applied because the project settings use a lower version of the compiler.

We need a more detailed UX/UI concept how to do this.

Displaying links for additional information on hoover

Currently, when user hoovers over C# language version, "What is new in C# X.Y?" tip displays to the right of it. It would be an improvement if the tip would be displayed (as well) when hovering over tip displaying location.

The same is true for suggestions and "Learn more..." tip.

Sign the VSIX package

File on the Microsoft VS Store doesn't include VSIX file and the packaged extension you can get in VS is not signed.

C# 9.0?

Hey man, great extension.
Do you have any plans to update it to C# 9?

False positives in c# 8 nullable reference suggestions

I'm seeing a lot of C# 8 Nullable Reference Type issues.

Issue 1

These issues remind me to turn on nullable context, which is already enabled project wide in the csproj.

Issue 2

See Example 1 for a constructor issue, declaring a property as nullable in an API indicates you intend for the consumer to pass nulls. In this case we don't (and throw exceptions).

Issue 3

See example 2, There's no way to declare a var as nullable and the target of the foreach is non-null.

Issue 4

See example 3, There's no way to declare a var as nullable.

Code snippets are from OAT which triggers many of the same types of issues: https://github.com/microsoft/OAT

For context, I do have nullable reference types enabled project wide.

Example 1

This code triggers "Enable Nullable Context and declare constructor parameters as nullable. I explicitly don't want nulls passed in, but am already handling nulls. Setting these parameters to be nullable would indicate to consumers that they may expect reasonable behavior if they pass in nulls. This is explicitly an API you may not pass nulls to.

public Violation(string description, Rule rule, Clause[] clauses)
{
    this.description = description ?? throw new ArgumentNullException(nameof(description), $"{nameof(description)} may not be null");
    this.rule = rule ?? throw new ArgumentNullException(nameof(rule), $"{nameof(rule)} may not be null");
    this.clauses = clauses ?? throw new ArgumentNullException(nameof(clauses), $"{nameof(clauses)} may not be null");
}

Example 2

This is a false positive. The target is both not null and there's no way to declare a "var" as nullable.
NullableWarning

Example 3

This is another false positive. There's no way to declare a var as nullable. It's fine that this is may be null here.

public bool Applies(Rule rule, object? state1 = null, object? state2 = null)
{
    if (rule != null)
    {
        var sample = state1 is null ? state2 : state1;

NullableWarning2

Adds Sharpen icon to the Sharpen menu and Sharpen Results window

Acceptance Criteria

  • Create icon (#).
  • Add icon to Sharpen's menu on the Tools menu.
  • Add icon to Sharpen Results window:
    • Visible on View -> Other Windows -> Sharpen Results.
    • Visible when switching open documents and windows with Ctrl + Tab.
  • It could be that the icon will be visible on Tools -> Sharpen -> Sharpen Results. This we don't want.

Resources

Decide on how to support different versions of Roslyn within a single VS extension

The issue is explained in detail in: https://github.com/sharpenrocks/Sharpen/tree/master/lab/RoslynDependenciesAtBuildAndRuntime

Here is a short summary:

  • At compile-time we compile Sharpen Engine with a certain version of Roslyn. At the moment, this is a very old v2.4.0.0.
    This way we do not have access to the new Roslyn API like e.g. IOperation but we assume that most of the analyzers can be comfortably written using this old Roslyn version.
  • At runtime, VS will bind Sharpen Engine to the version of Roslyn coming with that particular instance of VS. This can be anything between v2.4.0.0 and the newest version of Roslyn.
  • Here we face two issues:
    • If there are breaking changes in the API between the v2.4.0.0 and the actual runtime version, Sharpen will crash.
    • If the newer version supports some language constructs required for the proper analysis, we cannot use them.
      E.g. , IsNullableContextEnabled(...) method can simply return false in VS2017 but must do some actual work in VS2019. This is the reason for the issue #38.

So, we want to achieve the following:

  • Have just a single VSIX that supports VS2017+ and different versions of Roslyn.
  • Be able to develop the analyzers by using the newer language feature when available at runtime, and using the appropriate fallbacks when they are not available. E.g. IsNullableContextEnabled(...) method has different implementations for Roslyn version older than v3.0.0.0 (C# 8.0) and newer or equal then v3.0.0.0.

Proposed solution and its alterantives are described in detail here: https://github.com/sharpenrocks/Sharpen/tree/master/lab/RoslynDependenciesAtBuildAndRuntime

Here is the summary of the proposed solution:

  • Create an abstract interface of the Sharpen.Engine. Put it in the Sharpen.Engine.Abstractions and compile against v2.4.0.0 of Roslyn. This interface will require only the very basic Roslyn types like SyntaxTree and Workspace.
  • Create a shared project called Sharpen.Engine. Put all the common code here. Implement the Sharpen.Engine.Abstractions here.
  • Create separate project for each supported version of Roslyn, reference the shared Sharpen Engine project, and compile it against the specific version of Roslyn. Add Roslyn version-specific code to those projects.
  • In the VS Extension, link to Engine DLLs produced by each of the projects and copy them to the VSIX as files.
  • In the VS Extension, load the closest of those DLLs at runtime based on the exact Roslyn version used at runtime.

The proof-of-concept of the approach can be seen here: https://github.com/sharpenrocks/Sharpen/tree/master/lab/RoslynDependenciesAtBuildAndRuntime/solution-04

Questions:

  • Is this the final way to go?
  • Are there any better alternatives?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.