Skip to content

Commit

Permalink
Fix language service cache invalidation
Browse files Browse the repository at this point in the history
  • Loading branch information
wkillerud committed Nov 25, 2024
1 parent ff0b0ab commit 86d8ab5
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
5 changes: 2 additions & 3 deletions pkgs/sass_language_server/lib/src/language_server.dart
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,8 @@ class LanguageServer {
connection: _connection,
onDidChangeContent: (params) async {
try {
// Reparse the stylesheet to update the cache with the new
// version of the document.
_ls.parseStylesheet(params.document);
// Update the cache with the new version of the document.
_ls.cache.onDocumentChanged(params.document);
if (initialScan != null) {
await initialScan;
}
Expand Down
29 changes: 29 additions & 0 deletions pkgs/sass_language_services/lib/src/language_services_cache.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,35 @@ class LanguageServicesCache {
return stylesheet;
}

sass.Stylesheet onDocumentChanged(TextDocument document) {
// We need this non-version checking method because of
// the rename feature. With that feature the client can
// send us "the first version" of a TextDocument after
// a rename, except we already have our own version 1
// from initial scan.

late final sass.Stylesheet stylesheet;
final languageId = document.languageId;
switch (languageId) {
case 'css':
stylesheet = sass.Stylesheet.parseCss(document.getText());
break;
case 'scss':
stylesheet = sass.Stylesheet.parseScss(document.getText());
break;
case 'sass':
stylesheet = sass.Stylesheet.parseSass(document.getText());
break;
default:
throw 'Unsupported language ID $languageId';
}

final key = document.uri.toString();
_cache[key] = CacheEntry(document: document, stylesheet: stylesheet);

return stylesheet;
}

TextDocument? getDocument(Uri uri) {
return _cache[uri.toString()]?.document;
}
Expand Down

0 comments on commit 86d8ab5

Please sign in to comment.