How to replace method calls in VS Code? - visual-studio-code

We are replacing Redux with useContext, so dispatch() method need to be replaced with auth. how?
We have this:
dispatch(
updateClientAndServer({
keyPath: [organizationShortId, 'posts', postId, 'text'],
value: v,
operation: 'setValue',
})
)
and would have this:
auth.updateClientAndServer({
keyPath: [organizationShortId, 'posts', postId, 'text'],
value: v,
operation: 'setValue',
})
Is it possible make a find and replace like this?
The challenge is the closing bracket. :) I hope I do not have to replace them manually. Of course the content, the object can be arbitrary.

Yes, You can simply replace the entire code with find and replace;
copy and paste the entire dispatch block in find and the auth block in replace

Related

How to place the cursor in a custom postion when completion happens?

I am trying to create a completion for JavaScript. I have the following exampe for the completion.
var jsonob = {
label: String(counter.label),
kind: counter.kind,
insertText: counter.insertText,
};
completionList.push(jsonob);
and counter.insertText has the "function ${1:functionName} (){\n\t\n}" String
when the completion happens what it shows is
function ${1:functionName} (){
}
but it should highlight the word functionName. How to fix this issue?
I'm assuming that your counter.insertText is a plain string. CompletionItem.insertText is defined as follows:
insertText?: string | SnippetString
A string or snippet that should be inserted in a document when selecting this completion. When falsy the label is used.
So if you want your insertText to be treated as a snippet, you'll have to wrap it in a SnippetString instance.
insertText: new vscode.SnippetString("function ${1:functionName} (){\n\t\n}")
If you're using the Language Server Protocol rather than the VSCode API, there is no SnippetString. Simply use a snippet string directly in insertText and then set CompletionItem.insertTextFormat to InsertTextFormat.Snippet:
insertText: "function ${1:functionName} (){\n\t\n}",
insertTextFormat: InsertTextFormat.Snippet
Note: insertTextFormat applies to both insertText and textEdit.

FilterOperator bug in using quotes, same code different behavior across systems/time

