Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wip: feat: Add getDefinitionAtPosition
- Loading branch information
Showing
5 changed files
with
112 additions
and
1 deletion.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
src/graphql-language-service-adapter/get-definition-at-position.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import ts from 'typescript'; | ||
import { mark, type Frets } from 'fretted-strings'; | ||
import { AdapterFixture } from './testing/adapter-fixture'; | ||
import { getDefinitionAtPosition } from './get-definition-at-position'; | ||
|
||
function createFixture(name: string) { | ||
return new AdapterFixture(name); | ||
} | ||
|
||
describe(getDefinitionAtPosition, () => { | ||
const delegateFn = jest.fn(() => []); | ||
it('should works', () => { | ||
const fixture = createFixture('input.ts'); | ||
const frets: Frets = {}; | ||
fixture.source = mark( | ||
` | ||
const query = \` | ||
query MyQuery { | ||
...MyFragment | ||
%%% ^ %%% | ||
%%% cur %%% | ||
} | ||
fragment MyFragment on Query { | ||
%%% ^ ^ %%% | ||
%%% d1 d2 %%% | ||
__typename | ||
} | ||
\` | ||
`, | ||
frets, | ||
); | ||
const actual = fixture.adapter.getDefinitionAtPosition(delegateFn, 'input.ts', frets.cur.pos); | ||
expect(actual).toMatchObject([ | ||
{ | ||
fileName: 'input.ts', | ||
textSpan: { | ||
start: frets.d1.pos, | ||
length: frets.d2.pos - frets.d1.pos, | ||
}, | ||
}, | ||
] as Partial<ts.DefinitionInfo>[]); | ||
}); | ||
}); |
54 changes: 54 additions & 0 deletions
54
src/graphql-language-service-adapter/get-definition-at-position.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import ts from 'typescript'; | ||
import { visit, type FragmentSpreadNode } from 'graphql'; | ||
import { getFragmentsInDocument } from '../gql-ast-util'; | ||
import type { AnalysisContext, GetDefinitionAtPosition } from './types'; | ||
|
||
export function getDefinitionAtPosition( | ||
ctx: AnalysisContext, | ||
delegate: GetDefinitionAtPosition, | ||
fileName: string, | ||
position: number, | ||
) { | ||
if (ctx.getScriptSourceHelper().isExcluded(fileName)) return delegate(fileName, position); | ||
const node = ctx.findAscendantTemplateNode(fileName, position); | ||
if (!node) return delegate(fileName, position); | ||
const { resolvedInfo } = ctx.resolveTemplateInfo(fileName, node); | ||
if (!resolvedInfo) return delegate(fileName, position); | ||
const { combinedText, getInnerPosition, getSourcePosition } = resolvedInfo; | ||
const documentNode = ctx.getGraphQLDocumentNode(combinedText); | ||
if (!documentNode) return delegate(fileName, position); | ||
|
||
try { | ||
const innerPosition = getInnerPosition(position); | ||
if (innerPosition.isInOtherExpression) return delegate(fileName, position); | ||
let fragmentSpreadNodeUnderCursor: FragmentSpreadNode | undefined = undefined; | ||
visit(documentNode, { | ||
FragmentSpread: node => { | ||
if (node.loc!.start <= innerPosition.pos && innerPosition.pos < node.loc!.end) { | ||
fragmentSpreadNodeUnderCursor = node; | ||
} | ||
}, | ||
}); | ||
if (!fragmentSpreadNodeUnderCursor) return delegate(fileName, position); | ||
const foundFragmentDefinitionNode = getFragmentsInDocument(documentNode).find( | ||
fragmentDef => fragmentDef.name.value === fragmentSpreadNodeUnderCursor?.name.value, | ||
); | ||
if (!foundFragmentDefinitionNode) return delegate(fileName, position); | ||
const definitionSourcePosition = getSourcePosition(foundFragmentDefinitionNode.name.loc!.start); | ||
return [ | ||
{ | ||
fileName: definitionSourcePosition.fileName, | ||
name: foundFragmentDefinitionNode.name.value, | ||
textSpan: { | ||
start: definitionSourcePosition.pos, | ||
length: foundFragmentDefinitionNode.name.loc!.end - foundFragmentDefinitionNode.name.loc!.start, | ||
}, | ||
kind: ts.ScriptElementKind.unknown, | ||
containerKind: ts.ScriptElementKind.unknown, | ||
containerName: '', | ||
}, | ||
] satisfies ts.DefinitionInfo[]; | ||
} catch { | ||
return delegate(fileName, position); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters