Skip to content

Commit c71f888

Browse files
committed
feat: document symbols
1 parent 2927e58 commit c71f888

File tree

16 files changed

+273
-95
lines changed

16 files changed

+273
-95
lines changed

examples/graphiql-cdn/CHANGELOG.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ See [Conventional Commits](https://conventionalcommits.org) for commit guideline
1313

1414
## 0.0.8-alpha.1 (2020-01-18)
1515

16-
1716
## [0.0.7](https://github.com/graphql/graphiql/compare/[email protected]@0.0.7) (2019-12-03)
1817

1918
**Note:** Version bump only for package graphiql-example-cdn

packages/codemirror-graphql/CHANGELOG.md

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,6 @@ See [Conventional Commits](https://conventionalcommits.org) for commit guideline
1414

1515
- convert LSP Server to Typescript, remove watchman ([#1138](https://github.com/graphql/graphiql/issues/1138)) ([8e33dbb](https://github.com/graphql/graphiql/commit/8e33dbb))
1616

17-
# [0.12.0-alpha.0](https://github.com/graphql/graphiql/compare/[email protected]@0.12.0-alpha.0) (2020-01-18)
18-
19-
### Bug Fixes
20-
21-
- linting issues, trailingCommas: all ([#1099](https://github.com/graphql/graphiql/issues/1099)) ([de4005b](https://github.com/graphql/graphiql/commit/de4005b))
22-
- screenshot/gif urls ([e3ea2fc](https://github.com/graphql/graphiql/commit/e3ea2fc))
23-
24-
### Features
25-
26-
- convert LSP Server to Typescript, remove watchman ([#1138](https://github.com/graphql/graphiql/issues/1138)) ([8e33dbb](https://github.com/graphql/graphiql/commit/8e33dbb))
27-
2817
## [0.11.6](https://github.com/graphql/graphiql/compare/[email protected]@0.11.6) (2019-12-09)
2918

3019
### Bug Fixes

packages/graphql-language-service-interface/CHANGELOG.md

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,6 @@ See [Conventional Commits](https://conventionalcommits.org) for commit guideline
55

66
# [2.4.0-alpha.1](https://github.com/graphql/graphiql/compare/graphql-language-service-interface@[email protected]) (2020-01-18)
77

8-
### Bug Fixes
9-
10-
- linting issues, trailingCommas: all ([#1099](https://github.com/graphql/graphiql/issues/1099)) ([de4005b](https://github.com/graphql/graphiql/commit/de4005b))
11-
12-
### Features
13-
14-
- convert LSP Server to Typescript, remove watchman ([#1138](https://github.com/graphql/graphiql/issues/1138)) ([8e33dbb](https://github.com/graphql/graphiql/commit/8e33dbb))
15-
16-
# [2.4.0-alpha.0](https://github.com/graphql/graphiql/compare/graphql-language-service-interface@[email protected]) (2020-01-18)
17-
18-
### Bug Fixes
19-
20-
- linting issues, trailingCommas: all ([#1099](https://github.com/graphql/graphiql/issues/1099)) ([de4005b](https://github.com/graphql/graphiql/commit/de4005b))
21-
228
### Features
239

2410
- convert LSP Server to Typescript, remove watchman ([#1138](https://github.com/graphql/graphiql/issues/1138)) ([8e33dbb](https://github.com/graphql/graphiql/commit/8e33dbb))

packages/graphql-language-service-interface/src/GraphQLLanguageService.ts

Lines changed: 73 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,17 @@ import {
2626
Uri,
2727
Position,
2828
CustomValidationRule,
29+
Outline,
30+
OutlineTree,
2931
} from 'graphql-language-service-types';
3032

3133
// import { Position } from 'graphql-language-service-utils';
32-
import { Hover, DiagnosticSeverity } from 'vscode-languageserver-types';
34+
import {
35+
Hover,
36+
DiagnosticSeverity,
37+
SymbolInformation,
38+
SymbolKind,
39+
} from 'vscode-languageserver-types';
3340

3441
import { Kind, parse, print } from 'graphql';
3542
import { getAutocompleteSuggestions } from './getAutocompleteSuggestions';
@@ -41,6 +48,8 @@ import {
4148
getDefinitionQueryResultForNamedType,
4249
} from './getDefinition';
4350

51+
import { getOutline } from './getOutline';
52+
4453
import {
4554
getASTNodeAtPosition,
4655
requireFile,
@@ -67,6 +76,15 @@ const {
6776
NAMED_TYPE,
6877
} = Kind;
6978

79+
const KIND_TO_SYMBOL_KIND: { [key: string]: SymbolKind } = {
80+
Field: SymbolKind.Field,
81+
OperationDefinition: SymbolKind.Class,
82+
FragmentDefinition: SymbolKind.Class,
83+
FragmentSpread: SymbolKind.Struct,
84+
ObjectType: SymbolKind.Class,
85+
InputType: SymbolKind.Class,
86+
};
87+
7088
export class GraphQLLanguageService {
7189
_graphQLCache: GraphQLCache;
7290
_graphQLConfig: GraphQLConfig;
@@ -84,7 +102,7 @@ export class GraphQLLanguageService {
84102
throw Error(`No config found for uri: ${uri}`);
85103
}
86104

87-
async getDiagnostics(
105+
public async getDiagnostics(
88106
query: string,
89107
uri: Uri,
90108
isRelayCompatMode?: boolean,
@@ -187,7 +205,7 @@ export class GraphQLLanguageService {
187205
return validateQuery(validationAst, schema, customRules, isRelayCompatMode);
188206
}
189207

190-
async getAutocompleteSuggestions(
208+
public async getAutocompleteSuggestions(
191209
query: string,
192210
position: Position,
193211
filePath: Uri,
@@ -203,7 +221,7 @@ export class GraphQLLanguageService {
203221
return [];
204222
}
205223

206-
async getHoverInformation(
224+
public async getHoverInformation(
207225
query: string,
208226
position: Position,
209227
filePath: Uri,
@@ -219,7 +237,7 @@ export class GraphQLLanguageService {
219237
return '';
220238
}
221239

222-
async getDefinition(
240+
public async getDefinition(
223241
query: string,
224242
position: Position,
225243
filePath: Uri,
@@ -266,6 +284,53 @@ export class GraphQLLanguageService {
266284
return null;
267285
}
268286

287+
public async getDocumentSymbols(
288+
document: string,
289+
filePath: Uri,
290+
): Promise<SymbolInformation[]> {
291+
const outline = await this.getOutline(document);
292+
if (!outline) {
293+
return [];
294+
}
295+
296+
const output: Array<SymbolInformation> = [];
297+
const input = outline.outlineTrees.map((tree: OutlineTree) => [null, tree]);
298+
while (input.length > 0) {
299+
const res = input.pop();
300+
if (!res) {
301+
return [];
302+
}
303+
const [parent, tree] = res;
304+
if (!tree) {
305+
return [];
306+
}
307+
output.push({
308+
// @ts-ignore
309+
name: tree.representativeName,
310+
kind: KIND_TO_SYMBOL_KIND[tree.kind],
311+
location: {
312+
uri: filePath,
313+
range: {
314+
start: tree.startPosition,
315+
// @ts-ignore
316+
end: tree.endPosition,
317+
},
318+
},
319+
containerName: parent ? parent.representativeName : undefined,
320+
});
321+
input.push(...tree.children.map(child => [tree, child]));
322+
}
323+
return output;
324+
}
325+
//
326+
// public async getReferences(
327+
// document: string,
328+
// position: Position,
329+
// filePath: Uri,
330+
// ): Promise<Location[]> {
331+
//
332+
// }
333+
269334
async _getDefinitionForNamedType(
270335
query: string,
271336
ast: DocumentNode,
@@ -350,4 +415,7 @@ export class GraphQLLanguageService {
350415

351416
return result;
352417
}
418+
async getOutline(query: string): Promise<Outline | null | undefined> {
419+
return getOutline(query);
420+
}
353421
}

packages/graphql-language-service-interface/src/__tests__/GraphQLLanguageService-test.ts

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@
77
*
88
*/
99

10-
import { Position } from 'graphql-language-service-types';
1110
import { join } from 'path';
1211
import * as fs from 'fs';
1312
import { buildSchema } from 'graphql';
1413

1514
import { GraphQLConfig } from 'graphql-config';
1615
import { GraphQLLanguageService } from '../GraphQLLanguageService';
16+
import { SymbolKind } from 'vscode-languageserver-protocol';
17+
import { Position } from 'graphql-language-service-utils';
1718

1819
const MOCK_CONFIG = {
1920
schemaPath: './__schema__/StarWarsSchema.graphql',
@@ -110,4 +111,36 @@ describe('GraphQLLanguageService', () => {
110111
'String\n\nThe `String` scalar type represents textual data, represented as UTF-8 character sequences. The String type is most often used by GraphQL to represent free-form human-readable text.',
111112
);
112113
});
114+
115+
it('runs document symbol requests as expected', async () => {
116+
const validQuery = `
117+
query OperationExample {
118+
item(episode: EMPIRE){
119+
...testFragment
120+
}
121+
}
122+
`;
123+
124+
const result = await languageService.getDocumentSymbols(
125+
validQuery,
126+
'file://file.graphql',
127+
);
128+
129+
expect(result).not.toBeUndefined();
130+
expect(result.length).toEqual(3);
131+
// expect(result[0].name).toEqual('item');
132+
expect(result[1].name).toEqual('item');
133+
expect(result[1].kind).toEqual(SymbolKind.Field);
134+
expect(result[1].location.range.start).toContain({
135+
line: 2,
136+
character: 4,
137+
lessThanOrEqualTo: function lessThanOrEqualTo() {
138+
return null;
139+
},
140+
});
141+
expect(result[1].location.range.end).toContain({
142+
line: 2,
143+
character: 4,
144+
});
145+
});
113146
});

packages/graphql-language-service-interface/src/getOutline.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ function outlineTreeConverter(docText: string): OutlineTreeConverterType {
8989
representativeName: node.name,
9090
startPosition: offsetToPosition(docText, node.loc.start),
9191
endPosition: offsetToPosition(docText, node.loc.end),
92+
kind: node.kind,
9293
children: node.selectionSet || [],
9394
});
9495

packages/graphql-language-service-parser/CHANGELOG.md

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,6 @@ See [Conventional Commits](https://conventionalcommits.org) for commit guideline
1010
- exclusion for nonbreakable whitespace chars ([#1091](https://github.com/graphql/graphiql/issues/1091)) ([91763dd](https://github.com/graphql/graphiql/commit/91763dd)), closes [graphql/graphql-language-service#220](https://github.com/graphql/graphql-language-service/issues/220)
1111
- linting issues, trailingCommas: all ([#1099](https://github.com/graphql/graphiql/issues/1099)) ([de4005b](https://github.com/graphql/graphiql/commit/de4005b))
1212

13-
## [1.5.3-alpha.0](https://github.com/graphql/graphiql/compare/graphql-language-service-parser@[email protected]) (2020-01-18)
14-
15-
### Bug Fixes
16-
17-
- exclusion for nonbreakable whitespace chars ([#1091](https://github.com/graphql/graphiql/issues/1091)) ([91763dd](https://github.com/graphql/graphiql/commit/91763dd)), closes [graphql/graphql-language-service#220](https://github.com/graphql/graphql-language-service/issues/220)
18-
- linting issues, trailingCommas: all ([#1099](https://github.com/graphql/graphiql/issues/1099)) ([de4005b](https://github.com/graphql/graphiql/commit/de4005b))
19-
2013
## [1.5.2](https://github.com/graphql/graphiql/compare/graphql-language-service-parser@[email protected]) (2019-12-09)
2114

2215
### Bug Fixes

packages/graphql-language-service-server/CHANGELOG.md

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,6 @@ See [Conventional Commits](https://conventionalcommits.org) for commit guideline
1313

1414
- convert LSP Server to Typescript, remove watchman ([#1138](https://github.com/graphql/graphiql/issues/1138)) ([8e33dbb](https://github.com/graphql/graphiql/commit/8e33dbb))
1515

16-
# [2.4.0-alpha.0](https://github.com/graphql/graphiql/compare/graphql-language-service-server@[email protected]) (2020-01-18)
17-
18-
### Bug Fixes
19-
20-
- linting issues, trailingCommas: all ([#1099](https://github.com/graphql/graphiql/issues/1099)) ([de4005b](https://github.com/graphql/graphiql/commit/de4005b))
21-
22-
### Features
23-
24-
- convert LSP Server to Typescript, remove watchman ([#1138](https://github.com/graphql/graphiql/issues/1138)) ([8e33dbb](https://github.com/graphql/graphiql/commit/8e33dbb))
25-
2616
## [2.3.3](https://github.com/graphql/graphiql/compare/graphql-language-service-server@[email protected]) (2019-12-09)
2717

2818
### Bug Fixes

0 commit comments

Comments
 (0)