Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 427931
Collapse All | Expand All

(-)a/bundles/org.eclipse.orion.client.javascript/web/javascript/finder.js (-3 / +21 lines)
Lines 110-119 Link Here
110
					idx = 0;
110
					idx = 0;
111
					token = tokens[0];
111
					token = tokens[0];
112
				if(offset >= token.range[0] && offset < token.range[1]) {
112
				if(offset >= token.range[0] && offset < token.range[1]) {
113
					token.index = offset;
113
					return token;
114
					return token;
114
				}
115
				}
115
				token = tokens[max];
116
				token = tokens[max];
116
				if(offset >= token.range[0] && offset < token.range[1]) {
117
				if(offset >= token.range[0]) {
118
					token.index = max;
117
					return token;
119
					return token;
118
				}
120
				}
119
				token = null;
121
				token = null;
Lines 123-136 Link Here
123
					if(offset < token.range[0]) {
125
					if(offset < token.range[0]) {
124
						max = idx-1;
126
						max = idx-1;
125
					}
127
					}
126
					else if(offset >= token.range[1]) {
128
					else if(offset > token.range[1]) {
127
						min = idx+1;
129
						min = idx+1;
128
					}
130
					}
131
					else if(offset === token.range[1]) {
132
						var next = tokens[idx+1];
133
						if(next.range[0] === token.range[1]) {
134
							min = idx+1;
135
						}
136
						else {
137
							token.index = idx;
138
							return token;
139
						}
140
					}
129
					else if(offset >= token.range[0] && offset < token.range[1]) {
141
					else if(offset >= token.range[0] && offset < token.range[1]) {
142
						token.index = idx;
130
						return token;
143
						return token;
131
					}
144
					}
132
					if(min === max) {
145
					if(min === max) {
133
						return tokens[min];
146
						token = tokens[min];
147
						if(offset >= token.range[0] && offset <= token.range[1]) {
148
							token.index = min;
149
							return token;
150
						}
151
						return null;
134
					}
152
					}
135
				}
153
				}
136
			}
154
			}
(-)a/bundles/org.eclipse.orion.client.javascript/web/javascript/occurrences.js (-33 / +71 lines)
Lines 42-47 Link Here
42
		 * @returns The status if we should continue visiting
42
		 * @returns The status if we should continue visiting
43
		 */
43
		 */
