Auto completion in VS Code - vscode-extensions

I find that in vs code I can start to type console.log I can leverage intellisense to auto complete the console.log and the cursor is inside the parentheses. However, after typing what to log I still have to either continue to type the closing parentheses or use the right arrow to put me at the end of the command. Is there a plugin so that I don't have to do that? Thanks.
console.log('hello');
^ after this I have to right arrow to the end of semicolon

Not sure which language but Typescript automatically comes with a snippet completion that does this. Just type 'log' and choose the snippet completion that pops up. After typing your log message you can hit Tab to go to the next line.
If your language doesn't have this or you want to customize the snippet you can create your own snippet completions.
https://code.visualstudio.com/docs/editor/userdefinedsnippets#_create-your-own-snippets
If you look at the Typescript/Javascript snippet json it even has the code example for the console.log snippet completion.
"Print to console": {
"prefix": "log",
"body": [
"console.log('$1');",
"$2"
],
"description": "Log output to console"
}
When creating your own snippets I like putting short categories in the prefix itself.
So if you created your own snippet based on the above, the prefix would be:
"sn fn clog" (Note the spaces)
So you would prepend all your user defined snippet prefixes with 'sn '. That way if you type 'sn ' , you'll see all your custom completion pop up in the completion window. If you type 'fn ' , you'll seen all function custom snippets. Typing 'clog' will just pop up the correct custom logging snippet for you.

Related

Keyboard shortcuts on Flutter web

I'm adding keyboard shortcuts to a Flutter web application.
I have a form within a custom FocusableActionDetector where the shortcut has form like this:
SingleActivator(LogicalKeyboardKey.digit2)
and action is like:
CustomActivateIntent: CallbackAction<CustomActivateIntent>(
onInvoke: (intent) { provider.value = "2"; },)
In the same form I have a couple of numeric TextFormFields. To permit writing the character "2" I have to put these text fields inside some new FocusableActionDetector, otherwise the previous detector catches the command and the text field loses the "2" character, and this is already quite weird... Moreover, after writing in any of the text fields the form focus detector doesn't work anymore.
I think this could be related to the focus system, which is yet not that clear to me.
Can anyone help find a clean solution?
I found a workaround: the FocusableActionDetector is now preceded by an if statement. The code looks like the following:
// I extract the form to a widget to make it clearer
var searchWidget = SearchWidget();
child: textEditingInProgress
? searchWidget
: FocusableActionDetector(
child: searchWidget,
...,
),
The textEditingInProgress bool is a field in a provider and is controlled by the FocusNodes belonging to the TextControllers.
Still this is not a perfect solution, in particular I'd like to understand why the previous approach was not working.

how to instantly set/send_keys a textarea in capybara

