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
86 changes: 66 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ It combines HTML, CSS and JavaScript syntax and indent in one file.
Supports

- Svelte directives.
- Less/Sass/Scss, Pug with [vim-pug][4], Coffee with [vim-coffee-script][6], TypeScript with [typescript-vim][14] or [yats.vim][15].^
- Blocks with any syntax by `lang="..."`, like TypeScript, Less/Sass/Scss, Stylus, Pug with [vim-pug][4] and Coffee with [vim-coffee-script][6].^

Relative plugins need to be installed.
Relative plugins need to be installed if not provided by Vim.

- A builtin `foldexpr` foldmethod.^
- [emmet-vim][10] HTML/CSS/JavaScript filetype detection.
Expand All @@ -63,31 +63,77 @@ Supports

## Configuration

Set global variable to `1` to enable or `0` to disable. Ex:
`g:vim_svelte_plugin_config` is the recommended configuration. Its default value is below, which you can copy as a starting point

```vim
let g:vim_svelte_plugin_config = {
\ 'syntax': {
\ 'script': ['javascript'],
\ 'style': ['css'],
\ 'template': [],
\ },
\ 'full_syntax': ['pug', 'coffee', 'typescript'],
\ 'initial_indent': ['script', 'style'],
\ 'attribute': 0,
\ 'foldexpr': 0,
\ 'open_devdocs': '<F1>',
\ 'debug': 0,
\ }
```

let g:vim_svelte_plugin_load_full_syntax = 1
- **syntax**: syntax of `<script>`, `<style>` and `<template lang="...">` blocks
- **key**: `script`, `style` or `template`. Others are ignored, as markup outside `<script>` and `<style>` is always the svelte template
- **value**: *string list*. Syntax for the block
- `lang="..."` on the block tag decides the syntax, in double or single quotes or without quotes. `typescript` matches `lang="ts"` and `lang="typescript"`, `sass` matches `lang="sass"` and SugarSS `lang="sss"`
- Without `lang="..."`, or if it's not in the list, the first syntax is used. `<script lang="ts">` is highlighted and indented as the first syntax of `script` if `typescript` is not in the list
- Syntax plugins need to be installed if not provided by Vim, like [vim-pug][4], [vim-coffee-script][6], [typescript-vim][14] or [yats.vim][15]
- Each key replaces the default of the same key, so `{'syntax': {'script': ['javascript', 'typescript']}}` keeps `style` as `['css']`
- **full_syntax**: *string list*. Syntax whose files are loaded from all of `runtimepath`, including plugins. Others are loaded only from `$VIMRUNTIME/syntax`, `~/.vim/syntax` and `$VIM/vimfiles/syntax`, or from all of `runtimepath` if not found there\*
- **initial_indent**: *string list*. Block tag, syntax or `tag.syntax` like `style.scss` to indent one level initially
- **attribute**: Highlight the key of svelte attributes like `on:click` and `bind:value` as `Type`, and their value `{...}` as normal text instead of string
- **foldexpr**: Enable builtin `foldexpr` foldmethod
- **open_devdocs**: Key mapping to search documentation on https://devdocs.io for the word under the cursor or selected. `''` to disable it
- **debug**: Echo debug messages in `messages` list. Useful to debug if unexpected indents occur

For `0`/`1` options, set `1` to enable or `0` to disable. Example:

```vim
let g:vim_svelte_plugin_config = {
\ 'syntax': {
\ 'script': ['javascript', 'typescript'],
\ 'style': ['css', 'scss', 'sass'],
\ },
\ 'foldexpr': 1,
\ }
```

