-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGenerateVim.xtend
504 lines (406 loc) · 15.6 KB
/
GenerateVim.xtend
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
package org.xtext.example.mydsl3
import com.google.inject.Injector
import org.eclipse.emf.ecore.EObject
import org.eclipse.emf.mwe2.runtime.Mandatory
import org.eclipse.xtend.lib.annotations.Accessors
import org.eclipse.xtext.AbstractElement
import org.eclipse.xtext.Alternatives
import org.eclipse.xtext.CharacterRange
import org.eclipse.xtext.EOF
import org.eclipse.xtext.GrammarUtil
import org.eclipse.xtext.Group
import org.eclipse.xtext.Keyword
import org.eclipse.xtext.NegatedToken
import org.eclipse.xtext.ParserRule
import org.eclipse.xtext.RuleCall
import org.eclipse.xtext.TerminalRule
import org.eclipse.xtext.UntilToken
import org.eclipse.xtext.Wildcard
import org.eclipse.xtext.util.Strings
import org.eclipse.xtext.util.XtextSwitch
import org.eclipse.xtext.xtext.generator.AbstractXtextGeneratorFragment
import org.eclipse.xtext.xtext.generator.model.IXtextGeneratorFileSystemAccess
import org.eclipse.xtext.xtext.generator.model.XtextGeneratorFileSystemAccess
import org.eclipse.xtext.xtext.generator.parser.antlr.AntlrGrammarGenUtil
import java.util.HashSet
class GenerateVim extends AbstractXtextGeneratorFragment {
String absolutePath
@Accessors(#[PROTECTED_GETTER, PUBLIC_SETTER])
boolean ^override = false
IXtextGeneratorFileSystemAccess outputLocation
override generate() {
// content is produced here
// Currently processed Grammar is obtained via getGrammar
val grammarName = GrammarUtil.getSimpleName(grammar)
val resourceName = language.fileExtensions.get(0)
val ftAccess = language.fileExtensions.map[ ext |
'''au BufNewFile,BufRead *.«ext» set filetype=«grammarName»'''
].join("\n")
outputLocation.generateFile('ftdetect/ftaccess.vim', ftAccess)
val autoload = '''
function! «grammarName»#Errorformat()
return '%f: %l: %m'
endfunction
function! «grammarName»#GetValidatorExePath()
" http://superuser.com/questions/119991/how-do-i-get-vim-home-directory
let $VIMHOME=expand('<sfile>:p:h')
return escape('./' . findfile('validator.rb', $VIMHOME.';'), ' \')
endfunction
function! «grammarName»#GetCompleterExePath()
let $VIMHOME=expand('<sfile>:p:h')
return escape('./' . findfile('completer.rb', $VIMHOME.';'), ' \')
endfunction
function! «grammarName»#GetFormatterExePath()
let $VIMHOME=expand('<sfile>:p:h')
return escape('./' . findfile('formatter.rb', $VIMHOME.';'), ' \')
endfunction
function! «grammarName»#GetCurrBuffContents()
return join(getline(1, "$"), "\n")
endfunction
function! «grammarName»#Complete(findstart, base)
if a:findstart
return getline('.') =~# '\v^\s*$' ? -1 : 0
else
if empty(a:base)
return []
endif
let l:results = []
let l:file_contents = «grammarName»#GetCurrBuffContents()
" line2byte(line('.')) gives the number of bytes in the buffer up until
" this point. This is the place that the base needs to be inserted at
let l:byte_index = line2byte(line('.')) - 1
let l:completion_point = l:byte_index + len(a:base)
let l:full_file_contents = strpart(l:file_contents, 0, l:byte_index).a:base.(strpart(l:file_contents, l:byte_index))
let l:completions =
\ system(«grammarName»#GetCompleterExePath().' '.l:completion_point, l:full_file_contents)
let l:cmd = substitute(a:base, '\v\S+$', '', '')
for l:line in split(l:completions, '\n')
let l:tokens = split(l:line, '\t')
call add(l:results, {'word': l:cmd.l:tokens[0],
\'abbr': l:tokens[0],
\'menu': get(l:tokens, 1, '')})
endfor
return l:results
endif
endfunction
'''
outputLocation.generateFile("autoload/" + grammarName + ".vim", autoload)
var compiler = '''
if exists('current_compiler')
finish
endif
" Essentially a polyfill for old versions of vim
if exists(":CompilerSet") != 2
command -nargs=* CompilerSet setlocal <args>
endif
let current_compiler = «grammarName»#GetValidatorExePath()
execute 'CompilerSet makeprg=' . «grammarName»#GetValidatorExePath() . '\ %'
execute 'CompilerSet errorformat='.escape(«grammarName»#Errorformat(), ' ')
'''
outputLocation.generateFile("compiler/" + grammarName + ".vim", compiler)
var ftplugin = '''
setlocal omnifunc=«grammarName»#Complete
setlocal equalprg=./formatter.rb
setlocal formatprg=./formatter.rb
compiler «grammarName»
'''
outputLocation.generateFile("ftplugin/" + grammarName + ".vim", ftplugin)
var keywords = GrammarUtil.getAllKeywords(grammar).join(" ")
// TODO
var extra = '''
«/*collectTerminalsAsVimRules(grammarName)*/»
'''
var syntax = '''
syntax keyword «grammarName»Keyword «keywords»
«extra»
highlight link «grammarName»Keyword Keyword
'''
outputLocation.generateFile("syntax/" + grammarName + ".vim", syntax)
var syntastic = '''
if exists('g:loaded_syntastic_«grammarName»_«grammarName»_checker')
finish
endif
let g:loaded_syntastic_«grammarName»_«grammarName»_checker = 1
let s:save_cpo = &cpo
set cpo&vim
function! SyntaxCheckers_«grammarName»_«grammarName»_GetLocList() dict
" TODO switch the 'exe' here to 'exec'
let l:makeprg = self.makeprgBuild({'exe': «grammarName»#GetValidatorExePath() })
return SyntasticMake({'makeprg': l:makeprg,
\ 'errorformat': «grammarName»#Errorformat() })
endfunction
function! SyntaxCheckers_«grammarName»_«grammarName»_IsAvailable() dict
" TODO make this actually check whether or not its available
return 1
endfunction
call g:SyntasticRegistry.CreateAndRegisterChecker({'filetype': '«grammarName»',
\'name': '«grammarName»'})
if exists('g:syntastic_extra_filetypes')
call add(g:syntastic_extra_filetypes, '«grammarName»')
else
let g:syntastic_extra_filetypes = ['«grammarName»']
endif
let &cpo = s:save_cpo
unlet s:save_cpo
'''
outputLocation.generateFile('''syntax_checkers/«grammarName»/«grammarName».vim''', syntastic)
var completer = '''
#!/usr/bin/ruby
require 'JSON'
require 'stringio'
require 'uri'
require 'net/http'
file_contents = ''
stdin_present = false
if STDIN.tty?
file_contents = IO.read ARGF.argv[0]
else
file_contents = $stdin.read
stdin_present = true
end
file_offset = 0
num_args = ARGF.argv.length
arg_offset = stdin_present ? -1 : 0
if ARGF.argv.length == 3 || (stdin_present && num_args == 2)
target_line = ARGF.argv[1 + arg_offset].to_i
target_offset = ARGF.argv[2 + arg_offset].to_i
# $/ is a ruby builtin that gets you the current system's line separator
file_contents.split($/).each.with_index do |line, num|
if (num + 1) == target_line
file_offset += target_offset
break
end
file_offset += line.length + $/.length
end
elsif ARGF.argv.length == 2 || (stdin_present && num_args == 1)
file_offset = ARGF.argv[1 + arg_offset].to_i
else
puts "Usage:\n ./complete.rb <filepath> (<char offset>)|(<line> <column>)"
puts "OR\n cat <filepath> | ./complete.rb (<char offset>)|(<line> <column>)"
exit
end
params_str = "?resource=text.«resourceName»"
params_str += "&fullText=#{URI.escape(file_contents)}"
params_str += "&caretOffset=#{file_offset}"
uri = URI.parse "http://localhost:8080/xtext-service/assist#{params_str}"
res = Net::HTTP.post_form uri, {}
json = JSON.parse res.body
s = StringIO.new
json['entries'].each do |entry|
s << entry['proposal']
s << "\n"
end
puts s.string
'''
outputLocation.generateFile("completer.rb", completer)
var validator = '''
#!/usr/bin/ruby
require 'JSON'
require 'stringio'
require 'uri'
require 'net/http'
file_contents = IO.read ARGF.argv[0]
params_str = "?resource=text.«resourceName»&fullText=#{URI.escape(file_contents)}"
uri = URI.parse "http://localhost:8080/xtext-service/validate#{params_str}"
res = Net::HTTP.post_form uri, {}
json = JSON.parse res.body
s = StringIO.new
json['issues'].each do |issue|
s<< ARGF.argv[0]
s << ': '
s << issue['line']
s << ': '
s << issue['description']
s << "\n"
end
puts s.string
'''
outputLocation.generateFile("validator.rb", validator)
var formatter = '''
#!/usr/bin/ruby
require 'JSON'
require 'stringio'
require 'uri'
require 'net/http'
file_contents = $stdin.read
params_str = "?resource=text.«resourceName»&fullText=#{URI.escape(file_contents)}"
uri = URI.parse "http://localhost:8080/xtext-service/format#{params_str}"
res = Net::HTTP.post_form uri, {}
json = JSON.parse res.body
puts json['formattedText']
'''
outputLocation.generateFile("formatter.rb", formatter)
}
def String collectTerminalsAsVimRules(String grammarName) {
var terminals = GrammarUtil.allTerminalRules(grammar)
var result = new StringBuilder()
var mappingsNeeded = new HashSet()
for (TerminalRule rule: terminals){
val regex = "\"\\v" + TerminalRuleToRegEx.toRegEx(rule) + "\""
if (rule.name.toLowerCase().contains("ID")){
result.append('''syntax match «grammarName»Identifier «regex»''').append("\n")
mappingsNeeded.add("identifier")
} else if (rule.name.toLowerCase().contains("comment")){
result.append('''syntax match «grammarName»Comment «regex»''').append("\n")
mappingsNeeded.add("comment");
} else if (rule.name.toLowerCase().contains("boolean")){
result.append('''syntax match «grammarName»Boolean «regex»''').append("\n")
mappingsNeeded.add("boolean");
} else if (rule.name.toLowerCase().contains("constant")){
result.append('''syntax match «grammarName»Constant «regex»''').append("\n")
mappingsNeeded.add("constant");
}
}
for (String mapping: mappingsNeeded){
if (mapping.equals("identifier")){
result.append('''highlight link «grammarName»Identifier Identifier''').append("\n")
} else if (mapping.equals("comment")){
result.append('''highlight link «grammarName»Comment Comment''').append("\n")
} else if (mapping.equals("boolean")){
result.append('''highlight link «grammarName»Boolean Boolean''').append("\n")
} else if (mapping.equals("comment")){
result.append('''highlight link «grammarName»Boolean Boolean''').append("\n")
}
}
return result.toString()
}
protected def getOutputLocation() {
return outputLocation
}
override initialize(Injector injector) {
super.initialize(injector)
this.outputLocation = new XtextGeneratorFileSystemAccess(absolutePath, override)
injector.injectMembers(outputLocation)
}
protected def getAbsolutePath() {
return absolutePath
}
@Mandatory
def void setAbsolutePath(String absolutePath) {
this.absolutePath = absolutePath
}
}
// vim regex reference:
// http://vimregex.com/
// TODO
// need to correlate the XText types of terminal rules with vim's character classes
// then link each character class used to the master vim one
// eg
// syntax keyword smKeyword input signal
// highlight link smKeyword Keyword
// possible vim character classes are
/*
Comment
Constant
Identifier
Statement
Operator
PreProc
Type
Special
Underlined
Ignore
Error
Todo
*/
// Oh and make sure that the regexes produced are all prefixed with verymagic
class TerminalRuleToRegEx extends XtextSwitch<String> {
final StringBuilder result
private new(){
this.result = new StringBuilder()
}
def static String toRegEx(TerminalRule rule) {
return new TerminalRuleToRegEx().print(rule)
}
def String print(TerminalRule rule) {
doSwitch(rule.getAlternatives())
return result.toString()
}
// see vim's help docs for details on what very magic is
// basically very magic (\v) is as close as vim gets to perl regex
// :help magic
def static private String veryMagicEscape(String str){
return str
.replaceAll("\\n", "\\\\n") //replaces a newline with \n
.replaceAll("\\r\\n", "\\\\r\\\\n") // replaces newline carriage return with \r\n
.replaceAll(".", "\\.") // the only sane one here
.replaceAll("\\{", "\\\\{") // replaces a left curly with backslash left curly
.replaceAll("\\\\", "\\\\\\\\") // replaces single backslash with double backslash
}
override String caseAlternatives(Alternatives object) {
result.append(Character.valueOf('(').charValue)
var boolean first=true
for (AbstractElement elem : object.getElements()) {
if (!first) result.append(Character.valueOf('|').charValue)
first=false
doSwitch(elem)
}
result.append(Character.valueOf(')').charValue).append(Strings.emptyIfNull(object.getCardinality()))
return ""
}
override String caseCharacterRange(CharacterRange object) {
if (!Strings.isEmpty(object.getCardinality())) result.append(Character.valueOf('(').charValue)
doSwitch(object.getLeft())
result.append("..")
doSwitch(object.getRight())
if (!Strings.isEmpty(object.getCardinality())) {
result.append(Character.valueOf(')').charValue)
result.append(Strings.emptyIfNull(object.getCardinality()))
}
return ""
}
override String defaultCase(EObject object) {
throw new IllegalArgumentException('''«object.eClass().getName()» is not a valid argument.''')
}
override String caseGroup(Group object) {
if (!Strings.isEmpty(object.getCardinality())) result.append(Character.valueOf('(').charValue)
var boolean first=true
for (AbstractElement elem : object.getElements()) {
if (!first) result.append(Character.valueOf('|').charValue)
first=false
doSwitch(elem)
}
if (!Strings.isEmpty(object.getCardinality())) result.append(Character.valueOf(')').charValue)
result.append(Strings.emptyIfNull(object.getCardinality()))
return ""
}
override String caseKeyword(Keyword object) {
var String value = veryMagicEscape(object.value)
result.append(value)
result.append(Strings.emptyIfNull(object.getCardinality()))
return ""
}
override String caseWildcard(Wildcard object) {
result.append(Character.valueOf('.').charValue)
result.append(Strings.emptyIfNull(object.getCardinality()))
return ""
}
override String caseEOF(EOF object) {
result.append("EOF")
result.append(Strings.emptyIfNull(object.getCardinality()))
return ""
}
override String caseTerminalRule(TerminalRule object) {
result.append(AntlrGrammarGenUtil.getRuleName(object))
return ""
}
override String caseParserRule(ParserRule object) {
throw new IllegalStateException("Cannot call parser rules that are not terminal rules.")
}
override String caseRuleCall(RuleCall object) {
doSwitch(object.getRule())
result.append(Strings.emptyIfNull(object.getCardinality()))
return ""
}
override String caseNegatedToken(NegatedToken object) {
result.append("[^")
doSwitch(object.getTerminal())
result.append("]").append(Strings.emptyIfNull(object.getCardinality()))
return ""
}
override String caseUntilToken(UntilToken object) {
result.append(".\\{-}")
doSwitch(object.getTerminal())
return ""
}
}