diff --git a/.changeset/dry-queens-return.md b/.changeset/dry-queens-return.md new file mode 100644 index 00000000000..2e5ac52b735 --- /dev/null +++ b/.changeset/dry-queens-return.md @@ -0,0 +1,5 @@ +--- +'graphql-language-service': patch +--- + +Fix off-by-one when hovering over token diff --git a/packages/graphql-language-service/src/interface/__tests__/getHoverInformation.test.ts b/packages/graphql-language-service/src/interface/__tests__/getHoverInformation.test.ts index d676f96e4d5..267e6ad8795 100644 --- a/packages/graphql-language-service/src/interface/__tests__/getHoverInformation.test.ts +++ b/packages/graphql-language-service/src/interface/__tests__/getHoverInformation.test.ts @@ -30,14 +30,35 @@ describe('getHoverInformation', () => { return getHoverInformation(schema, query, point); } - it('provides leaf field information', () => { - const actual = testHover( - 'query { thing { testField } }', - new Position(0, 20), - ); - expect(actual).toEqual( - 'TestType.testField: String\n\nThis is field documentation for TestType.testField', - ); + describe('provides leaf field information', () => { + it('when in middle of token', () => { + const actual = testHover( + 'query { thing { testField } }', + new Position(0, 20), + ); + expect(actual).toEqual( + 'TestType.testField: String\n\nThis is field documentation for TestType.testField', + ); + }); + + it('when at start of token', () => { + const actual = testHover( + 'query { thing { testField } }', + new Position(0, 16), + ); + expect(actual).toEqual( + 'TestType.testField: String\n\nThis is field documentation for TestType.testField', + ); + }); + it('when at end of token', () => { + const actual = testHover( + 'query { thing { testField } }', + new Position(0, 24), + ); + expect(actual).toEqual( + 'TestType.testField: String\n\nThis is field documentation for TestType.testField', + ); + }); }); it('provides aliased field information', () => { diff --git a/packages/graphql-language-service/src/interface/getAutocompleteSuggestions.ts b/packages/graphql-language-service/src/interface/getAutocompleteSuggestions.ts index dfc055cba8b..68a1035c5fa 100644 --- a/packages/graphql-language-service/src/interface/getAutocompleteSuggestions.ts +++ b/packages/graphql-language-service/src/interface/getAutocompleteSuggestions.ts @@ -148,6 +148,7 @@ export function getAutocompleteSuggestions( schema, contextToken, options, + 1, ); if (!context) { return []; diff --git a/packages/graphql-language-service/src/parser/api.ts b/packages/graphql-language-service/src/parser/api.ts index f5fd4bf0675..8346f743ea6 100644 --- a/packages/graphql-language-service/src/parser/api.ts +++ b/packages/graphql-language-service/src/parser/api.ts @@ -176,6 +176,7 @@ export function getContextAtPosition( schema: GraphQLSchema, contextToken?: ContextToken, options?: { mode?: GraphQLDocumentMode; uri?: string }, + offset = 0, ): { token: ContextToken; state: State; @@ -183,7 +184,7 @@ export function getContextAtPosition( mode: GraphQLDocumentMode; } | null { const token: ContextToken = - contextToken || getTokenAtPosition(queryText, cursor, 1); + contextToken || getTokenAtPosition(queryText, cursor, offset); if (!token) { return null; }