Skip to content

Commit b1afd7d

Browse files
committed
Merge branch 'feature/improved-console-support' into develop
2 parents d4623fa + ca54c94 commit b1afd7d

File tree

2 files changed

+95
-3
lines changed

2 files changed

+95
-3
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: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,24 @@ if (file_exists($autoload = __DIR__ . '/vendor/autoload.php')) {
1212
);
1313
}
1414

15-
$options = getopt('af:R',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
));
2627

28+
// option -V is an alternative to --version
29+
if (isset($options['V'])) {
30+
$options['version'] = FALSE;
31+
}
32+
2733
if(!isset($options['debug'])) {
2834
error_reporting(0);
2935
}
@@ -43,6 +49,30 @@ array_shift($argv);
4349

4450
$file = array_pop($argv);
4551

52+
// option -o is an alternative to -f
53+
if (isset($options['o']) && !isset($options['f'])) {
54+
$options['f'] = $options['o'];
55+
}
56+
57+
// if both -n and -N options are given, use the last specified one
58+
if (isset($options['n']) && isset($options['N'])) {
59+
if (array_search('n', array_keys($options)) < array_search('N', array_keys($options))) {
60+
unset($options['n']);
61+
} else {
62+
unset($options['N']);
63+
}
64+
}
65+
66+
// option -n is equivalent to --excmd=number
67+
if (isset($options['n']) && !isset($options['N'])) {
68+
$options['excmd'] = 'number';
69+
}
70+
71+
// option -N is equivalent to --excmd=pattern
72+
if (isset($options['N']) && !isset($options['n'])) {
73+
$options['excmd'] = 'pattern';
74+
}
75+
4676
if(!isset($options['excmd']))
4777
$options['excmd'] = 'pattern';
4878
if(!isset($options['format']))
@@ -55,6 +85,28 @@ if(!isset($options['fields'])) {
5585
$options['fields'] = str_split($options['fields']);
5686
}
5787

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+
58110
// if the memory limit option is set and is valid, adjust memory
59111
if (isset($options['memory'])) {
60112
$memory_limit = trim($options['memory']);
@@ -86,10 +138,15 @@ try {
86138
die("phpctags: {$e->getMessage()}");
87139
}
88140

141+
// write to a specified file
89142
if (isset($options['f']) && $options['f'] !== '-') {
90143
$tagfile = fopen($options['f'], isset($options['a']) ? 'a' : 'w');
91-
} else {
144+
// write to stdout only when instructed
145+
} else if (isset($options['f']) && $options['f'] === '-') {
92146
$tagfile = fopen('php://stdout', 'w');
147+
// write to file 'tags' by default
148+
} else {
149+
$tagfile = fopen('tags', isset($options['a']) ? 'a' : 'w');
93150
}
94151
fwrite($tagfile, $result);
95152
fclose($tagfile);

0 commit comments

Comments
 (0)