Skip to content

Add support for symbol search to glint-language-server #50

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions packages/core/__tests__/language-server/symbol-search.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import Project from '../utils/project';
import { stripIndent } from 'common-tags';
import { SymbolKind } from 'vscode-languageserver-types';

describe('Language Server: Symbol Search', () => {
let project!: Project;

beforeEach(async () => {
jest.setTimeout(20_000);
project = await Project.create();
});

afterEach(async () => {
await project.destroy();
});

test('component definition', () => {
project.write({
'greeting.ts': stripIndent`
import Component, { hbs } from '@glimmerx/component';

export interface GreetingArgs {
message: string;
}

export default class Greeting extends Component<GreetingArgs> {
static template = hbs\`{{@message}}, World!\`;
}
`,
'index.ts': stripIndent`
import Component, { hbs } from '@glimmerx/component';
import Greeting from './greeting';

export class Application extends Component {
static template = hbs\`
<Greeting @message="Hello" />
\`;
}
`,
});

let server = project.startLanguageServer();
let expectedSymbols = new Set([
{
name: 'Greeting',
kind: SymbolKind.Class,
location: {
uri: project.fileURI('greeting.ts'),
range: {
start: { line: 6, character: 0 },
end: { line: 8, character: 1 },
},
},
},
{
name: 'Greeting',
kind: SymbolKind.Variable,
location: {
uri: project.fileURI('index.ts'),
range: {
start: { line: 1, character: 7 },
end: { line: 1, character: 15 },
},
},
},
{
name: 'GreetingArgs',
kind: SymbolKind.Interface,
location: {
uri: project.fileURI('greeting.ts'),
range: {
start: { line: 2, character: 0 },
end: { line: 4, character: 1 },
},
},
},
]);

expect(new Set(server.findSymbols('greeting'))).toEqual(expectedSymbols);
});
});
23 changes: 20 additions & 3 deletions packages/core/src/language-server/glint-language-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,15 @@ import {
MarkedString,
WorkspaceEdit,
Range,
SymbolInformation,
} from 'vscode-languageserver';
import DocumentCache, { isTemplate } from '../common/document-cache';
import { Position, positionToOffset } from './util/position';
import { severityForDiagnostic, tagsForDiagnostic } from './util/protocol';
import {
scriptElementKindToSymbolKind,
severityForDiagnostic,
tagsForDiagnostic,
} from './util/protocol';
import { TextEdit } from 'vscode-languageserver-textdocument';

export default class GlintLanguageServer {
Expand Down Expand Up @@ -106,6 +111,18 @@ export default class GlintLanguageServer {
});
}

public findSymbols(query: string): Array<SymbolInformation> {
return this.service
.getNavigateToItems(query)
.map(({ name, kind, fileName, textSpan }) => {
let location = this.textSpanToLocation(fileName, textSpan);
if (location) {
return { name, location, kind: scriptElementKindToSymbolKind(kind) };
}
})
.filter((info): info is SymbolInformation => Boolean(info));
}

public getCompletions(uri: string, position: Position): CompletionItem[] | undefined {
let { transformedFileName, transformedOffset } = this.getTransformedOffset(uri, position);
let completions = this.service.getCompletionsAtPosition(
Expand Down Expand Up @@ -256,11 +273,11 @@ export default class GlintLanguageServer {

private calculateOriginalLocations(spans: ReadonlyArray<ts.DocumentSpan>): Array<Location> {
return spans
.map((span) => this.documentSpanToLocation(span))
.map((span) => this.textSpanToLocation(span.fileName, span.textSpan))
.filter((loc): loc is Location => Boolean(loc));
}

private documentSpanToLocation({ fileName, textSpan }: ts.DocumentSpan): Location | undefined {
private textSpanToLocation(fileName: string, textSpan: ts.TextSpan): Location | undefined {
let { originalFileName, originalStart, originalEnd } = this.transformManager.getOriginalRange(
fileName,
textSpan.start,
Expand Down
5 changes: 5 additions & 0 deletions packages/core/src/language-server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ connection.onInitialize(() => ({
referencesProvider: true,
hoverProvider: true,
definitionProvider: true,
workspaceSymbolProvider: true,
renameProvider: {
prepareProvider: true,
},
Expand Down Expand Up @@ -93,5 +94,9 @@ connection.onReferences(({ textDocument, position }) => {
return gls.getReferences(textDocument.uri, position);
});

connection.onWorkspaceSymbol(({ query }) => {
return gls.findSymbols(query);
});

documents.listen(connection);
connection.listen();
42 changes: 41 additions & 1 deletion packages/core/src/language-server/util/protocol.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import ts from 'typescript';
import { CompletionItemKind, DiagnosticSeverity, DiagnosticTag } from 'vscode-languageserver';
import {
CompletionItemKind,
DiagnosticSeverity,
DiagnosticTag,
SymbolKind,
} from 'vscode-languageserver';

/*
* This module contains utilities for converting between conventions used
Expand Down Expand Up @@ -50,6 +55,41 @@ export function scriptElementKindToCompletionItemKind(
}
}

export function scriptElementKindToSymbolKind(kind: ts.ScriptElementKind): SymbolKind {
switch (kind) {
case ts.ScriptElementKind.memberVariableElement:
case ts.ScriptElementKind.indexSignatureElement:
return SymbolKind.Field;
case ts.ScriptElementKind.memberGetAccessorElement:
case ts.ScriptElementKind.memberSetAccessorElement:
case ts.ScriptElementKind.memberFunctionElement:
return SymbolKind.Method;
case ts.ScriptElementKind.functionElement:
case ts.ScriptElementKind.localFunctionElement:
case ts.ScriptElementKind.constructSignatureElement:
case ts.ScriptElementKind.callSignatureElement:
return SymbolKind.Function;
case ts.ScriptElementKind.enumElement:
return SymbolKind.Enum;
case ts.ScriptElementKind.moduleElement:
return SymbolKind.Module;
case ts.ScriptElementKind.classElement:
case ts.ScriptElementKind.localClassElement:
return SymbolKind.Class;
case ts.ScriptElementKind.interfaceElement:
return SymbolKind.Interface;
case ts.ScriptElementKind.scriptElement:
return SymbolKind.File;
case ts.ScriptElementKind.jsxAttribute:
return SymbolKind.Property;
case ts.ScriptElementKind.constElement:
case ts.ScriptElementKind.enumMemberElement:
return SymbolKind.Constant;
default:
return SymbolKind.Variable;
}
}

export function tagsForDiagnostic(diagnostic: ts.Diagnostic): DiagnosticTag[] {
let tags: Array<DiagnosticTag> = [];

Expand Down