Skip to content

Provide completions for template globals #61

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 2 commits into from
Mar 13, 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
27 changes: 27 additions & 0 deletions packages/core/__tests__/language-server/completions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,33 @@ describe('Language Server: Completions', () => {
expect(details.detail).toEqual('(parameter) letter: string');
});

test('globals', () => {
let code = stripIndent`
import Component, { hbs } from '@glint/environment-glimmerx/component';

export default class MyComponent extends Component {
static template = hbs\`
{{deb}}
\`;
}
`;

project.write('index.ts', code);

let server = project.startLanguageServer();
let completions = server.getCompletions(project.fileURI('index.ts'), {
line: 4,
character: 9,
});

let completion = completions?.find((completion) => completion.label === 'debugger');

expect(completion).toMatchObject({
kind: CompletionItemKind.Field,
label: 'debugger',
});
});

test('referencing module-scope identifiers', async () => {
let code = stripIndent`
import Component, { hbs } from '@glint/environment-glimmerx/component';
Expand Down
8 changes: 4 additions & 4 deletions packages/transform/__tests__/offset-mapping.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ describe('Source-to-source offset mapping', () => {
describe('path segments', () => {
test('simple path', () => {
let module = rewriteCompanionTemplate({ contents: '{{foo.bar}}' });
expectTokenMapping(module, 'foo', { transformedToken: '"foo"' });
expectTokenMapping(module, 'foo');
expectTokenMapping(module, 'bar');
});

Expand All @@ -168,7 +168,7 @@ describe('Source-to-source offset mapping', () => {

test('simple out-of-scope paths', () => {
let module = rewriteInlineTemplate({ contents: '{{foo.bar}}' });
expectTokenMapping(module, 'foo', { transformedToken: '"foo"' });
expectTokenMapping(module, 'foo');
expectTokenMapping(module, 'bar');
});

Expand Down Expand Up @@ -263,12 +263,12 @@ describe('Source-to-source offset mapping', () => {
`,
});

expectTokenMapping(module, 'each', { occurrence: 0, transformedToken: '"each"' });
expectTokenMapping(module, 'each', { occurrence: 0 });
expectTokenMapping(module, 'this');
expectTokenMapping(module, 'items');
expectTokenMapping(module, 'num', { occurrence: 0 });
expectTokenMapping(module, 'num', { occurrence: 1 });
expectTokenMapping(module, 'each', { occurrence: 1, transformedToken: '"each"' });
expectTokenMapping(module, 'each', { occurrence: 1 });
});

test('angle bracket params', () => {
Expand Down
14 changes: 14 additions & 0 deletions packages/transform/src/scope-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,20 @@ export default class ScopeStack {
return this.top.has(identifier);
}

public hasMatchingBinding(prefix: string): boolean {
if (this.hasBinding(prefix)) {
return true;
}

for (let identifier of this.top) {
if (identifier.startsWith(prefix)) {
return true;
}
}

return false;
}

private get top(): Set<string> {
return this.stack[0];
}
Expand Down
8 changes: 4 additions & 4 deletions packages/transform/src/template-to-typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,12 +278,12 @@ export function templateToTypescript(
}

function emitIdentifierReference(name: string, hbsOffset: number, hbsLength?: number): void {
if (scope.hasBinding(name)) {
if (scope.hasMatchingBinding(name)) {
emit.identifier(name, hbsOffset, hbsLength);
} else {
emit.text('χ.Globals[');
emit.identifier(JSON.stringify(name), hbsOffset, hbsLength ?? name.length);
emit.text(']');
emit.text('χ.Globals["');
emit.identifier(JSON.stringify(name).slice(1, -1), hbsOffset, hbsLength ?? name.length);
emit.text('"]');
}
}

Expand Down