fix(language-server): bypass cache for version-less documents
This commit is contained in:
@@ -100,12 +100,16 @@ export function isInsideHtml(document: TextDocument, offset: number): boolean {
|
||||
const regionCache = new Map<string, { version: number; regions: HtmlRegion[] }>();
|
||||
|
||||
function regionsFor(document: TextDocument): HtmlRegion[] {
|
||||
const version = document.version ?? -1;
|
||||
// Documents without a version have no way to signal changes, so bypass cache.
|
||||
if (document.version === undefined) {
|
||||
return viewRegions(document.text);
|
||||
}
|
||||
|
||||
const cached = regionCache.get(document.uri);
|
||||
if (cached && cached.version === version) return cached.regions;
|
||||
if (cached && cached.version === document.version) return cached.regions;
|
||||
|
||||
const regions = viewRegions(document.text);
|
||||
regionCache.set(document.uri, { version, regions });
|
||||
regionCache.set(document.uri, { version: document.version, regions });
|
||||
return regions;
|
||||
}
|
||||
|
||||
|
||||
@@ -107,3 +107,19 @@ test("isInsideHtml distinguishes markup from surrounding code", () => {
|
||||
expect(isInsideHtml(source, markupOffset)).toBe(true);
|
||||
expect(isInsideHtml(source, keywordOffset)).toBe(false);
|
||||
});
|
||||
|
||||
test("documents without a version field scan every time and detect mutations", () => {
|
||||
// Without a version, the cache has no key to validate freshness. Mutations
|
||||
// must be detected on every call, even when uri and document are reused.
|
||||
const source = doc(PAGE);
|
||||
// Explicitly verify version is undefined (not set by doc() helper).
|
||||
expect(source.version).toBeUndefined();
|
||||
|
||||
const markupOffset = PAGE.indexOf("<div");
|
||||
expect(isInsideHtml(source, markupOffset)).toBe(true);
|
||||
|
||||
// Mutate the text: remove the view block.
|
||||
source.text = "page A { }";
|
||||
// Same uri, same missing version, but different text: mutation must be detected.
|
||||
expect(isInsideHtml(source, markupOffset)).toBe(false);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user