vimscript insert string before a search pattern - coffeescript

Im currently write a coffeescript refactore plugin.
Currently i'am experimenting an issue, when i try to add parameter to a function.
so far in can do
#my code
->
# after call vim function
(new_param) ->
but
when now i want insert it just before the arrow like
#my code
foo ->
# what i want
foo (new_param) ->
i have this unwanted result
(new_param) foo ->
What i use in vim script is
exec "?->"
execute "normal i(new_param) \<Esc>"
What should i do to insert just before the arrow.
I read about searchpos() but i don't find good example to understand how it work.
Know the cursor position is important i guess for handel corner case like
(old_param) ->
#add parameter to existing parameter
(old_param, new_param) ->

this command :
%s/\s\?[-=]>/ (new_param)&/g
will transform:
foo ->
foo =>
into:
foo (new_param) ->
foo (new_param) =>
for the adding new_param case:
you could do :
%s/)\s\?[-=]>/, new_param&/g

Related

konacha - helper methods coffeescript

I'm trying to define a helper method in konacha in coffeescript, something like this
#expect_int_is_universal = (i) ->
expect(i).to.equal 42
describe '#test', ->
it 'checks if integer is 42', ->
#expect_int_is_universal(42)
Is it possible in konacha?
edit: error log here:
update: the fix is putting it inside a beforeEach block
beforeEach ->
#expect_int_is_universal = (i) ->
expect(i).to.equal 42
describe '#test', ->
it 'checks if integer is 42', ->
#expect_int_is_universal(42)
mu is too short hasn't successfully converted his comment as the answer, but i'll provide it here below:
# (AKA this) inside your it callback is not the same # as at the top level so you're defining expect_int_is_universal as a method on one object but trying to call it as a method on another object. Try without the #s. I don't know enough Konocha, Mocha, or Chai to say any more than that

Is this joining coffeescript classes over files a valid aproach?