44
		enter: function(node) {
44
		enter: function(node) {
45
			var len, idx;
45
			switch(node.type) {
46
			switch(node.type) {
46
				case Estraverse.Syntax.Program:
47
				case Estraverse.Syntax.Program:
47
					this.occurrences = [];
48
					this.occurrences = [];
Lines 54-61 Link Here
54
					//we want the parent scope for a declaration, otherwise we leave it right away
55
					//we want the parent scope for a declaration, otherwise we leave it right away
55
					this._enterScope(node);
56
					this._enterScope(node);
56
					if (node.params) {
57
					if (node.params) {
57
						for (var i = 0; i < node.params.length; i++) {
58
						len = node.params.length;
58
							if(this.checkId(node.params[i], true)) {
59
						for (idx = 0; idx < len; idx++) {
60
							if(this.checkId(node.params[idx], true)) {
59
								return Estraverse.VisitorOption.Skip;
61
								return Estraverse.VisitorOption.Skip;
60
							}
62
							}
61
						}
63
						}
Lines 66-73 Link Here
66
						if(this._enterScope(node)) {
68
						if(this._enterScope(node)) {
67
							return Estraverse.VisitorOption.Skip;
69
							return Estraverse.VisitorOption.Skip;
68
						}
70
						}
69
						for (var j = 0; j < node.params.length; j++) {
71
						len = node.params.length;
70
							if(this.checkId(node.params[j], true)) {
72
						for (idx = 0; idx < len; idx++) {
73
							if(this.checkId(node.params[idx], true)) {
71
								return Estraverse.VisitorOption.Skip;
74
								return Estraverse.VisitorOption.Skip;
72
							}
75
							}
73
						}
76
						}
Lines 79-86 Link Here
79
					break;
82
					break;
80
				case Estraverse.Syntax.ArrayExpression: 
83
				case Estraverse.Syntax.ArrayExpression: 
81
					if (node.elements) {
84
					if (node.elements) {
82
						for (var k = 0; k < node.elements.length; k++) {
85
						len = node.elements.length;
83
							this.checkId(node.elements[k]);
86
						for (idx = 0; idx < len; idx++) {
87
							this.checkId(node.elements[idx]);
84
						}
88
						}
85
					}
89
					}
86
					break;
90
					break;
Lines 114-121 Link Here
114
				case Estraverse.Syntax.CallExpression:
118
				case Estraverse.Syntax.CallExpression:
115
					this.checkId(node.callee, false);
119
					this.checkId(node.callee, false);
116
					if (node.arguments) {
120
					if (node.arguments) {
117
						for (var l = 0; l < node.arguments.length; l++) {
121
						len = node.arguments.length;
118
							this.checkId(node.arguments[l]);
122
						for (idx = 0; idx < len; idx++) {
123
							this.checkId(node.arguments[idx]);
119
						}
124
						}
120
					}
125
					}
121
					break;
126
					break;
Lines 127-135 Link Here
127
						return Estraverse.VisitorOption.Skip;
132
						return Estraverse.VisitorOption.Skip;
128
					}
133
					}
129
					if(node.properties) {
134
					if(node.properties) {
130
						var len = node.properties.length;
135
						len = node.properties.length;
131
						for (var m = 0; m < len; m++) {
136
						for (idx = 0; idx < len; idx++) {
132
							var prop = node.properties[m];
137
							var prop = node.properties[idx];
133
							if(this.thisCheck && prop.value && prop.value.type === Estraverse.Syntax.FunctionExpression) {
138
							if(this.thisCheck && prop.value && prop.value.type === Estraverse.Syntax.FunctionExpression) {
134
								//tag it 
139
								//tag it 
135
								prop.value.isprop = true;
140
								prop.value.isprop = true;
Lines 145-153 Link Here
145
				case Estraverse.Syntax.NewExpression:
150
				case Estraverse.Syntax.NewExpression:
146
					this.checkId(node.callee, false);
151
					this.checkId(node.callee, false);
147
					if(node.arguments) {
152
					if(node.arguments) {
148
						var len = node.arguments.length;
153
						len = node.arguments.length;
149
						for(var m = 0; m < len; m++) {
154
						for(idx = 0; idx < len; idx++) {
150
							this.checkId(node.arguments[m]);
155
							this.checkId(node.arguments[idx]);
151
						}
156
						}
152
					}
157
					}
153
					break;
158
					break;
Lines 163-169 Link Here
163
							end: node.range[1]
168
							end: node.range[1]
164
						});
169
						});
165
						// if this node is the selected this we are in the right scope
170
						// if this node is the selected this we are in the right scope
166
						if (node.range[0] === this.context.node.range[0]){
171
						if (node.range[0] === this.context.token.range[0]){
167
							this.defscope = scope;
172
							this.defscope = scope;
168
						}
173
						}
169
					}
174
					}
Lines 343-349 Link Here
343
				this.visitor.enter = this.visitor.enter.bind(this.visitor);
348
				this.visitor.enter = this.visitor.enter.bind(this.visitor);
344
				this.visitor.leave = this.visitor.leave.bind(this.visitor);
349
				this.visitor.leave = this.visitor.leave.bind(this.visitor);
345
			}
350
			}
346
			this.visitor.thisCheck = context.node && context.node.type === Estraverse.Syntax.ThisExpression;
351
			this.visitor.thisCheck = context.token && context.token.type === 'Keyword' && context.token.value === 'this';
347
			this.visitor.context = context;
352
			this.visitor.context = context;
348
			return this.visitor;			
353
			return this.visitor;			
349
		},
354
		},
Lines 352-364 Link Here
352
		 * @description Computes the node name to use while searching
357
		 * @description Computes the node name to use while searching
353
		 * @function
358
		 * @function
354
		 * @private
359
		 * @private
355
		 * @param {Object} node The AST node
360
		 * @param {Object} token The AST token
356
		 * @returns {String} The node name to use while seraching
361
		 * @returns {String} The node name to use while seraching
357
		 */
362
		 */
358
		_nameFromNode: function(node) {
363
		_nameFromNode: function(token) {
359
			switch(node.type) {
364
			switch(token.type) {
360
				case Estraverse.Syntax.Identifier: return node.name;
365
				case Estraverse.Syntax.Identifier: return token.value;
361
				case Estraverse.Syntax.ThisExpression: return 'this';
366
				case 'Keyword': return 'this';
362
			}
367
			}
363
		},
368
		},