| variable | description | default |
|----------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---------|
| g:vim_svelte_plugin_load_full_syntax \* | Enable: load all syntax files in `runtimepath` to enable related syntax plugins.<br> Disable: only in `$VIMRUNTIME/syntax`, `~/.vim/syntax` and `$VIM/vimfiles/syntax` | 0 |
| g:vim_svelte_plugin_use_pug \* | Enable pug syntax for `<template lang="pug">`. | 0 |
| g:vim_svelte_plugin_use_coffee | Enable coffee syntax for `<script lang="coffee">`. | 0 |
| g:vim_svelte_plugin_use_typescript | Enable typescript syntax for `<script lang="ts">`. If disabled, it is highlighted and indented as JavaScript. | 0 |
| g:vim_svelte_plugin_use_less | Enable less syntax for `<style lang="less">`. | 0 |
| g:vim_svelte_plugin_use_sass | Enable scss syntax for `<style lang="scss">`(or sass for `lang="sass"` and SugarSS `lang="sss"`). | 0 |
| g:vim_svelte_plugin_use_stylus | Enable stylus syntax for `<style lang="stylus">`. | 0 |
| g:vim_svelte_plugin_has_init_indent | Initially indent one tab inside `style/script` tags. | 1 |
| g:vim_svelte_plugin_use_foldexpr | Enable builtin `foldexpr` foldmethod. | 0 |
| g:vim_svelte_plugin_debug | Echo debug messages in `messages` list. Useful to debug if unexpected indents occur. | 0 |
| g:vim_svelte_plugin_highlight_svelte_attr | Highlight the key of svelte attributes like `on:click` and `bind:value` as `Type`, and their value `{...}` as normal text instead of string. | 0 |
| g:vim_svelte_plugin_open_devdocs | Key mapping to search documentation on https://devdocs.io for the word under the cursor or selected | `<F1>` |
\*: Vim may be slow when loading many syntax files. Find a balance between syntax highlight and speed. By the way, custom syntax can be added in `~/.vim/syntax` or `$VIM/vimfiles/syntax`.

\*: Vim may be slow if the feature is enabled. Find a balance between syntax highlight and speed. By the way, custom syntax can be added in `~/.vim/syntax` or `$VIM/vimfiles/syntax`.
<details>
<summary><a>Previous variables</a></summary>

Variables below still work. They decide the default of `g:vim_svelte_plugin_config`, whose options take precedence over them.

| variable | same as in `g:vim_svelte_plugin_config` |
|----------|-----------------------------------------|
| g:vim_svelte_plugin_load_full_syntax | `full_syntax` with `html`, `css`, `javascript`, `less`, `sass` and `stylus` |
| g:vim_svelte_plugin_use_pug | `pug` in `syntax.template` |
| g:vim_svelte_plugin_use_coffee | `coffee` in `syntax.script` |
| g:vim_svelte_plugin_use_typescript | `typescript` in `syntax.script` |
| g:vim_svelte_plugin_use_less | `less` in `syntax.style` |
| g:vim_svelte_plugin_use_sass | `scss` and `sass` in `syntax.style` |
| g:vim_svelte_plugin_use_stylus | `stylus` in `syntax.style` |
| g:vim_svelte_plugin_has_init_indent | `initial_indent` as `['script', 'style']`, or `[]` if `0` |
| g:vim_svelte_plugin_use_foldexpr | `foldexpr` |
| g:vim_svelte_plugin_debug | `debug` |
| g:vim_svelte_plugin_highlight_svelte_attr | `attribute` |
| g:vim_svelte_plugin_open_devdocs | `open_devdocs` |

</details>

**Note**

- `filetype` is set to `svelte` so autocmds and other custom settings for `javascript` have to be manually enabled for `svelte`.
- `g:vim_svelte_plugin_load_full_syntax` applies to `HTML/Sass/Less`.
- See <https://svelte.dev/docs/svelte/svelte-compiler#preprocess> for how to use `Less/Sass/Pug`... in svelte.

### Neovim & [Treesitter](https://github.com/nvim-treesitter/nvim-treesitter)
Expand Down
68 changes: 68 additions & 0 deletions autoload/svelte.vim
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,74 @@ function! svelte#GetConfig(name, default)
return exists(name) ? eval(name) : a:default
endfunction

