Note:

If you want to create a new page for developers, you should create it on the Moodle Developer Resource site.

ctags: Difference between revisions

From MoodleDocs
No edit summary
m (Update the page after removing tags.txt from the moodle.git)
(14 intermediate revisions by 8 users not shown)
Line 1: Line 1:
The root directory of the Moodle CVS distribution contains a file called '''tags'''. This contains information about the location of symbols throughout the moodle code. An editor can make use of this to automatically jump to the place where a function is defined.
[[Image:vimomnisearch.png|thumb|Example of auto-completion menu provided by the PHP omnicomplete extension in ViM]]


== Using tags with Vim ==
'''Ctags''' is a tool that generates a tag file of names found in source files. In Moodle, it can be used to index PHP functions, variables, classes and so on. These tags allow definitions to be quickly and easily located by a text editor such as [[vim]].


In order to use the tags file with the Vim editor (on a Unix style system), you should first add the following line to the file '''.vimrc''' in your home directory:
In the past, the Moodle source code contained a generated tags file to be used directly. If you use this utility these days, you have to generate (and keep up-to-date) the tags file yourself. Please note that most modern IDEs do not need this to provide the functionality.


    set tags = tags;/
== Obtaining ctags ==


If .vimrc does not exist you should create it. This directive will force Vim to search for the tags file in the directory tree in which you are working (it searched up from where you are) so it will work for multiple versions of the code automatically.
There are several implementations of the original ctags available these days. Most Moodle developers who use ctags, found https://ctags.io/ a good one.


=== How to jump to a tag with Vim ===
== Generating a tags file ==


There are a number of things you can do:
Example of how the tags file can be generated, tuned for Moodle development:


* Place the cursor over the tag (e.g., function name) and press '''CTRL+]'''
    # cd /path/to/moodle/dirroot/
* Type the command ''':tag <tagname>'''
    # ctags -R --languages=php --fields=+aimS --php-kinds=cdfint --tag-relative=yes --totals=yes --exclude=tags --exclude="config*.php" \
* To open in a new editor window type the command ''':stag <tagname>'''
        --exclude="lang/*" --exclude="install/lang/*" --exclude="vendor" --exclude="node_modules" --exclude="moodledata/*" \
* To return to your previous location type '''CTRL+t'''
        --exclude="phpunit.xml" --exclude=".git/*" --extra=+q


=== Generating the tags file ===
but there are alternative ways too - such as [https://pastebin.com/V2EPbR4z this one].


If you need to generate your own tags file (perhaps because you are working on optional modules), from the Moodle root directory issue the command:
== Using in ViM ==


    find . -name "*.php" | xargs ctags
The [[vim]] editor provides the support for tags file. If you have the <tt>tags</tt> file generated in the root of the Moodle code, you will probably want something like this in your <tt>vimrc</tt> file.
 
  set tags=tags,../tags,../../tags,../../../tags,../../../../tags,../../../../../tags,../../../../../../tags,../../../../../../../tags
 
Once loaded, you can use the tags file to quickly jump and navigate over the Moodle code base:
 
* Place the cursor over the tag (e.g., function name) and press <tt>CTRL+]</tt>.
* Type the command <tt>:tag <tagname></tt>
* To return to your previous location, press <tt>CTRL+t</tt>
 
Please refer to your editor help for details and other options.
 
=== Integration with PHP omnicomplete ===
 
Ctags also play well the the ViM omnicomplete.
 
* [https://github.com/shawncplus/phpcomplete.vim PHP omnicompletion]
* [https://pastebin.com/Q4N52z6h Example of configuration]


==See also==
==See also==


* [[http://www.vim.org/tips/tip.php?tip_id=94 Vim tags usage tip]]
* [http://www.emacswiki.org/emacs/EmacsTags EmacsTags]


[[Category:Ctags]]
[[Category:Developer tools]]

Revision as of 12:03, 16 October 2017

Example of auto-completion menu provided by the PHP omnicomplete extension in ViM

Ctags is a tool that generates a tag file of names found in source files. In Moodle, it can be used to index PHP functions, variables, classes and so on. These tags allow definitions to be quickly and easily located by a text editor such as vim.

In the past, the Moodle source code contained a generated tags file to be used directly. If you use this utility these days, you have to generate (and keep up-to-date) the tags file yourself. Please note that most modern IDEs do not need this to provide the functionality.

Obtaining ctags

There are several implementations of the original ctags available these days. Most Moodle developers who use ctags, found https://ctags.io/ a good one.

Generating a tags file

Example of how the tags file can be generated, tuned for Moodle development:

   # cd /path/to/moodle/dirroot/
   # ctags -R --languages=php --fields=+aimS --php-kinds=cdfint --tag-relative=yes --totals=yes --exclude=tags --exclude="config*.php" \
       --exclude="lang/*" --exclude="install/lang/*" --exclude="vendor" --exclude="node_modules" --exclude="moodledata/*" \
       --exclude="phpunit.xml" --exclude=".git/*" --extra=+q

but there are alternative ways too - such as this one.

Using in ViM

The vim editor provides the support for tags file. If you have the tags file generated in the root of the Moodle code, you will probably want something like this in your vimrc file.

set tags=tags,../tags,../../tags,../../../tags,../../../../tags,../../../../../tags,../../../../../../tags,../../../../../../../tags

Once loaded, you can use the tags file to quickly jump and navigate over the Moodle code base:

  • Place the cursor over the tag (e.g., function name) and press CTRL+].
  • Type the command :tag <tagname>
  • To return to your previous location, press CTRL+t

Please refer to your editor help for details and other options.

Integration with PHP omnicomplete

Ctags also play well the the ViM omnicomplete.

See also