Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ README.html
test.vue
.DS_STORE
test/deps/
doc/tags
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ Vim syntax and indent plugin for `.vue` files. Mainly inspired by [mxw/vim-jsx][

You could install it just like other plugins. The filetype will be set to `vue`. Feel free to open an issue or a pull request if any questions

See `:help vim-vue-plugin` for the documentation in Vim.

<details>
<summary><a>How to install</a></summary>

Expand Down Expand Up @@ -92,6 +94,7 @@ It has the following options
- `lang="..."` on block tag decides the effective syntax
- When no valid `lang="..."` is present, the first syntax in the list will be used.
- By default, only syntax files from `['$VIMRUNTIME', '$VIM/vimfiles', '$HOME/.vim']` are loaded. If none is found, then **full** syntax files, including those from plugins, will be loaded
- Each key replaces the default of the same key, so `{'syntax': {'script': ['javascript', 'typescript']}}` keeps `template` as `['html']` and `style` as `['css']`. Set a key to `[]` to remove the block
- **full_syntax**: *string list*. Syntax whose **full** syntax files will always be loaded
- **initial_indent**: *string list*. Tag/syntax with initial one tab indent. The format can be `tag.syntax`, `tag`, or `syntax`

Expand Down
8 changes: 7 additions & 1 deletion autoload/vue.vim
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,16 @@ function! s:MergeUserConfigIntoDefault(user)
\'debug': 0,
\}

" Each key of syntax replaces the default of the same key, so blocks not in
" user config keep their default syntax
let user = a:user
for key in keys(default)
if has_key(user, key)
let default[key] = user[key]
if key ==# 'syntax' && type(user[key]) == v:t_dict
call extend(default[key], user[key])
else
let default[key] = user[key]
endif
endif
endfor

Expand Down
166 changes: 166 additions & 0 deletions doc/vim-vue-plugin.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
*vim-vue-plugin.txt* Syntax and indent for Vue files

Author: leafOfTree
License: The Unlicense

==============================================================================
CONTENTS *vim-vue-plugin-contents*

1. Introduction ............................... |vim-vue-plugin-intro|
2. Configuration .............................. |vim-vue-plugin-config|
3. Functions .................................. |vim-vue-plugin-functions|
4. Avoid overload ............................. |vim-vue-plugin-overload|

==============================================================================
1. Introduction *vim-vue-plugin-intro*

Vim syntax and indent plugin for `.vue` files, also `.wpy` files. It loads
multiple syntax and indent files for `.vue` and enables them to work
together, with

- Blocks, both <template>/<script>/<style> and custom blocks, with any
syntax by lang="...", like pug, typescript, coffee, scss, sass, less and
stylus. Syntax plugins need to be installed if not provided by Vim.
- Attribute, directive and keyword highlight.
- emmet-vim html/javascript/css filetype detection.
- Functions to get the current tag or syntax, see
|vim-vue-plugin-functions|.
- A builtin 'foldexpr'.

'filetype' is set to `vue`. Add this plugin to the start of 'runtimepath'
when installing manually, as Vim 9 and Neovim come with placeholder `vue`
syntax and indent files: >

set rtp^=path/to/this_plugin
<
==============================================================================
2. Configuration *vim-vue-plugin-config*

*g:vim_vue_plugin_config*
g:vim_vue_plugin_config is the only configuration, set before opening a vue
file. Its default value: >

let g:vim_vue_plugin_config = {
\'syntax': {
\ 'template': ['html'],
\ 'script': ['javascript'],
\ 'style': ['css'],
\},
\'full_syntax': [],
\'initial_indent': [],
\'attribute': 0,
\'keyword': 0,
\'foldexpr': 0,
\'debug': 0,
\}
<
syntax
Block tag name to its syntax list. A string can be used for a list with
one item.
- lang="..." on the block tag decides the syntax, in double or single
quotes or without quotes. `typescript` matches lang="ts" and
lang="typescript".
- Without lang="...", or if it's not in the list, the first syntax is
used.
- Syntax files are loaded only from $VIMRUNTIME/syntax, ~/.vim/syntax and
$VIM/vimfiles/syntax, or from all of 'runtimepath', including plugins,
if not found there.
- Each key replaces the default of the same key, so >
{'syntax': {'script': ['javascript', 'typescript']}}
< keeps `template` as ['html'] and `style` as ['css']. Set a key to [] to
remove the block.

full_syntax
Syntax list whose files are always loaded from all of 'runtimepath'.

initial_indent
Block tag, syntax or tag.syntax like `i18n.json` to indent one level
initially. For `.wpy` files, it defaults to ['script', 'style'].

attribute
1 to highlight attribute values as expression instead of string.

keyword
1 to highlight keywords like `data` and `methods`.

foldexpr
1 to use the builtin 'foldexpr' with 'foldmethod' set to `expr`.

debug
1 to echo debug messages in |:messages|.

Example, only for demo. Set syntax as little as possible for performance: >

let g:vim_vue_plugin_config = {
\'syntax': {
\ 'template': ['html', 'pug'],
\ 'script': ['javascript', 'typescript', 'coffee'],
\ 'style': ['css', 'scss', 'sass', 'less', 'stylus'],
\ 'i18n': ['json', 'yaml'],
\ 'route': 'json',
\},
\'full_syntax': ['json'],
\'initial_indent': ['i18n', 'i18n.json', 'yaml'],
\'attribute': 1,
\'keyword': 1,
\'foldexpr': 1,
\}
<
Options can be changed later like global variables: >

let g:vim_vue_plugin_config.foldexpr = 0
<
==============================================================================
3. Functions *vim-vue-plugin-functions*

As there is more than one language in a `.vue` file, different mappings,
completion and local options may be wanted by tags or syntax, the current
language.

*GetVueTag()*
GetVueTag([{lnum}])
Return the block tag at line {lnum}, or the cursor line, like
`'template'`, `'script'`, `'style'`, or `''`. Example: >

autocmd FileType vue inoremap <buffer><expr> : InsertColon()

function! InsertColon()
let tag = GetVueTag()
return tag == 'template' ? ':' : ': '
endfunction
<
*GetVueSyntax()*
*GetVueSubtype()*
GetVueSyntax()
Return the syntax at the cursor line, like `'html'`, `'javascript'`,
`'css'`, `'scss'`. GetVueSubtype() is its previous name.

*OnChangeVueSyntax()*
*OnChangeVueSubtype()*
OnChangeVueSyntax({syntax})
If defined in your vimrc, it is called whenever the syntax under the
cursor changes. OnChangeVueSubtype() is its previous name. Example: >

function! OnChangeVueSyntax(syntax)
if a:syntax == 'html'
setlocal commentstring=<!--%s-->
elseif a:syntax =~ 'css'
setlocal commentstring=/*%s*/
else
setlocal commentstring=//%s
endif
endfunction
<
==============================================================================
4. Avoid overload *vim-vue-plugin-overload*

Most delays come from loading syntax files of many languages.
`b:current_loading_main_syntax` is set to `vue` while loading, which can be
used as a condition in syntax files that load others. For example, the
builtin sass.vim and less.vim load css.vim, which this plugin has loaded: >

if !exists("b:current_loading_main_syntax")
runtime! syntax/css.vim
endif
<
vim:tw=78:ts=8:ft=help:norl:
7 changes: 5 additions & 2 deletions test/cases/no-css.vim
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
" Without css syntax, like only script in config
let g:vim_vue_plugin_config = {'syntax': {'script': ['javascript']}}
" Without css syntax, like only script in config, removing template and style
" by []
let g:vim_vue_plugin_config = {
\'syntax': {'script': ['javascript'], 'template': [], 'style': []},
\}
let g:test_flatten = 1

function! Check()
Expand Down
10 changes: 10 additions & 0 deletions test/cases/syntax-merge.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
" Each key of syntax replaces the default of the same key, so template and
" style keep html and css
let g:vim_vue_plugin_config = {'syntax': {'script': ['javascript', 'typescript']}}
let g:test_flatten = 1

function! Check()
call ExpectBlock('<div>', 'template', 'html', 'htmlTemplateBlock')
call ExpectBlock('let count', 'script', 'typescript', 'typescriptScriptBlock')
call ExpectBlock('color: red', 'style', 'css', 'cssStyleBlock')
endfunction
13 changes: 13 additions & 0 deletions test/cases/syntax-merge.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<template>
<div>{{ count }}</div>
</template>

<script lang="ts">
let count: number = 0
</script>

<style>
p {
color: red;
}
</style>
Loading