Skip to content

Commit

Permalink
Initial implementation of text folding
Browse files Browse the repository at this point in the history
  • Loading branch information
xolox committed Mar 30, 2015
1 parent 3609ad4 commit 8f5bec9
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 22 deletions.
20 changes: 16 additions & 4 deletions README.md
Expand Up @@ -6,7 +6,8 @@ everything needed to efficiently develop TLV code in Vim:

* File type detection
* Syntax highlighting (including an optional color scheme)
* Smart indentation
* Automatic (smart) indentation
* Automatic text folding
* Checking for syntax errors (work in progress)

Each of the features outlined above is discussed in more detail below.
Expand Down Expand Up @@ -74,11 +75,21 @@ Of course you are free to decide whether to use this color scheme or not, the
choice is up to you. The syntax highlighting mode is intended to work well with
other Vim color schemes as well.

### Smart indentation
### Automatic (smart) indentation

The *vim-tlv-mode* plug-in uses Vim's ['indentexpr'] [indentexpr] option to
implement smart indentation that understands TLV scope rules and automatically
increases the indentation level as needed.
implement smart indentation (that understands TLV scope rules and knows to
ignore line type characters) and automatically increases the indentation level
as needed.

### Automatic text folding

The *vim-tlv-mode* plug-in uses Vim's ['foldexpr'] [foldexpr] option to
implement automatic text folding (that understands TLV scope rules and knows to
ignore line type characters). This method of text folding is already a big
improvement over indentation based folding (because that falls apart as soon as
line type characters are introduced) but it's not perfect yet; I'd like to
improve it further.

### Checking for syntax errors

Expand Down Expand Up @@ -110,6 +121,7 @@ This software is licensed under the [MIT license] [mit].


[download-tlv-mode]: http://peterodding.com/code/vim/downloads/tlv-mode.zip
[foldexpr]: http://vimdoc.sourceforge.net/htmldoc/options.html#'foldexpr'
[github-tlv-mode]: https://github.com/xolox/vim-tlv-mode
[homepage]: http://peterodding.com/code/vim/tlv-mode/
[indentexpr]: http://vimdoc.sourceforge.net/htmldoc/options.html#'indentexpr'
Expand Down
9 changes: 0 additions & 9 deletions TODO.md
Expand Up @@ -4,7 +4,6 @@ This document lists work to be done on the *vim-tlv-mode* plug-in. The
following points are in rough order of priority (in descending order):

* Get integrated syntax checking working (out of the box).
* Implement useful text folding for TLV files.

Each point is discussed in more detail below.

Expand Down Expand Up @@ -41,11 +40,3 @@ plug-in. Impediments for the integration between *vim-tlv-mode* and the

My goal is to have these issues resolved (in one way or another :-) as quickly
as possible.

## Implement useful text folding for TLV files

It should be possible to hide code under a given scope. I believe that Vim's
text folding feature is the best way to implement this. I'm not yet sure
whether indentation based folding is good enough or if I should implement a
smart text folding expression function that actually understands scoping rules
instead of just looking at the indentation.
32 changes: 25 additions & 7 deletions autoload/tlv.vim
@@ -1,10 +1,10 @@
" Vim auto-load script
" Language: TLV (Transaction-Level Verilog)
" Author: Peter Odding <peter@peterodding.com>
" Last Change: March 13, 2015
" Last Change: March 30, 2015
" URL: https://github.com/xolox/vim-tlv-mode

let g:tlv#version = '0.1'
let g:tlv#version = '0.2'

function! tlv#compiler_is_installed() " {{{1
" Check if the TLV compiler is installed. Returns true (1) when the compiler
Expand Down Expand Up @@ -64,15 +64,19 @@ function! tlv#auto_check_syntax() " {{{1
endif
endfunction

function! tlv#foldexpr() " {{{1
" Support for automatic (smart) text folding. The result of this folding
" expression isn't exactly ideal yet, but it's one step up from indentation
" based text folding (that falls apart as soon as line type characters are
" used :-). I'd like to improve this further, but I'm not yet sure how...
return s:calculate_indent(getline(v:lnum)) / &tabstop
endfunction

function! tlv#indentexpr() " {{{1
" Support for automatic (smart) indentation.
let previous_lnum = prevnonblank(v:lnum - 1)
let previous_line = getline(previous_lnum)
" Line type characters (any character in the first column other than a space
" or backslash) are to be considered indentation so we have to include them
" (manually) in the calculation of indentation. This explains why we can't
" use Vim's otherwise very handy indent() function :-).
let previous_indent = len(matchstr(previous_line, '^[^\\] *'))
let previous_indent = s:calculate_indent(previous_line)
if previous_line =~ '^\s*[>|?@\\-]'
" When the previous non blank line starts with one of the characters in
" the [character class] above, the scope (and thus indentation) is
Expand All @@ -84,4 +88,18 @@ function! tlv#indentexpr() " {{{1
endif
endfunction

function! s:calculate_indent(line) " {{{1
" Calculate the indentation level of a line (as the number of spaces). This
" is complicated by line type characters (any character in the first column
" other than a space or backslash) because they are to be considered
" indentation so we have to include them (manually) in the calculation of
" indentation. This explains why we can't use Vim's otherwise very handy
" indent() function :-).
if a:line =~ '^[^\\ ] '
return matchend(a:line, '^[^\\] \+')
else
return matchend(a:line, '^ *')
endif
endfunction

" vim: ts=2 sw=2 et
8 changes: 6 additions & 2 deletions ftplugin/tlv.vim
@@ -1,7 +1,7 @@
" Vim file type plug-in
" Language: TLV (Transaction-Level Verilog)
" Author: Peter Odding <peter@peterodding.com>
" Last Change: March 13, 2015
" Last Change: March 30, 2015
" URL: https://github.com/xolox/vim-tlv-mode

if exists('b:did_ftplugin')
Expand All @@ -21,10 +21,14 @@ call add(s:undo_ftplugin, 'setlocal expandtab<')
setlocal tabstop=3 shiftwidth=3
call add(s:undo_ftplugin, 'setlocal tabstop< shiftwidth<')

" Enable automatic indentation support. {{{1
" Enable support for automatic indentation. {{{1
setlocal indentexpr=tlv#indentexpr()
call add(s:undo_ftplugin, 'setlocal indentexpr<')

" Enable support for automatic text folding. {{{1
setlocal foldmethod=expr foldexpr=tlv#foldexpr()
call add(s:undo_ftplugin, 'setlocal foldmethod< foldexpr<')

" }}}1

" Let Vim know how to disable the plug-in.
Expand Down

0 comments on commit 8f5bec9

Please sign in to comment.