@@ -15,6 +15,11 @@ define(function (require, exports, module) {
15
15
PerfUtils = brackets . getModule ( "utils/PerfUtils" ) ,
16
16
NGUtils = require ( "NGUtils" ) ;
17
17
18
+ var patterns = {
19
+ directive : / \. d i r e c t i v e \( [ ' " ] ( [ a - z A - Z - ] + ) [ ' " ] / g,
20
+ controller : / \. c o n t r o l l e r \( [ ' " ] ( \w + ) [ ' " ] / g
21
+ }
22
+
18
23
/**
19
24
* Return the token string that is at the specified position.
20
25
*
@@ -40,6 +45,27 @@ define(function (require, exports, module) {
40
45
return token . string . replace ( / \- \w / g, function ( x ) { return x . charAt ( 1 ) . toUpperCase ( ) ; } ) ;
41
46
}
42
47
48
+
49
+ /**
50
+ * Return the token string that is at the specified position.
51
+ *
52
+ * @param hostEditor {!Editor} editor
53
+ * @param {!{line:Number, ch:Number} } pos
54
+ * @return {String } token string at the specified position
55
+ */
56
+ function _getControllerName ( hostEditor , pos ) {
57
+ var token = hostEditor . _codeMirror . getTokenAt ( pos , true ) ;
58
+
59
+ var attribute = hostEditor . _codeMirror . getTokenAt ( { line : pos . line , ch : token . start - 2 } , true ) ;
60
+
61
+ // Return valid function expressions only (function call or reference)
62
+ if ( ! ( attribute . string === "ng-controller" ) ) {
63
+ return null ;
64
+ }
65
+
66
+ return token . string . substr ( 1 , token . string . length - 2 ) ;
67
+ }
68
+
43
69
/**
44
70
* @private
45
71
* For unit and performance tests. Allows lookup by function name instead of editor offset
@@ -48,14 +74,14 @@ define(function (require, exports, module) {
48
74
* @param {!string } directiveName
49
75
* @return {$.Promise } a promise that will be resolved with an array of function offset information
50
76
*/
51
- function _findInProject ( directiveName ) {
77
+ function _findInProject ( directiveName , pattern ) {
52
78
var result = new $ . Deferred ( ) ;
53
79
54
80
FileIndexManager . getFileInfoList ( "all" )
55
81
. done ( function ( fileInfos ) {
56
82
PerfUtils . markStart ( PerfUtils . ANGULARJS_FIND_DIRECTIVE ) ;
57
83
58
- NGUtils . findMatchingDirectives ( directiveName , fileInfos , true )
84
+ NGUtils . findMatches ( pattern , directiveName , fileInfos , true )
59
85
. done ( function ( functions ) {
60
86
PerfUtils . addMeasurement ( PerfUtils . ANGULARJS_FIND_DIRECTIVE ) ;
61
87
result . resolve ( functions ) ;
@@ -81,7 +107,7 @@ define(function (require, exports, module) {
81
107
* @return {$.Promise } a promise that will be resolved with an InlineWidget
82
108
* or null if we're not going to provide anything.
83
109
*/
84
- function _createInlineEditor ( hostEditor , directiveName ) {
110
+ function _createInlineEditor ( hostEditor , directiveName , pattern ) {
85
111
// Use Tern jump-to-definition helper, if it's available, to find InlineEditor target.
86
112
var helper = brackets . _jsCodeHintsHelper ;
87
113
if ( helper === null ) {
@@ -102,7 +128,7 @@ define(function (require, exports, module) {
102
128
var fileInfos = [ ] ;
103
129
fileInfos . push ( { name : jumpResp . resultFile , fullPath : resolvedPath } ) ;
104
130
// JSUtils.findMatchingFunctions(directiveName, fileInfos, true)
105
- NGUtils . findMatchingDirectives ( directiveName , fileInfos , true )
131
+ NGUtils . findMatches ( pattern , directiveName , fileInfos , true )
106
132
. done ( function ( functions ) {
107
133
if ( functions && functions . length > 0 ) {
108
134
var jsInlineEditor = new MultiRangeInlineEditor ( functions ) ;
@@ -123,7 +149,7 @@ define(function (require, exports, module) {
123
149
124
150
} else { // no result from Tern. Fall back to _findInProject().
125
151
126
- _findInProject ( directiveName ) . done ( function ( functions ) {
152
+ _findInProject ( directiveName , pattern ) . done ( function ( functions ) {
127
153
if ( functions && functions . length > 0 ) {
128
154
var jsInlineEditor = new MultiRangeInlineEditor ( functions ) ;
129
155
jsInlineEditor . load ( hostEditor ) ;
@@ -161,35 +187,34 @@ define(function (require, exports, module) {
161
187
* @return {$.Promise } a promise that will be resolved with an InlineWidget
162
188
* or null if we're not going to provide anything.
163
189
*/
164
- function directiveProvider ( hostEditor , pos ) {
165
- // Only provide a JavaScript editor when cursor is in JavaScript content
190
+ function provider ( hostEditor , pos ) {
191
+ // Only provide an editor when cursor is in HTML content
166
192
if ( hostEditor . getModeForSelection ( ) !== "html" ) {
167
193
return null ;
168
194
}
169
195
170
- // Only provide JavaScript editor if the selection is within a single line
196
+ // Only provide an editor if the selection is within a single line
171
197
var sel = hostEditor . getSelection ( ) ;
172
198
if ( sel . start . line !== sel . end . line ) {
173
199
return null ;
174
200
}
175
201
176
202
// Always use the selection start for determining the function name. The pos
177
203
// parameter is usually the selection end.
178
- var directiveName = _getDirectiveName ( hostEditor , sel . start ) ;
179
- if ( ! directiveName ) {
180
- return null ;
204
+ var directiveName , controllerName ;
205
+ if ( directiveName = _getDirectiveName ( hostEditor , sel . start ) ) {
206
+ return _createInlineEditor ( hostEditor , directiveName , patterns . directive ) ;
181
207
}
182
-
183
- return _createInlineEditor ( hostEditor , directiveName ) ;
208
+
209
+ if ( controllerName = _getControllerName ( hostEditor , sel . start ) ) {
210
+ return _createInlineEditor ( hostEditor , _getControllerName ( hostEditor , sel . start ) , patterns . controller ) ;
211
+ }
212
+
213
+ return null ;
184
214
}
185
215
186
216
// init
187
- EditorManager . registerInlineEditProvider ( directiveProvider ) ;
217
+ EditorManager . registerInlineEditProvider ( provider ) ;
188
218
PerfUtils . createPerfMeasurement ( "ANGULARJS_INLINE_CREATE" , "AngularJS Inline Editor Creation" ) ;
189
219
PerfUtils . createPerfMeasurement ( "ANGULARJS_FIND_DIRECTIVE" , "AngularJS Find Directive" ) ;
190
-
191
- // for unit tests only
192
- exports . directiveProvider = directiveProvider ;
193
- exports . _createInlineEditor = _createInlineEditor ;
194
- exports . _findInProject = _findInProject ;
195
220
} ) ;
0 commit comments