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?

6
  • 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 ? Commented Feb 24, 2020 at 9: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? Commented Feb 24, 2020 at 9: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' :) Commented Feb 24, 2020 at 9:17
  • Made it clearer, I hope. Have to improve myself here. Commented Feb 24, 2020 at 9: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. Commented Feb 24, 2020 at 21:53

2 Answers 2

11

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})"
Sign up to request clarification or add additional context in comments.

8 Comments

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})"
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.
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, try "(${TM_SELECTED_TEXT/(.+|(?=\\r?\\n[^]))(\\r?\\n(?!$))?/'$1'${2:+, }/g})"
The above works for me and converts empty line to empty string. great. @mklement0
|
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

3 Comments

That's quite near to perfect.
@theq then why I am the first person upvoting it?
This will convert the list to a single line where the text of every original line is wrapped with both single quotes and brackets. This is not what the OP asked for. Converting this line1\nline2\nline3 to this line1'),('line2'),('line3'),(' does not meet the requested outcome

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.