Skip to content

Commit 9654486

Browse files
authored
Merge pull request #58 from jchristgit/add-new-documentation-features
Update Erlang syntax for recent language changes
2 parents 2754541 + 43d18d3 commit 9654486

File tree

2 files changed

+259
-38
lines changed

2 files changed

+259
-38
lines changed

syntax/erlang.vim

Lines changed: 115 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,80 @@ endif
3737
let s:cpo_save = &cpo
3838
set cpo&vim
3939

40+
" "g:erlang_old_style_highlight": Whether to use old style highlighting.
41+
"
42+
" * "g:erlang_old_style_highlight == 0" (default): Use new style
43+
" highlighting.
44+
"
45+
" * "g:erlang_old_style_highlight == 1": Use old style highlighting.
46+
let s:old_style = (exists("g:erlang_old_style_highlight") &&
47+
\g:erlang_old_style_highlight == 1)
48+
49+
" "g:erlang_use_markdown_for_docs": Whether to use Markdown highlighting in
50+
" docstrings.
51+
"
52+
" * "g:erlang_use_markdown_for_docs == 1": Enable Markdown highlighting in
53+
" docstrings.
54+
"
55+
" * "g:erlang_use_markdown_for_docs == 0" (default): Disable Markdown
56+
" highlighting in docstrings.
57+
if exists("g:erlang_use_markdown_for_docs")
58+
let s:use_markdown = g:erlang_use_markdown_for_docs
59+
else
60+
let s:use_markdown = 0
61+
endif
62+
63+
" "g:erlang_docstring_default_highlight": How to highlight the text inside
64+
" docstrings (except the text which is highlighted by Markdown).
65+
"
66+
" If "g:erlang_use_markdown_for_docs == 1":
67+
"
68+
" * "g:erlang_docstring_default_highlight == 'Comment'" (default): the plugin
69+
" highlights the plain text inside Markdown as Markdown normally does,
70+
" with comment highlighting to regular text in the docstring.
71+
"
72+
" * If you set g:erlang_docstring_default_highlight to the name of highlight
73+
" group, for example "String", the plugin highlights the plain text inside
74+
" Markdown with the specified highlight group. See ":highlight" for the
75+
" available groups. You may also set it to an empty string to disable any
76+
" specific highlighting.
77+
"
78+
" If "g:erlang_use_markdown_for_docs == 0":
79+
"
80+
" * "g:erlang_docstring_default_highlight == 'Comment'" (default): the plugin
81+
" does not highlight the contents of the docstring as markdown, but
82+
" continues to display them in the style of comments.
83+
"
84+
" * If you set g:erlang_docstring_default_highlight to the name of highlight
85+
" group, for example "String", the plugin highlights the plain text inside
86+
" Markdown with the specified highlight group. See ":highlight" for the
87+
" available groups. You may also set it to an empty string to disable any
88+
" specific highlighting.
89+
"
90+
" Configuration examples:
91+
"
92+
" " Highlight docstrings as Markdown.
93+
" let g:erlang_use_markdown_for_docs = 1
94+
" let g:erlang_docstring_default_highlight = 'Comment'
95+
"
96+
" " 1. Highlight Markdown elements in docstrings as Markdown.
97+
" " 2. Highlight the plain text in docstrings as String.
98+
" let g:erlang_use_markdown_for_docs = 1
99+
" let g:erlang_docstring_default_highlight = 'String'
100+
"
101+
" " Highlight docstrings as strings.
102+
" let g:erlang_use_markdown_for_docs = 0
103+
" let g:erlang_docstring_default_highlight = 'String'
104+
"
105+
" " Highlight docstrings as comments (default).
106+
" let g:erlang_use_markdown_for_docs = 0
107+
" let g:erlang_docstring_default_highlight = 'Comment'
108+
if exists("g:erlang_docstring_default_highlight")
109+
let s:docstring_default_highlight = g:erlang_docstring_default_highlight
110+
else
111+
let s:docstring_default_highlight = 'Comment'
112+
endif
113+
40114
" Case sensitive
41115
syn case match
42116

@@ -55,6 +129,21 @@ syn match erlangNumberFloat '\<\d\+\.\d\+\%([eE][+-]\=\d\+\)\=\>'
55129

