how to wirte a python snippet in vscode to print something - json

I found that when you type.print at the end of the line in pycharm, it will automatically convert this line to print(line). So I want to know whether vscode can achieve such a function with snippet.
I have written a snippet after googling.
"print": {
"prefix": "print",
"body": "\nprint(${TM_CURRENT_LINE-})"
}
However, there is a problem in this snippet: the prefix will be added during conversion. Besides, it only adds an additional line instead of changing the current line.
demonstration of snippet now

you can do it like this:
".print": {
"prefix": ".print",
"body": ["\nprint(${TM_CURRENT_LINE/(^.+)(\\..+)/$1/g})"],
}

Related

How can i generate number for same piece of text using VS Code

i have many pieces of code same for example like this
alt="Greece"
alt="Greece"
alt="Greece"
alt="Greece"
Can i somehow modify it to this? Is there any kind of function like in Excel or something like that please? Imagination write it manualy each is horrible
alt="Greece 1"
alt="Greece 2"
alt="Greece 3"
...
alt="Greece 200"
You can use the extension Regex Text Generator
select the word Greece
select all cases you want with
Ctrl+D multiple times
Ctrl+Shift+L to select all
use Shift+Alt+Click to create multiple cursors
if needed RightArrow to get all cursors after the word Greece
execute command: Generate text based on Regular Expression (regex)
As Original Text Regex use: .*
As Generator Regex use: {{=i+1}} (watch the space as first char)
Look at the preview, use Esc to cancel and Enter to accept
You can use any calculation based on i you want and you can also match number in the selected text and use that in the calculation N[...].
You can also add the word Greece by using Greece {{=i+1}}
You have a couple of options. First, using the extension, Find and Transform (disclaimer, I wrote that extension, this is very easy. Make this keybinding in your keybindings.json (after installing the extension):
{
"key": "alt+n", // whatever keybinding you like
"command": "findInCurrentFile",
"args": {
"find": "(alt=\"Greece\")",
"replace": "$1 ${matchNumber}",
"isRegex": true
}
},
Actually you can make it even easier if you first select what you want to modify (see the demo below). Then use this simple keybinding:
{
"key": "alt+n",
"command": "findInCurrentFile",
"args": {
"replace": "$1 ${matchNumber}", // what you select will be put into $1
"isRegex": true
}
},
Another option - not quite as easy
Snippets have a variable $CURSOR_NUMBER which is useful here.
Make this keybinding:
{
"key": "alt+n",
"command": "editor.action.insertSnippet",
"args": {
"snippet": "$TM_SELECTED $CURSOR_NUMBER"
},
"when": "editorHasSelection"
},
Do a find on your desired text match: alt="Greece"
Ctrl+Shift+L to select all occurrences of the find match.
Trigger the snippet via its keybinding.
Demo of this method:
So this second method is more steps but doesn't require an extension.

Huginn: Extract text from html tag using Website Agent css selector

I'm trying to understand how the web-scraping is done in Huginn but I cannot find proper documentation on the options available.
I'd like to extract the price of the Gold oz. from this website for example:
https://www.xe.com/currencyconverter/convert/?Amount=1&From=XAU&To=USD
For which I use a Huginn Website Agent with this code:
{
"expected_update_period_in_days": "2",
"url": "https://www.xe.com/currencyconverter/convert/?Amount=1&From=XAU&To=USD",
"type": "html",
"mode": "on_change",
"extract": {
"price": {
"css": ".converterresult-toAmount",
"value": "."
}
}
}
I got the css selector using SelectorGadget and I've tried multiple values like: ./node(), string(.), normalize-space(.), . , //text() and others, but I cannot find the way to extract the content of the span html tag that contains that value. Here the code of that section of the web:
<span class="converterresult-toAmount">1,730.35</span>
And what I want to extract is: 1,730.35
i got it working on another site.
i used "xpath" for it.
I used a different site because it wouldn't work on the one you posted.
But i hope this still helps someone.
{
"expected_update_period_in_days": "2",
"url": "https://walletinvestor.com/converter/xau/usd/1",
"type": "html",
"mode": "on_change",
"extract": {
"gold_in_dollar_price": {
"xpath": "/html/body/div[4]/div/div[3]/div[1]/h2/strong/span",
"value": "string(.)"
}
}
}
Here is how you get the XPath of any Element/Object on a website:
(i used Yandex Browser based on chrome for this)
Open the Developer Tools in the Browser (or right click and select "Inspect element code")
Select / Click with the Inspector on your Element/Object
You should now see something like:
<span class="converterresult-toAmount">1,730.35</span>
Right click on this and click: "copy" > "Copy XPath"
Im using Huginn since 2 Days so anyone feel free to tell me any faster way if there is any :)

