JavaScript code generation using WebStorm - create function format - webstorm

My question is about the interface. When I'm inside a JavaScript file, I type a function name, then right-click on the function name -> Show context actions -> the popup gives me an option to select between global or function functionName (handleClick2 in my case).
When I make my selection, the WebStorm generates:
function functionName() { //<-- this is what is generated
}
My question is, can I edit the template that WebStorm uses to create an arrow function instead? such as:
const functionName = () => { //<-- this is what I would like to be generated
}
I tend to always make this adjustment nowadays anytime a function is created for me this way.
Any advice is appreciated

Related

PyCharm unittest error: Provide a qualified name of function, class or a module

How to get rid of this annoying error? I do not understand what it want me to do.
This happens when I am trying to run a test
Clicking on "Run" follows up with the following
Screen
Clicking on "Continue Anyway" runs the tests normally.
So what should I do in order to stop this window from popping up every time I ran the tests?
Updated:
Here is what I've found myself meanwhile:
From here
if (targetType == PyRunTargetVariant.PYTHON && !isWellFormed()) {
throw RuntimeConfigurationError("Provide a qualified name of function, class or a module")
}
And a function isWellFormed() declaration from here
/**
* Sanity check for "target" value. Does not resolve target, only check its syntax
* CUSTOM type is not checked.
*/
fun TargetWithVariant.isWellFormed() = when (targetVariant) {
PyRunTargetVariant.PYTHON -> Regex("^[a-zA-Z0-9._]+[a-zA-Z0-9_]$").matches(target ?: "")
PyRunTargetVariant.PATH -> !VfsUtil.isBadName(target)
else -> true
}
Everything looks good with a regex of my test class and method names.
Ok, this is really weird.
I took a good look at a regex and found that it doesn't want any '-' in the target path. So renaming a filename from ads_wrapper-tests.py to ads_wrapper_tests.py solves the problem and the window is not popping up any more.
With this issue Error: Provide a qualified name of a function, class or module just got fixed, soon Pycharm would allow using all valid identifier names in target name.

Using preprocess hook on specific node type in Drupal 8

I've had success using preprocess page hooks such as:
function mytheme_preprocess_page__node_front(&$variables) {
...
}
and
function mytheme_preprocess_page__node_12(&$variables) {
...
}
which correlate with custom templates named page--front.html.twig and page--12.html.twig, respectively.
I'm trying to implement the same hook and template pairing for a content type called Video. I understand that there is a difference in that my examples have been custom templates for specific pages while my goal is a custom template for an entire content type, but I got a custom template called node--video.html.twig that works as a template for all video pages. However when I try to write a hook based on this template name:
function mytheme_preprocess_node__video(&$variables) {
...
}
this does not work. I think that I either can't define a hook like this, or I'm just naming it incorrectly. I found a couple threads somewhat relating to this such as this that seem to imply that I need to define a hook for all nodes and then write an if statement that handles each type separately.
So.......
Final Question: Can I define a hook for an entire content type, and if so what am I doing wrong?
Use condition within the preprocessor to get the node type and then either do your logic within, or invoke another function.
function mytheme_preprocess_node(&$variables) {
switch ($variables['node']->getType()) {
case "video":
// ...
break;
case "something_else":
// ...
break;
}
}
You could in theory emulate what you are trying to achieve by trying to invoke a function named mytheme_preprocess_node__" . $variables['node']->getType() if it exists, but is too much fuss without a clear benefit.
In drupal 7 from zen template I used to use this generic solution. I think it is still a viable solution on drupal 8:
function mytheme_preprocess_node(&$variables) {
...
// Add global modification that works for all node type
$function = __FUNCTION__ . '__' . $variables['node']->getType();
// Each node type can have its own specific function
if (function_exists($function)) {
$function($variables, $hook);
}
...
}
You can then now add preprocess function that will only works for you node type.
function mytheme_preprocess_node__video(&$variables) {
...
}
Instead of having one big function, each node type's preprocess logic has its own function. It's better for maintainability.

How to add code in runtime

