Skip to content

Commit

Permalink
account for offsets in suggestions (#81)
Browse files Browse the repository at this point in the history
* account for offsets

* add test
  • Loading branch information
JoviDeCroock authored Jun 22, 2023
1 parent c1e9d11 commit a7ddbdf
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/new-pens-think.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@0no-co/graphqlsp': patch
---

Account for offsets in auto-complete as well
1 change: 1 addition & 0 deletions packages/example/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const PokemonsQuery = gql`
id
name
fleeRate
__typenam
}
}
Expand Down
11 changes: 11 additions & 0 deletions packages/graphqlsp/src/autoComplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@ export function getGraphQLCompletions(
filename,
info
);

const amountOfLines = resolvedSpans
.filter(
x =>
x.original.start < cursorPosition &&
x.original.start + x.original.length < cursorPosition
)
.reduce((acc, span) => acc + (span.lines - 1), 0);

foundToken.line = foundToken.line + amountOfLines;

const cursor = new Cursor(foundToken.line, foundToken.start);

const [suggestions, spreadSuggestions] = getSuggestionsInternal(
Expand Down
78 changes: 74 additions & 4 deletions test/e2e/combinations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ import path from 'node:path';
import fs from 'node:fs';
import url from 'node:url';
import ts from 'typescript/lib/tsserverlibrary';
import { waitForExpect } from './util';

const __dirname = path.dirname(url.fileURLToPath(import.meta.url));

const projectPath = path.resolve(__dirname, 'fixture-project');
describe('Fragments', () => {
describe('Fragment + operations', () => {
const outfileCombinations = path.join(projectPath, 'Combination.ts');

let server: TSServer;
Expand Down Expand Up @@ -85,11 +84,11 @@ describe('Fragments', () => {
"category": "error",
"code": 52001,
"end": {
"line": 16,
"line": 17,
"offset": 1,
},
"start": {
"line": 15,
"line": 16,
"offset": 7,
},
"text": "Cannot query field \\"__typenam\\" on type \\"Post\\".",
Expand Down Expand Up @@ -125,4 +124,75 @@ describe('Fragments', () => {
`Query.posts: [Post]\n\nList out all posts`
);
}, 30000);

it('gives suggestions with preceding fragments', async () => {
server.send({
seq: 10,
type: 'request',
command: 'completionInfo',
arguments: {
file: outfileCombinations,
line: 15,
offset: 7,
includeExternalModuleExports: true,
includeInsertTextCompletions: true,
triggerKind: 1,
},
});

await server.waitForResponse(
response =>
response.type === 'response' && response.command === 'completionInfo'
);

const res = server.responses
.reverse()
.find(
resp => resp.type === 'response' && resp.command === 'completionInfo'
);

expect(res).toBeDefined();
expect(typeof res?.body.entries).toEqual('object');
expect(res?.body.entries).toMatchInlineSnapshot(`
[
{
"kind": "var",
"kindModifiers": "declare",
"labelDetails": {
"detail": " ID!",
},
"name": "id",
"sortText": "0id",
},
{
"kind": "var",
"kindModifiers": "declare",
"labelDetails": {
"detail": " String!",
},
"name": "title",
"sortText": "1title",
},
{
"kind": "var",
"kindModifiers": "declare",
"labelDetails": {
"detail": " String!",
},
"name": "content",
"sortText": "2content",
},
{
"kind": "var",
"kindModifiers": "declare",
"labelDetails": {
"description": "The name of the current Object type at runtime.",
"detail": " String!",
},
"name": "__typename",
"sortText": "3__typename",
},
]
`);
}, 30000);
});
1 change: 1 addition & 0 deletions test/e2e/fixture-project/fixtures/Combination.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const query = gql`
query Po {
posts {
__typenam
...fields
}
Expand Down

0 comments on commit a7ddbdf

Please sign in to comment.