3 # Author: The Geany contributors
4 # License: GPL v2 or later
6 # This is a helper library for Python scripts to create tag files in ctags format,
7 # e.g. create_py_tags.py and create_php_tags.py.
14 CTAGS_FILE_HEADER
= '''!_TAG_FILE_FORMAT 2 /extended format; --format=1 will not append ;" to lines/
15 !_TAG_FILE_SORTED 1 /0=unsorted, 1=sorted, 2=foldcase/
16 !_TAG_OUTPUT_EXCMD mixed /number, pattern, mixed, or combineV2/
17 !_TAG_OUTPUT_FILESEP slash /slash or backslash/
18 !_TAG_OUTPUT_MODE u-ctags /u-ctags or e-ctags/
19 !_TAG_PATTERN_LENGTH_LIMIT 96 /0 for no limit/
20 !_TAG_PROGRAM_NAME {program_name} Automatically generated file - do not edit (created on {timestamp} with Python {python_version})
24 def format_tag(tagname
, kind
, signature
, parent
, return_type
=None):
28 @param signature (str)
30 @param return_type (tuple of two elements or None)
34 signature_field
= f
'\tsignature:{signature}'
39 parent_field
= f
'\tclass:{parent}'
44 return_type_field
= f
'\ttyperef:{return_type[0]}:{return_type[1]}'
46 return_type_field
= ''
48 return f
'{tagname}\t/unknown\t1;"\tkind:{kind}{parent_field}{signature_field}{return_type_field}\n'
51 def write_ctags_file(filename
, tags
, program_name
):
53 Sort the found tags and write them into the file specified by filename
57 @param program_name (str)
61 ctags_file_header
= CTAGS_FILE_HEADER
.format(
62 program_name
=program_name
,
63 timestamp
=datetime
.datetime
.now().ctime(),
64 python_version
=platform
.python_version())
66 with
open(filename
, 'w', encoding
='utf-8') as target_file
:
67 target_file
.write(ctags_file_header
)
69 if symbol
!= '\n': # skip empty lines
70 target_file
.write(symbol
)