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
17 changes: 15 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,23 @@ on:

jobs:
test:
name: ${{ matrix.neovim && 'nvim' || 'vim' }} ${{ matrix.version }}
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
include:
- { neovim: false, version: v8.2.0000 }
- { neovim: false, version: stable }
- { neovim: true, version: v0.9.5 }
- { neovim: true, version: stable }
steps:
- uses: actions/checkout@v4
- name: Install Vim and Neovim
run: sudo apt-get update && sudo apt-get install -y vim neovim
- uses: rhysd/action-setup-vim@v1
with:
neovim: ${{ matrix.neovim }}
version: ${{ matrix.version }}
- name: Run tests
run: ./test/run.sh
env:
SVELTE_TEST_EDITORS: ${{ matrix.neovim && 'nvim' || 'vim' }}
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.svelte
.idea
test/deps/
doc/tags
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ Vim syntax and indent plugin for `.svelte` files. Forked from [vim-vue-plugin][3

This plugin works if it has set `filetype` to `svelte`. Please stay up to date. Feel free to open an issue or pull request.

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

## How it works

It combines HTML, CSS and JavaScript syntax and indent in one file.
Expand Down Expand Up @@ -70,7 +72,7 @@ Set global variable to `1` to enable or `0` to disable. Ex:
| 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">`. | 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 |
Expand Down Expand Up @@ -160,6 +162,7 @@ Run tests with Vim and Neovim, whichever is installed
```sh
./test/run.sh
./test/run.sh basic template # Only these cases
SVELTE_TEST_EDITORS=nvim ./test/run.sh # Only Neovim
```

Each case is a `.svelte` file in [test/cases](test/cases), with an optional `.vim` file for the config and checks. A case fails if
Expand All @@ -169,7 +172,7 @@ Each case is a `.svelte` file in [test/cases](test/cases), with an optional `.vi
- a check in `Check()` fails, such as syntax, `GetSvelteTag()`, `GetSvelteSubtype()` or fold level
- there is an error message

Tests also run on GitHub Actions for each push and pull request.
Tests also run on GitHub Actions for each push and pull request, with Vim 8.2.0000, the latest Vim, Neovim 0.9.5 and the latest Neovim.

## See also

Expand Down
4 changes: 0 additions & 4 deletions autoload/svelte.vim
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
let s:name = 'vim-svelte-plugin'
let s:debug = exists("g:vim_svelte_plugin_debug")
\ && g:vim_svelte_plugin_debug == 1

function! svelte#GetConfig(name, default)
let name = 'g:vim_svelte_plugin_'.a:name
return exists(name) ? eval(name) : a:default
Expand Down
160 changes: 160 additions & 0 deletions doc/vim-svelte-plugin.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
*vim-svelte-plugin.txt* Syntax and indent for Svelte files

Author: leafOfTree
License: The Unlicense

==============================================================================
CONTENTS *vim-svelte-plugin-contents*

1. Introduction ............................. |vim-svelte-plugin-intro|
2. Configuration ............................ |vim-svelte-plugin-config|
3. Functions ................................ |vim-svelte-plugin-functions|
4. Mappings ................................. |vim-svelte-plugin-mappings|
5. Avoid overload ........................... |vim-svelte-plugin-overload|

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

Vim syntax and indent plugin for `.svelte` files. It combines HTML, CSS and
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.
- A builtin 'foldexpr'.
- emmet-vim HTML/CSS/JavaScript filetype detection.

It works if 'filetype' is `svelte`, which is set for `*.svelte` files. As
'filetype' isn't `javascript`, autocmds and settings for `javascript` have to
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_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>'
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.

==============================================================================
3. Functions *vim-svelte-plugin-functions*

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

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

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

function! InsertColon()
let tag = GetSvelteTag()
return tag == 'template' ? ':' : ': '
endfunction
<
*GetSvelteSubtype()*
GetSvelteSubtype()
Return the subtype at the cursor line, like `'html'`, `'javascript'`,
`'typescript'`, `'css'`, `'scss'`.

*OnChangeSvelteSubtype()*
OnChangeSvelteSubtype({subtype})
If defined in your vimrc, it is called whenever the subtype under the
cursor changes. Example: >

function! OnChangeSvelteSubtype(subtype)
if empty(a:subtype) || a:subtype == 'html'
setlocal commentstring=<!--%s-->
elseif a:subtype =~ 'css'
setlocal commentstring=/*%s*/
else
setlocal commentstring=//%s
endif
endfunction
<
==============================================================================
4. Mappings *vim-svelte-plugin-mappings*

<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|.

If matchit is loaded, |%| also jumps between tags and svelte blocks like
{#if}, {:else} and {/if}.

==============================================================================
5. Avoid overload *vim-svelte-plugin-overload*

Most delays come from loading syntax files of many languages.
`b:current_loading_main_syntax` is set to `svelte` 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:
29 changes: 17 additions & 12 deletions ftplugin/svelte.vim
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,14 @@ endif

if !exists(':Open')
function! s:Open(arg)
let cmd = ''
if has('mac')
let cmd = 'open'
endif
if has('linux')
let cmd = 'xdg-open'
endif
if has('win32')
let cmd = 'start'
elseif has('mac') || has('macunix')
let cmd = 'open'
else
let cmd = 'xdg-open'
endif

execute '!'.cmd.' "'.a:arg.'"'
endfunction

Expand All @@ -56,15 +53,23 @@ function! s:OpenDevDocs() range
let text = expand('<cword>')
endif

" Remove special symbols
let clean_text = substitute(text, '[#@$%^&*(){}[\]<>]', '', 'g')
" Remove special symbols, also '!', '%' and '#' expanded by :!
let clean_text = substitute(text, '[!#@$%^&*(){}[\]<>]', '', 'g')

let filetype = &filetype
execute 'Open https://devdocs.io?q='.filetype.' '.clean_text
endfunction

let s:open_devdocs = svelte#GetConfig('open_devdocs', '<F1>')
if !empty(s:open_devdocs)
execute 'noremap <buffer> '.s:open_devdocs.' <Cmd>call <SID>OpenDevDocs()<CR>'
let b:undo_ftplugin .= " | exe 'unmap <buffer> ".s:open_devdocs."'"
if has('patch-8.2.1978') || has('nvim-0.5')
execute 'nnoremap <buffer> '.s:open_devdocs.' <Cmd>call <SID>OpenDevDocs()<CR>'
execute 'xnoremap <buffer> '.s:open_devdocs.' <Cmd>call <SID>OpenDevDocs()<CR>'
let b:undo_ftplugin .= " | exe 'nunmap <buffer> ".s:open_devdocs."'"
\ . " | exe 'xunmap <buffer> ".s:open_devdocs."'"
else
" No <Cmd> in older Vim, only for the word under the cursor
execute 'nnoremap <buffer> '.s:open_devdocs.' :<C-u>call <SID>OpenDevDocs()<CR>'
let b:undo_ftplugin .= " | exe 'nunmap <buffer> ".s:open_devdocs."'"
endif
endif
6 changes: 3 additions & 3 deletions ftplugin/svelte/fold.vim
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ if !s:use_foldexpr | finish | endif
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
setlocal foldmethod=expr
setlocal foldexpr=GetSvelteFold(v:lnum)
let b:undo_ftplugin = (exists('b:undo_ftplugin') ? b:undo_ftplugin.' | ' : '')
\ . 'setlocal foldmethod< foldexpr<'
"}}}

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
Expand All @@ -24,7 +26,6 @@ setlocal foldexpr=GetSvelteFold(v:lnum)
"
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
let s:empty_line = '\v^\s*$'
let s:block_end = '\v^\s*}|]|\)'
let s:svelte_tag_start = '\v^\<\w+'
let s:svelte_tag_end = '\v^\<\/\w+'
let s:svelte_internal_blocks = '\v:(else|then|catch)'
Expand All @@ -51,7 +52,6 @@ let s:svelte_internal_blocks = '\v:(else|then|catch)'
" "<1", "<2", .. a fold with this level ends at this line
function! GetSvelteFold(lnum)
let this_line = getline(a:lnum)
let next_line = getline(a:lnum + 1)

" Handle empty lines
if this_line =~ s:empty_line
Expand Down Expand Up @@ -106,7 +106,7 @@ endfunction

function! s:IndentLevel(lnum)
" Add 1 to indentLevel, so start/end tags can fold properly
return indent(a:lnum) / &shiftwidth + 1
return indent(a:lnum) / shiftwidth() + 1
endfunction

function! s:NextNonBlankLine(lnum)
Expand Down
Loading
Loading