Skip to content

Handle symbol rename requests in the language server #47

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 4 commits into from
Feb 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
34 changes: 9 additions & 25 deletions packages/core/__tests__/language-server/diagnostics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ describe('Language Server: Diagnostics', () => {
let server = project.startLanguageServer();
let diagnostics = server.getDiagnostics(project.fileURI('index.ts'));

expect(diagnostics.length).toEqual(1);
expect(diagnostics[0].uri).toEqual(project.fileURI('index.ts'));
expect(diagnostics[0].diagnostics).toMatchInlineSnapshot(`
expect(diagnostics).toMatchInlineSnapshot(`
Array [
Object {
"message": "Property 'startupTimee' does not exist on type 'Application'. Did you mean 'startupTime'?",
Expand Down Expand Up @@ -80,12 +78,7 @@ describe('Language Server: Diagnostics', () => {
server.openFile(project.fileURI('index.ts'), code);
server.updateFile(project.fileURI('index.ts'), code.replace('startupTimee', 'startupTime'));

expect(server.getDiagnostics(project.fileURI('index.ts'))).toEqual([
{
uri: project.fileURI('index.ts'),
diagnostics: [],
},
]);
expect(server.getDiagnostics(project.fileURI('index.ts'))).toEqual([]);
});

test('reports diagnostics for a companion template type error', () => {
Expand All @@ -111,11 +104,10 @@ describe('Language Server: Diagnostics', () => {
project.write('index.hbs', template);

let server = project.startLanguageServer();
let diagnostics = server.getDiagnostics(project.fileURI('index.ts'));
let scriptDiagnostics = server.getDiagnostics(project.fileURI('index.ts'));
let templateDiagnostics = server.getDiagnostics(project.fileURI('index.hbs'));

expect(diagnostics.length).toEqual(2);
expect(diagnostics[0].uri).toEqual(project.fileURI('index.ts'));
expect(diagnostics[0].diagnostics).toMatchInlineSnapshot(`
expect(scriptDiagnostics).toMatchInlineSnapshot(`
Array [
Object {
"message": "'startupTime' is declared but its value is never read.",
Expand All @@ -137,8 +129,8 @@ describe('Language Server: Diagnostics', () => {
},
]
`);
expect(diagnostics[1].uri).toEqual(project.fileURI('index.hbs'));
expect(diagnostics[1].diagnostics).toMatchInlineSnapshot(`

expect(templateDiagnostics).toMatchInlineSnapshot(`
Array [
Object {
"message": "Property 'startupTimee' does not exist on type 'Application'. Did you mean 'startupTime'?",
Expand All @@ -165,15 +157,7 @@ describe('Language Server: Diagnostics', () => {
template.replace('startupTimee', 'startupTime')
);

expect(server.getDiagnostics(project.fileURI('index.ts'))).toEqual([
{
uri: project.fileURI('index.ts'),
diagnostics: [],
},
{
uri: project.fileURI('index.hbs'),
diagnostics: [],
},
]);
expect(server.getDiagnostics(project.fileURI('index.ts'))).toEqual([]);
expect(server.getDiagnostics(project.fileURI('index.hbs'))).toEqual([]);
});
});
265 changes: 265 additions & 0 deletions packages/core/__tests__/language-server/rename.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,265 @@
import Project from '../utils/project';
import { stripIndent } from 'common-tags';

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

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

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

test('preparing rename-able and unrename-able elements', () => {
project.write({
'index.ts': stripIndent`
import Component, { hbs } from '@glimmerx/component';

export default class Greeting extends Component<GreetingArgs> {
private foo = 'hi';

static template = hbs\`
{{this.foo}}
{{@missingArg}}
\`;
}
`,
});

let server = project.startLanguageServer();
let renameSuccessful = server.prepareRename(project.fileURI('index.ts'), {
line: 6,
character: 12,
});

expect(renameSuccessful).toEqual({
start: { line: 6, character: 11 },
end: { line: 6, character: 14 },
});

let renameFail = server.prepareRename(project.fileURI('index.ts'), {
line: 7,
character: 10,
});

expect(renameFail).toBeUndefined();
});

test('renaming an arg', () => {
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 expectedWorkspaceEdit = {
changes: {
[project.fileURI('greeting.ts')]: [
{
newText: 'greeting',
range: {
end: { character: 9, line: 3 },
start: { character: 2, line: 3 },
},
},
{
newText: 'greeting',
range: {
end: { character: 34, line: 7 },
start: { character: 27, line: 7 },
},
},
],
[project.fileURI('index.ts')]: [
{
newText: 'greeting',
range: {
end: { character: 22, line: 5 },
start: { character: 15, line: 5 },
},
},
],
},
};

// Rename `@message` at the point where we pass it to the component
let renamePassedArg = server.getEditsForRename(
project.fileURI('index.ts'),
{ line: 5, character: 17 },
'greeting'
);

expect(renamePassedArg).toEqual(expectedWorkspaceEdit);

// Rename `@message` where we use it in the template
let renameReferencedArg = server.getEditsForRename(
project.fileURI('greeting.ts'),
{ line: 7, character: 31 },
'greeting'
);

expect(renameReferencedArg).toEqual(expectedWorkspaceEdit);

// Rename `@message` where we its type is declared
let renameDeclaredArg = server.getEditsForRename(
project.fileURI('greeting.ts'),
{ line: 3, character: 2 },
'greeting'
);

expect(renameDeclaredArg).toEqual(expectedWorkspaceEdit);
});

test('renaming a block param', () => {
project.write({
'index.ts': stripIndent`
import Component, { hbs } from '@glimmerx/component';

export default class Application extends Component {
static template = hbs\`
{{#each (array 'a' 'b' 'c') as |letter|}}
{{letter}}
{{/each}}
\`;
}
`,
});

let server = project.startLanguageServer();
let expectedWorkspaceEdit = {
changes: {
[project.fileURI('index.ts')]: [
{
newText: 'character',
range: {
start: { line: 4, character: 36 },
end: { line: 4, character: 42 },
},
},
{
newText: 'character',
range: {
start: { line: 5, character: 8 },
end: { line: 5, character: 14 },
},
},
],
},
};

// Rename the param where it's defined in bars
let renameDefinition = server.getEditsForRename(
project.fileURI('index.ts'),
{ line: 4, character: 38 },
'character'
);

expect(renameDefinition).toEqual(expectedWorkspaceEdit);

// Rename the param where it's used in curlies
let renameUsage = server.getEditsForRename(
project.fileURI('index.ts'),
{ line: 5, character: 10 },
'character'
);

expect(renameUsage).toEqual(expectedWorkspaceEdit);
});

test('renaming a component', async () => {
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 expectedWorkspaceEdit = {
changes: {
[project.fileURI('greeting.ts')]: [
{
newText: 'Salutation',
range: {
start: { line: 6, character: 21 },
end: { line: 6, character: 29 },
},
},
],
[project.fileURI('index.ts')]: [
{
newText: 'Salutation',
range: {
start: { line: 1, character: 7 },
end: { line: 1, character: 15 },
},
},
{
newText: 'Salutation',
range: {
start: { line: 5, character: 5 },
end: { line: 5, character: 13 },
},
},
],
},
};

// Rename the component class where it's defined
let renameDefinition = server.getEditsForRename(
project.fileURI('greeting.ts'),
{ line: 6, character: 24 },
'Salutation'
);

expect(renameDefinition).toEqual(expectedWorkspaceEdit);

// Rename the component class from where it's invoked
let renameUsage = server.getEditsForRename(
project.fileURI('index.ts'),
{ line: 5, character: 9 },
'Salutation'
);

expect(renameUsage).toEqual(expectedWorkspaceEdit);
});
});
2 changes: 1 addition & 1 deletion packages/core/__tests__/utils/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export default class Project {
return (this.server = new GlintLanguageServer(
ts,
glintConfig,
rootFileNames,
() => rootFileNames,
tsConfig.options
));
}
Expand Down
10 changes: 10 additions & 0 deletions packages/core/src/common/scheduling.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export function debounce(threshold: number, f: () => void): () => void {
let pending: NodeJS.Timeout | undefined;
return () => {
if (pending) {
clearTimeout(pending);
}

setTimeout(f, threshold);
};
}
Loading