Zero Width Space character in string literals: Detect and/or prevent - string

Visual Studio allows the Unicode character Zero Width Space (U+200B) to be pasted into the code editor. This character is (as the name implies) invisible.
This can lead to nasty bugs such as the one I just spent time troubleshooting where HttpWebRequest.CreateHttp(string url) threw a UriFormatException when passed the innocuous-looking string literal "​http://www.umich.edu".
The exception occurred because the first character in that string literal is not h, as it appears, but is the Zero Width Space character. It was put onto the clipboard when I copied the URL from the body of a web page, and was dutifully pasted into my code by Visual Studio when I hit Ctrl+V in the code editor window.
Turning on the "View White Space" option (Edit > Advanced > View White Space) does not cause Visual Studio to reveal that there's a Zero Width Space character present.
I would like Visual Studio to:
Give some kind of indication or warning when there's a Zero Width Space character (or other invisible character) in a string literal in my code, and/or
Prevent such control characters from being pasted into the code editor in the first place.
Is there a way to make Visual Studio do this?

The Gremlins tracker for Visual Studio Code worked for me.
https://github.com/nhoizey/vscode-gremlins
P.s.: IntelliJ recognizes those hidden troublesome chars OOTB

As of December 2021, Visual Studio Code introduced highlighting of invisible characters.
From this
to this
Preferences -> Settings (ctrl + ,) -> search for "invisible characters" -> tick "Editor › Unicode Highlight: Invisible Characters"
This option should be on by default.

If you are using "VSCODE" then it's simple, just install gremlins in vscode. After Installing where there are gremlins remove those spaces(with red line) and you are good to go. It works for me.

Related

Android Studio autocomplete on special character (space, period, etc.) like in Visual Studio

Is it possible to enable autocompletion of the selected suggestion when entering special characters like in Visual Studio?
For example:
When typing 'MenuI' Android Studio suggests 'MenuInflater' that I can autocomplete by hitting TAB after which I type SPACE to separate my variable name.
In Visual Studio I can type SPACE directly (instead of TAB) to get the autocompletion plus my space character. This works for other special characters like period, brackets and so on, too.
Yes, I have also faced the same issue in Android studio. And finally I found it here.
Open the Android Studio settings go to Editor --> General --> Code Completion
and check the option "Insert selected variant by typing dot, space, etc."
And uncheck the options "Basic completion (Ctrl+Space)" and "Smart Type Completion (Ctrl+Shift+Space)"
Check this Android studio settings image
Well, this is implemented, slightly.
If you have MenuInflater and type in MenuI, and then CTRL+SPACE, which means you tell the IDE that you want to use the autocomplete dropdown, and then press space, it works as you wish.
So, I could type in MenuIn, then I could either press the down key, or use CTRL+SPACE ("MenuInflater" Should now be highlighted in blue:
), and then press space, and it'll work as you wish.
I suck at explaining, so if you don't get it, I'll try more.

Spaces displayed as dots and tabs displayed as arrows

In my Visual studio 2012 editor I erroneously pressed some obscure key combination (Ctrl-Alt-something or so) and now spaces are displayed as little dots and tabs are displayed as arrows.
I'd like to get rid of this, but I haven't found the corresponding option in TOOLS-Options-Text editor.
Go to Edit -> Advanced -> View White Space to rectify this. Alternatively, you may use the key combination Ctrl + R, Ctrl + W.
A quick tip:
An efficient way to find Visual Studio options and commands is to use the "Quick Launch" feature (Ctrl+Q - or just type in the box at the top right of the Visual Studio IDE) - in this case typing "white space" or just "space" in that box shows the command you need and lets you take the action from there too.

Unexpected token: Error:(40, 61) error: illegal character: \8232 in Android Studio