I need to instantly fill a textarea with a very long string for testing purposes.
set/send_keys simulates typing and is too slow for Sauce Labs causing time outs.
Is there a way to instantly fill a textarea in Capybara?
The only way to instantly do it would be using execute_script to set the value via JS
element = find('textarea') # however you locate the element
execute_script('arguments[0].value = arguments[1]', element, text_to_set)
Note: this won't trigger all the events a user would generate when inputting into the textarea
TL;DR: Use Javascript/JQuery (.val() function) to set the field's value via the .execute_script()/.evaluate_script() function. It will automatically send the full string. You have more details bellow.
Example:
def execute_script(script)
browser.execute(function() {
$('<yourSelectorHere>').val("blablabla");
})
nil
end
Selenium team decided a LOOOONG way back to make it work this way, because it will actually simulate the real way a user would fill that input/textarea/field/etc.
Note: I wrote the command in WebdriverIO, but now have updated to Capybara as well.
Indeed, using the .setValue()/.set(), or the .keys()/.send_keys() methods will issue a POST action on the target element (presumably an <input>) in the form of an array of characters. See example:
Command: browser.setValue('input[connectqa-input="rename-device"]','stackoverflowstackoverflowstack');
Output:
> browser.setValue('input[connectqa-input="rename-device"]','stackoverflowstackoverflowstack')
{ state: 'pending' }
> [21:52:25] COMMAND POST "/wd/hub/session/3d830ffd-21c6-4e5f-a6b3-4f8a03821991/elements"
[21:52:25] DATA {"using":"css selector","value":"input[connectqa-input=\"rename-device\"]"}
[21:52:25] RESULT [{"ELEMENT":"6"}]
[21:52:25] COMMAND POST "/wd/hub/session/3d830ffd-21c6-4e5f-a6b3-4f8a03821991/element/6/clear"
[21:52:25] DATA {}
[21:52:25] COMMAND POST "/wd/hub/session/3d830ffd-21c6-4e5f-a6b3-4f8a03821991/element/6/value"
[21:52:25] DATA {"value":["s","t","a","c","k","o","v","e","r","f","(21 more items)"],"text":"stackoverflowstackoverflowstack"}
One quick and easy way to escape this is to go ahead and abuse the .val() JQuery function via the .execute()/.executeScript() methods:
Command: browser.execute(function() { $('input[connectqa-input="rename-device"]').val("dwadawdawdawdawdawdwadawawdadawdawdaw"); }) (WebdriverIO)
For Capybara syntax, see this question. It covers both .execute_script() & .evaluate_script(). (I don't want to mooch-off their views). Also you should document on the methods before-hand here.
Output:
> [21:59:38] COMMAND POST "/wd/hub/session/3d830ffd-21c6-4e5f-a6b3-4f8a03821991/execute"
[21:59:38] DATA {"script":"return (function () { $('input[connectqa-input=\"rename-device\"]').val(\"dwadawdawdawdawdawdwadawawdadawdawdaw\"); }).apply(null, arguments)","args":[]}
Hope it helped!

InteliJ - How to create shortcuts for code samples

I am using InteliJ and really love using it. One of the questions I have is this:
Is there a way to create code short cuts?
For instance, while bug testing, I am forever writing:
<?php die(var_dump($var)); ?>
and figured it would be great to have a shortcut key to automate this. i.e.
"Cmd Option D"
or something similar to dump the pre-defined statement into my code...
Any thoughts on this?
You can use Live Templates:
To define a template go to Settings/Live templates, then select group or create new group of templates and hit the green plus button and select Live Template.
In the abbreviation field type for example vd which will be the trigger for your snippet, define context, which represents the languages this template will be available for and put this in the Template Text field:
<?php die(var_dump($SELECTION$)); ?>
The $SELECTION$ part is a variable which represents current selection.
Now when you are in editor, you can just type vd and hit Tab. That will expand your snippet and put your cursor inside var_dump().
You can event type the variable name you want to dump, select it, hit CTRL+ALT+T, which will show you a Surround with dialog, where you can choose your template. After you select it your variable name will be surrounded with the var_dump snippet.
Another way to invoke a live template is to hit CTRL+J which will show you autocomplete popup with the available templates.

Is it possible to chain key binding commands in sublime text 2?

There are times in Sublime Text when I want to reveal the current file in the side bar and then navigate around the folder structure.
This can be achieved using the commands reveal_in_side_bar and focus_side_bar however they have to be bound to two separate key combinations so I have to do 2 keyboard combinations to achieve my goal when ideally I'd like just one (I'm lazy).
Is there any way to bind multiple commands to a single key combination? e.g. something like this:
{
"keys": ["alt+shift+l"],
"commands": ["reveal_in_side_bar", "focus_side_bar"]
},
Solution
Based on #artem-ivanyk's and #d_rail's answers
1) Tools → New Plugin
import sublime, sublime_plugin
class RevealInSideBarAndFocusCommand(sublime_plugin.WindowCommand):
def run(self):
self.window.run_command("reveal_in_side_bar")
self.window.run_command("focus_side_bar")
Save as RevealInSideBarAndFocus.py
2) Sublime Text 2 → Preferences → Key Bindings — User
Bind it to shortcut:
{ "keys": ["alt+shift+l"], "command": "reveal_in_side_bar_and_focus" }
Although the question is a year old, this might help people that are still looking for an answer.
Recently, a new package was developed by jisaacks, called Chain of command. It has the primary task to do exactly what you request, to chain several commands at once.
The package can be found here:
https://github.com/jisaacks/ChainOfCommand
An example of the working can be found below.
Let's say you wanted a key binding to duplicate the current file. You could set this key binding:
{
"keys": ["super+shift+option+d"],
"command": "chain",
"args": {
"commands": [
["select_all"],
["copy"],
["new_file"],
["paste"],
["save"]
]
}
}
This would select all the text, copy it, create a new file, paste the text, then open the save file dialog.
Source: https://sublime.wbond.net/packages/Chain%20of%20Command.
Updating #Artem Ivanyk's answer. I do not know what changed in Sublime, but that solution did not work for me, but I got this to work:
import sublime, sublime_plugin
class RevealInSideBarAndFocusCommand(sublime_plugin.WindowCommand):
def run(self):
self.window.run_command("reveal_in_side_bar")
self.window.run_command("focus_side_bar")
.
{ "keys": ["ctrl+shift+8"], "command": "reveal_in_side_bar_and_focus" }
Btw, I'm using Build 2220
Stumbled upon similar problem. When trying to record macros, which involved „Save“ command, console threw at me „Unknown macros command save“ message.
Worked my way around with elementary plugin.
1) Tools → New Plugin
import sublime, sublime_plugin
class MyChainedActionsCommand():
def run(self):
self.view.run_command("reveal_in_side_bar")
self.view.run_command("focus_side_bar")
You need to use upper camel case notation for the class name. ST2 exposes this class for the command name with „Command“ suffix removed and the rest converted into the lowercase-underscore notation. I.e. in this example MyChainedActionsCommand could be run in sublime's console typing: view.run_command("my_chained_actions")
2) Sublime Text 2 → Preferences → Key Bindings — User
Bind it to shortcut:
{ "keys": ["alt+shift+l"], "command": "my_chained_actions" }
Heed commas.
Take a look at this gist.
I've been trying to implement this in a long time and found this by accident.
Don't forget to read the "documentation" provided. I kept trying to make this work, until I reallized I was not passing the "context" key.
You can create a macro to do this. For Sublime Text, macros are essentially just chained commands. You then create a keybinding for that macro. You can create a macro by using Tools > Record Macro, then executing your commands (beware that macros record keystrokes as well, so you'll want to use the commands from the menu bar to not cause conflicts), then Stop Recording, then Save Macro. After you save the macro, you can open it back up in Sublime Text to make sure that it recorded only what you want.
Building on Artem Ivanyk reply, here is a version of ChainedActions that works with arguments. It takes two arguments for actions and args. Both are lists and each command in the list gets executed with the corresponding arguments. This admittedly stupid example inserts two snippets: view.run_command("chained_actions", {"actions":["insert_snippet","insert_snippet"],"args":[{"contents": "($0)"},{"contents": "1($0)"}]})`
import sublime
import sublime_plugin
class ChainedActionsCommand(sublime_plugin.TextCommand):
def run(self, edit, actions, args):
for i, action in enumerate(actions):
self.view.run_command(action, args[i])
I've tried to use the same command but I ended up with a bug that when the file's folder was already unfolded sublime moved my focus sidebar's top, where I can see the open files. To improve this behavior I've wrote a new plugin that ensures it'll behave as I want to, here it is https://github.com/miguelgraz/FocusFileOnSidebar
I am using Sublime text3 build - 3083. It solves the problem just by 'Reveal it in side bar', the focus comes automatically.
I have added a custom keyboard shortcut for 'Reveal in sidebar' by adding the following statement under Preferences->Key Bindings-User :
[
{ "keys": ["ctrl+shift+r"], "command": "reveal_in_side_bar"}
]
The option - 'Reveal in sidebar' was missing for image file types, since the context menu doesn't appear with the right click of the mouse. The custom keyboard shortcut comes handy in this situation.
Starting from Sublime Text Build 4103 the feature is supported natively:
"Added the chain command, which accepts a list of commands to run in its "commands" argument. This allows binding a key to run multiple commands without having to use a macro"
See Changelog on https://www.sublimetext.com/dev

Simulate TAB keypress event in Selenium RC

I need to simulate a tab keypress in Selenium RC, using the Java API.
I do this after having entered some text using:
selenium.type(input, "mytext");
I've tried 3 alternatives to get the tab working:
selenium.keyPress(input, "\\9");
and:
selenium.focus(input);
selenium.keyPressNative("09");
and even:
selenium.getEval("var evt = window.document.createEvent('KeyboardEvent');evt.initKeyEvent ('keypress', true, true, window,0, 0, 0, 0,0, 9,0);window.document.getElementsByTagName('input')[2].dispatchEvent(evt);")
The best I can get is a "tab space" to be inserted after my text so I end up with this in the input field:
"mytext "
What I actually want is to tab to the next control. Any clues? Thanks!
(Note: I have to use tab and can not use focus or select to chose the element I want to go to, for various reasons, so no suggestions along these lines please!)
selenium.keyPressNative(java.awt.event.KeyEvent.VK_TAB + "");
I don't use the Java API, but this post from google groups suggests it is your solution. I can't imagine that "9" is different from "09" in your question, but give it a try?
Try the official TAB char: \t or \u0009
Some functions may used Onblur. It will trigger the function when the field lose the key focus. here we can use fireEvent with "blur" or "focus" command as follows:
command: fireEvent
target: id=your_field_identification
value: blur
Reference: http://qaselenium.blogspot.com/2011/01/how-to-triger-tab-key-in-selenium.html
Improvising Ryley's answer, we can use
selenium.keyDownNative(java.awt.event.KeyEvent.VK_TAB + "");
selenium.keyUpNative(java.awt.event.KeyEvent.VK_TAB + "");
I tried this method for VK_CONTROL in IE and it worked good.
Use typeKeys():
Quoting the above link:
Unlike the simple "type" command, which forces the specified value into the page directly, this command may or may not have any visible effect, even in cases where typing keys would normally have a visible effect. For example, if you use "typeKeys" on a form element, you may or may not see the results of what you typed in the field.
In some cases, you may need to use the simple "type" command to set the value of the field and then the "typeKeys" command to send the keystroke events corresponding to what you just typed.