From 86d8ab541a3d7f9e5b726ef3f7a9bca19aa23e34 Mon Sep 17 00:00:00 2001 From: William Killerud Date: Mon, 25 Nov 2024 20:49:32 +0100 Subject: [PATCH] Fix language service cache invalidation --- .../lib/src/language_server.dart | 5 ++-- .../lib/src/language_services_cache.dart | 29 +++++++++++++++++++ 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/pkgs/sass_language_server/lib/src/language_server.dart b/pkgs/sass_language_server/lib/src/language_server.dart index 8b94fd2..11171b4 100644 --- a/pkgs/sass_language_server/lib/src/language_server.dart +++ b/pkgs/sass_language_server/lib/src/language_server.dart @@ -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; } diff --git a/pkgs/sass_language_services/lib/src/language_services_cache.dart b/pkgs/sass_language_services/lib/src/language_services_cache.dart index 8b0d7aa..4aea22f 100644 --- a/pkgs/sass_language_services/lib/src/language_services_cache.dart +++ b/pkgs/sass_language_services/lib/src/language_services_cache.dart @@ -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; }