Skip to content

Commit 74e4e44

Browse files
committed
Add support for --sort and -u options
1 parent d2fb914 commit 74e4e44

File tree

2 files changed

+60
-2
lines changed

2 files changed

+60
-2
lines changed

PHPCtags.class.php

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,32 @@ private function getNodeAccess($node)
4848
return 'public';
4949
}
5050

51+
/**
52+
* stringSortByLine
53+
*
54+
* Sort a string based on its line delimiter
55+
*
56+
* @author Techlive Zheng
57+
*
58+
* @access public
59+
* @static
60+
*
61+
* @param string $str string to be sorted
62+
* @param boolean $foldcse case-insensitive sorting
63+
*
64+
* @return string sorted string
65+
**/
66+
public static function stringSortByLine($str, $foldcase=FALSE)
67+
{
68+
$arr = explode("\n", $str);
69+
if (!$foldcase)
70+
sort($arr, SORT_STRING);
71+
else
72+
sort($arr, SORT_STRING | SORT_FLAG_CASE);
73+
$str = implode("\n", $arr);
74+
return $str;
75+
}
76+
5177
private static function helperSortByLine($a, $b) {
5278
return $a['line'] > $b['line'] ? 1 : 0;
5379
}
@@ -163,7 +189,10 @@ private function struct($node, $reset=FALSE, $parent=array())
163189

164190
if(!empty($parent)) array_pop($scope);
165191

166-
usort($structs, 'self::helperSortByLine');
192+
// if no --sort is given, sort by occurrence
193+
if (!isset($options['sort']) || $options['sort'] == 'no') {
194+
usort($structs, 'self::helperSortByLine');
195+
}
167196

168197
return $structs;
169198
}
@@ -243,6 +272,12 @@ private function render($structs, $options)
243272

244273
$str .= "\n";
245274
}
275+
276+
// sort the result as instructed
277+
if (isset($options['sort']) && ($options['sort'] == 'yes' || $options['sort'] == 'foldcase')) {
278+
$str = self::stringSortByLine($str, $options['sort'] == 'foldcase');
279+
}
280+
246281
return $str;
247282
}
248283

phpctags

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@ if (file_exists($autoload = __DIR__ . '/vendor/autoload.php')) {
1212
);
1313
}
1414

15-
$options = getopt('af:Nno:RV',array(
15+
$options = getopt('af:Nno:RuV',array(
1616
'append::',
1717
'debug',
1818
'exclude:',
1919
'excmd::',
2020
'fields::',
2121
'format::',
2222
'recurse::',
23+
'sort::',
2324
'version',
2425
'memory::',
2526
));
@@ -84,6 +85,28 @@ if(!isset($options['fields'])) {
8485
$options['fields'] = str_split($options['fields']);
8586
}
8687

88+
// handle -u or --sort options
89+
if (isset($options['sort'])) {
90+
// --sort or --sort=[Y,y,YES,Yes,yes]
91+
if ($options['sort'] === FALSE || yes_or_no($options['sort']) == 'yes') {
92+
$options['sort'] = 'yes';
93+
// --sort=[N,n,NO,No,no]
94+
} else if (yes_or_no($options['sort']) == 'no') {
95+
$options['sort'] = 'no';
96+
// --sort=foldcase, case insensitive sorting
97+
} else if ($options['sort'] == 'foldcase') {
98+
$options['sort'] = 'foldcase';
99+
} else {
100+
die('phpctags: Invalid value for "sort" option');
101+
}
102+
// option -n is equivalent to --sort=no
103+
} else if (isset($options['u'])) {
104+
$options['sort'] = 'no';
105+
// sort the result by default
106+
} else {
107+
$options['sort'] = 'yes';
108+
}
109+
87110
// if the memory limit option is set and is valid, adjust memory
88111
if (isset($options['memory'])) {
89112
$memory_limit = trim($options['memory']);

0 commit comments

Comments
 (0)