Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/dry-queens-return.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'graphql-language-service': patch
---

Fix off-by-one when hovering over token
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ export function getAutocompleteSuggestions(
schema,
contextToken,
options,
1,
);
if (!context) {
return [];
Expand Down
3 changes: 2 additions & 1 deletion packages/graphql-language-service/src/parser/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,14 +176,15 @@ export function getContextAtPosition(
schema: GraphQLSchema,
contextToken?: ContextToken,
options?: { mode?: GraphQLDocumentMode; uri?: string },
offset = 0,
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to explain this change: getContextAtPosition is used in 2 places:

In getHoverInformation, the offset needs to be changed to 0 to fix the off-by-one problem on hover.

const context = getContextAtPosition(queryText, cursor, schema, contextToken);

In getAutocompleteSuggestions, the offset needs to be kept at 1 as the current behavior is correct.

const context = getContextAtPosition(
queryText,
cursor,
schema,
contextToken,
options,
1,
);

): {
token: ContextToken;
state: State;
typeInfo: ReturnType<typeof getTypeInfo>;
mode: GraphQLDocumentMode;
} | null {
const token: ContextToken =
contextToken || getTokenAtPosition(queryText, cursor, 1);
contextToken || getTokenAtPosition(queryText, cursor, offset);
if (!token) {
return null;
}
Expand Down
Loading