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
m (→‎Generating a tags file: Simplify ctags params)
 
(6 intermediate revisions by 6 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 searches 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 --fields=+aimS --php-kinds=cdfint --languages=php --extras=+q --tag-relative=yes --exclude=".git" --exclude="vendor" \
* To open in a new editor window type the command ''':stag <tagname>'''
        --exclude="node_modules" --exclude="composer.phar" --totals=yes
* To return to your previous location type '''CTRL+t'''


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


The file should be of the order of 1MB in size.
set tags=tags,../tags,../../tags,../../../tags,../../../../tags,../../../../../tags,../../../../../../tags,../../../../../../../tags


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


As of Moodle 1.9.6 the tags file is no longer included in the Moodle distribution. Instead a file tags.txt is included providing instructions on creating the tag file yourself. The text of this file is as follows:
* 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>


<pre>
Please refer to your editor help for details and other options.
Generating a tags file
======================


If you need a tags file so that you can jump around Moodle code
=== Integration with PHP omnicomplete ===
easily in your editor (eg vim or emacs), you can generate one:


Exuberant ctags (default on Linux, can be compiled on other platforms):
Ctags also play well the the ViM omnicomplete.
----------------------------------------------------------------------


ctags -R --languages=php --exclude="CVS" --php-kinds=f \
* [https://github.com/shawncplus/phpcomplete.vim PHP omnicompletion]
--regex-PHP='/abstract class ([^ ]*)/\1/c/' \
* [https://pastebin.com/Q4N52z6h Example of configuration]
--regex-PHP='/interface ([^ ]*)/\1/c/' \
--regex-PHP='/(public |static |abstract |protected |private )+function ([^ (]*)/\2/f/'
 
 
Downloading the tags file
=========================
 
If for some reason you can't generate one, you can download a recent one from here:
 
http://download.moodle.org/tags            (for head)
http://download.moodle.org/stable19/tags  (Moodle 1.9.x)
http://download.moodle.org/stable18/tags  (Moodle 1.8.x)
</pre>


==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]
* [http://vimdoc.sourceforge.net/htmldoc/usr_29.html#29.1 Vim documentation: Using tags]
* [http://vimdoc.sourceforge.net/htmldoc/tagsrch.html Vim documentation: Tags and special searches]


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

Latest revision as of 17:43, 2 December 2019

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 --fields=+aimS --php-kinds=cdfint --languages=php --extras=+q --tag-relative=yes --exclude=".git" --exclude="vendor" \
       --exclude="node_modules" --exclude="composer.phar" --totals=yes

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