Skip to content

Commit 0ddeb1b

Browse files
committed
fix(language-service): improve largeFileThreshold behaviour
1 parent 8a9fa96 commit 0ddeb1b

File tree

2 files changed

+30
-26
lines changed

2 files changed

+30
-26
lines changed

packages/graphql-language-service-server/src/MessageProcessor.ts

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ const configDocLink =
8888
type CachedDocumentType = {
8989
version: number;
9090
contents: CachedContent[];
91+
size: number;
9192
};
9293

9394
function toPosition(position: VscodePosition): IPosition {
@@ -498,17 +499,11 @@ export class MessageProcessor {
498499

499500
// As `contentChanges` is an array, and we just want the
500501
// latest update to the text, grab the last entry from the array.
501-
502-
// If it's a .js file, try parsing the contents to see if GraphQL queries
503-
// exist. If not found, delete from the cache.
504502
const { contents } = await this._parseAndCacheFile(
505503
uri,
506504
project,
507505
contentChanges.at(-1)!.text,
508506
);
509-
// // If it's a .graphql file, proceed normally and invalidate the cache.
510-
// await this._invalidateCache(textDocument, uri, contents);
511-
512507
const diagnostics: Diagnostic[] = [];
513508

514509
if (project?.extensions?.languageService?.enableValidation !== false) {
@@ -718,7 +713,10 @@ export class MessageProcessor {
718713
const contents = await this._parser(fileText, uri);
719714
const cachedDocument = this._textDocumentCache.get(uri);
720715
const version = cachedDocument ? cachedDocument.version++ : 0;
721-
await this._invalidateCache({ uri, version }, uri, contents);
716+
await this._invalidateCache(
717+
{ uri, version },
718+
{ contents, size: fileText.length },
719+
);
722720
await this._updateFragmentDefinition(uri, contents);
723721
await this._updateObjectTypeDefinition(uri, contents, project);
724722
await this._updateSchemaIfChanged(project, uri);
@@ -954,14 +952,13 @@ export class MessageProcessor {
954952

955953
const { textDocument } = params;
956954
const cachedDocument = this._getCachedDocument(textDocument.uri);
957-
if (!cachedDocument?.contents[0]) {
955+
if (!cachedDocument?.contents?.length) {
958956
return [];
959957
}
960958

961959
if (
962960
this._settings.largeFileThreshold !== undefined &&
963-
this._settings.largeFileThreshold <
964-
cachedDocument.contents[0].query.length
961+
this._settings.largeFileThreshold < cachedDocument.size
965962
) {
966963
return [];
967964
}
@@ -1015,7 +1012,13 @@ export class MessageProcessor {
10151012
documents.map(async ([uri]) => {
10161013
const cachedDocument = this._getCachedDocument(uri);
10171014

1018-
if (!cachedDocument) {
1015+
if (!cachedDocument?.contents?.length) {
1016+
return [];
1017+
}
1018+
if (
1019+
this._settings.largeFileThreshold !== undefined &&
1020+
this._settings.largeFileThreshold < cachedDocument.size
1021+
) {
10191022
return [];
10201023
}
10211024
const docSymbols = await this._languageService.getDocumentSymbols(
@@ -1044,7 +1047,10 @@ export class MessageProcessor {
10441047
try {
10451048
const contents = await this._parser(text, uri);
10461049
if (contents.length > 0) {
1047-
await this._invalidateCache({ version, uri }, uri, contents);
1050+
await this._invalidateCache(
1051+
{ version, uri },
1052+
{ contents, size: text.length },
1053+
);
10481054
await this._updateObjectTypeDefinition(uri, contents, project);
10491055
}
10501056
} catch (err) {
@@ -1256,7 +1262,10 @@ export class MessageProcessor {
12561262
}
12571263
await this._updateObjectTypeDefinition(uri, contents);
12581264
await this._updateFragmentDefinition(uri, contents);
1259-
await this._invalidateCache({ version: 1, uri }, uri, contents);
1265+
await this._invalidateCache(
1266+
{ version: 1, uri },
1267+
{ contents, size: document.rawSDL.length },
1268+
);
12601269
}),
12611270
);
12621271
} catch (err) {
@@ -1365,27 +1374,20 @@ export class MessageProcessor {
13651374
}
13661375
private async _invalidateCache(
13671376
textDocument: VersionedTextDocumentIdentifier,
1368-
uri: Uri,
1369-
contents: CachedContent[],
1377+
meta: Omit<CachedDocumentType, 'version'>,
13701378
): Promise<Map<string, CachedDocumentType> | null> {
1379+
const { uri, version } = textDocument;
13711380
if (this._textDocumentCache.has(uri)) {
13721381
const cachedDocument = this._textDocumentCache.get(uri);
1373-
if (
1374-
cachedDocument &&
1375-
textDocument?.version &&
1376-
cachedDocument.version < textDocument.version
1377-
) {
1382+
if (cachedDocument && version && cachedDocument.version < version) {
13781383
// Current server capabilities specify the full sync of the contents.
13791384
// Therefore always overwrite the entire content.
1380-
return this._textDocumentCache.set(uri, {
1381-
version: textDocument.version,
1382-
contents,
1383-
});
1385+
return this._textDocumentCache.set(uri, { ...meta, version });
13841386
}
13851387
}
13861388
return this._textDocumentCache.set(uri, {
1387-
version: textDocument.version ?? 0,
1388-
contents,
1389+
...meta,
1390+
version: version ?? 0,
13891391
});
13901392
}
13911393
}

packages/graphql-language-service-server/src/parseDocument.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,12 @@ export async function parseDocument(
3434
return [];
3535
}
3636

37+
// If it's a .js file, parse the contents to see if GraphQL queries exist.
3738
if (fileExtensions.includes(ext)) {
3839
const templates = await findGraphQLTags(text, ext, uri, logger);
3940
return templates.map(({ template, range }) => ({ query: template, range }));
4041
}
42+
// If it's a .graphql file, use the entire file
4143
if (graphQLFileExtensions.includes(ext)) {
4244
const query = text;
4345
const lines = query.split('\n');

0 commit comments

Comments
 (0)