Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on May 25, 2019. It is now read-only.

Commit 13c262f

Browse files
author
Dean Sofer
committed
Added support for controllers
1 parent 65dd629 commit 13c262f

File tree

4 files changed

+68
-30
lines changed

4 files changed

+68
-30
lines changed

‎NGUtils.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ define(function (require, exports, module) {
2020
StringUtils = brackets.getModule("utils/StringUtils");
2121

2222
/**
23-
* Tracks dirty documents between invocations of findMatchingDirectives.
23+
* Tracks dirty documents between invocations of findMatches.
2424
* @type {ChangedDocumentTracker}
2525
*/
2626
var _changedDocumentTracker = new ChangedDocumentTracker();
@@ -180,7 +180,7 @@ define(function (require, exports, module) {
180180

181181
// Cache the result in the fileInfo object
182182
fileInfo.JSUtils = {};
183-
fileInfo.JSUtils.functions = allFunctions;
183+
fileInfo.JSUtils[_functionRegExp] = allFunctions;
184184
fileInfo.JSUtils.timestamp = doc.diskTimestamp;
185185

186186
result.resolve({doc: doc, functions: allFunctions});
@@ -200,7 +200,7 @@ define(function (require, exports, module) {
200200
var result = new $.Deferred(),
201201
isChanged = _changedDocumentTracker.isPathChanged(fileInfo.fullPath);
202202

203-
if (isChanged && fileInfo.JSUtils) {
203+
if (isChanged && fileInfo.JSUtils&&fileInfo.JSUtils[_functionRegExp]) {
204204
// See if it's dirty and in the working set first
205205
var doc = DocumentManager.getOpenDocumentForPath(fileInfo.fullPath);
206206

@@ -221,7 +221,7 @@ define(function (require, exports, module) {
221221
}
222222
} else {
223223
// Use the cache if the file did not change and the cache exists
224-
result.resolve(!isChanged && fileInfo.JSUtils);
224+
result.resolve(!isChanged && fileInfo.JSUtils&&fileInfo.JSUtils[_functionRegExp]);
225225
}
226226

227227
return result.promise();
@@ -292,7 +292,9 @@ define(function (require, exports, module) {
292292
if (useCache) {
293293
// Return cached data. doc property is undefined since we hit the cache.
294294
// _getOffsets() will fetch the Document if necessary.
295-
result.resolve({/*doc: undefined,*/fileInfo: fileInfo, functions: fileInfo.JSUtils.functions});
295+
var data = {/*doc: undefined,*/fileInfo: fileInfo};
296+
data[_functionRegExp] = fileInfo.JSUtils[_functionRegExp];
297+
result.resolve(data);
296298
} else {
297299
_readFile(fileInfo, result);
298300
}
@@ -350,7 +352,8 @@ define(function (require, exports, module) {
350352
* source document, start line, and end line (0-based, inclusive range) for each matching function list.
351353
* Does not addRef() the documents returned in the array.
352354
*/
353-
function findMatchingDirectives(functionName, fileInfos, keepAllFiles) {
355+
function findMatches(pattern, functionName, fileInfos, keepAllFiles) {
356+
_functionRegExp = pattern;
354357
var result = new $.Deferred(),
355358
jsFiles = [],
356359
docEntries = [];
@@ -411,5 +414,5 @@ define(function (require, exports, module) {
411414

412415
exports.findAllMatchingDirectivesInText = findAllMatchingDirectivesInText;
413416
exports._getFunctionEndOffset = _getFunctionEndOffset; // For testing only
414-
exports.findMatchingDirectives = findMatchingDirectives;
417+
exports.findMatches = findMatches;
415418
});

‎main.js

Lines changed: 44 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ define(function (require, exports, module) {
1515
PerfUtils = brackets.getModule("utils/PerfUtils"),
1616
NGUtils = require("NGUtils");
1717

18+
var patterns = {
19+
directive: /\.directive\(['"]([a-zA-Z-]+)['"]/g,
20+
controller: /\.controller\(['"](\w+)['"]/g
21+
}
22+
1823
/**
1924
* Return the token string that is at the specified position.
2025
*
@@ -40,6 +45,27 @@ define(function (require, exports, module) {
4045
return token.string.replace(/\-\w/g, function(x){ return x.charAt(1).toUpperCase(); });
4146
}
4247

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+
4369
/**
4470
* @private
4571
* For unit and performance tests. Allows lookup by function name instead of editor offset
@@ -48,14 +74,14 @@ define(function (require, exports, module) {
4874
* @param {!string} directiveName
4975
* @return {$.Promise} a promise that will be resolved with an array of function offset information
5076
*/
51-
function _findInProject(directiveName) {
77+
function _findInProject(directiveName,pattern) {
5278
var result = new $.Deferred();
5379

5480
FileIndexManager.getFileInfoList("all")
5581
.done(function (fileInfos) {
5682
PerfUtils.markStart(PerfUtils.ANGULARJS_FIND_DIRECTIVE);
5783

58-
NGUtils.findMatchingDirectives(directiveName, fileInfos, true)
84+
NGUtils.findMatches(pattern,directiveName, fileInfos, true)
5985
.done(function (functions) {
6086
PerfUtils.addMeasurement(PerfUtils.ANGULARJS_FIND_DIRECTIVE);
6187
result.resolve(functions);
@@ -81,7 +107,7 @@ define(function (require, exports, module) {
81107
* @return {$.Promise} a promise that will be resolved with an InlineWidget
82108
* or null if we're not going to provide anything.
83109
*/
84-
function _createInlineEditor(hostEditor, directiveName) {
110+
function _createInlineEditor(hostEditor, directiveName,pattern) {
85111
// Use Tern jump-to-definition helper, if it's available, to find InlineEditor target.
86112
var helper = brackets._jsCodeHintsHelper;
87113
if (helper === null) {
@@ -102,7 +128,7 @@ define(function (require, exports, module) {
102128
var fileInfos = [];
103129
fileInfos.push({name: jumpResp.resultFile, fullPath: resolvedPath});
104130
// JSUtils.findMatchingFunctions(directiveName, fileInfos, true)
105-
NGUtils.findMatchingDirectives(directiveName, fileInfos, true)
131+
NGUtils.findMatches(pattern,directiveName, fileInfos, true)
106132
.done(function (functions) {
107133
if (functions && functions.length > 0) {
108134
var jsInlineEditor = new MultiRangeInlineEditor(functions);
@@ -123,7 +149,7 @@ define(function (require, exports, module) {
123149

124150
} else { // no result from Tern. Fall back to _findInProject().
125151

126-
_findInProject(directiveName).done(function (functions) {
152+
_findInProject(directiveName,pattern).done(function (functions) {
127153
if (functions && functions.length > 0) {
128154
var jsInlineEditor = new MultiRangeInlineEditor(functions);
129155
jsInlineEditor.load(hostEditor);
@@ -161,35 +187,34 @@ define(function (require, exports, module) {
161187
* @return {$.Promise} a promise that will be resolved with an InlineWidget
162188
* or null if we're not going to provide anything.
163189
*/
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
166192
if (hostEditor.getModeForSelection() !== "html") {
167193
return null;
168194
}
169195

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
171197
var sel = hostEditor.getSelection();
172198
if (sel.start.line !== sel.end.line) {
173199
return null;
174200
}
175201

176202
// Always use the selection start for determining the function name. The pos
177203
// 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);
181207
}
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;
184214
}
185215

186216
// init
187-
EditorManager.registerInlineEditProvider(directiveProvider);
217+
EditorManager.registerInlineEditProvider(provider);
188218
PerfUtils.createPerfMeasurement("ANGULARJS_INLINE_CREATE", "AngularJS Inline Editor Creation");
189219
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;
195220
});

‎package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"title": "AngularJS for Brackets",
44
"description": "AngularJS extension for Brackets by the AngularUI team",
55
"homepage": "https://github.com/angular-ui/AngularJS-brackets",
6-
"version": "0.0.1",
6+
"version": "1.0.1",
77
"author": "AngularUI (http://angular-ui.github.io)",
88
"license": "MIT",
99
"engines": {

‎test.html

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<head>
33
<script>
44
var myModule = angular.module('myModule', []);
5-
myModule.directive('ng-repeat', function(){
5+
myModule.directive('myDirective', function(){
66
return {
77
template: '<cool>I hope this works</cool>',
88
scope: {},
@@ -12,6 +12,15 @@
1212
}
1313
};
1414
});
15+
myModule.controller('Testing', function(){
16+
// i sure hope this works too
17+
});
18+
19+
function tester() {
20+
//cool
21+
}
22+
23+
tester();
1524
</script>
1625
<link type="text/css" href="" />
1726
<title>AngularJS Brackets Demo</title>
@@ -20,8 +29,9 @@
2029

2130
<h1>Kickass</h1>
2231

23-
<ul>
24-
<li ng-repeat="i in items"></li>
32+
<ul ng-controller="Testing">
33+
<li my-directive></li>
34+
2535
</ul>
2636
</body>
2737
</html>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /