3

I have often lines of strings in VSCode, but would need an array for PowerShell or SQL.

I have in VSCode

line1
line2
line3

I would need somthing like that in Vscode

('line1','line2','line3')

Resulting in

Select * from Table where Column in ('line1','line2','line3')

or

$a = 'line1','line2','line3'

Is there an function / extension for this?

theq
  • 111
  • 2
  • 10
  • 3
    It's unclear what your are trying to achieve. You said you work in VScode but want an array for powershell or sql... What's the relation ? – Steve B Feb 24 '20 at 09:03
  • Do you want a script that transformes a couple of pasted lines (or the content of a file) into a powershell object of type array? – T-Me Feb 24 '20 at 09:12
  • I could script it, but I find it tedious. You can also edit it with several cursors - but sometimes there are 20 elements. So i would love to mark the part and click 'transform' :) – theq Feb 24 '20 at 09:17
  • Made it clearer, I hope. Have to improve myself here. – theq Feb 24 '20 at 09:18
  • Good question; an aside: Please don't use `@(...)` for array literals - not only is it unnecessary, it is inefficient in PowerShell versions up to 5.0, but, more importantly, it invites conceptual confusion by falsely suggesting that it _constructs_ arrays - see the bottom section of [this answer](https://stackoverflow.com/a/45091504/45375). – mklement0 Feb 24 '20 at 21:53
  • Thanks for you input - changed my example to spread the word. – theq Feb 27 '20 at 07:36

2 Answers2

10

array build demoYou could make a snippet that'll do this easily. Add a keybinding to trigger that snippet in keybindings.json:

{
  "key": "alt+b",                             // or whatever keybinding you want
  "command": "editor.action.insertSnippet",
  "args": {
    "snippet": "(${TM_SELECTED_TEXT/\\s*(.+)(\\s)?/'$1'${2:+,}/g})"
  }
}

${TM_SELECTED_TEXT/\\s*(.+)(\\s)?/'$1'${2:+,}/g} get the selected text. The regex will get the text of each line in capture group 1, and any newline character in capture group 2.

Then 'group 1' is added followed by a , only if there is a group 2. Because of the g regex flag this will happen for all lines.


If you want empty lines to be represented as an empty strings in your output, use

  "snippet": "(${TM_SELECTED_TEXT/((.+)(\\r\\n))|(.+)|(\\r\\n)/'$2$4'${3:+,}${5:+,}/g})"

Demo of this:

empty lines demo


If you want this output of the snippet:

Select * from Table where Column in ('line1','line2','line3')

then use:

"snippet": "Select * from Table where Column in (${TM_SELECTED_TEXT/\\s*(.+)(\\s)?/'$1'${2:+,}/g})"
Mark
  • 143,421
  • 24
  • 428
  • 436
  • Nicely done; for a strictly line-oriented transformation where you don't need to worry about having a trailing newline in the selection, use `"(${TM_SELECTED_TEXT/(.+)(\\n(?=\\S))?/'$1'${2:+,}/g})"` – mklement0 Feb 24 '20 at 21:51
  • Really like this! Wanted to improve it, so in a PowerShell file VSCode will do the PowerShell line and in SQL Files there will be the select - but it seems that keyboard bindings cannot be bound to a filetype yet. Thx again. – theq Feb 24 '20 at 22:19
  • Did not get the solution from @mklement0 to run properly, because in my test it failed to create the comma ",". And I really have to improve my regular expression skills. – theq Feb 24 '20 at 22:30
  • @theq, I forgot about CRLF line feeds; try `"(${TM_SELECTED_TEXT/(.+)(\\r?\\n(?=\\S))?/'$1'${2:+,}/g})"` – mklement0 Feb 24 '20 at 23:48
  • @mklement0 Thanks again! - Works better, but if you have a newline in the data rows there will stay a newline in the result. Any way to convert this to an empty string like ''? – theq Feb 25 '20 at 07:50
  • I am a little confused. What does the alternative input look like that you are worried about? – Mark Feb 25 '20 at 15:30
  • 1
    @theq, try `"(${TM_SELECTED_TEXT/(.+|(?=\\r?\\n[^]))(\\r?\\n(?!$))?/'$1'${2:+, }/g})"` – mklement0 Feb 25 '20 at 22:05
  • 1
    The above works for me and converts empty line to empty string. great. @mklement0 – theq Feb 27 '20 at 07:40
5

You can do that using regex replace:

  • Select lines of code you wish to join
  • ctrl+h, change mode to Regular Expressions
  • in Find type \n
  • in Replace type '),('
  • Replace All

There's no need to add \r in Find or escape parenthesis in Replace.

screen

AdamL
  • 12,421
  • 5
  • 50
  • 74