1+ /*
2+ * Copyright (c) 2013 Adobe Systems Incorporated. All rights reserved.
3+ *
4+ * Permission is hereby granted, free of charge, to any person obtaining a
5+ * copy of this software and associated documentation files (the "Software"),
6+ * to deal in the Software without restriction, including without limitation
7+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8+ * and/or sell copies of the Software, and to permit persons to whom the
9+ * Software is furnished to do so, subject to the following conditions:
10+ *
11+ * The above copyright notice and this permission notice shall be included in
12+ * all copies or substantial portions of the Software.
13+ *
14+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20+ * DEALINGS IN THE SOFTWARE.
21+ *
22+ */
23+
24+ /*jslint vars: true, plusplus: true, devel: true, nomen: true, indent: 4, maxerr: 50, regexp: true */
25+ /*global define */
26+
27+ /**
28+ * HintUtils2 was created as a place to put utilities that do not require third party dependencies so
29+ * they can be used by tern-worker.js and other JS files.
30+ * This is done because of the require config in tern-worker.js needed to load tern libraries. Libraries
31+ * that include, say "acorn", will fail to load.
32+ */
33+ define ( function ( require , exports , module ) {
34+ "use strict" ;
35+
36+ /**
37+ * Format the given parameter array. Handles separators between
38+ * parameters, syntax for optional parameters, and the order of the
39+ * parameter type and parameter name.
40+ *
41+ * @param {!Array.<{name: string, type: string, isOptional: boolean}> } params -
42+ * array of parameter descriptors
43+ * @param {function(string)= } appendSeparators - callback function to append separators.
44+ * The separator is passed to the callback.
45+ * @param {function(string, number)= } appendParameter - callback function to append parameter.
46+ * The formatted parameter type and name is passed to the callback along with the
47+ * current index of the parameter.
48+ * @param {boolean= } typesOnly - only show parameter types. The
49+ * default behavior is to include both parameter names and types.
50+ * @return {string } - formatted parameter hint
51+ */
52+ function formatParameterHint ( params , appendSeparators , appendParameter , typesOnly ) {
53+ var result = "" ,
54+ pendingOptional = false ;
55+
56+ params . forEach ( function ( value , i ) {
57+ var param = value . type ,
58+ separators = "" ;
59+
60+ if ( value . isOptional ) {
61+ // if an optional param is following by an optional parameter, then
62+ // terminate the bracket. Otherwise enclose a required parameter
63+ // in the same bracket.
64+ if ( pendingOptional ) {
65+ separators += "]" ;
66+ }
67+
68+ pendingOptional = true ;
69+ }
70+
71+ if ( i > 0 ) {
72+ separators += ", " ;
73+ }
74+
75+ if ( value . isOptional ) {
76+ separators += "[" ;
77+ }
78+
79+ if ( appendSeparators ) {
80+ appendSeparators ( separators ) ;
81+ }
82+
83+ result += separators ;
84+
85+ if ( ! typesOnly ) {
86+ param += " " + value . name ;
87+ }
88+
89+ if ( appendParameter ) {
90+ appendParameter ( param , i ) ;
91+ }
92+
93+ result += param ;
94+
95+ } ) ;
96+
97+ if ( pendingOptional ) {
98+ if ( appendSeparators ) {
99+ appendSeparators ( "]" ) ;
100+ }
101+
102+ result += "]" ;
103+ }
104+
105+ return result ;
106+ }
107+
108+ exports . formatParameterHint = formatParameterHint ;
109+ } ) ;
0 commit comments