Skip to content

Commit

Permalink
feat: get definition requests working for template imports
Browse files Browse the repository at this point in the history
  • Loading branch information
lifeart committed Feb 6, 2022
1 parent cbc4bf1 commit 430fc0c
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { CompletionItem, TextDocumentPositionParams } from 'vscode-languageserve
import Server from '../server';
import { getFileRanges, RangeWalker, getPlaceholderPathFromAst, getScope, documentPartForPosition } from '../utils/glimmer-script';
import { parseScriptFile as parse } from 'ember-meta-explorer';
import { containsPosition, toPosition } from '../estree-utils';
import { getFocusPath } from '../utils/glimmer-template';
import { TextDocument } from 'vscode-languageserver-textdocument';

Expand Down
10 changes: 6 additions & 4 deletions src/definition-providers/glimmer-script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
import Server from './../server';
import ASTPath from './../glimmer-utils';
import { TextDocumentPositionParams, Definition, Location } from 'vscode-languageserver/node';
import { parseScriptFile as parse } from 'ember-meta-explorer';
import { containsPosition, toPosition } from './../estree-utils';
import { toPosition } from './../estree-utils';
import { queryELSAddonsAPIChain } from './../utils/addon-api';
import { Project } from '../project';
import { preprocess } from '@glimmer/syntax';
import { documentPartForPosition, getFileRanges, RangeWalker } from '../utils/glimmer-script';
import { TextDocument } from 'vscode-languageserver-textdocument';

Expand All @@ -27,7 +27,9 @@ export default class GlimmerScriptDefinitionProvider {
let rangeWalker = new RangeWalker(ranges);

// strip not needed scopes example
rangeWalker = rangeWalker.subtract([...rangeWalker.hbsInlineComments(true), ...rangeWalker.hbsComments(true)]);
// strip not needed scopes example
rangeWalker = rangeWalker.subtract(rangeWalker.hbsInlineComments(true));
rangeWalker = rangeWalker.subtract(rangeWalker.hbsComments(true));
rangeWalker = rangeWalker.subtract(rangeWalker.htmlComments(true));

const templates = rangeWalker.templates(true);
Expand All @@ -39,7 +41,7 @@ export default class GlimmerScriptDefinitionProvider {
// here we need glimmer logic to collect all available tokens from scope for autocomplete
const templateDocument = TextDocument.create(uri, 'handlebars', document.version, templateForPosition.absoluteContent);

const ast = templateForPosition.ast;
const ast = preprocess(templateDocument.getText());
const focusPath = ASTPath.toPosition(ast, toPosition(params.position), templateDocument.getText());

if (!focusPath) {
Expand Down
6 changes: 5 additions & 1 deletion src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ export default class Server {
this.connection.onDidChangeWatchedFiles(this.onDidChangeWatchedFiles.bind(this));
this.connection.onDidChangeConfiguration(this.onDidChangeConfiguration.bind(this));
this.connection.onDocumentSymbol(this.onDocumentSymbol.bind(this));
this.connection.onDefinition(this.definitionProvider.handler);
this.connection.onDefinition(this.onDefinition.bind(this));
this.connection.onCompletion(this.onCompletion.bind(this));
this.connection.onCompletionResolve(this.onCompletionResolve.bind(this));
this.connection.onExecuteCommand(this.onExecute.bind(this));
Expand Down Expand Up @@ -679,6 +679,10 @@ export default class Server {
return await this.hoverProvider.provideHover(params);
}

private async onDefinition(params: TextDocumentPositionParams) {
return this.definitionProvider.handle(params);
}

private async onCompletionResolve(item: CompletionItem) {
return item;
}
Expand Down
31 changes: 31 additions & 0 deletions test/__snapshots__/template-imports-integration-test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,34 @@ Object {
],
}
`;
exports[`has basic template imports support DefinitionRequest inside <template> template imports definition requests working fine [legacy mode] support component definition in .gjs files 1`] = `
Object {
"addonsMeta": Array [],
"registry": Object {
"component": Object {
"hello": Array [
"app/components/hello/index.gjs",
],
"world": Array [
"app/components/world/index.gts",
],
},
},
"response": Array [
Object {
"range": Object {
"end": Object {
"character": 0,
"line": 0,
},
"start": Object {
"character": 0,
"line": 0,
},
},
"uri": "/app/components/world/index.gts",
},
],
}
`;
46 changes: 45 additions & 1 deletion test/template-imports-integration-test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createServer, getResult, ServerBucket } from './test_helpers/public-integration-helpers';
import { MessageConnection } from 'vscode-jsonrpc/node';
import { CompletionRequest } from 'vscode-languageserver-protocol/node';
import { CompletionRequest, DefinitionRequest } from 'vscode-languageserver-protocol/node';

function createPointer(tpl = '') {
const findMe = '⚡';
Expand Down Expand Up @@ -121,4 +121,48 @@ describe('has basic template imports support', function () {
});
});
});

describe('DefinitionRequest inside <template>', () => {
beforeAll(async () => {
instance = await createServer({ asyncFsEnabled: false });
connection = instance.connection;
});

afterAll(async () => {
await instance.destroy();
});

describe('template imports definition requests working fine [legacy mode]', () => {
it('support component definition in .gjs files', async () => {
const tpl = `
export default class Foo extends Bar {
<template>
<W⚡orld/>
</template>
}
`;
const { content, position } = createPointer(tpl);
const result = await getResult(
DefinitionRequest.method,
connection,
{
app: {
components: {
hello: {
'index.gjs': content,
},
world: {
'index.gts': '',
},
},
},
},
'app/components/hello/index.gjs',
position
);

expect(result).toMatchSnapshot();
});
});
});
});

0 comments on commit 430fc0c

Please sign in to comment.