I was searching through stackoverflow questions but none of them answered my question. I have a game engine and I want to load player AI (written in c++) in runtime.
Click on button, file dialog appears
Choose file with AI (.dll or something?)
Click on 'start' button, game starts using AI's that I add.
AI could be a method or whole class, it doesn't matter. I think I should generate .dll but I not sure how to do that. This class should look like this:
class PlayerAI
{
void computeSomething(list of argument, Object& output)
{
// some logic
}
}
Assuming pure Windows platform since none specified -
If you want to inject DLL, first obtain a handle to it using LoadLibrary-function like so:
HINSTANCE handleLib;
handleLib = LoadLibrary(TEXT("YourDLL.dll"));
You may then obtain a function pointer to a specific function in the lib. Like this:
FUNC_PTR func;
func = (FUNC_PTR) GetProcAddress(handleLib, "yourFunc");
Then you can call the function like so:
(func) (L"TESTSTRING HERE");
When done, call FreeLibrary(libhandle)
How to declare a function as exported is in VS for instance like this (this is needed to mark your function in your DLL that you precompile:
__declspec(dllexport) int __cdecl yourFunc(LPWSTR someString)
{
//Code here...
}
Since you mention already compiled DLLs, you want to look at LoadLibrary and GetProcAddress. That's how you do runtime loads of DLLs and extract specific functions from them.
Examples can be found under Using Run-Time Dynamic Linking.

Eclipse CDT method auto generation support

When using other IDEs, I usually type non-existent methods which gives an unresolved method/variable error. In the screenshot below, the method Arrangement::check for the specific arguments does not exist. I was hoping to generate this method declaration automatically now after this.
Usually other IDEs gives me an option to create methods to resolve that error (This this there in IntelliJ Idea as well as Eclipse JDT, IIRC), but in Eclipse CDT, if I type in a non-existent method it gives me an error but without any way of auto generating the missing method. Now, if I press Cmd+1 on this error, I only see two options "Rename in File" or "Rename in Workspace". You can see this in the screenshot above.
I expected to see something like "create method" or "create instance method". Is there a plugin or another way which allows me to auto generate methods like this?
I would love to have such a feature. What is possible at the moment is to implement methods out of their declaration signatures. Imagine you have the following:
class Arrangment {
public:
void check(Edge* e, Edge* f, Events& heap, CirclePairSet &cpet) const;
}
Then set the cursor on check. Press Ctrl + Shift + S to open the Source Code context menu. Select Implement method... to create a stub with the method body.

Luabind objects disapearing when I enter a new file

I'm using Luabind to bind my Lua scripts to my C++ engine. (it uses lua 5.1.4)
I added a new lua script called "controller.lua" which my entities script, called "cat.lua", will reference and use. One the c++ calls the method "Update" it's all in the hands of Lua.
But once I try to pass my binded c++ methods to the new script file, it feels like all the bindings from that c++ object disapear. I get the following error:
Expression: scripts/controller.lua:5(method MoveUp)
scripts/controller.lua:5: attempt to call method 'GetComponent' (a nil value)
Here is some C++ snippets
// Definitions
module(luaState)
[
class_<Entity>("Entity")
.def("GetComponent", &Entity::GetComponent)
class_<Component>("Component")
.enum_("eComponentTypes")
[
value("Steering", kComponentType_Steering)
],
class_<SteeringComponent>("SteeringComponent")
];
// The script components update
void ScriptComponent::Update() {
const Entity* owner = this.GetOwner();
mLuaDataTable["Update"](owner); // Executes the Update function on the script Cat.lua
}
The entities code being called by c++ (When it executes it returns the Cat table to c++.)
-- Cat.lua
local controller = loadfile("scripts/controller.lua")
local Cat = {}
function Cat.Update(entity)
steeringComponent = entity:GetComponent(Component.Steering) -- Works fine
controller:MoveUp(entity)
end
return Cat
and the Controller
--controller.lua
local up = vec2(0.0, 1.0)
local Controller = {}
function Controller.MoveUp(entity)
steeringComponent = entity:GetComponent(Component.Steering) -- Fails
end
return Controller
Bonus points:
When I make a change to the controller that doesn't work (like if I just threw an s character anywhere), the controller loads up nil, no warnings. Is there some way to make it throw warnings?
Is there a better way I should be doing to "link" to other lua files, like the way im working with Controller?
Thanks to ToxicFrog on Freenode chat for helping me figure this one out.
Basically: I was calling controller MoveUp like so:
controller:MoveUp(entity)
which of course translates into
controller.MoveUp(controller, entity)
and the function was defined as
function Controller.MoveUp(entity)
this "entity" was accepted as the first parameter, controller, while the actual entity is discarded per spec.
http://lua-users.org/wiki/ObjectOrientationTutorial