56130
" Strings, atoms, characters
57131
syn region erlangString start=/"/ end=/"/ contains=erlangStringModifier
132+
syn region erlangStringTripleQuoted matchgroup=String start=/"""/ end=/\%(^\s*\)\@<="""/ keepend
133+
134+
" Documentation
135+
syn region erlangDocString start=/^-\%(module\)\=doc\s*\~\="""/ end=/\%(^\s*\)\@<="""\.$/ contains=@erlangDocStringCluster keepend
136+
syn region erlangDocString start=/^-\%(module\)\=doc\s*<<"""/ end=/\%(^\s*\)\@<=""">>\.$/ contains=@erlangDocStringCluster keepend
137+
syn region erlangDocString start=/^-\%(module\)\=doc\s*\~\="/ end=/"\.$/ contains=@erlangDocStringCluster keepend
138+
syn region erlangDocString start=/^-\%(module\)\=doc\s*<<"/ end=/">>\.$/ contains=@erlangDocStringCluster keepend
139+
syn cluster erlangDocStringCluster contains=erlangInnerDocAttribute,erlangDocStringDelimiter
140+
syn region erlangDocStringDelimiter matchgroup=erlangString start=/"/ end=/"/ contains=@erlangDocStringContained contained
141+
syn region erlangDocStringDelimiter matchgroup=erlangString start=/"""/ end=/"""/ contains=@erlangDocStringContained contained
142+
143+
if s:use_markdown
144+
syn cluster erlangDocStringContained contains=@markdown
145+
endif
146+
58147
syn region erlangQuotedAtom start=/'/ end=/'/ contains=erlangQuotedAtomModifier
59148
syn match erlangStringModifier '\\\%(\o\{1,3}\|x\x\x\|x{\x\+}\|\^.\|.\)\|\~\%([ni~]\|\%(-\=\d\+\|\*\)\=\.\=\%(\*\|\d\+\)\=\%(\..\)\=[tl]*[cfegswpWPBX#bx+]\)' contained
60149
syn match erlangQuotedAtomModifier '\\\%(\o\{1,3}\|x\x\x\|x{\x\+}\|\^.\|.\)' contained
@@ -94,12 +183,14 @@ syn match erlangBitType '\%(\/\%(\s\|\n\|%.*\n\)*\)\@<=\%(integer\|float\|binary
94183

95184
" Constants and Directives
96185
syn match erlangUnknownAttribute '^\s*-\%(\s\|\n\|%.*\n\)*\l[[:alnum:]_@]*' contains=erlangComment
97-
syn match erlangAttribute '^\s*-\%(\s\|\n\|%.*\n\)*\%(behaviou\=r\|compile\|export\(_type\)\=\|file\|import\|module\|author\|copyright\|doc\|vsn\|on_load\|optional_callbacks\|feature\|mode\)\>' contains=erlangComment
186+
syn match erlangAttribute '^\s*-\%(\s\|\n\|%.*\n\)*\%(behaviou\=r\|compile\|dialyzer\|export\|export_type\|file\|import\|module\|author\|copyright\|vsn\|on_load\|optional_callbacks\|feature\|mode\)\>' contains=erlangComment
187+
syn match erlangDocAttribute '^\s*-\%(\s\|\n\|%.*\n\)*\%(moduledoc\|doc\)\>' contains=erlangComment,erlangDocString
188+
syn match erlangInnerDocAttribute '^\s*-\%(\s\|\n\|%.*\n\)*\%(moduledoc\|doc\)\>' contained
98189
syn match erlangInclude '^\s*-\%(\s\|\n\|%.*\n\)*\%(include\|include_lib\)\>' contains=erlangComment
99190
syn match erlangRecordDef '^\s*-\%(\s\|\n\|%.*\n\)*record\>' contains=erlangComment
100191
syn match erlangDefine '^\s*-\%(\s\|\n\|%.*\n\)*\%(define\|undef\)\>' contains=erlangComment
101192
syn match erlangPreCondit '^\s*-\%(\s\|\n\|%.*\n\)*\%(ifdef\|ifndef\|else\|endif\)\>' contains=erlangComment
102-
syn match erlangType '^\s*-\%(\s\|\n\|%.*\n\)*\%(spec\|type\|opaque\|callback\)\>' contains=erlangComment
193+
syn match erlangType '^\s*-\%(\s\|\n\|%.*\n\)*\%(spec\|type\|opaque\|nominal\|callback\)\>' contains=erlangComment
103194

104195
" Keywords
105196
syn keyword erlangKeyword after begin case catch cond end fun if let of else
@@ -147,9 +238,21 @@ let b:erlang_syntax_synced = 1
147238
" Define the default highlighting. See ":help group-name" for the groups and
148239
" their colors.
149240

150-
let s:old_style = (exists("g:erlang_old_style_highlight") &&
151-
\g:erlang_old_style_highlight == 1)
241+
if s:use_markdown
242+
" Add markdown syntax elements for docstrings (actually, for all
243+
" triple-quoted strings).
244+
unlet! b:current_syntax
152245

246+
syn include @markdown syntax/markdown.vim
247+
let b:current_syntax = "erlang"
248+
249+
" markdown-erlang.vim includes html.vim, which includes css.vim, which adds
250+
" the dash character (-) to the list of syntax keywords, which causes
251+
" `-VarName` not to be highlighted as a variable in the Erlang code.
252+
"
253+
" Here we override that.
254+
syntax iskeyword @,48-57,192-255,$,_
255+
endif
153256

154257
" Comments
155258
hi def link erlangComment Comment
@@ -163,6 +266,12 @@ hi def link erlangNumberFloat Float
163266

164267
" Strings, atoms, characters
165268
hi def link erlangString String
269+
hi def link erlangStringTripleQuoted String
270+
271+
" Triple quoted strings
272+
if s:docstring_default_highlight != ''
273+
execute 'hi def link erlangDocStringDelimiter '. s:docstring_default_highlight
274+
endif
166275

167276
if s:old_style
168277
hi def link erlangQuotedAtom Type
@@ -232,6 +341,8 @@ hi def link erlangPreCondit Type
232341
hi def link erlangType Type
233342
else
234343
hi def link erlangAttribute Keyword
344+
hi def link erlangDocAttribute Keyword
345+
hi def link erlangInnerDocAttribute Keyword
235346
hi def link erlangMacroDef Macro
236347
hi def link erlangUnknownAttribute Normal
237348
hi def link erlangInclude Include

0 commit comments

Comments
 (0)