|
| 1 | +import 'package:lsp_server/lsp_server.dart' as lsp; |
| 2 | +import 'package:sass_language_services/sass_language_services.dart'; |
| 3 | +import 'package:test/test.dart'; |
| 4 | + |
| 5 | +import '../../memory_file_system.dart'; |
| 6 | +import '../../position_utils.dart'; |
| 7 | +import '../../test_client_capabilities.dart'; |
| 8 | + |
| 9 | +final fs = MemoryFileSystem(); |
| 10 | +final ls = LanguageServices(fs: fs, clientCapabilities: getCapabilities()); |
| 11 | + |
| 12 | +void expectRanges(TextDocument document, lsp.SelectionRange ranges, |
| 13 | + List<(int, String)> expected) { |
| 14 | + var pairs = <(int, String)>[]; |
| 15 | + lsp.SelectionRange? current = ranges; |
| 16 | + while (current != null) { |
| 17 | + pairs.add(( |
| 18 | + document.offsetAt(current.range.start), |
| 19 | + document.getText(range: current.range), |
| 20 | + )); |
| 21 | + current = current.parent; |
| 22 | + } |
| 23 | + expect(pairs, equals(expected)); |
| 24 | +} |
| 25 | + |
| 26 | +void main() { |
| 27 | + group('selection ranges', () { |
| 28 | + setUp(() { |
| 29 | + ls.cache.clear(); |
| 30 | + }); |
| 31 | + |
| 32 | + test('style rules', () { |
| 33 | + var document = fs.createDocument('''.foo { |
| 34 | + color: red; |
| 35 | +
|
| 36 | + .bar { |
| 37 | + color: blue; |
| 38 | + } |
| 39 | +} |
| 40 | +'''); |
| 41 | + |
| 42 | + var result = ls.getSelectionRanges(document, [position(4, 5)]); |
| 43 | + expect(result, hasLength(1)); |
| 44 | + expectRanges(document, result.first, [ |
| 45 | + (0, ".foo {\n color: red;\n\n .bar {\n color: blue;\n }\n}\n"), |
| 46 | + (0, ".foo {\n color: red;\n\n .bar {\n color: blue;\n }\n}"), |
| 47 | + (24, ".bar {\n color: blue;\n }"), |
| 48 | + (35, "color: blue"), |
| 49 | + (35, "color") |
| 50 | + ]); |
| 51 | + }); |
| 52 | + |
| 53 | + test('mixin rules', () { |
| 54 | + var document = fs.createDocument('''@mixin foo { |
| 55 | + color: red; |
| 56 | +
|
| 57 | + .bar { |
| 58 | + color: blue; |
| 59 | + } |
| 60 | +} |
| 61 | +'''); |
| 62 | + |
| 63 | + var result = ls.getSelectionRanges(document, [position(4, 5)]); |
| 64 | + expect(result, hasLength(1)); |
| 65 | + expectRanges(document, result.first, [ |
| 66 | + ( |
| 67 | + 0, |
| 68 | + "@mixin foo {\n color: red;\n\n .bar {\n color: blue;\n }\n}\n" |
| 69 | + ), |
| 70 | + ( |
| 71 | + 0, |
| 72 | + "@mixin foo {\n color: red;\n\n .bar {\n color: blue;\n }\n}" |
| 73 | + ), |
| 74 | + (30, ".bar {\n color: blue;\n }"), |
| 75 | + (41, "color: blue"), |
| 76 | + (41, "color") |
| 77 | + ]); |
| 78 | + }); |
| 79 | + }); |
| 80 | +} |
0 commit comments