I want to join (use) classe in Coffescript files, and i found some usefull ideas here (sry for not including all links), since none fitted my needs (at least not as i like) I tried to find an own solution, this will work fine, but is there something I have overseen?
following "base" and "base class" are not OO words, I just did not find better names
I have a base (class) file TechDraw
class TechDraw
constructor: ->
$(window).load () =>
... do somthing
wave_to_me: ->
say 'I wave' # say is a basic func, I always use for debugging (console.log)
#TechDraw = new TechDraw
this works fine
Now I want to expand/extend my class with "sub classes/modules" in other files; ie. I have a TechDrawLine, and a TechDrawCalc, ans so on
What I did, was building a coffee file for each of them like:
class TechDrawConnector
constructor: (p)->
#parent=p
wave: (msg) ->
say 'yes its me, the connector:',msg
`window.TechDrawConnector = function(p) { return new TechDrawConnector(p) };`
# the last instead of a simple new like
# #TechDrawConnector = new TechDrawConnector
and my base(ic) class/module I extendet like this:
class TechDraw
constructor: ->
$(window).load () =>
#Connector=window.TechDrawConnector(this)
#LineGuy=window.TechDrawLineGuy(this)
#Connector.wave('init')
Since I am a newbee in coffeescript (yes javascript also) my solution feels to simple ...
Have I overseen somthing? The Polution of the global namespace is not so bad I think
You cant create an "extension" that way.
If you define the same class in the same namespace a second time the first class will simply be overwritten and become in accessible. This will mostly be dependent on the order of loading of the compiled JavaScript files.
However you could either later add an method to the prototype of the class
#file a.coffee
class A
constructor: ->
#foo()
#file B.coffee
A::foo = -> #do something
However this is no good style and can certainly be very confusing some time and lead to brittle errors.
Better would be to use a form of dependency injection
#file a.coffee
class A
constructor: (#closure) ->
$(window).load () => #closure()
#file B.coffee
new A () ->
#Connector=window.TechDrawConnector(#)
#LineGuy=window.TechDrawLineGuy(#)
#Connector.wave('init')

Purpose of # symbol infront of functions [duplicate]

This question already has an answer here:
Static classes and methods in coffeescript
(1 answer)
Closed 8 years ago.
I cant understand exactly what # (this) does infront of class functions. I am using Spine framework, and I got a class Contact which extends Spine.model. I override the Spine methods for creating, deleting, fetching etc, and have them print on the console first like this:
create: ->
console.log('create')
super
destroy: ->
console.log("destroy")
super
Some of these methods will work the same if I add # infront, for example create, while others will not work without #, and others will not work with #. All the methods I override can be seen on the link above.
Is it possible to explain me the effect of # symbol infront of
functions, and why its causing this behaviour?
Answer from mu is too short :
You can define class methods by prefixing them with #:
class Box2DUtility
constructor: () ->
#drawWorld: (world, context) -> alert 'World drawn!
'
And then draw your world...
Box2DUtility.drawWorld()
Demo: http://jsfiddle.net/ambiguous/5yPh7/
And if you want your drawWorld to act like a constructor then you can say new # like this:
class Box2DUtility
constructor: (s) -> #s = s
m: () -> alert "instance method called: #{#s}"
#drawWorld: (s) -> new # s
Box2DUtility.drawWorld('pancakes').m()
Demo: http://jsfiddle.net/ambiguous/bjPds/1/

how to refer the parent object in an event handler using Coffeescript fat arrow

Hi I'm new to Js and Coffeescript, here's a situation I feel difficult to refer the property of parent object which is App in the following example
App =
init: ->
this.foo = 'bar'
this.bindEvent()
bindEvent: ->
$('#test').click(this.show)
show: ->
alert this.foo
App.init()
I think the fat arrow may do the trick but once I've changed to show: =>, this in the context of show method refers to the window object, instead of App object that I want to
. Anybody can tell me how to do it right?
http://jsfiddle.net/kZpHX/
When you define your show function, # (AKA this) actually is window so
show: => console.log(#)
will bind show to window. The problem is that you're simply defining an object so there isn't anything to bind to: you aren't defining a class so this is window. You could refer to App explicitly like this:
App =
#...
show: -> alert(App.foo)
Demo: http://jsfiddle.net/ambiguous/3sRVh/
The this.foo in init will do the right thing because saying App.init() sets up the expected this.
You could also hook up the desired this manually:
bindEvent: ->
$('#test').click => #show()
# or
bindEvent: ->
_this = # # JavaScript style
$('#test').click -> _this.show()
Demos: http://jsfiddle.net/ambiguous/byL45/, http://jsfiddle.net/ambiguous/MT8fG/
Or you could create a class for your App instead:
class App
constructor: ->
#foo = 'bar'
#bindEvent()
bindEvent: ->
$('#test').click(#show)
show: =>
console.log(#foo)
new App
That way your show: => will behave the way you expect it to.
Demo: http://jsfiddle.net/ambiguous/byatH/

#function in CoffeeScript does not work as advertised

Rails 3.2.8. In converting some of my JS functions over to CoffeeScript, I have come across several questions that say declaring a function like so:
#foo = (bar) ->
puts foo in the global namespace. But it doesn't, because my function calls elsewhere in the application, especially ones that are in .js.erb files.
Here's what does work:
foo = (bar) ->
window.foo = foo
With that, all my calls in .js.erb files work fine.
What is the reason that the #foo = notation doesn't work as I am expecting it to? That would be a lot easier than having to remember to add an extra line to expose the function to the global namespace.
#foo translates to this.foo
foo translates to var foo
There is a big difference between the two of those.
For example:
bar = (baz) ->
#bar = 5
lemon = #bar + baz
#foo = (bar) ->
lemon
return #
Bar = new bar(12)
Translates to:
var Bar, bar;
bar = function(baz) {
var lemon;
this.bar = 5;
lemon = this.bar + baz;
this.foo = function(bar) {
return lemon;
};
return this;
};
Bar = new bar(12);
See fiddle demo of generated code: http://jsfiddle.net/maniator/rXWw2/
Here is a link showing you the CoffeeScript and it's generated code
I just wanted to add something that seems important to understanding why the initial
#foo = () -> "abc"
doesn't add to the global window object.
Coffeescript wraps - once compiled, it wraps all contents of every .coffee-file into a surrounding anonymous function that is immediately executed.
Thusly, and explicitely for that reason, the global namespace is not polluted, thereby implicitely protecting the dev from creating "evil" globals.
So, your foo becomes a member function of an anonymous wrapper function - how seriously useless gg ...
I guess what you want anyway is your global config object or something, to which you simply add your definitions --- you surely didn't mean to really create globals just for quick and easy access now, did you?? :)