From 798d782d346d064db6b37c48e276ed63e064b8a0 Mon Sep 17 00:00:00 2001 From: Rikki Schulte Date: Sun, 19 May 2024 09:54:52 +0200 Subject: [PATCH] coverage for locateCommand --- .../src/MessageProcessor.ts | 76 +++++++++--------- .../src/__tests__/MessageProcessor.test.ts | 77 +++++++++++++++++++ 2 files changed, 117 insertions(+), 36 deletions(-) diff --git a/packages/graphql-language-service-server/src/MessageProcessor.ts b/packages/graphql-language-service-server/src/MessageProcessor.ts index 5a61fdc091..0a25f0a62f 100644 --- a/packages/graphql-language-service-server/src/MessageProcessor.ts +++ b/packages/graphql-language-service-server/src/MessageProcessor.ts @@ -895,42 +895,13 @@ export class MessageProcessor { } } if (locateCommand && result && result?.printedName) { - try { - const locateResult = locateCommand( - project.name, - result.printedName, - { - node: result.node, - type: result.type, - project, - }, - ); - if (typeof locateResult === 'string') { - const [uri, startLine = '1', endLine = '1'] = - locateResult.split(':'); - return { - uri, - range: new Range( - new Position(parseInt(startLine, 10), 0), - new Position(parseInt(endLine, 10), 0), - ), - }; - } - return ( - locateResult || { - uri: res.path, - range: defRange, - } - ); - } catch (error) { - this._logger.error( - 'There was an error executing user defined locateCommand\n\n' + - (error as Error).toString(), - ); - return { - uri: res.path, - range: defRange, - }; + const locateResult = this._getCustomLocateResult( + project, + result, + locateCommand, + ); + if (locateResult) { + return locateResult; } } return { @@ -950,6 +921,39 @@ export class MessageProcessor { ); return formatted; } + _getCustomLocateResult( + project: GraphQLProjectConfig, + result: DefinitionQueryResponse, + locateCommand: LocateCommand, + ) { + if (!result.printedName) { + return null; + } + try { + const locateResult = locateCommand(project.name, result.printedName, { + node: result.node, + type: result.type, + project, + }); + if (typeof locateResult === 'string') { + const [uri, startLine = '1', endLine = '1'] = locateResult.split(':'); + return { + uri, + range: new Range( + new Position(parseInt(startLine, 10), 0), + new Position(parseInt(endLine, 10), 0), + ), + }; + } + return locateResult; + } catch (error) { + this._logger.error( + 'There was an error executing user defined locateCommand\n\n' + + (error as Error).toString(), + ); + return null; + } + } public async handleDocumentSymbolRequest( params: DocumentSymbolParams, diff --git a/packages/graphql-language-service-server/src/__tests__/MessageProcessor.test.ts b/packages/graphql-language-service-server/src/__tests__/MessageProcessor.test.ts index b22af3ff77..a10e674b20 100644 --- a/packages/graphql-language-service-server/src/__tests__/MessageProcessor.test.ts +++ b/packages/graphql-language-service-server/src/__tests__/MessageProcessor.test.ts @@ -410,6 +410,83 @@ describe('MessageProcessor', () => { const result = await messageProcessor.handleDefinitionRequest(test); await expect(result[0].uri).toEqual(`${queryPathUri}/test3.graphql`); }); + + it('retrieves custom results from locateCommand', async () => { + jest.setTimeout(10000); + const validQuery = ` + { + hero(episode: EMPIRE){ + ...testFragment + } + } + `; + + const newDocument = { + textDocument: { + text: validQuery, + uri: `${queryPathUri}/test3.graphql`, + version: 1, + }, + }; + messageProcessor._getCachedDocument = (_uri: string) => ({ + version: 1, + contents: [ + { + query: validQuery, + range: new Range(new Position(0, 0), new Position(20, 4)), + }, + ], + }); + + await messageProcessor.handleDidOpenOrSaveNotification(newDocument); + + const test = { + position: new Position(3, 15), + textDocument: newDocument.textDocument, + }; + const result = await messageProcessor._languageService.getDefinition( + validQuery, + test.position, + test.textDocument.uri, + ); + const project = messageProcessor._graphQLCache.getProjectForFile( + test.textDocument.uri, + )!; + + const customResult = messageProcessor._getCustomLocateResult( + project, + { definitions: result, printedName: 'example' }, + () => 'hello', + ); + expect(customResult.uri).toEqual(`hello`); + + const customResult2 = messageProcessor._getCustomLocateResult( + project, + { definitions: result, printedName: 'example' }, + () => 'hello:2:4', + ); + expect(customResult2.uri).toEqual(`hello`); + expect(customResult2.range.start.line).toEqual(2); + expect(customResult2.range.start.character).toEqual(0); + expect(customResult2.range.end.line).toEqual(4); + + const customResult3 = messageProcessor._getCustomLocateResult( + project, + { definitions: result, printedName: 'example' }, + () => ({ + uri: 'hello1', + range: { + start: { character: 2, line: 2 }, + end: { character: 4, line: 4 }, + }, + }), + ); + expect(customResult3.uri).toEqual(`hello1`); + expect(customResult3.range.start.line).toEqual(2); + expect(customResult3.range.start.character).toEqual(2); + expect(customResult3.range.end.line).toEqual(4); + expect(customResult3.range.end.character).toEqual(4); + }); it('runs hover requests', async () => { const validQuery = ` {