DevToys-app / DevToysMac

DevToys For mac
MIT License
9.2k stars 354 forks source link

Double quote gets transformed. #45

Closed jacola closed 2 years ago

jacola commented 2 years ago

Copy and paste works fine, but when typing double quotes into the JSON formatter, macOS transforms them to /slanted/ quotes.

{ "test": "darn" } becomes { “test”: “darn” } resulting in invalid JSON.

image

DanielSouzaBertoldi commented 2 years ago

I dug a deep into this problem and to me (unexperienced Swift developer) it seems that the problem is the Smart Quotes feature that Mac has enabled by default. Disabling it in the System Preferences fixes this issue.

I tried to code something to fix this without needing to disable the Smart Quotes feature, but ran into a few problems:

There's a flag that can be used to disable this behavior programmatically, called isAutomaticQuoteSubstitutionEnabled but it's only available for the NSTextView view. Since the textView attribute of the TextViewSectionBase is actually of type NSView we can't directly access it (At least I think we can't, I tried a few things and got nowhere), nor convert it to a NSTextView.

One thing that worked was to use the replaceOccurrences function, but I didn't create a PR since to me seems kinda ugly to use it for this problem, and I don't know if there's another way to do it. The code ended up like:

    var string = "" {
        didSet {
            // using two replacingOccurrences since both quotes are actually different from one another
            self.textView.string = string.replacingOccurrences(of: "“", with: "\"").replacingOccurrences(of: "”", with: "\"")
            self.copyButton.stringContent = string
        }
    }

Note: I forgot to mention, but this 'solution' will not work for French quote symbols ( « » ) nor German ones ( „ “ ) :/

If anyone got any ideias on how to solve this problem more elegantly, let me know, I'll gladly create the PR fixing this 😄

ObuchiYuki commented 2 years ago

@jacola @DanielSouzaBertoldi Thank you for your issue!

I certainly think it's a serious problem too. I want to fix it. But I can't reproduce the problem. Could you please let me know your keyboard settings and how it is occured?

DanielSouzaBertoldi commented 2 years ago

@ObuchiYuki my keyboard layout is Brazilian - ABNT2

my keyboard layout

If we copy and paste a json then it's formatted properly, but if we type a json then the Mac replaces the quotes for the slanted version of it, as can be seen in my GIF below.

Kapture 2022-02-16 at 12 59 15

jacola commented 2 years ago

I have standard keyboard settings as well for an English OS in Japanese region. Screen Shot 2022-02-17 at 11 24 47

As @DanielSouzaBertoldi mentioned, this setting should stop the issue from having, but it's true by default. defaults write NSGlobalDomain NSAutomaticQuoteSubstitutionEnabled -bool false

ObuchiYuki commented 2 years ago

@jacola @DanielSouzaBertoldi

I was able to reproduce and fix the bug. Thanks for your help!

タイトルなし

ObuchiYuki commented 2 years ago

@DanielSouzaBertoldi @jacola Thank you for your support! I can fix this bug!

You can get fixed version on https://github.com/ObuchiYuki/DevToysMac/releases/tag/0.0.8.2