dnSpyEx / dnSpy

Unofficial revival of the well known .NET debugger and assembly editor, dnSpy
GNU General Public License v3.0
7.02k stars 462 forks source link

Prettify decompiled code #311

Open Mrgaton opened 7 months ago

Mrgaton commented 7 months ago

Problem Description

Prettify code from this

image

to something more like this

image

Proposal

It would be great to prettify some ifs if the bool definition isn't too big

also would be great if the variables could get more family-friendly names like

from this

    bool flag = DocumentSettings.DefaultKeyLength != null;
            if (flag)
            {
                this.KeyLength = DocumentSettings.DefaultKeyLength;
            }

to this

            bool defaultKeyLength = DocumentSettings.DefaultKeyLength != null;

            if (defaultKeyLength)
            {
                this.KeyLength = DocumentSettings.DefaultKeyLength;
            }

Alternatives

No response

Additional Context

No response

Mrgaton commented 7 months ago

for example here is checking if secret is null so would be nice if the variable could be named something like the

from

        public async Task<bool> Remove(string secret)
        {
            bool flag = secret == null;
            if (flag)
            {
                throw new ArgumentNullException("secret");
            }
            return await JSPasteClient.Remove(this._key, secret);
        }

to

        public async Task<bool> Remove(string secret)
        {
            bool secretNull = secret == null;

            if (secretNull)
            {
                throw new ArgumentNullException("secret");
            }

            return await JSPasteClient.Remove(this._key, secret);
        }
Mrgaton commented 7 months ago

also put some new lines to some code like for ifs

Mrgaton commented 7 months ago

and the nullables replace it with the '?' like in normal code

from this

        [Nullable(2)]
        public string Password
        {
            [NullableContext(2)]
            get;
            [NullableContext(2)]
            set;
        }

to this

        public string? Password { get; set; }
GazziFX commented 7 months ago

Nullable support is available in new-ilspy branch, and local name generation might be also changed a bit

Mrgaton commented 7 months ago

If I get a new idea I will post it here

KieranDevvs commented 5 months ago

I don't even know how this would work. This is a purely subjective feature. Your idea of "prettyfying" the branches into single lines is horrible in my opinion. Branches should objectively always be wrapped in a scope so you don't fall for the famous apple Apple 'GOTO FAIL;' bug. In regards to separating lines out by whitespace, what is the logic behind that? ie which one is correct? (its a trick question, neither are "correct")

var a = SomeClass1.Abc();
var b = SomeClass2.Xyz();

vs

var a = SomeClass1.Abc();

var b = SomeClass2.Xyz();

In regards to the nullability issue, that's the only valid concern that I see and its already fixed by a newer version of ILSpy. The only way I see this being even remotely possible is to have local linting/style configurations that each user can change. But that begs the question, does that really belong in a decompiler? This isnt an IDE in the traditional sense. Export the code into a Visual Studio project and use the actual IDE to format it instead.

It looks like this has already been suggested: https://github.com/dnSpyEx/dnSpy/issues/2

Mrgaton commented 5 months ago

Ok you're right