" Config from g:vim_svelte_plugin_config, on top of the default derived from
" previous g:vim_svelte_plugin_* variables, which still work. Keys of
" 'syntax' in g:vim_svelte_plugin_config replace the ones in the default
" separately, others replace the whole value.
function! svelte#GetSvelteConfig()
let config = s:GetDefaultConfig()
let user = svelte#GetConfig('config', {})
for [key, value] in items(user)
if key ==# 'syntax' && type(value) == v:t_dict
call extend(config.syntax, value)
else
let config[key] = value
endif
endfor
for [tag, value] in items(config.syntax)
let config.syntax[tag] = s:ToList(value)
endfor
let config.full_syntax = s:ToList(config.full_syntax)
let config.initial_indent = s:ToList(config.initial_indent)
return config
endfunction

function! s:GetDefaultConfig()
let Get = function('svelte#GetConfig')
let script = ['javascript']
\ + (Get('use_typescript', 0) ? ['typescript'] : [])
\ + (Get('use_coffee', 0) ? ['coffee'] : [])
let style = ['css']
\ + (Get('use_less', 0) ? ['less'] : [])
\ + (Get('use_sass', 0) ? ['scss', 'sass'] : [])
\ + (Get('use_stylus', 0) ? ['stylus'] : [])
let template = Get('use_pug', 0) ? ['pug'] : []
let full_syntax = ['pug', 'coffee', 'typescript']
if Get('load_full_syntax', 0)
let full_syntax += ['html', 'css', 'javascript', 'less', 'sass', 'stylus']
endif
return {
\ 'syntax': {'script': script, 'style': style, 'template': template},
\ 'full_syntax': full_syntax,
\ 'initial_indent': Get('has_init_indent', 1) ? ['script', 'style'] : [],
\ 'attribute': Get('highlight_svelte_attr', 0),
\ 'foldexpr': Get('use_foldexpr', 0),
\ 'open_devdocs': Get('open_devdocs', '<F1>'),
\ 'debug': Get('debug', 0),
\ }
endfunction

function! s:ToList(value)
return type(a:value) == v:t_list ? copy(a:value) : [a:value]
endfunction

function! svelte#Has(list, item)
return index(a:list, a:item) != -1
endfunction

" Syntax in all blocks, without duplicates, in config order
function! svelte#GetSyntaxList(config_syntax)
let syntax_list = []
for tag in ['template', 'script', 'style']
for syntax in get(a:config_syntax, tag, [])
if index(syntax_list, syntax) == -1
call add(syntax_list, syntax)
endif
endfor
endfor
return syntax_list
endfunction

" Since svelte#Log and svelte#GetConfig are always called
" in syntax and indent files,
" this file will be sourced when opening the first svelte file
Expand Down
156 changes: 94 additions & 62 deletions doc/vim-svelte-plugin.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ JavaScript syntax and indent in one file, with

