-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
237 additions
and
0 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
extension/test/electron/definition/selection-ranges.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
const assert = require('node:assert'); | ||
const path = require('node:path'); | ||
const vscode = require('vscode'); | ||
const { showFile, sleepCI } = require('../util'); | ||
|
||
const stylesUri = vscode.Uri.file( | ||
path.resolve(__dirname, 'fixtures', 'styles.scss') | ||
); | ||
|
||
before(async () => { | ||
await showFile(stylesUri); | ||
await sleepCI(); | ||
}); | ||
|
||
after(async () => { | ||
await vscode.commands.executeCommand('workbench.action.closeAllEditors'); | ||
}); | ||
|
||
/** | ||
* @param {import('vscode').Uri} documentUri | ||
* @param {Array<import('vscode').Position>} positions | ||
* @returns {Promise<Array<import('vscode').SelectionRange>>} | ||
*/ | ||
async function getSelectionRanges(documentUri, positions) { | ||
const result = await vscode.commands.executeCommand( | ||
'vscode.executeSelectionRangeProvider', | ||
documentUri, | ||
positions | ||
); | ||
return result; | ||
} | ||
|
||
test('gets document selection ranges', async () => { | ||
const [result] = await getSelectionRanges(stylesUri, [ | ||
new vscode.Position(7, 5), | ||
]); | ||
|
||
assert.ok(result, 'Should have gotten selection ranges'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
pkgs/sass_language_services/lib/src/features/selection_ranges/selection_ranges_feature.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import 'package:lsp_server/lsp_server.dart' as lsp; | ||
import 'package:sass_language_services/sass_language_services.dart'; | ||
import 'package:sass_language_services/src/features/selection_ranges/selection_ranges_visitor.dart'; | ||
import 'package:sass_language_services/src/utils/sass_lsp_utils.dart'; | ||
|
||
import '../go_to_definition/scope_visitor.dart'; | ||
import '../go_to_definition/scoped_symbols.dart'; | ||
import '../language_feature.dart'; | ||
|
||
class SelectionRangesFeature extends LanguageFeature { | ||
SelectionRangesFeature({required super.ls}); | ||
|
||
List<lsp.SelectionRange> getSelectionRanges( | ||
TextDocument document, List<lsp.Position> positions) { | ||
var stylesheet = ls.parseStylesheet(document); | ||
var symbols = ls.cache.getDocumentSymbols(document) ?? | ||
ScopedSymbols( | ||
stylesheet, | ||
document.languageId == 'sass' ? Dialect.indented : Dialect.scss, | ||
); | ||
ls.cache.setDocumentSymbols(document, symbols); | ||
|
||
var result = <lsp.SelectionRange>[]; | ||
|
||
for (var position in positions) { | ||
var visitor = SelectionRangesVisitor( | ||
document.offsetAt(position), | ||
); | ||
stylesheet.accept(visitor); | ||
|
||
var ranges = visitor.ranges; | ||
lsp.SelectionRange? current; | ||
for (var i = ranges.length - 1; i >= 0; i--) { | ||
var range = ranges[i]; | ||
|
||
// Avoid duplicates | ||
if (current != null && isSameRange(current.range, range.range)) { | ||
continue; | ||
} | ||
|
||
current = lsp.SelectionRange( | ||
range: range.range, | ||
parent: current, | ||
); | ||
} | ||
if (current == null) { | ||
result.add( | ||
lsp.SelectionRange( | ||
range: lsp.Range(start: position, end: position), | ||
), | ||
); | ||
} | ||
result.add(current!); | ||
} | ||
|
||
return result; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
pkgs/sass_language_services/lib/src/features/selection_ranges/selection_ranges_visitor.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import 'package:lsp_server/lsp_server.dart' as lsp; | ||
import 'package:sass_api/sass_api.dart' as sass; | ||
|
||
import '../../utils/sass_lsp_utils.dart'; | ||
import '../node_at_offset_visitor.dart'; | ||
|
||
class SelectionRangesVisitor extends NodeAtOffsetVisitor { | ||
final ranges = <lsp.SelectionRange>[]; | ||
|
||
SelectionRangesVisitor(super._offset); | ||
|
||
@override | ||
void processCandidate(sass.AstNode node) { | ||
ranges.add(lsp.SelectionRange(range: toRange(node.span))); | ||
|
||
if (node is sass.Declaration) { | ||
ranges.add(lsp.SelectionRange(range: toRange(node.name.span))); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
pkgs/sass_language_services/test/features/selection_ranges/selection_ranges_test.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import 'package:lsp_server/lsp_server.dart' as lsp; | ||
import 'package:sass_language_services/sass_language_services.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
import '../../memory_file_system.dart'; | ||
import '../../position_utils.dart'; | ||
import '../../test_client_capabilities.dart'; | ||
|
||
final fs = MemoryFileSystem(); | ||
final ls = LanguageServices(fs: fs, clientCapabilities: getCapabilities()); | ||
|
||
void expectRanges(TextDocument document, lsp.SelectionRange ranges, | ||
List<(int, String)> expected) { | ||
var pairs = <(int, String)>[]; | ||
lsp.SelectionRange? current = ranges; | ||
while (current != null) { | ||
pairs.add(( | ||
document.offsetAt(current.range.start), | ||
document.getText(range: current.range), | ||
)); | ||
current = current.parent; | ||
} | ||
expect(pairs, equals(expected)); | ||
} | ||
|
||
void main() { | ||
group('selection ranges', () { | ||
setUp(() { | ||
ls.cache.clear(); | ||
}); | ||
|
||
test('style rules', () { | ||
var document = fs.createDocument('''.foo { | ||
color: red; | ||
.bar { | ||
color: blue; | ||
} | ||
} | ||
'''); | ||
|
||
var result = ls.getSelectionRanges(document, [position(4, 5)]); | ||
expect(result, hasLength(1)); | ||
expectRanges(document, result.first, [ | ||
(0, ".foo {\n color: red;\n\n .bar {\n color: blue;\n }\n}\n"), | ||
(0, ".foo {\n color: red;\n\n .bar {\n color: blue;\n }\n}"), | ||
(24, ".bar {\n color: blue;\n }"), | ||
(35, "color: blue"), | ||
(35, "color") | ||
]); | ||
}); | ||
|
||
test('mixin rules', () { | ||
var document = fs.createDocument('''@mixin foo { | ||
color: red; | ||
.bar { | ||
color: blue; | ||
} | ||
} | ||
'''); | ||
|
||
var result = ls.getSelectionRanges(document, [position(4, 5)]); | ||
expect(result, hasLength(1)); | ||
expectRanges(document, result.first, [ | ||
( | ||
0, | ||
"@mixin foo {\n color: red;\n\n .bar {\n color: blue;\n }\n}\n" | ||
), | ||
( | ||
0, | ||
"@mixin foo {\n color: red;\n\n .bar {\n color: blue;\n }\n}" | ||
), | ||
(30, ".bar {\n color: blue;\n }"), | ||
(41, "color: blue"), | ||
(41, "color") | ||
]); | ||
}); | ||
}); | ||
} |