Skip to content

Commit

Permalink
feat: document symbols
Browse files Browse the repository at this point in the history
  • Loading branch information
acao committed Jan 20, 2020
1 parent 2927e58 commit c71f888
Show file tree
Hide file tree
Showing 16 changed files with 273 additions and 95 deletions.
1 change: 0 additions & 1 deletion examples/graphiql-cdn/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ See [Conventional Commits](https://conventionalcommits.org) for commit guideline

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


## [0.0.7](https://github.com/graphql/graphiql/compare/graphiql-example-cdn@0.0.6...graphiql-example-cdn@0.0.7) (2019-12-03)

**Note:** Version bump only for package graphiql-example-cdn
Expand Down
11 changes: 0 additions & 11 deletions packages/codemirror-graphql/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,6 @@ See [Conventional Commits](https://conventionalcommits.org) for commit guideline

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

# [0.12.0-alpha.0](https://github.com/graphql/graphiql/compare/codemirror-graphql@0.11.6...codemirror-graphql@0.12.0-alpha.0) (2020-01-18)

### Bug Fixes

- linting issues, trailingCommas: all ([#1099](https://github.com/graphql/graphiql/issues/1099)) ([de4005b](https://github.com/graphql/graphiql/commit/de4005b))
- screenshot/gif urls ([e3ea2fc](https://github.com/graphql/graphiql/commit/e3ea2fc))

### Features

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

## [0.11.6](https://github.com/graphql/graphiql/compare/codemirror-graphql@0.11.5...codemirror-graphql@0.11.6) (2019-12-09)

### Bug Fixes
Expand Down
14 changes: 0 additions & 14 deletions packages/graphql-language-service-interface/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,6 @@ See [Conventional Commits](https://conventionalcommits.org) for commit guideline

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

### Bug Fixes

- linting issues, trailingCommas: all ([#1099](https://github.com/graphql/graphiql/issues/1099)) ([de4005b](https://github.com/graphql/graphiql/commit/de4005b))

### Features

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

# [2.4.0-alpha.0](https://github.com/graphql/graphiql/compare/graphql-language-service-interface@2.3.3...graphql-language-service-interface@2.4.0-alpha.0) (2020-01-18)

### Bug Fixes

- linting issues, trailingCommas: all ([#1099](https://github.com/graphql/graphiql/issues/1099)) ([de4005b](https://github.com/graphql/graphiql/commit/de4005b))

### Features

- convert LSP Server to Typescript, remove watchman ([#1138](https://github.com/graphql/graphiql/issues/1138)) ([8e33dbb](https://github.com/graphql/graphiql/commit/8e33dbb))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,17 @@ import {
Uri,
Position,
CustomValidationRule,
Outline,
OutlineTree,
} from 'graphql-language-service-types';

// import { Position } from 'graphql-language-service-utils';
import { Hover, DiagnosticSeverity } from 'vscode-languageserver-types';
import {
Hover,
DiagnosticSeverity,
SymbolInformation,
SymbolKind,
} from 'vscode-languageserver-types';

import { Kind, parse, print } from 'graphql';
import { getAutocompleteSuggestions } from './getAutocompleteSuggestions';
Expand All @@ -41,6 +48,8 @@ import {
getDefinitionQueryResultForNamedType,
} from './getDefinition';

import { getOutline } from './getOutline';

import {
getASTNodeAtPosition,
requireFile,
Expand All @@ -67,6 +76,15 @@ const {
NAMED_TYPE,
} = Kind;

const KIND_TO_SYMBOL_KIND: { [key: string]: SymbolKind } = {
Field: SymbolKind.Field,
OperationDefinition: SymbolKind.Class,
FragmentDefinition: SymbolKind.Class,
FragmentSpread: SymbolKind.Struct,
ObjectType: SymbolKind.Class,
InputType: SymbolKind.Class,
};

export class GraphQLLanguageService {
_graphQLCache: GraphQLCache;
_graphQLConfig: GraphQLConfig;
Expand All @@ -84,7 +102,7 @@ export class GraphQLLanguageService {
throw Error(`No config found for uri: ${uri}`);
}

async getDiagnostics(
public async getDiagnostics(
query: string,
uri: Uri,
isRelayCompatMode?: boolean,
Expand Down Expand Up @@ -187,7 +205,7 @@ export class GraphQLLanguageService {
return validateQuery(validationAst, schema, customRules, isRelayCompatMode);
}

async getAutocompleteSuggestions(
public async getAutocompleteSuggestions(
query: string,
position: Position,
filePath: Uri,
Expand All @@ -203,7 +221,7 @@ export class GraphQLLanguageService {
return [];
}

async getHoverInformation(
public async getHoverInformation(
query: string,
position: Position,
filePath: Uri,
Expand All @@ -219,7 +237,7 @@ export class GraphQLLanguageService {
return '';
}

async getDefinition(
public async getDefinition(
query: string,
position: Position,
filePath: Uri,
Expand Down Expand Up @@ -266,6 +284,53 @@ export class GraphQLLanguageService {
return null;
}

public async getDocumentSymbols(
document: string,
filePath: Uri,
): Promise<SymbolInformation[]> {
const outline = await this.getOutline(document);
if (!outline) {
return [];
}

const output: Array<SymbolInformation> = [];
const input = outline.outlineTrees.map((tree: OutlineTree) => [null, tree]);
while (input.length > 0) {
const res = input.pop();
if (!res) {
return [];
}
const [parent, tree] = res;
if (!tree) {
return [];
}
output.push({
// @ts-ignore
name: tree.representativeName,
kind: KIND_TO_SYMBOL_KIND[tree.kind],
location: {
uri: filePath,
range: {
start: tree.startPosition,
// @ts-ignore
end: tree.endPosition,
},
},
containerName: parent ? parent.representativeName : undefined,
});
input.push(...tree.children.map(child => [tree, child]));
}
return output;
}
//
// public async getReferences(
// document: string,
// position: Position,
// filePath: Uri,
// ): Promise<Location[]> {
//
// }

async _getDefinitionForNamedType(
query: string,
ast: DocumentNode,
Expand Down Expand Up @@ -350,4 +415,7 @@ export class GraphQLLanguageService {

return result;
}
async getOutline(query: string): Promise<Outline | null | undefined> {
return getOutline(query);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
*
*/

import { Position } from 'graphql-language-service-types';
import { join } from 'path';
import * as fs from 'fs';
import { buildSchema } from 'graphql';

import { GraphQLConfig } from 'graphql-config';
import { GraphQLLanguageService } from '../GraphQLLanguageService';
import { SymbolKind } from 'vscode-languageserver-protocol';
import { Position } from 'graphql-language-service-utils';

const MOCK_CONFIG = {
schemaPath: './__schema__/StarWarsSchema.graphql',
Expand Down Expand Up @@ -110,4 +111,36 @@ describe('GraphQLLanguageService', () => {
'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.',
);
});

it('runs document symbol requests as expected', async () => {
const validQuery = `
query OperationExample {
item(episode: EMPIRE){
...testFragment
}
}
`;

const result = await languageService.getDocumentSymbols(
validQuery,
'file://file.graphql',
);

expect(result).not.toBeUndefined();
expect(result.length).toEqual(3);
// expect(result[0].name).toEqual('item');
expect(result[1].name).toEqual('item');
expect(result[1].kind).toEqual(SymbolKind.Field);
expect(result[1].location.range.start).toContain({
line: 2,
character: 4,
lessThanOrEqualTo: function lessThanOrEqualTo() {
return null;
},
});
expect(result[1].location.range.end).toContain({
line: 2,
character: 4,
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ function outlineTreeConverter(docText: string): OutlineTreeConverterType {
representativeName: node.name,
startPosition: offsetToPosition(docText, node.loc.start),
endPosition: offsetToPosition(docText, node.loc.end),
kind: node.kind,
children: node.selectionSet || [],
});

Expand Down
7 changes: 0 additions & 7 deletions packages/graphql-language-service-parser/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,6 @@ See [Conventional Commits](https://conventionalcommits.org) for commit guideline
- 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)
- linting issues, trailingCommas: all ([#1099](https://github.com/graphql/graphiql/issues/1099)) ([de4005b](https://github.com/graphql/graphiql/commit/de4005b))

## [1.5.3-alpha.0](https://github.com/graphql/graphiql/compare/graphql-language-service-parser@1.5.2...graphql-language-service-parser@1.5.3-alpha.0) (2020-01-18)

### Bug Fixes

- 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)
- linting issues, trailingCommas: all ([#1099](https://github.com/graphql/graphiql/issues/1099)) ([de4005b](https://github.com/graphql/graphiql/commit/de4005b))

## [1.5.2](https://github.com/graphql/graphiql/compare/graphql-language-service-parser@1.5.1...graphql-language-service-parser@1.5.2) (2019-12-09)

### Bug Fixes
Expand Down
10 changes: 0 additions & 10 deletions packages/graphql-language-service-server/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,6 @@ See [Conventional Commits](https://conventionalcommits.org) for commit guideline

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

# [2.4.0-alpha.0](https://github.com/graphql/graphiql/compare/graphql-language-service-server@2.3.3...graphql-language-service-server@2.4.0-alpha.0) (2020-01-18)

### Bug Fixes

- linting issues, trailingCommas: all ([#1099](https://github.com/graphql/graphiql/issues/1099)) ([de4005b](https://github.com/graphql/graphiql/commit/de4005b))

### Features

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

## [2.3.3](https://github.com/graphql/graphiql/compare/graphql-language-service-server@2.3.2...graphql-language-service-server@2.3.3) (2019-12-09)

### Bug Fixes
Expand Down
Loading

0 comments on commit c71f888

Please sign in to comment.