Skip to content

Commit 5127def

Browse files
committed
Tests for scope
And some bugfixes
1 parent fb4c94d commit 5127def

File tree

3 files changed

+76
-13
lines changed

3 files changed

+76
-13
lines changed

pkgs/sass_language_services/lib/src/features/go_to_definition/scope.dart

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class Scope {
1212

1313
void addChild(Scope scope) {
1414
children.add(scope);
15-
scope.setParent(scope);
15+
scope.setParent(this);
1616
}
1717

1818
void setParent(Scope scope) {
@@ -29,21 +29,17 @@ class Scope {
2929
}
3030

3131
Scope findInScope({required int offset, int length = 0}) {
32-
var limit = offset + length;
33-
var index = children.indexWhere((scope) => scope.offset > limit);
34-
if (index == 0) {
35-
return this;
36-
}
32+
var scopeAtOffset = children.firstWhere(
33+
(scope) =>
34+
scope.offset <= offset &&
35+
scope.offset + scope.length > offset + length,
36+
orElse: () => this);
3737

38-
var childScope = children.elementAt(index - 1);
39-
var childScopeHasOffset = childScope.offset <= offset &&
40-
childScope.offset + childScope.length >= offset + length;
41-
42-
if (childScopeHasOffset) {
43-
return childScope.findInScope(offset: offset, length: length);
38+
if (scopeAtOffset == this) {
39+
return this;
4440
}
4541

46-
return this;
42+
return scopeAtOffset.findInScope(offset: offset, length: length);
4743
}
4844

4945
void addSymbol(StylesheetDocumentSymbol symbol) {
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import 'package:sass_language_services/sass_language_services.dart';
2+
import 'package:test/test.dart';
3+
4+
import '../../memory_file_system.dart';
5+
import '../../test_client_capabilities.dart';
6+
7+
final fs = MemoryFileSystem();
8+
final ls = LanguageServices(fs: fs, clientCapabilities: getCapabilities());
9+
void main() {
10+
group('selectors', () {
11+
setUp(() {
12+
ls.cache.clear();
13+
});
14+
15+
16+
});
17+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import 'package:sass_language_services/sass_language_services.dart';
2+
import 'package:sass_language_services/src/features/go_to_definition/scope.dart';
3+
import 'package:test/test.dart';
4+
5+
import '../../memory_file_system.dart';
6+
import '../../test_client_capabilities.dart';
7+
8+
final fs = MemoryFileSystem();
9+
final ls = LanguageServices(fs: fs, clientCapabilities: getCapabilities());
10+
11+
void main() {
12+
group('scope', () {
13+
setUp(() {
14+
ls.cache.clear();
15+
});
16+
17+
test('relationship between scopes', () {
18+
var global = Scope(offset: 0, length: double.maxFinite.floor());
19+
var first = Scope(offset: 10, length: 5);
20+
var second = Scope(offset: 15, length: 5);
21+
22+
global.addChild(first);
23+
global.addChild(second);
24+
25+
expect(global.children, hasLength(2));
26+
27+
expect(first.parent, equals(global));
28+
expect(second.parent, equals(global));
29+
});
30+
31+
test('findScope', () {
32+
var global = Scope(offset: 0, length: double.maxFinite.floor());
33+
var first = Scope(offset: 10, length: 5);
34+
var second = Scope(offset: 15, length: 5);
35+
36+
global.addChild(first);
37+
global.addChild(second);
38+
39+
expect(global.findScope(offset: -1), isNull);
40+
41+
expect(global.findScope(offset: 0), equals(global));
42+
expect(global.findScope(offset: 20), equals(global));
43+
44+
expect(global.findScope(offset: 10), equals(first));
45+
expect(global.findScope(offset: 14), equals(first));
46+
expect(global.findScope(offset: 15), equals(second));
47+
expect(global.findScope(offset: 19), equals(second));
48+
});
49+
});
50+
}

0 commit comments

Comments
 (0)