Skip to content

Commit 86d8ab5

Browse files
committed
Fix language service cache invalidation
1 parent ff0b0ab commit 86d8ab5

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

pkgs/sass_language_server/lib/src/language_server.dart

+2-3
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,8 @@ class LanguageServer {
8585
connection: _connection,
8686
onDidChangeContent: (params) async {
8787
try {
88-
// Reparse the stylesheet to update the cache with the new
89-
// version of the document.
90-
_ls.parseStylesheet(params.document);
88+
// Update the cache with the new version of the document.
89+
_ls.cache.onDocumentChanged(params.document);
9190
if (initialScan != null) {
9291
await initialScan;
9392
}

pkgs/sass_language_services/lib/src/language_services_cache.dart

+29
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,35 @@ class LanguageServicesCache {
4646
return stylesheet;
4747
}
4848

49+
sass.Stylesheet onDocumentChanged(TextDocument document) {
50+
// We need this non-version checking method because of
51+
// the rename feature. With that feature the client can
52+
// send us "the first version" of a TextDocument after
53+
// a rename, except we already have our own version 1
54+
// from initial scan.
55+
56+
late final sass.Stylesheet stylesheet;
57+
final languageId = document.languageId;
58+
switch (languageId) {
59+
case 'css':
60+
stylesheet = sass.Stylesheet.parseCss(document.getText());
61+
break;
62+
case 'scss':
63+
stylesheet = sass.Stylesheet.parseScss(document.getText());
64+
break;
65+
case 'sass':
66+
stylesheet = sass.Stylesheet.parseSass(document.getText());
67+
break;
68+
default:
69+
throw 'Unsupported language ID $languageId';
70+
}
71+
72+
final key = document.uri.toString();
73+
_cache[key] = CacheEntry(document: document, stylesheet: stylesheet);
74+
75+
return stylesheet;
76+
}
77+
4978
TextDocument? getDocument(Uri uri) {
5079
return _cache[uri.toString()]?.document;
5180
}

0 commit comments

Comments
 (0)