-
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
7 changed files
with
240 additions
and
3 deletions.
There are no files selected for viewing
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').FoldingRange>>} | ||
*/ | ||
async function getFoldingRanges(documentUri, positions) { | ||
const result = await vscode.commands.executeCommand( | ||
'vscode.executeFoldingRangeProvider', | ||
documentUri, | ||
positions | ||
); | ||
return result; | ||
} | ||
|
||
test('gets document folding ranges', async () => { | ||
const [result] = await getFoldingRanges(stylesUri, [ | ||
new vscode.Position(7, 5), | ||
]); | ||
|
||
assert.ok(result, 'Should have gotten folding 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
47 changes: 47 additions & 0 deletions
47
pkgs/sass_language_services/lib/src/features/folding_ranges/folding_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,47 @@ | ||
import 'package:lsp_server/lsp_server.dart' as lsp; | ||
import 'package:sass_language_services/sass_language_services.dart'; | ||
|
||
import '../go_to_definition/scope.dart'; | ||
import '../go_to_definition/scope_visitor.dart'; | ||
import '../go_to_definition/scoped_symbols.dart'; | ||
import '../language_feature.dart'; | ||
|
||
class FoldingRangesFeature extends LanguageFeature { | ||
FoldingRangesFeature({required super.ls}); | ||
|
||
List<lsp.FoldingRange> getFoldingRanges(TextDocument document) { | ||
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.FoldingRange>[]; | ||
// Omit the global scope. | ||
for (var childScope in symbols.globalScope.children) { | ||
result.addAll(_toFoldingRanges(document, childScope)); | ||
} | ||
return result; | ||
} | ||
|
||
List<lsp.FoldingRange> _toFoldingRanges(TextDocument document, Scope scope) { | ||
var result = <lsp.FoldingRange>[]; | ||
result.add(_toFoldingRange(document, scope)); | ||
if (scope.children.isEmpty) { | ||
return result; | ||
} | ||
for (var childScope in scope.children) { | ||
result.addAll(_toFoldingRanges(document, childScope)); | ||
} | ||
return result; | ||
} | ||
|
||
lsp.FoldingRange _toFoldingRange(TextDocument document, Scope scope) { | ||
var startLine = document.positionAt(scope.offset).line; | ||
var endLine = document.positionAt(scope.offset + scope.length).line; | ||
return lsp.FoldingRange(startLine: startLine, endLine: endLine); | ||
} | ||
} |
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
83 changes: 83 additions & 0 deletions
83
pkgs/sass_language_services/test/features/folding_ranges/folding_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,83 @@ | ||
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 '../../test_client_capabilities.dart'; | ||
|
||
final fs = MemoryFileSystem(); | ||
final ls = LanguageServices(fs: fs, clientCapabilities: getCapabilities()); | ||
|
||
lsp.FoldingRange fr(int startLine, int endLine) { | ||
return lsp.FoldingRange( | ||
startLine: startLine, | ||
endLine: endLine, | ||
); | ||
} | ||
|
||
void main() { | ||
group('folding ranges', () { | ||
setUp(() { | ||
ls.cache.clear(); | ||
}); | ||
|
||
test('style rules', () { | ||
var document = fs.createDocument(''' | ||
.foo { | ||
color: red; | ||
.bar { | ||
color: blue; | ||
} | ||
} | ||
'''); | ||
|
||
var result = ls.getFoldingRanges(document); | ||
expect(result, hasLength(2)); | ||
expect( | ||
result, | ||
equals([ | ||
fr(0, 6), | ||
fr(3, 5), | ||
]), | ||
); | ||
}); | ||
|
||
test('mixin rules', () { | ||
var document = fs.createDocument('''@mixin foo { | ||
color: red; | ||
.bar { | ||
color: blue; | ||
} | ||
} | ||
'''); | ||
|
||
var result = ls.getFoldingRanges(document); | ||
expect(result, hasLength(2)); | ||
expect( | ||
result, | ||
equals([ | ||
fr(0, 6), | ||
fr(3, 5), | ||
]), | ||
); | ||
}); | ||
|
||
test('include rules', () { | ||
var document = fs.createDocument('''@include foo { | ||
--color-foo: red; | ||
} | ||
'''); | ||
|
||
var result = ls.getFoldingRanges(document); | ||
expect(result, hasLength(1)); | ||
expect( | ||
result, | ||
equals([ | ||
fr(0, 2), | ||
]), | ||
); | ||
}); | ||
}); | ||
} |