When I copy paste code into Android Studio sometimes (from WEB or other external source) I get some unvisible characters and I can't compile. Is there a way to avoid this? Reformat code automatically? Plugin? Change settings?
Error:(40, 61) error: illegal character: \8232
So far I delete the code any type it in by hand again.
I believe your file encoding is windows-1252 (bottom right corner in AS)?
Here is a Blog about file encoding. I also use UTF-8 for everything.
Blog about file encodings in IntelliJ
Since AS is IntelliJ-based, this also applies to AS.
\8232 is decimal for U+2028
on a Mac, it usually gets into IntelliJ/AndroidStudio when text is copied and pasted from Notes
in System Preferences, open Keyboard/Input Sources and add Unicode Hex Input
in IntelliJ/AndroidStudio, open Edit/Find/Replace and type 2028 while holding Option key, then click Replace all
I used this site (https://r12a.github.io/apps/conversion/), copied and pasted my code surrounding where the invisible error was, and pasted it into the top box right under the button "Convert."
Then I copied and pasted the result code that was automatically generated in the box under the title "Unicode U+hex notation", and noticed it had something like this:
//Remove the last 2 characters (The last ", ").
U+2028title.substring(0, title.length() - 2);
return title;
I just deleted the U+2028 and pasted that code back into Android Studio, and it compiled.
#Rock Lee
After you open
SystemPreferences>Keyboard>InputSources
click the '+' icon, then in the Search box at the bottom enter
"Unicode"
or
scroll all the way to the bottom of the language list to
"Others", where it is listed as well.

How do I get line ending glyphs in Visual Studio 2012?

Is there any way to get line ending glyphs in Visual Studio 2012?
The following screen shot (from notepad++) show what I want (the CR LF part):
You might want to check out this extension: End of The Line by Rolf W. Rasmussen. The current binary version displays CR and LF identifiers at the end of the line all the time, but the source version ties it into VS's "View Whitespace" menu option.
It's for Visual Studio 2013, but it's open source and should be fairly straightforward to recompile as a VS2012 compatible plugin
(You might need to change the references - the VSSDK packages on nuget can be really useful here. You'd need VSSDK.CoreUtility and VSSDK.Text. In fact, that might be worth a PR regardless...)
Nope, Visual Studio has no feature to show End of Line Markers. (Reference)
But it gives the ability to show Whitespace with (Ctrl + E + S) which is adressed here.
If any incosistencies are included, VS should suggest to fix the document automatically.
But if you need to do actions other than that, I recommend using notepad++ as described by Hanselman here
If you think Visual Studio should include this feature, you better vote for this request
Edit:
By the way, VS shows glyphs for wrapped words by enabling through:
Tools -> Options -> Text Editor -> All Languages -> Show visual glyphs for word wrap
Like:

apostrophes changes when I copy and paste

When I cut and paste apostrophes between programs (ie. notepad2, notepad++, hiedisql, word, etc etc) and wysiwyg's, and source codes my apostrophe is changing a little. It looks different and it behaves differently depending on the copy source and paste source.
Is this normal? Is there any way to make sure everything renders the apostrophe the same way? is this a windows thing?
I feel like this is a new thing, but maybe it has always been like this.
Do you mean that ’ -- should look like: -- turns into ' ??
If so, your editor program is "helpfully" changing a non-Ascii right apostrophe into the only apostrophe available in Ascii, the "straight" apostrophe: ' (Or vice-versa.)
The right and left apostrophes are defined in Unicode and ISO 8859 character sets, but not Ascii.
Solution Check the docs for your editors to see if you can turn off the behavior. It may not be possible depending on the specific editing software and its version.
Added Even this editor in Stack Overflow caused me some problems: I entered the Unicode right apostrophe, and it looked fine in the editor. But the preview window (and the display of my answer) shows the apostrophe as being a "straight" apostrophe: it appears that the font used by Chrome for displaying and previewing the answer shows a straight apostrophe instead of a right one. So I added a graphic for the right apostrophe.
Blog post about the problem
For writing software, SQL, etc, you should stick to the straight apostrophe. Editors should not convert the straight apostrophe to the right apostrophe. The issues of single quotes (apostrophes) and straight quotes vs opening and closing quotes is a primary difference between a document editor and a code (software) editor. If your editor changes apostrophes and quotes for you, it can cause a lot of problems in your software. You can usually turn off the behavior of "smart quotes" or similar.

Resources