Skip to content

Commit f47b9b0

Browse files
forivallacao
authored andcommitted
fix(language-service): improve largeFileThreshold behaviour
1 parent 0702c45 commit f47b9b0

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) {
@@ -1260,7 +1266,10 @@ export class MessageProcessor {
12601266

12611267
await this._updateObjectTypeDefinition(uri, contents);
12621268
await this._updateFragmentDefinition(uri, contents);
1263-
await this._invalidateCache({ version: 1, uri }, uri, contents);
1269+
await this._invalidateCache(
1270+
{ version: 1, uri },
1271+
{ contents, size: document.rawSDL.length },
1272+
);
12641273
}),
12651274
);
12661275
} catch (err) {
@@ -1369,27 +1378,20 @@ export class MessageProcessor {
13691378
}
13701379
private async _invalidateCache(
13711380
textDocument: VersionedTextDocumentIdentifier,
1372-
uri: Uri,
1373-
contents: CachedContent[],
1381+
meta: Omit<CachedDocumentType, 'version'>,
13741382
): Promise<Map<string, CachedDocumentType> | null> {
1383+
const { uri, version } = textDocument;
13751384
if (this._textDocumentCache.has(uri)) {
13761385
const cachedDocument = this._textDocumentCache.get(uri);
1377-
if (
1378-
cachedDocument &&
1379-
textDocument?.version &&
1380-
cachedDocument.version < textDocument.version
1381-
) {
1386+
if (cachedDocument && version && cachedDocument.version < version) {
13821387
// Current server capabilities specify the full sync of the contents.
13831388
// Therefore always overwrite the entire content.
1384-
return this._textDocumentCache.set(uri, {
1385-
version: textDocument.version,
1386-
contents,
1387-
});
1389+
return this._textDocumentCache.set(uri, { ...meta, version });
13881390
}
13891391
}
13901392
return this._textDocumentCache.set(uri, {
1391-
version: textDocument.version ?? 0,
1392-
contents,
1393+
...meta,
1394+
version: version ?? 0,
13931395
});
13941396
}
13951397
}

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)