Skip to content

Commit

Permalink
fix: a race condition that caused partials not to be parsed sometimes
Browse files Browse the repository at this point in the history
  • Loading branch information
wkillerud committed Aug 20, 2024
1 parent 5cc973d commit 6d23443
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 16 deletions.
22 changes: 9 additions & 13 deletions packages/language-server/src/workspace-scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,17 @@ export default class WorkspaceScanner {
uri = URI.parse(file.toString().replace("/static/extensions/fs", ""));
}

const alreadyParsed = this.#ls.hasCached(uri);
if (alreadyParsed) {
// The same file may be referenced by multiple other files,
// so skip doing the parsing work if it's already been done.
// Changes to the file are handled by the `update` method.
return;
}

try {
const content = await this.#fs.readFile(uri);
let document: TextDocument | null | undefined =
this.#ls.getCachedTextDocument(uri);
if (!document) {
const content = await this.#fs.readFile(uri);

const document = getSCSSRegionsDocument(
TextDocument.create(uri.toString(), "scss", 1, content),
);
if (!document) return;
document = getSCSSRegionsDocument(
TextDocument.create(uri.toString(), "scss", 1, content),
);
if (!document) return;
}

this.#ls.parseStylesheet(document);

Expand Down
4 changes: 4 additions & 0 deletions packages/language-services/src/language-model-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ export class LanguageModelCache {
return Object.values(this.#languageModels).map((cached) => cached.document);
}

getCachedTextDocument(uri: string): TextDocument | undefined {
return this.#languageModels[uri]?.document;
}

has(uri: string) {
return typeof this.#languageModels[uri] !== "undefined";
}
Expand Down
2 changes: 1 addition & 1 deletion packages/language-services/src/language-services-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export interface LanguageService {
range: Range,
context?: CodeActionContext,
): Promise<CodeAction[]>;
hasCached(uri: URI): boolean;
getCachedTextDocument(uri: URI): TextDocument | undefined;
/**
* Utility function to reparse an updated document.
* Like {@link LanguageService.parseStylesheet}, but returns nothing.
Expand Down
4 changes: 2 additions & 2 deletions packages/language-services/src/language-services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@ class LanguageServiceImpl implements LanguageService {
return this.#findSymbols.findWorkspaceSymbols(query);
}

hasCached(uri: URI): boolean {
return this.#cache.has(uri.toString());
getCachedTextDocument(uri: URI): TextDocument | undefined {
return this.#cache.getCachedTextDocument(uri.toString());
}

getColorPresentations(document: TextDocument, color: Color, range: Range) {
Expand Down

0 comments on commit 6d23443

Please sign in to comment.