364
		
369
		
Lines 367-385 Link Here
367
		 * @function
372
		 * @function
368
		 * @private
373
		 * @private
369
		 * @param {Object} context The selection context from the editor
374
		 * @param {Object} context The selection context from the editor
370
		 * @param {Object} node The AST node
375
		 * @param {Object} token The AST token
371
		 * @param {Object} ast The AST
376
		 * @param {Object} ast The AST
372
		 * @returns {Boolean} True if we shoud skip computing occurrences
377
		 * @returns {Boolean} True if we shoud skip computing occurrences
373
		 */
378
		 */
374
		_skip: function(context, node, ast) {
379
		_skip: function(context, token, ast) {
375
			if(!node || node.type === Estraverse.Syntax.Literal) {
380
			if(!token) {
376
				return true;
377
			}	
378
			var comment = Finder.findComment(context.selection.start, ast);
379
			if(comment) {
380
				return true;
381
				return true;
381
			}
382
			}
382
			return false;
383
			if(token.type === 'Keyword') {
384
				return token.value !== 'this';
385
			}
386
			return token.type !== Estraverse.Syntax.Identifier
387
		},
388
		
389
		/**
390
		 * @description Gets the token from the given offset or the proceeding token if the found token 
391
		 * is a punctuator
392
		 * @function
393
		 * @private
394
		 * @param {Number} offset The offset into the source
395
		 * @param {Object} ast The AST
396
		 * @return {Object} The token for the given offset or null
397
		 */
398
		_getToken: function(offset, ast) {
399
			if(ast.tokens && ast.tokens.length > 0) {
400
				var token = Finder.findToken(offset, ast.tokens);
401
				if(token) {
402
					if(token.type === 'Punctuator') {
403
						var index = token.index;
404
						//only check back if we are at the start of the punctuator i.e. here -> {
405
						if(offset === token.range[0] && index != null && index > 0) {
406
							var prev = ast.tokens[index-1];
407
							if(prev.range[1] !== token.range[0]) {
408
								return null;
409
							}
410
							else {
411
								token = prev;
412
							}
413
						}
414
					}
415
					if(token.type === 'Identifier' || (token.type === 'Keyword' && token.value === 'this')) {
416
						return token;
417
					}
418
				}
419
			}
420
			return null;
383
		},
421
		},
384
		
422
		
385
		/**
423
		/**
Lines 395-407 Link Here
395
			var that = this;
433
			var that = this;
396
			return this.astManager.getAST(editorContext).then(function(ast) {
434
			return this.astManager.getAST(editorContext).then(function(ast) {
397
				if(ast) {
435
				if(ast) {
398
					var node = Finder.findNode(ctxt.selection.start, ast);
436
					var token = that._getToken(ctxt.selection.start, ast);
399
					if(!that._skip(ctxt, node, ast)) {
437
					if(!that._skip(ctxt, token, ast)) {
400
						var context = {
438
						var context = {
401
							start: ctxt.selection.start,
439
							start: ctxt.selection.start,
402
							end: ctxt.selection.end,
440
							end: ctxt.selection.end,
403
							word: that._nameFromNode(node),
441
							word: that._nameFromNode(token),
404
							node: node,
442
							token: token,
405
						};
443
						};
406
						var visitor = that.getVisitor(context);
444
						var visitor = that.getVisitor(context);
407
						Estraverse.traverse(ast, visitor);
445
						Estraverse.traverse(ast, visitor);
(-)a/bundles/org.eclipse.orion.client.javascript/web/js-tests/javascript/finderTests.js (+142 lines)
Lines 570-575 Link Here
570
		},
570
		},
571
		
571
		
572
		/**
572
		/**
573
		 * Find a token in an AST with copious whitespace
574
		 * @see https://bugs.eclipse.org/bugs/show_bug.cgi?id=427931
575
		 */
576
		test_findToken16: function() {
577
			var text = "var   foo = {f: function() {}}";
578
			return astManager.getAST(setUp(text)).then(function(ast) {
579
				try {
580
					var token = Finder.findToken(4, ast.tokens);
581
					Assert.equal(null, token, 'Should not have found a token');
582
				}
583
				finally {
584
					astManager.updated();
585
				}
586
			});
587
		},
588
		
589
		/**
590
		 * Find a token in an AST with copious whitespace
591
		 * @see https://bugs.eclipse.org/bugs/show_bug.cgi?id=427931
592
		 */
