From 06b4cda7747d4f5a6167f76b3d38d4cc0b369b99 Mon Sep 17 00:00:00 2001 From: Yasuhiro Matsumoto Date: Wed, 25 Apr 2018 10:03:38 +0900 Subject: [PATCH] expand local variables in custom snippets. closes #414 --- autoload/emmet.vim | 13 +++++++++++++ autoload/emmet/lang/html.vim | 3 +++ 2 files changed, 16 insertions(+) diff --git a/autoload/emmet.vim b/autoload/emmet.vim index 3169845..8cc13c5 100644 --- a/autoload/emmet.vim +++ b/autoload/emmet.vim @@ -196,6 +196,18 @@ function! s:itemno(itemno, current) abort endif endfunction +function! s:localvar(current, key) abort + let val = '' + let cur = a:current + while !empty(cur) + if has_key(cur, 'variables') && has_key(cur.variables, a:key) + return cur.variables[a:key] + endif + let cur = cur.parent + endwhile + return '' +endfunction + function! emmet#toString(...) abort let current = a:1 if a:0 > 1 @@ -291,6 +303,7 @@ function! emmet#toString(...) abort let inner = current.value[1:-2] endif let inner = substitute(inner, "\n", "\n" . indent, 'g') + let str = substitute(str, '\${\(\w\+\)}', '\=s:localvar(current, submatch(1))', '') let str = substitute(str, '\${child}', inner, '') endif let itemno = itemno + 1 diff --git a/autoload/emmet/lang/html.vim b/autoload/emmet/lang/html.vim index 9066ea2..73a7559 100644 --- a/autoload/emmet/lang/html.vim +++ b/autoload/emmet/lang/html.vim @@ -94,6 +94,7 @@ function! emmet#lang#html#parseIntoTree(abbr, type) abort let abbr = rabbr let root = emmet#newNode() + let root['variables'] = {} let parent = root let last = root let pos = [] @@ -257,9 +258,11 @@ function! emmet#lang#html#parseIntoTree(abbr, type) abort endif if item[0] ==# '#' let current.attr.id = item[1:] + let root['variables']['id'] = current.attr.id endif if item[0] ==# '.' let current.attr.class = substitute(item[1:], '\.', ' ', 'g') + let root['variables']['class'] = current.attr.class endif if item[0] ==# '[' let atts = item[1:-2]