-
Notifications
You must be signed in to change notification settings - Fork 51
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
Add JS/TS code folding support to language server #632
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,238 @@ | ||
import { Project } from 'glint-monorepo-test-utils'; | ||
import { describe, beforeEach, afterEach, test, expect } from 'vitest'; | ||
import { stripIndent } from 'common-tags'; | ||
|
||
describe('Language Server: Folding Ranges', () => { | ||
let project!: Project; | ||
|
||
beforeEach(async () => { | ||
project = await Project.create(); | ||
}); | ||
|
||
afterEach(async () => { | ||
await project.destroy(); | ||
}); | ||
|
||
test('function', () => { | ||
project.write({ | ||
'example.ts': stripIndent` | ||
function foo() { | ||
return 'bar'; | ||
} | ||
`, | ||
}); | ||
|
||
let server = project.startLanguageServer(); | ||
let folds = server.getFoldingRanges(project.fileURI('example.ts')); | ||
|
||
expect(folds).toEqual([ | ||
{ | ||
startLine: 0, | ||
endLine: 1, | ||
kind: undefined, | ||
}, | ||
]); | ||
}); | ||
|
||
test('nested function', () => { | ||
project.write({ | ||
'example.ts': stripIndent` | ||
function topLevel() { | ||
|
||
function nested() { | ||
return 'bar'; | ||
} | ||
|
||
return nested(); | ||
} | ||
`, | ||
}); | ||
|
||
let server = project.startLanguageServer(); | ||
let folds = server.getFoldingRanges(project.fileURI('example.ts')); | ||
|
||
expect(folds).toEqual([ | ||
{ | ||
startLine: 0, | ||
endLine: 6, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this excluding the last There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The last line is excluded if it's just a closing bracket (https://github.com/typed-ember/glint/pull/632/files#diff-cadffc59ac4fd3c9fd1cd7488980fc9cfea40cd5a93129f1021c54c810980e20R503). This is so that folded functions keep the // Like this when folded
function topLevel() {
}
// instead of this
function topLevel() { this is consistent with the TS language server's folding. |
||
kind: undefined, | ||
}, | ||
{ | ||
startLine: 2, | ||
endLine: 3, | ||
kind: undefined, | ||
}, | ||
]); | ||
}); | ||
|
||
test('imports', () => { | ||
project.write({ | ||
'example.ts': stripIndent` | ||
import Component, { hbs } from '@glimmerx/component'; | ||
import foo from 'bar'; | ||
import { baz } from 'qux'; | ||
|
||
export default { foo, baz, Component }; | ||
`, | ||
}); | ||
|
||
let server = project.startLanguageServer(); | ||
let folds = server.getFoldingRanges(project.fileURI('example.ts')); | ||
|
||
expect(folds).toEqual([ | ||
{ | ||
startLine: 0, | ||
endLine: 2, | ||
kind: 'imports', | ||
}, | ||
]); | ||
}); | ||
|
||
test('comments', () => { | ||
project.write({ | ||
'example.ts': stripIndent` | ||
// This is | ||
// a | ||
// multiline | ||
// comment | ||
|
||
const foo = 'bar'; | ||
`, | ||
}); | ||
|
||
let server = project.startLanguageServer(); | ||
let folds = server.getFoldingRanges(project.fileURI('example.ts')); | ||
|
||
expect(folds).toEqual([ | ||
{ | ||
startLine: 0, | ||
endLine: 3, | ||
kind: 'comment', | ||
}, | ||
]); | ||
}); | ||
|
||
test('region', () => { | ||
project.write({ | ||
'example.ts': stripIndent` | ||
const foo = 'bar'; | ||
|
||
// #region | ||
|
||
const bar = 'baz'; | ||
|
||
// #endregion | ||
|
||
export default { foo, bar }; | ||
`, | ||
}); | ||
|
||
let server = project.startLanguageServer(); | ||
let folds = server.getFoldingRanges(project.fileURI('example.ts')); | ||
|
||
expect(folds).toEqual([ | ||
{ | ||
startLine: 2, | ||
endLine: 6, | ||
kind: 'region', | ||
}, | ||
]); | ||
}); | ||
|
||
test('simple component', () => { | ||
project.write({ | ||
'example.ts': stripIndent` | ||
import Component, { hbs } from '@glimmerx/component'; | ||
import { tracked } from '@glimmer/tracking'; | ||
|
||
export interface EmberComponentArgs { | ||
message: string; | ||
} | ||
|
||
export interface EmberComponentSignature { | ||
Element: HTMLDivElement; | ||
Args: EmberComponentArgs; | ||
} | ||
|
||
/** | ||
* A simple component that renders a message. | ||
*/ | ||
export default class Greeting extends Component<EmberComponentSignature> { | ||
@tracked message = this.args.message; | ||
|
||
get capitalizedMessage() { | ||
return this.message.toUpperCase(); | ||
} | ||
} | ||
|
||
declare module '@glint/environment-ember-loose/registry' { | ||
export default interface Registry { | ||
EmberComponent: typeof EmberComponent; | ||
'ember-component': typeof EmberComponent; | ||
} | ||
} | ||
`, | ||
}); | ||
|
||
let server = project.startLanguageServer(); | ||
let folds = server.getFoldingRanges(project.fileURI('example.ts')); | ||
|
||
expect(folds).toEqual([ | ||
// Imports | ||
{ | ||
startLine: 0, | ||
endLine: 1, | ||
kind: 'imports', | ||
}, | ||
|
||
// EmberComponentArgs | ||
{ | ||
startLine: 3, | ||
endLine: 4, | ||
kind: undefined, | ||
}, | ||
|
||
// EmberComponentSignature | ||
{ | ||
startLine: 7, | ||
endLine: 9, | ||
kind: undefined, | ||
}, | ||
|
||
// Code Comment | ||
{ | ||
startLine: 12, | ||
endLine: 14, | ||
kind: 'comment', | ||
}, | ||
|
||
// Greeting Component | ||
{ | ||
startLine: 15, | ||
endLine: 20, | ||
kind: undefined, | ||
}, | ||
|
||
// capitalizedMessage | ||
{ | ||
startLine: 18, | ||
endLine: 19, | ||
kind: undefined, | ||
}, | ||
|
||
// declare module | ||
{ | ||
startLine: 23, | ||
endLine: 27, | ||
kind: undefined, | ||
}, | ||
|
||
// interface Registry | ||
{ | ||
startLine: 24, | ||
endLine: 26, | ||
kind: undefined, | ||
}, | ||
]); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I really appreciate you adding all these tests in your PRs <3