Skip to content

Commit

Permalink
Merge pull request #365 from HugoForrat/master
Browse files Browse the repository at this point in the history
LF filetype Vim plugin
  • Loading branch information
lhstrh authored Jul 6, 2021
2 parents b8f4719 + 5404bb0 commit 54db272
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 0 deletions.
3 changes: 3 additions & 0 deletions util/lingua-franca.vim/ftdetect/linguafranca.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
" ftdetect/linguafranca.vim

autocmd BufNewFile,BufRead *.lf setfiletype linguafranca
26 changes: 26 additions & 0 deletions util/lingua-franca.vim/ftplugin/linguafranca.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
" ftplugin/linguafranca.vim
" Arbitrary linguafranca related vim code

setlocal commentstring=//%s
setlocal formatoptions=jcroq
" 'comments' configuration stolen from the C file, let's see how it works
setlocal comments=sO:*\ -,mO:*\ \ ,exO:*/,s1:/*,mb:*,ex:*/,://

" function! CursorInTargetBlock()
" " TODO
" " See: :help searchpair(), searchpairpos(), `n` flag
" endfunction

" function! LFTargetTextObject()
" " TODO
" if ! CursorInTargetBlock()
" return
" endif
" endfunction

" Text objects for the target blocks
" Don't work very well but can be convenient
onoremap <buffer> i= :<C-u>execute "normal! ?{=?e+1\rv/=}/b-1\r"<cr>
xnoremap <buffer> i= :<C-u>execute "normal! ?{=?e+1\rv/=}/b-1\r"<cr>
onoremap <buffer> a= :<C-u>execute "normal! ?{=\rv/=}/e\r"<cr>
xnoremap <buffer> a= :<C-u>execute "normal! ?{=\rv/=}/e\r"<cr>
61 changes: 61 additions & 0 deletions util/lingua-franca.vim/syntax/linguafranca.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
" syntax/linguafranca.vim

" quit when a syntax file was already loaded
if exists("b:current_syntax")
finish
endif

function! LFGetTarget()
" cf https://github.com/icyphy/lingua-franca/blob/0870cf86185733180d7b09f32fa5221a948af83b/org.lflang/src/org/lflang/LinguaFranca.xtext#L118
" and https://www.eclipse.org/Xtext/documentation/301_grammarlanguage.html#common-terminals
let l:pattern = '^\s*target\s*\(\h\h*\)\s*\({\|;\)\?\s*'
for l in getbufline("", 1, "$")
if l =~# l:pattern
let l:result = substitute(l, l:pattern, '\1', "")
break
endif
endfor

if exists("l:result")
return l:result
else
" TODO Handle error
return "Cpp"
endif
endfunction

function! LFGetSyntaxFile()
return "syntax/". tolower(LFGetTarget()). ".vim"
endfunction

" case sensitive
syntax case match

" Keywords
syntax keyword lfKeywords target import main realtime reactor state time mutable input output timer
\ action reaction startup shutdown after deadline mutation preamble new federated at as from
syntax keyword lfActionOrigins logical physical
syntax keyword lfTimeUnits nsec nsecs usec usecs msec msecs sec secs second seconds
\ min mins minute minutes hour hours day days week weeks

highlight def link lfKeywords Keyword
highlight def link lfActionOrigins Keyword
highlight def link lfTimeUnits StorageClass

" Matches
syntax match lfComment :\(#.*$\|//.*$\):
hi def link lfComment Comment
syntax match lfTargetDelim :\({=\|=}\):
hi def link lfTargetDelim Delimiter

" Regions
execute "syntax include @TARGET ". LFGetSyntaxFile()
syntax region lfTargetLang keepend start=/{=/ contains=@TARGET end=/=}/

syntax region lfBlockComment start=:/\*: end=:\*/:
hi def link lfBlockComment Comment

syntax region lfString start=:": skip=:\\": end=:":
hi def link lfString String

let b:current_syntax = "linguafranca"

0 comments on commit 54db272

Please sign in to comment.