593
		test_findToken17: function() {
594
			var text = "var   foo = {f: function() {}}";
595
			return astManager.getAST(setUp(text)).then(function(ast) {
596
				try {
597
					var token = Finder.findToken(5, ast.tokens);
598
					Assert.equal(null, token, 'Should not have found a token');
599
				}
600
				finally {
601
					astManager.updated();
602
				}
603
			});
604
		},
605
		
606
		/**
607
		 * Find a token in an AST with copious whitespace
608
		 * @see https://bugs.eclipse.org/bugs/show_bug.cgi?id=427931
609
		 */
610
		test_findToken18: function() {
611
			var text = "var foo = {  f: function() {}}";
612
			return astManager.getAST(setUp(text)).then(function(ast) {
613
				try {
614
					var token = Finder.findToken(12, ast.tokens);
615
					Assert.equal(null, token, 'Should not have found a token');
616
				}
617
				finally {
618
					astManager.updated();
619
				}
620
			});
621
		},
622
		
623
		/**
624
		 * Find a token in an AST with copious whitespace
625
		 * @see https://bugs.eclipse.org/bugs/show_bug.cgi?id=427931
626
		 */
627
		test_findToken19: function() {
628
			var text = "var foo = {f:   function() {}}";
629
			return astManager.getAST(setUp(text)).then(function(ast) {
630
				try {
631
					var token = Finder.findToken(14, ast.tokens);
632
					Assert.equal(null, token, 'Should not have found a token');
633
				}
634
				finally {
635
					astManager.updated();
636
				}
637
			});
638
		},
639
		
640
		/**
641
		 * Find a token in an AST with copious whitespace
642
		 * @see https://bugs.eclipse.org/bugs/show_bug.cgi?id=427931
643
		 */
644
		test_findToken20: function() {
645
			var text = "var foo = {f:   function() {}}";
646
			return astManager.getAST(setUp(text)).then(function(ast) {
647
				try {
648
					var token = Finder.findToken(15, ast.tokens);
649
					Assert.equal(null, token, 'Should not have found a token');
650
				}
651
				finally {
652
					astManager.updated();
653
				}
654
			});
655
		},
656
		
657
		/**
658
		 * Find a token in an AST with copious whitespace
659
		 * @see https://bugs.eclipse.org/bugs/show_bug.cgi?id=427931
660
		 */
661
		test_findToken21: function() {
662
			var text = "  var foo = {f: function() {}}";
663
			return astManager.getAST(setUp(text)).then(function(ast) {
664
				try {
665
					var token = Finder.findToken(1, ast.tokens);
666
					Assert.equal(null, token, 'Should not have found a token');
667
				}
668
				finally {
669
					astManager.updated();
670
				}
671
			});
672
		},
673
		
674
		/**
675
		 * Find a token in an AST with copious whitespace
676
		 * @see https://bugs.eclipse.org/bugs/show_bug.cgi?id=427931
677
		 */
678
		test_findToken22: function() {
679
			var text = "  var foo = {f:   function() {}}";
680
			return astManager.getAST(setUp(text)).then(function(ast) {
681
				try {
682
					var token = Finder.findToken(0, ast.tokens);
683
					Assert.equal(null, token, 'Should not have found a token');
684
				}
685
				finally {
686
					astManager.updated();
687
				}
688
			});
689
		},
690
		
691
		/**
692
		 * Find a token in an AST with copious whitespace
693
		 * @see https://bugs.eclipse.org/bugs/show_bug.cgi?id=427931
694
		 */
695
		test_findToken23: function() {
696
			var text = "function f3(  foo   ,   bar   , baz) {};";
697
			return astManager.getAST(setUp(text)).then(function(ast) {
698
				try {
699
					var token = Finder.findToken(17, ast.tokens);
700
					if(!token) {
701
						Assert.fail('Should have found a token');
702
					}
703
					else {
704
						Assert.equal(token.type, 'Identifier', 'Should have found an Identifier token');
705
						Assert.equal(token.value, 'foo', 'Should have found a foo token');
706
					}
707
				}
708
				finally {
709
					astManager.updated();
710
				}
711
			});
712
		},
713
		
714
		/**
573
		 * Find a comment
715
		 * Find a comment
574
		 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=426033
716
		 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=426033
575
		 */
717
		 */

Return to bug 427931