Skip to content

Commit

Permalink
feat: public path go to definition for assets
Browse files Browse the repository at this point in the history
feat: path go-to definition

test
  • Loading branch information
lifeart committed Apr 25, 2022
1 parent 253e5a6 commit 0b9d932
Show file tree
Hide file tree
Showing 3 changed files with 162 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/builtin-addons/core/template-definition-provider.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as path from 'path';

import { Definition, Location } from 'vscode-languageserver/node';
import { Definition, Location, Range } from 'vscode-languageserver/node';
import { DefinitionFunctionParams } from './../../utils/addon-api';
import { isLinkToTarget, isLinkComponentRouteTarget, isOutlet } from './../../utils/ast-helpers';
import ASTPath from './../../glimmer-utils';
Expand Down Expand Up @@ -67,6 +67,12 @@ export default class TemplateDefinitionProvider {
this.server = server;
this.project = project;
}
isStringPath(focusPath: ASTPath): boolean {
const node = focusPath.node as ASTv1.StringLiteral | ASTv1.TextNode;
const isStringable = node.type === 'StringLiteral' || node.type === 'TextNode';

return isStringable ? true : false;
}
async onDefinition(_: string, params: DefinitionFunctionParams): Promise<Definition | null> {
const uri = params.textDocument.uri;

Expand Down Expand Up @@ -109,6 +115,17 @@ export default class TemplateDefinitionProvider {
definitions = await this.provideRouteDefinition((focusPath.node as ASTv1.PathExpression).original);
}

// Fallind back to script assets lookup in case of empty definitions
if (!definitions.length && this.isStringPath(focusPath)) {
const node = focusPath.node as ASTv1.StringLiteral | ASTv1.TextNode;
const text = node.type === 'StringLiteral' ? node.original : node.chars;
const itemPath = path.join(this.project.root, 'public', text);

if (await this.server.fs.exists(itemPath)) {
definitions = [Location.create(URI.file(itemPath).toString(), Range.create(0, 0, 0, 0))];
}
}

return definitions;
}
looksLikeClassicComponentName(name: string) {
Expand Down
112 changes: 112 additions & 0 deletions test/__snapshots__/integration-test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3184,6 +3184,62 @@ Object {
}
`;
exports[`integration async fs enabled: false Go to definition works for all supported cases works for string asset paths in public folder [StringLiteral] 1`] = `
Object {
"addonsMeta": Array [],
"registry": Object {
"component": Object {
"hello": Array [
"app/components/hello.hbs",
],
},
},
"response": Array [
Object {
"range": Object {
"end": Object {
"character": 0,
"line": 0,
},
"start": Object {
"character": 0,
"line": 0,
},
},
"uri": "/public/assets/img.jpg",
},
],
}
`;
exports[`integration async fs enabled: false Go to definition works for all supported cases works for string asset paths in public folder [TextNode] 1`] = `
Object {
"addonsMeta": Array [],
"registry": Object {
"component": Object {
"hello": Array [
"app/components/hello.hbs",
],
},
},
"response": Array [
Object {
"range": Object {
"end": Object {
"character": 0,
"line": 0,
},
"start": Object {
"character": 0,
"line": 0,
},
},
"uri": "/public/assets/img.jpg",
},
],
}
`;
exports[`integration async fs enabled: false Initialize request returns an initialize request 1`] = `
Object {
"capabilities": Object {
Expand Down Expand Up @@ -6738,6 +6794,62 @@ Object {
}
`;
exports[`integration async fs enabled: true Go to definition works for all supported cases works for string asset paths in public folder [StringLiteral] 1`] = `
Object {
"addonsMeta": Array [],
"registry": Object {
"component": Object {
"hello": Array [
"app/components/hello.hbs",
],
},
},
"response": Array [
Object {
"range": Object {
"end": Object {
"character": 0,
"line": 0,
},
"start": Object {
"character": 0,
"line": 0,
},
},
"uri": "/public/assets/img.jpg",
},
],
}
`;
exports[`integration async fs enabled: true Go to definition works for all supported cases works for string asset paths in public folder [TextNode] 1`] = `
Object {
"addonsMeta": Array [],
"registry": Object {
"component": Object {
"hello": Array [
"app/components/hello.hbs",
],
},
},
"response": Array [
Object {
"range": Object {
"end": Object {
"character": 0,
"line": 0,
},
"start": Object {
"character": 0,
"line": 0,
},
},
"uri": "/public/assets/img.jpg",
},
],
}
`;
exports[`integration async fs enabled: true Initialize request returns an initialize request 1`] = `
Object {
"capabilities": Object {
Expand Down
32 changes: 32 additions & 0 deletions test/integration-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,38 @@ describe('integration', function () {
});

describe('Go to definition works for all supported cases', () => {
it('works for string asset paths in public folder [TextNode]', async () => {
const key = 'assets/img.jpg';
const entry = 'app/components/hello.hbs';
const result = await getResult(
DefinitionRequest.method,
connection,
{
[`public/${key}`]: '',
[entry]: `<img src="${key}">`,
},
entry,
{ line: 0, character: 11 }
);

expect(result).toMatchSnapshot();
});
it('works for string asset paths in public folder [StringLiteral]', async () => {
const key = 'assets/img.jpg';
const entry = 'app/components/hello.hbs';
const result = await getResult(
DefinitionRequest.method,
connection,
{
[`public/${key}`]: '',
[entry]: `{{img-src "${key}"}}>`,
},
entry,
{ line: 0, character: 12 }
);

expect(result).toMatchSnapshot();
});
it('to to route defintion from LinkTo component', async () => {
const result = await getResult(
DefinitionRequest.method,
Expand Down

0 comments on commit 0b9d932

Please sign in to comment.