/
scala.xpt.vim
336 lines (271 loc) · 8.01 KB
/
scala.xpt.vim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
XPTemplate priority=personal
XPTvar $BRif ' '
XPTvar $BRel \n
XPTvar $BRloop ' '
XPTvar $BRfun ' '
XPTvar $author 'Derek Wyatt'
XPTvar $email derek@derekwyatt.org
XPTinclude
\ _common/personal
\ java/java
let s:f = g:XPTfuncs()
function! s:f.year(...)
return strftime("%Y")
endfunction
function! s:f.getPackageForFile(...)
let dir = expand('%:p:h')
let regexes = [
\ [ '/src/main/scala', '/src/main/scala' ],
\ [ '/src/test/scala', '/src/test/scala' ],
\ [ '/src/it/scala', '/src/it/scala' ],
\ [ '/src/multi-jvm/scala', '/src/multi-jvm/scala' ],
\ [ '/app/model/scala', '/app/model/scala' ],
\ [ '/app/controllers', '/app' ],
\ [ '/test/scala', '/test/scala' ]
\ ]
for e in regexes
let idx = match(dir, e[0])
if idx != -1
let subdir = strpart(dir, idx + strlen(e[1]) + 1)
let package = substitute(subdir, '/', '.', 'g')
return package
endif
endfor
return ''
endfunction
function! s:f.classname(...)
return expand('%:t:r')
endfunction
function! s:f.multijvmObject(...)
return substitute(s:f.classname(), 'Spec$', 'MultiJvmSpec', '')
endfunction
function! s:f.multijvmBase(...)
return substitute(s:f.classname(), 'Spec$', 'Base', '')
endfunction
function! s:f.classNameFromSpec(...)
return substitute(s:f.classname(), 'Spec$', '', '')
endfunction
function! s:f.multiJvmNode(num, ...)
let className = substitute(s:f.classname(), 'Spec$', 'MultiJvmNode', '') . a:num
let class = join(['class ' . className . ' extends AkkaRemoteSpec(' . s:f.multijvmObject() . '.nodeConfigs(' . (a:num - 1). '))',
\ ' with ImplicitSender',
\ ' with ' . s:f.multijvmBase() . ' {',
\ ' import ' . s:f.multijvmObject() . '._',
\ ' import ' . s:f.classNameFromSpec() . '._',
\ ' val nodes = NrOfNodes',
\ '',
\ ' "' . s:f.classNameFromSpec() . '" should {',
\ ' }',
\ '}'], "\n")
return class
endfunction
function! s:f.multiJvmNodes(num, ...)
let n = 1
let s = ''
while n <= a:num
let s = s . s:f.multiJvmNode(n) . "\n"
let n = n + 1
endwhile
return s
endfunction
function! s:f.getFilenameWithPackage(...)
let package = s:f.getPackageForFile()
if strlen(package) == 0
return expand('%:t:r')
else
return package . '.' . expand('%:t:r')
endif
endfunction
function! s:f.getPackageLine(...)
let package = s:f.getPackageForFile()
if strlen(package) == 0
return ''
else
return "package " . package
endif
endfunction
function! s:f.getCurrentDir(...)
return expand('%:p:h:t')
endfunction
XPT impakka hint=Imports\ basic\ akka\ stuff
import akka.actor.{Actor, ActorRef, ActorSystem, Props}
XPT file hint=Standard\ Scala\ source\ file
//
// `getFilenameWithPackage()^
//
// Copyright (c) `year()^ Derek Wyatt (derek@derekwyatt.org)
//
`getPackageLine()^
`cursor^
XPT main hint=Creates\ a\ "Main"\ object
object `objectName^ {
def main(args: Array[String]) {
`cursor^
}
}
XPT app hint=Creates\ an\ "App"\ object
object `classname()^ extends App {
`cursor^
}
XPT afun hint=Creates\ an\ anonymous\ function
() => {
`cursor^
}
XPT cclass hint=Creates\ a\ case\ class
case class `className^(`...^`attrName^: `type^`...^)
XPT cobj hint=Creates\ a\ case\ object
case object `objectName^
XPT case hint=Creates\ a\ case\ statement
case `matchAgainst^ =>
XPT akkamain hint=Creates\ a\ simple\ Akka\ App\ for\ PoC
import akka.actor._
class MyActor extends Actor {
def receive = {
case _ =>
}
}
object Main {
val sys = ActorSystem()
def main(args: Array[String]) {
val a = sys.actorOf(Props[MyActor], "MyActor")
`cursor^
}
}
XPT specfile hint=Creates\ a\ new\ Specs2\ test\ file
`getPackageLine()^
import org.specs2.mutable._
class `classname()^ extends Specification {
"`classNameFromSpec()^" should {
"`spec^" in {
`cursor^
}
}
}
XPT wrapin wrap=code hint=Wraps\ in\ a\ block
`prefix^ {
`code^
}
XPT match hint=Creates\ a\ pattern\ matching\ sequence
`target^ match {
`...^case `matchTo^ => `statement^
`...^
}
XPT spec hint=Creates\ a\ new\ specs2\ test
"`spec^" in {
`cursor^
}
XPT wst hint=Creates\ a\ new\ WordSpec\ test
"`spec^" in {
`cursor^
}
XPT groupwordspec hint=Creates\ a\ new\ WordSpec\ test\ group
"`spec^" should {
`cursor^
}
XPT filewordspec hint=Creates\ a\ new\ WordSpec\ test\ file
`getPackageLine()^
import org.scalatest.{WordSpec, Matchers}
class `classname()^ extends WordSpec with Matchers {
"`classNameFromSpec()^" should {
`cursor^
}
}
// vim:fdl=1:
XPT eorp hint=envOrNone.orElse.propOrNone
envOrNone("`Variable^").orElse(propOrNone("`property^"))
XPT rcv hint=Akka\ Receive\ def
def `receive^: Receive = {
`cursor^
}
XPT actor hint=Akka\ Actor\ class
class `ActorName^ extends Actor with ActorLogging {
def receive = {
`cursor^
}
}
XPT aactor hint=Anonymous\ Akka\ Actor
actorOf(new Actor {
def receive = {
`cursor^
}
})
XPT akkaimp hint=Common\ Akka\ Imports
import akka.actor._
import akka.actor.Actor._
import akka.config.Supervision._
XPT ssf hint=self.sender.foreach\(_\ !\ ...\)
self.sender.foreach(_ ! `message^)
XPT proj hint=SBT\ Project
import sbt._
class `project^Project(info: ProjectInfo) extends `DefaultProject^(info) {
`cursor^
}
XPT projDepend hint=SBT\ Project\ dependency
XSET type=ChooseStr( 'provided', 'test', 'compile' )
lazy val `depName^ = "`package^" % "`name^" % "`version^" % "`type^"
XPT package hint=package\ for\ this\ file
`getPackageLine()^
XPT akkatest hint=Test\ file\ for\ Akka\ code
`getPackageLine()^
import akka.actor.{ActorSystem, Props, Actor, ActorRef}
import akka.testkit.{TestKit, ImplicitSender}
import org.scalatest.{BeforeAndAfterAll, Matchers, WordSpecLike}
class `classname()^ extends TestKit(ActorSystem("`classname()^"))
with ImplicitSender
with WordSpecLike
with BeforeAndAfterAll
with Matchers {
override def afterAll() {
system.shutdown()
}
"`classNameFromSpec()^" should {
`cursor^
}
}
XPT multijvm hint=Multi\ JVM\ Test\ for\ Scala
`getPackageLine()^
import akka.remote.{AkkaRemoteSpec, AbstractRemoteActorMultiJvmSpec}
import akka.testkit.{TestKit, ImplicitSender}
import com.typesafe.config.{Config, ConfigFactory}
import org.scalatest.{WordSpec, BeforeAndAfterAll}
import org.scalatest.matchers.MustMatchers
object `multijvmObject()^ extends AbstractRemoteActorMultiJvmSpec {
override def NrOfNodes = `numberOfNodes^
def commonConfig = ConfigFactory.parseString("""
akka.actor.provider = "akka.remote.RemoteActorRefProvider",
akka.remote.transport = "akka.remote.netty.NettyRemoteTransport"
""")
}
trait `multijvmBase()^ extends WordSpec
with BeforeAndAfterAll
with MustMatchers {
override def beforeAll(configMap: Map[String, Any]) {
}
override def afterAll(configMap: Map[String, Any]) {
}
}
`expandNodes...^
XSETm expandNodes...|post
`multiJvmNodes(R("numberOfNodes"))^
XSETm END
XPT bookblock wrap=code hint=Wraps\ a\ block\ of\ code\ in\ BEGIN/END
// FILE_SECTION_BEGIN{`name^}
`code^
// FILE_SECTION_END{`name^}
XPT mod hint=New\ module\ for\ dependency
"`groupId^" `%%^ "`artifactId^" % "`revision^"
XPT be hint=x\ must\ be\ \(y\)
`object^ must be (`target^)
XPT notbe hint=x\ must\ not\ be\ \(y\)
`object^ must not be (`target^)
XPT sbt hint=Creates\ a\ new\ build.sbt\ file
XSET name|def=getCurrentDir()
name := "`name^"
version := "`0.1^"
scalaVersion := "`2.10.3^"
`^
XPT dep hint=libraryDependencies\ :=\ Seq\(...\)
libraryDependencies := Seq(
"`groupId^" % "`artifactId^" % "`revision^",
"org.scalatest" %% "scalatest" % "2.0" % "test"
)