VS Code : variables in custom snippet

How do I create a custom snippet that will automatically enter a variable's value that I type into its prefix?
I want a snippet that will create a html start-end tag comment block. For example if I type /se hello I want the result to be
<!-- $var start-->
<!-- $var end-->
Where $var is replaced with "hello". Thank you for reading!
As the VSCode snippet docs says, it uses TextMate to power its snippets. TextMate uses variables of the form $name and ${name:default}, and luckily supplies $TM_CURRENT_WORD which may be sufficient for your needs. However there is no built in variable to get multiple arguments directly after the snippet name i.e. $arg1 and $arg2 as variables. Thought you could do a similar effect with interpolated shell code, but unfortunately:
The snippet syntax follows the TextMate snippet syntax with the exceptions of 'interpolated shell code' and the use of \u; both are not supported.
Emphasis mine
However for this simple example, the following indexed variable example is probably sufficient.
<!-- $1 start-->
$0
<!-- $1 end-->
$i gives you a value to fill in, you can go between each one with tabbing. The $0 is where the cursor goes at the end(the end of the snippet by default). Optionally you can do something like:
<!-- ${1: default text} start-->
$0
<!-- $1 end-->
and it'll start looking like:
<!-- default text start-->
<!-- default text end-->
with both of the defaults selected to edit.
This all put together would look like this together in the snippets.json file:
{
"se": {
"scope": "html",
"prefix": "se",
"body": [
"<!-- ${1:default text} start-->",
"\t$0",
"<!--$1 end-->"
]
}
}
As #Mark pointed out, if you want it to work for more than just HTML you can use $BLOCK_COMMENT_START and $BLOCK_COMMENT_END which will vary for each language. The snippet would then look like this:
{
"se": {
// Leaving scope off will make it a global snippet
"prefix": "se",
"body": [
"$BLOCK_COMMENT_START ${1:default text} start $BLOCK_COMMENT_END",
"\t$0",
"$BLOCK_COMMENT_START$1 end $BLOCK_COMMENT_END"
]
}
}

SublimeText RegReplace Highlighted Text Only

I am trying to use the Sublime Text Plugin RegReplace package so I can highlight a line of text and replace spaces with dashes.
Goal
Hello My Name is Jesse, Highlight, CTRL + . replaces with:
Hello-My-Name-is-Jesse -- for the purpose for saving time creating markdown links.
Currently Working, But Runs Entire File
I would really like this to only replace the text I have highlighted wit a hotkey, is this possible?
I have this in reg_replace_rules.sublime-settings
{
"replacements": {
"replace_spaces_with_dash": {
"find" : "(\\s)",
"replace": "-",
"greedy": true,
"case": false
}
}
}
Here is my User Hotkeys
{
"keys": ["ctrl+."],
"command": "reg_replace",
"args": {"replacements": ["replace_spaces_with_dash"]}
},
However, this replaces the entire file with spaces. I am missing something I hope, I am not sure the scope. Any help would be appreciated.

SublimeText completion fails inside HTML attributes

SublimeText completion using .sublime-completions file fails to work on HTML attribute, inside the quotes. Typing the trigger (test in the example below) inside the alt="" attribute does not insert the completion. It does outside the image tag without problems. Any suggestions why?
Scope inside the quotes is matching the definition in the html-attr.sublime-completions file below: text.html.basic meta.tag.inline.any.html string.quoted.double.html punctuation.definition.string.end.html.
<body>
<img src="/images/poster.jpg" alt="">
</body>
I am using .sublime-completions file:
// html-attr.sublime-completions
{
"scope": "text.html.basic meta.tag.inline.any.html string.quoted.double.html punctuation.definition.string.end.html",
"completions":
[
{
"trigger": "test",
"contents": "test ok"
}
]
}
Other installed packages include Emmet, HTML5, Better Completion, HTMLAttributes and SublimeCodeIntel, but disabling them all does not help the situation.
// Preferences.sublime-settings
{
"auto_complete": true,
}
Update:
Emmet could have been a culprit, but not my case. Emmet overrides completions defined by .sublime-completions files, unless Emmet's tab_abbreviation is disabled for the corresponding scope:
// Emmet.sublime-settings
"disable_tab_abbreviations_for_scopes": "text.html.basic"
I solved the problem temporarily by using .sublime-snippet, but it didn't work on the XML files so well. Still looking for an answer to the question above.
<!-- html-attr.sublime-snippet -->
<snippet>
<content>test ok</content>
<tabTrigger>test</tabTrigger>
<scope>text.html.basic</scope>
<description>html-attr.sublime-completion does not work</description>
</snippet>