this.getView().getModel().read("/QualificationProficiencySet", {
filters: [new sap.ui.model.Filter({
path: "Qobjid",
operator: sap.ui.model.FilterOperator.EQ,
value1: nQObjid
})],
success: function(data) {
that._profData = data.results;
that._oQuickView.setModel(new sap.ui.model.json.JSONModel(that._profData), "proficiencyModel");
// delay because addDependent will do a async rerendering and the actionSheet will immediately close without it.
jQuery.sap.delayedCall(200, that, function() {
that._oQuickView.openBy(oLink);
});
},
error: function(evt) {}
});
nQObjidis of type string - always.
Yesterday on our development system I saw the error
"Invalid parametertype used at function 'eq' (Position: 8)"
I noticed that the filter was appended in the URL without single quotes around the value of nQObjid. Strange because at the moment it's added as the value of the filter operator it's clearly a string. I couldn't find any related issues, but I put a (dirty) workaround in place by doing value1: "'"+nQObjid+"'".
This worked, until today, same system, didn't change the code, but suddenly the quotes are part of the value inside the gateway. So I remove the "'"again and tested, works. Then I transport the solution to production to find out that I now have the same problem on production with "Invalid parametertype used at function 'eq'.. Another user on production does not have this issue, so I'm a bit lost.
Similar issue: new SAPUI5 updat to 1.42 has odata bug "Invalid Parameters...
This may not solve your problem but it's too long for a comment, that's why I am posting it here:
When doing a read request, the framework is making a call to a helper class: V2 ODataModel.js Line #4231
aUrlParams = ODataUtils._createUrlParamsArray(mUrlParams);
The helper class then calls a private method: ODataUtils.js Line #72
return "$filter=" + this._createFilterParams(aFilters, oMetadata, oEntityType);
This private method is doing a bunch of stuff, most importantly calling another private method which is actually building the strings ODataUtils.js Line #128
sFilterParam = that._createFilterSegment(oFilter.sPath, oMetadata, oEntityType, oFilterSegment.operator, oFilterSegment.value1, oFilterSegment.value2, sFilterParam);
One of the first thing this method does is formatting your value, and I guess here is where your problem occurs: ODataUtils.js Line #393
oValue1 = this.formatValue(oValue1, sType);
The formatValue function takes your value and its Edm.Type and depending on that type does different stuff. If your objectId is a string, then it should put single quotes at the beginning and the end: ODataUtils.js Line #468
sValue = "'" + String(vValue).replace(/'/g, "''") + "'";
If the type is undefined or some weird value that UI5 doesn't know, then your value is simply cast to a String (which is probably what happens in your case).
Why is the type undefined or weird? That's where you come in... you have to do a little debugging to find out what the actual values are. If the UI5 code is unreadable you can put sap-ui-debug=true as an URL parameter:
my.sap.system.com:8000/sap/bc/ui5_ui5/sap/ztest/index.html?sap-ui-debug=true
If it's a timing issue (metadata has not been loaded for whatever reasons) then wrapping your code in a Promise might help:
var oModel = this.getView().getModel();
oModel.metadataLoaded().then(function() {
oModel.read("/QualificationProficiencySet", {
// ...
});
}

How to PUT (save) object to REST endpoint using Restangular

Given I have resource id and data as plain JSON, how do I save that JSON to /cars/123/ ???
There seem to be no clear explanation. I don't have restangular element.
restangularizeElement might be the option, but it lacks any meaningful documentation.
All I know is that I want to save {make: 'honda', color: 'now blue, was red'} for car_id == 123.
Thanks.
If you have plain object you cannot use Restangular save function to update (it will make put request as object has id) object.
Easiest way to achieve it construct your end point url then call put function...
Restangular.one('cars', 123).customPUT(carObject).then(function(response){
TO-DO
})
If you want to restangularized your plain object you can use this one...
Restangular.restangularizeElement(null,carObject, 'cars').put().then(function (response) {
TO-DO
})
The comman way should be like this. Whenever you get object from backend with get or getList your object will be Restangularized unless you do not choose to turn them plain object by calling .plain() method of Restangular response. Then you can safely call .save() and it will automatically will be put or post accordingly...
Here is what worked for me. Thanks to #wickY26:
updateGroup: function (group, id_optional) {
if (!group.hasOwnProperty('restangularCollection')) {
// safe to assume it is NOT restangular element; sadly instanceof won't work here
// since there is no element class
// need to restangularize
group = Restangular.restangularizeElement(null, group, ENDPOINT);
group.id = id_optional;
}
return group.put();
},

what does ":" mean in this case in coffee script?

I am new to coffee script. When I am looking at this document https://atom.io/docs/api/v0.198.0/CommandRegistry#instance-add
I see a code segment like,
atom.commands.add 'atom-text-editor',
'user:insert-date': (event) ->
editor = #getModel()
editor.insertText(new Date().toLocaleString())
while the function signature looks,
::add(target, commandName, callback)
So in the code segment, what does : on the second line mean? My understanding is the 'user:insert-date' before : is commandName in the signature. The thing after : is "callback". So : is a argument separator like a ,? I don't find this introduced in the coffee script document http://coffeescript.org
That colon is just part of an object literal. The braces around object literals are optional in CoffeeScript when there's no ambiguity. If we add the optional braces, we get something that looks more like JavaScript:
atom.commands.add 'atom-text-editor', {
'user:insert-date': (event) ->
#...
}
So atom.commands.add is being called with two arguments. The first is the string 'atom-text-editor' and the second is an object with one key ('user:insert-date') whose value is an anonymous function that takes a single argument.
Appending mu is too short's answer (the user is absolutely correct that the second parameter commandNamecan be an object without the explicit braces {})
Atom's sourcecode:
https://github.com/atom/atom/blob/v0.198.0/src/command-registry.coffee#L81
add: (target, commandName, callback) ->
if typeof commandName is 'object'
commands = commandName
disposable = new CompositeDisposable
for commandName, callback of commands
disposable.add #add(target, commandName, callback)
return disposable

How do I set up a slot (event handler) that takes parameters, using gtk.go.Connect()?

I am using GTK bindings for Go.
Trying to connect a gtk.RadioButton toggle signal to a function. This code works fine:
...
radioButton.Connect("toggled", doRadioToggle)
func doRadioToggle() {
fmt.Println("toggled")
}
...
When radioButton is toggled, doRadioToggle is called - Good.
But I want to connect to a function that takes a parameter, for example:
func doRadioToggle(button *gtk.RadioButton) {
fmt.Println(button.GetState())
}
The gtk.go.Connect() function has this signature:
func (v *Widget) Connect(s string, f interface{}, datas ...interface{})
Based on what I know from gtkmm, I tried code like this (the go binding has apparently greatly simplified the call to connect())
radioButton.Connect("toggled", doRadioToggle, radioButton)
The third argument passed inconnect() - radioButton - corresponding todatasin
gtk.Connect(s string, f interface{}, datas ...interface{})
which I understood to mean data arguments to be passed to the slot.
But with this code, the doRadioToggle() handler never gets called, and I immediately exit with a panic:
panic: reflect: Call using *glib.CallbackContext as type *gtk.RadioButton
My question: How do I set up slot (event handler) that takes parameters using gtk.go.Connect()?
Based on response from the amazing mattn on GitHub:
https://github.com/mattn/go-gtk/blob/master/example/demo/demo.go#L58
Adopted, tested and working:
import(
...
"github.com/mattn/go-gtk/glib"
)
...
func ButtonHandler(ctx *glib.CallbackContext) {
fmt.Printf("Button Handler: %v\n", ctx.Data().(*gtk.RadioButton).GetState())
}
aRadioButton.Connect("toggled", ButtonHandler, aRadioButton)
...
Thedatasarguments inconnect()must be passed using*glib.CallbackContext in the handler's signature, then, in the handler code, use type assertion to grab the actual arguments .
The documentation is not clear about how to pass additional data directly; perhaps simply use a closure instead.
https://godoc.org/github.com/mattn/go-gtk/glib#CallbackContext