Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure ctags to work with CSS, SCSS, HTML?

I've already read a lot of blog posts and answers on stackoverflow, but it seems I do something wrong, because I still have the E388: Couldn't find definition error. What I did:

  • Download ctags from here http://ctags.sourceforge.net/
  • Install it by: ./configure && make install
  • Set tags+=tags;$HOME in my .vimrc file
  • Add some lines into ~/.ctags (see below)
  • Do ctags -R . in the project root and get some warnings (see below)
  • Check classes by :tag .<C-D> which gives me a big list of classes (that pleases)

The warnings:

ctags: Warning: regcomp ([A-Za-z0-9._$]+)[ t]*[:=][ t]*{: Unmatched \{
ctags: Warning: regcomp ([A-Za-z0-9._$()]+)[ t]*[:=][ t]*function[ t]*(: Unmatched ( or \(
ctags: Warning: regcomp ([A-Za-z0-9._$]+)[ t]*[:=][ t]*[: Invalid regular expression
ctags: Warning: cannot open source file "static/img/touch/packages" : No such file or directory

My ~/.ctags file looks as follows:

--exclude=*.min.js
--exclude=*.min.css
--exclude=*.map
--exclude=.backup
--exclude=.sass-cache
--exclude=vendors
--exclude=.git

--langdef=css
--langmap=css:.css
--langmap=css:+.sass
--langmap=css:+.styl
--langmap=css:+.less
--regex-css=/^[ \t]*\.([A-Za-z0-9_-]+)/.\1/c,class,classes/
--regex-css=/^[ \t]*#([A-Za-z0-9_-]+)/#\1/i,id,ids/
--regex-css=/^[ \t]*(([A-Za-z0-9_-]+[ \t\n,]+)+)\{/\1/t,tag,tags/
--regex-css=/^[ \t]*@media\s+([A-Za-z0-9_-]+)/\1/m,media,medias/

--langdef=scss
--langmap=scss:.scss
--regex-scss=/^[ \t]*@mixin ([A-Za-z0-9_-]+)/\1/m,mixin,mixins/
--regex-scss=/^[ \t]*\$([A-Za-z0-9_-]+)/\1/v,variable,variables/
--regex-scss=/^([A-Za-z0-9_-]*)*(\.[A-Za-z0-9_-]+) *[,{]/\2/c,class,classes/
--regex-scss=/^[ \t]+(\.[A-Za-z0-9_-]+) *[,{]/\1/c,class,classes/
--regex-scss=/^(.*)*\#([A-Za-z0-9_-]+) *[,{]/\2/i,id,ids/
--regex-scss=/^[ \t]*#([A-Za-z0-9_-]+)/\1/i,id,ids/
--regex-scss=/(^([A-Za-z0-9_-])*([A-Za-z0-9_-]+)) *[,|\{]/\1/t,tag,tags/
--regex-scss=/(^([^\/\/])*)[ \t]+([A-Za-z0-9_-]+)) *[,|\{]/\3/t,tag,tags/
--regex-scss=/(^(.*, *)([A-Za-z0-9_-]+)) *[,|\{]/\3/t,tag,tags/
--regex-scss=/(^[ \t]+([A-Za-z0-9_-]+)) *[,|\{]/\1/t,tag,tags/
--regex-scss=/^[ \t]*@media\s+([A-Za-z0-9_-]+)/\1/d,media,media/

--regex-html=/id="([A-Za-z0-9_-]+)"/\1/i,id,ids/
--regex-html=/class="([A-Za-z0-9_-]+)"/\1/c,class,classes/

--langdef=js
--langmap=js:.js
--regex-js=/([A-Za-z0-9._$]+)[ t]*[:=][ t]*{/1/,object/
--regex-js=/([A-Za-z0-9._$()]+)[ t]*[:=][ t]*function[ t]*(/1/,function/
--regex-js=/function[ t]+([A-Za-z0-9._$]+)[ t]*(([^)]))/1/,function/
--regex-js=/([A-Za-z0-9._$]+)[ t]*[:=][ t]*[/1/,array/
--regex-js=/([^= ]+)[ t]*=[ t]*[^"]'[^']*/1/,string/
--regex-js=/([^= ]+)[ t]*=[ t]*[^']"[^"]*/1/,string/

Project structure:

enter image description here

Wherever (static/index.html, static/css/main.scss or static/css/components/set.scss) I try to "jump to definition" using ]^D I always get E388: Couldn't find definition. What happens?

UPDATE

<C-]> pressed at the beginning of _col-2 name:

  • in case of <div class="_col-2">.. gives E426: Tag not found: _col. It looks like vim doesn't detect classname correctly.
  • in case of .class { @extend ._col-2; } gives the same.

As suggest @romainl, after adding set iskeyword+=- in .vimrc to dash be a part of the keyword, pressing on <div class="_col-2">.. throws me at the beginning of the line.

like image 911
Timur Fayzrakhmanov Avatar asked Nov 11 '15 09:11

Timur Fayzrakhmanov


2 Answers

About E426: Tag not found: txal-center

These lines in your .ctags will index .foo-barwith the leading dot — in *.scss files:

--regex-scss=/^([A-Za-z0-9_-]*)*(\.[A-Za-z0-9_-]+) *[,{]/\2/c,class,classes/
--regex-scss=/^[ \t]+(\.[A-Za-z0-9_-]+) *[,{]/\1/c,class,classes/

When you press <C-]> over a class name in an HTML document, Vim will look for that exact class name, say foo-bar, but it won't find it because you only have .foo-bar in your tags file. That's because Vim does "whole-word" search by default.

The solution to this specific problem is to leave the dot out of the tag:

--regex-scss=/^([A-Za-z0-9_-]*)*\.([A-Za-z0-9_-]+) *[,{]/\2/c,class,classes/
--regex-scss=/^[ \t]+\.([A-Za-z0-9_-]+) *[,{]/\1/c,class,classes/
like image 65
romainl Avatar answered Nov 05 '22 21:11

romainl


The ]<C-D> motion is for macro definitions, powered by Vim's internal file search and the 'define' option. It doesn't use the tags database, and is mostly for C / C++ macros. What you want is the equivalent to the :tag command (which apparently does work for you). This is <C-]> (or <C-LeftMouse>); you'll find it directly under :help :tag.

like image 34
Ingo Karkat Avatar answered Nov 05 '22 23:11

Ingo Karkat