-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from rintoj/feat/reference-enhanced
Feat/reference enhanced
- Loading branch information
Showing
12 changed files
with
225 additions
and
86 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
export * from './definition-provider-from-schema' | ||
export * from './definition-provider-from-source' | ||
export * from './reference-provider-from-schema' | ||
export * from './symbol-provider-from-schema' | ||
export * from './definition-provider-for-schema' | ||
export * from './definition-provider-for-source' | ||
export * from './reference-provider-for-schema' | ||
export * from './symbol-provider-for-schema' |
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
132 changes: 132 additions & 0 deletions
132
src/definition-provider/reference-provider-for-schema.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,132 @@ | ||
import { globStream } from 'fast-glob' | ||
import * as gql from 'graphql' | ||
import { Location, Position } from '../diff' | ||
import { getGQLNodeRange, getGQLNodeRangeWithoutDescription, makeQueryParsable } from '../gql' | ||
import { isPositionWithInRange } from '../position/is-position-within-range' | ||
import { parseGraphQLDocumentFromTS, readTSFile } from '../ts' | ||
|
||
interface SelectedField { | ||
parent: string | ||
name: string | ||
} | ||
|
||
function isInRange(node: gql.ASTNode, position: Position, offset?: Position) { | ||
const nodeRange = getGQLNodeRange(node, offset) | ||
return isPositionWithInRange(position, nodeRange, true) | ||
} | ||
|
||
function referencesByType( | ||
document: gql.DocumentNode, | ||
sourcePath: string, | ||
type: string | undefined, | ||
) { | ||
if (!type) return [] | ||
const locations: Location[] = [] | ||
gql.visit(document, { | ||
NamedType(node) { | ||
if (node.name.value !== type) return | ||
locations.push(new Location(sourcePath, getGQLNodeRangeWithoutDescription(node))) | ||
}, | ||
}) | ||
return locations | ||
} | ||
|
||
function processFile(file: string, field: SelectedField, schema: gql.GraphQLSchema) { | ||
const locations: Location[] = [] | ||
try { | ||
const sourceFile = readTSFile(file) | ||
const { document, offset } = parseGraphQLDocumentFromTS(sourceFile) | ||
if (!document) return locations | ||
const typeInfo = new gql.TypeInfo(schema) | ||
gql.visit( | ||
document, | ||
gql.visitWithTypeInfo(typeInfo, { | ||
Field(node) { | ||
if (node.name.value !== field.name) return | ||
const parent = typeInfo.getParentType() | ||
if (!parent || parent.name !== field.parent) return | ||
locations.push(new Location(file, getGQLNodeRange(node.name, offset))) | ||
}, | ||
}), | ||
) | ||
} catch (e) { | ||
console.log(`Failed to process file ${file}`, e.message) | ||
} | ||
return locations | ||
} | ||
|
||
async function referencesByField( | ||
schema: gql.GraphQLSchema, | ||
field: SelectedField | undefined, | ||
pattern: string, | ||
) { | ||
if (!field || !pattern) return [] | ||
const stream = globStream(pattern) | ||
let locations: Location[] = [] | ||
for await (const file of stream) { | ||
locations = locations.concat(processFile(file as string, field, schema)) | ||
} | ||
return locations | ||
} | ||
|
||
export async function provideReferenceForSchema( | ||
source: string, | ||
sourcePath: string, | ||
position: Position, | ||
pattern: string, | ||
) { | ||
try { | ||
const fixed = makeQueryParsable(source) | ||
const document = gql.parse(fixed) | ||
let type: string | undefined | ||
let selectedField: SelectedField | undefined | ||
const processNode = (node: gql.NameNode) => { | ||
if (!isInRange(node, position)) return | ||
type = node.value | ||
return gql.BREAK | ||
} | ||
const processField = (node: gql.TypeDefinitionNode) => { | ||
switch (node.kind) { | ||
case gql.Kind.OBJECT_TYPE_DEFINITION: | ||
case gql.Kind.INPUT_OBJECT_TYPE_DEFINITION: | ||
case gql.Kind.INTERFACE_TYPE_DEFINITION: | ||
for (const field of node.fields ?? []) { | ||
if (isInRange(field.name, position)) { | ||
selectedField = { parent: node.name.value, name: field.name.value } | ||
return gql.BREAK | ||
} | ||
} | ||
} | ||
} | ||
gql.visit(document, { | ||
Name(node) { | ||
return processNode(node) | ||
}, | ||
EnumTypeDefinition(node) { | ||
for (const value of node.values ?? []) { | ||
if (isInRange(value.name, position)) { | ||
selectedField = { parent: node.name.value, name: value.name.value } | ||
return | ||
} | ||
} | ||
}, | ||
ObjectTypeDefinition(node) { | ||
return processField(node) | ||
}, | ||
InterfaceTypeDefinition(node) { | ||
return processField(node) | ||
}, | ||
InputObjectTypeDefinition(node) { | ||
return processField(node) | ||
}, | ||
}) | ||
if (type) { | ||
return referencesByType(document, sourcePath, type) | ||
} | ||
const schema = gql.buildSchema(source) | ||
return await referencesByField(schema, selectedField, pattern) | ||
} catch (e) { | ||
console.error(e) | ||
return [] | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.