- Svelte directives, blocks like {#if}, {#each}, {#snippet} and tags like
{@html}, {@render}, {@const}.
- Less, Sass, Scss, Stylus, Pug, CoffeeScript and TypeScript in blocks with
lang="...", if enabled. Pug and CoffeeScript need vim-pug and
vim-coffee-script.
- Blocks with any syntax by lang="...", like TypeScript, Less, Sass, Scss,
Stylus, Pug and CoffeeScript, see |g:vim_svelte_plugin_config|. Pug and
CoffeeScript need vim-pug and vim-coffee-script.
- A builtin 'foldexpr'.
- emmet-vim HTML/CSS/JavaScript filetype detection.

Expand All @@ -33,68 +33,99 @@ be enabled for `svelte` manually.
==============================================================================
2. Configuration *vim-svelte-plugin-config*

Set a global variable to 1 to enable or 0 to disable, before opening a svelte
file. Example: >

let g:vim_svelte_plugin_use_typescript = 1
*g:vim_svelte_plugin_config*
g:vim_svelte_plugin_config is the recommended configuration, set before
opening a svelte file. Its default value: >

let g:vim_svelte_plugin_config = {
\ 'syntax': {
\ 'script': ['javascript'],
\ 'style': ['css'],
\ 'template': [],
\ },
\ 'full_syntax': ['pug', 'coffee', 'typescript'],
\ 'initial_indent': ['script', 'style'],
\ 'attribute': 0,
\ 'foldexpr': 0,
\ 'open_devdocs': '<F1>',
\ 'debug': 0,
\ }
<
*g:vim_svelte_plugin_load_full_syntax*
g:vim_svelte_plugin_load_full_syntax Default: 0
Enable: load all syntax files in 'runtimepath' to enable related syntax
plugins. Disable: only in $VIMRUNTIME/syntax, ~/.vim/syntax and
$VIM/vimfiles/syntax. It applies to HTML, Sass and Less. Vim may be slow
if enabled.

*g:vim_svelte_plugin_use_pug*
g:vim_svelte_plugin_use_pug Default: 0
Pug syntax for <template lang="pug">. Vim may be slow if enabled.

*g:vim_svelte_plugin_use_coffee*
g:vim_svelte_plugin_use_coffee Default: 0
CoffeeScript syntax for <script lang="coffee">.

*g:vim_svelte_plugin_use_typescript*
g:vim_svelte_plugin_use_typescript Default: 0
TypeScript syntax and indent for <script lang="ts">. If disabled, it is
highlighted and indented as JavaScript.

*g:vim_svelte_plugin_use_less*
g:vim_svelte_plugin_use_less Default: 0
Less syntax for <style lang="less">.

*g:vim_svelte_plugin_use_sass*
g:vim_svelte_plugin_use_sass Default: 0
Scss syntax for <style lang="scss">, sass syntax and indent for
<style lang="sass"> and SugarSS <style lang="sss">.

*g:vim_svelte_plugin_use_stylus*
g:vim_svelte_plugin_use_stylus Default: 0
Stylus syntax for <style lang="stylus">.

The lang attribute can also be in single quotes or without quotes, like
lang='ts' or lang=scss, for TypeScript and the enabled languages.

*g:vim_svelte_plugin_has_init_indent*
g:vim_svelte_plugin_has_init_indent Default: 1
Indent one level initially inside <script> and <style>.

*g:vim_svelte_plugin_use_foldexpr*
g:vim_svelte_plugin_use_foldexpr Default: 0
Use the builtin 'foldexpr' with 'foldmethod' set to `expr`.

*g:vim_svelte_plugin_highlight_svelte_attr*
g:vim_svelte_plugin_highlight_svelte_attr Default: 0
Highlight the key of svelte attributes like `on:click` and `bind:value` as
|hl-Type|, and their value {...} as normal text instead of string.

*g:vim_svelte_plugin_open_devdocs*
g:vim_svelte_plugin_open_devdocs Default: '<F1>'
syntax
Syntax list of `script`, `style` and `template` blocks. Other keys are
ignored, as markup outside <script> and <style> is always the svelte
template. Each key replaces the default of the same key.
- lang="..." on the block tag decides the syntax, in double or single
quotes or without quotes. `typescript` matches lang="ts" and
lang="typescript", `sass` matches lang="sass" and SugarSS lang="sss".
- Without lang="...", or if it's not in the list, the first syntax is
used. <script lang="ts"> is highlighted and indented as the first syntax
of `script` if `typescript` is not in the list.
- Syntax plugins need to be installed if not provided by Vim, like vim-pug
or vim-coffee-script.
A string can be used for a list with one item.

full_syntax
Syntax whose files are loaded from all of 'runtimepath', including
plugins. Others are loaded only from $VIMRUNTIME/syntax, ~/.vim/syntax
and $VIM/vimfiles/syntax, or from all of 'runtimepath' if not found there.
Vim may be slow when loading many syntax files.

initial_indent
Block tag, syntax or tag.syntax like `style.scss` to indent one level
initially.

attribute
1 to highlight the key of svelte attributes like `on:click` and
`bind:value` as |hl-Type|, and their value {...} as normal text instead of
string.

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

open_devdocs
Key mapping to search https://devdocs.io, see
|vim-svelte-plugin-mappings|. Set to '' to disable it.

*g:vim_svelte_plugin_debug*
g:vim_svelte_plugin_debug Default: 0
Echo debug messages in |:messages|. Useful to debug unexpected indents.
debug
1 to echo debug messages in |:messages|. Useful to debug unexpected
indents.

Example: >

let g:vim_svelte_plugin_config = {
\ 'syntax': {
\ 'script': ['javascript', 'typescript'],
\ 'style': ['css', 'scss', 'sass'],
\ },
\ 'foldexpr': 1,
\ }
<
*vim-svelte-plugin-previous-variables*
*g:vim_svelte_plugin_load_full_syntax* *g:vim_svelte_plugin_use_pug*
*g:vim_svelte_plugin_use_coffee* *g:vim_svelte_plugin_use_typescript*
*g:vim_svelte_plugin_use_less* *g:vim_svelte_plugin_use_sass*
*g:vim_svelte_plugin_use_stylus* *g:vim_svelte_plugin_has_init_indent*
*g:vim_svelte_plugin_use_foldexpr* *g:vim_svelte_plugin_debug*
*g:vim_svelte_plugin_highlight_svelte_attr* *g:vim_svelte_plugin_open_devdocs*
Previous variables below still work. They decide the default of
|g:vim_svelte_plugin_config|, whose options take precedence over them.

g:vim_svelte_plugin_load_full_syntax `full_syntax` with html, css,
javascript, less, sass and stylus
g:vim_svelte_plugin_use_pug `pug` in `syntax.template`
g:vim_svelte_plugin_use_coffee `coffee` in `syntax.script`
g:vim_svelte_plugin_use_typescript `typescript` in `syntax.script`
g:vim_svelte_plugin_use_less `less` in `syntax.style`
g:vim_svelte_plugin_use_sass `scss` and `sass` in `syntax.style`
g:vim_svelte_plugin_use_stylus `stylus` in `syntax.style`
g:vim_svelte_plugin_has_init_indent `initial_indent` as
['script', 'style'], or [] if 0
g:vim_svelte_plugin_use_foldexpr `foldexpr`
g:vim_svelte_plugin_debug `debug`
g:vim_svelte_plugin_highlight_svelte_attr
`attribute`
g:vim_svelte_plugin_open_devdocs `open_devdocs`

==============================================================================
3. Functions *vim-svelte-plugin-functions*
Expand Down Expand Up @@ -140,7 +171,8 @@ OnChangeSvelteSubtype({subtype})

<F1>
Search https://devdocs.io for the word under the cursor, or the selected
text in Visual mode. Change the key by |g:vim_svelte_plugin_open_devdocs|.
text in Visual mode. Change the key by `open_devdocs` in
|g:vim_svelte_plugin_config|.

If matchit is loaded, |%| also jumps between tags and svelte blocks like
{#if}, {:else} and {/if}.
Expand Down
2 changes: 1 addition & 1 deletion ftplugin/svelte.vim
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ function! s:OpenDevDocs() range
execute 'Open https://devdocs.io?q='.filetype.' '.clean_text
endfunction

let s:open_devdocs = svelte#GetConfig('open_devdocs', '<F1>')
let s:open_devdocs = svelte#GetSvelteConfig().open_devdocs
if !empty(s:open_devdocs)
if has('patch-8.2.1978') || has('nvim-0.5')
execute 'nnoremap <buffer> '.s:open_devdocs.' <Cmd>call <SID>OpenDevDocs()<CR>'
Expand Down
Loading
Loading