1

Visual studio code under certain circumstances doesn't seem to match letters and I'm not sure if I'm missing something incredibly obvious or if this is a bug. In my case I'm standardizing a code base and I want to change things like:

}
else {
  Write-Warning

to:

} else {
  Write-Warning

I wrote the following regex:

((?:(?=[^\r\n])\s))\}
.*else \{
(there's a newline here you can't see)

to identify the lines I want to change. On one file it worked just fine:

enter image description here

However on the next file I got the following:

enter image description here

As you can see I changed the regex a bit, but it works as expected and matches everything up to and include the 'e'. However, if I add an l:

enter image description here

It no longer matches. I tried copying and pasting the 'l' thinking maybe it was some weird unicode problem or something - still doesn't match. Am I missing something?

Update

I checked the line endings with a hex editor and confirmed everything was only 0A (LF). There were no carriage returns:

enter image description here

I also confirmed in Visual Studio code the line endings were set to LF.

enter image description here

Grant Curell
  • 1,321
  • 2
  • 16
  • 32
  • 3
    Not a regex solution but a suggestion - look into code formatters. VSCode can format code on the fly and you should be able to just format all files in one sweep. It's probably easier than trying to match with regular expressions and it will make maintaining the style easier in the future. – VLAZ Nov 10 '20 at 13:12
  • @VLAZ Do you know of one that can accomplish this? I'm not really familiar with the topic – Grant Curell Nov 10 '20 at 13:14
  • 1
    Try `([^\S\r\n])\}\n[^\S\r\n]*else[^\S\r\n]*\{` and replace with your `$1} else {` – Wiktor Stribiżew Nov 10 '20 at 13:16
  • 1
    @GrantCurell It seems you have PowerShell code. I'm not familiar with the tooling around it but [here is an article](https://dscottraynsford.wordpress.com/2017/11/18/auto-formatting-powershell-in-visual-studio-code/) about formatting PS in VSCode. It's using a plugin to accomplish that - I assume it is configurable to at least some extent. There might also be alternative plugins. At any rate, once there, VSCode has standard format functionality you can make it to format `.ps1` files. You can also auto-format on save (quite useful). – VLAZ Nov 10 '20 at 13:20
  • 1
    @GrantCurell last but not least, you can [format all files in your project](https://stackoverflow.com/questions/43666270/how-do-i-format-all-files-in-a-visual-studio-code-project). This is likely going to be a one time task just to bring everything in line with the formatting rules. If you have enabled auto formatting for each file, you will maintain the style – VLAZ Nov 10 '20 at 13:22
  • @WiktorStribiżew That worked! If you want, throw that in an answer and I'll mark it! Why did it work though??? – Grant Curell Nov 10 '20 at 13:24
  • @VLAZ Thanks! I'll check that out! I didn't even know that was a thing. That's awesome! – Grant Curell Nov 10 '20 at 13:24
  • Well, it is really difficult to judge without exact text you have in the files – Wiktor Stribiżew Nov 10 '20 at 13:29
  • @WiktorStribiżew I checked - they seem to be the same. If I do a regex search in the not working file for \n I match every line. If I search \r\n I get no matches. – Grant Curell Nov 10 '20 at 13:33
  • Right, `\n` in VSCode regex matches any linebreak sequence. No need for `\r\n` or `\r?\n`. – Wiktor Stribiżew Nov 10 '20 at 13:34
  • @WiktorStribiżew one sec, hex editor check inbound – Grant Curell Nov 10 '20 at 13:37
  • @WiktorStribiżew I don't think that was it - post updated with my findings. – Grant Curell Nov 10 '20 at 13:43
  • Well, if you just paste *text* you have into the question, it would be much more helpful than images. – Wiktor Stribiżew Nov 10 '20 at 13:56

1 Answers1

1

If you use a regex for the task, consider changing yours to

([^\S\r\n])\}\n[^\S\r\n]*else[^\S\r\n]*\{

Replace with $1} else {.

See the regex demo.

Details

  • ([^\S\r\n]) - Group 1: any whitespace but \r and \n
  • \}\n - a } and any line break (that is how \n works in VSCode regex search field)
  • [^\S\r\n]* - any 0 or more whitespaces but \r and \n
  • else - a word else
  • [^\S\r\n]* - any 0 or more whitespaces but \r and \n
  • \{ - a { char.

Demo:

enter image description here

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563