-
Notifications
You must be signed in to change notification settings - Fork 54
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
feat: Introduce InlayHints #532
Open
chadhietala
wants to merge
1
commit into
typed-ember:main
Choose a base branch
from
chadhietala:chietala/inlay-hints
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { Project } from 'glint-monorepo-test-utils'; | ||
import { describe, beforeEach, afterEach, test, expect } from 'vitest'; | ||
import { Position, Range } from 'vscode-languageserver'; | ||
|
||
describe('Language Server: iInlays', () => { | ||
let project!: Project; | ||
|
||
beforeEach(async () => { | ||
project = await Project.create(); | ||
}); | ||
|
||
afterEach(async () => { | ||
await project.destroy(); | ||
}); | ||
|
||
test('it provides inlays for return types when preference is set', async () => { | ||
project.setGlintConfig({ environment: 'ember-template-imports' }); | ||
let content = 'const bar = () => true;'; | ||
project.write('foo.gts', content); | ||
let server = project.startLanguageServer(); | ||
|
||
const inlays = server.getInlayHints( | ||
{ | ||
textDocument: { | ||
uri: project.fileURI('foo.gts'), | ||
}, | ||
range: Range.create(Position.create(0, 0), Position.create(0, content.length)), | ||
}, | ||
{ | ||
includeInlayFunctionLikeReturnTypeHints: true, | ||
} | ||
); | ||
|
||
expect(inlays.length).toBe(1); | ||
expect(inlays[0].kind).toBe(1); | ||
expect(inlays[0].label).toBe(': boolean'); | ||
}); | ||
|
||
test('it provides inlays for variable types when preference is set', async () => { | ||
project.setGlintConfig({ environment: 'ember-template-imports' }); | ||
let content = 'const bar = globalThis.thing ?? null;'; | ||
project.write('foo.gts', content); | ||
let server = project.startLanguageServer(); | ||
|
||
const inlays = server.getInlayHints( | ||
{ | ||
textDocument: { | ||
uri: project.fileURI('foo.gts'), | ||
}, | ||
range: Range.create(Position.create(0, 0), Position.create(0, content.length)), | ||
}, | ||
{ | ||
includeInlayVariableTypeHints: true, | ||
} | ||
); | ||
|
||
expect(inlays.length).toBe(1); | ||
expect(inlays[0].kind).toBe(1); | ||
expect(inlays[0].label).toBe(': any'); | ||
}); | ||
|
||
test('it provides inlays for property types when preference is set', async () => { | ||
project.setGlintConfig({ environment: 'ember-template-imports' }); | ||
let content = 'class Foo { date = Date.now() }'; | ||
project.write('foo.gts', content); | ||
let server = project.startLanguageServer(); | ||
|
||
const inlays = server.getInlayHints( | ||
{ | ||
textDocument: { | ||
uri: project.fileURI('foo.gts'), | ||
}, | ||
range: Range.create(Position.create(0, 0), Position.create(0, content.length)), | ||
}, | ||
{ | ||
includeInlayPropertyDeclarationTypeHints: true, | ||
} | ||
); | ||
|
||
expect(inlays.length).toBe(1); | ||
expect(inlays[0].kind).toBe(1); | ||
expect(inlays[0].label).toBe(': number'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,10 @@ import { | |
TextDocumentEdit, | ||
OptionalVersionedTextDocumentIdentifier, | ||
TextEdit, | ||
InlayHint, | ||
InlayHintParams, | ||
Position as LSPPosition, | ||
InlayHintKind, | ||
} from 'vscode-languageserver'; | ||
import DocumentCache from '../common/document-cache.js'; | ||
import { Position, positionToOffset } from './util/position.js'; | ||
|
@@ -381,6 +385,42 @@ export default class GlintLanguageServer { | |
} | ||
} | ||
|
||
public getInlayHints(hint: InlayHintParams, preferences: ts.UserPreferences = {}): InlayHint[] { | ||
let { uri } = hint.textDocument; | ||
let { range } = hint; | ||
let fileName = uriToFilePath(uri); | ||
|
||
let { transformedStart, transformedEnd, transformedFileName } = | ||
this.getTransformedOffsetsFromPositions( | ||
uri, | ||
{ | ||
line: range.start.line, | ||
character: range.start.character, | ||
}, | ||
{ | ||
line: range.end.line, | ||
character: range.end.character, | ||
} | ||
); | ||
Comment on lines
+394
to
+404
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. Can this just be |
||
|
||
const inlayHints = this.service.provideInlayHints( | ||
transformedFileName, | ||
{ | ||
start: transformedStart, | ||
length: transformedEnd - transformedStart, | ||
}, | ||
preferences | ||
); | ||
|
||
let content = this.documents.getDocumentContents(fileName); | ||
|
||
return inlayHints | ||
.map((tsInlayHint) => { | ||
return this.transformTSInlayToLSPInlay(tsInlayHint, transformedFileName, content); | ||
}) | ||
.filter(isHint); | ||
} | ||
|
||
public getCodeActions( | ||
uri: string, | ||
actionKind: string, | ||
|
@@ -404,6 +444,32 @@ export default class GlintLanguageServer { | |
return this.glintConfig.environment.isTypedScript(file) ? 'typescript' : 'javascript'; | ||
} | ||
|
||
private transformTSInlayToLSPInlay( | ||
hint: ts.InlayHint, | ||
fileName: string, | ||
contents: string | ||
): InlayHint | undefined { | ||
let { position, text } = hint; | ||
let { originalStart } = this.transformManager.getOriginalRange( | ||
fileName, | ||
position, | ||
position + text.length | ||
); | ||
|
||
const { line, character } = offsetToPosition(contents, originalStart); | ||
|
||
let kind = | ||
hint.kind === 'Parameter' | ||
? InlayHintKind.Parameter | ||
: hint.kind === 'Type' | ||
? InlayHintKind.Type | ||
: undefined; // enums are not supported by LSP; | ||
|
||
if (isInlayHintKind(kind)) { | ||
return InlayHint.create(LSPPosition.create(line, character), hint.text, kind); | ||
} | ||
} | ||
|
||
private applyCodeAction( | ||
uri: string, | ||
range: Range, | ||
|
@@ -651,3 +717,11 @@ export default class GlintLanguageServer { | |
function onlyNumbers(entry: number | undefined): entry is number { | ||
return entry !== undefined; | ||
} | ||
|
||
function isHint(hint: InlayHint | undefined): hint is InlayHint { | ||
return hint !== undefined; | ||
} | ||
|
||
function isInlayHintKind(kind: number | undefined): kind is InlayHintKind { | ||
return kind !== undefined; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { type WorkspaceConfiguration, window } from 'vscode'; | ||
import type * as ts from 'typescript/lib/tsserverlibrary'; | ||
|
||
// vscode does not hold formatting config with the same interface as typescript | ||
// the following maps the vscode formatting options into what typescript expects | ||
// This is heavily borrowed from how the TypeScript works in vscode | ||
// https://github.com/microsoft/vscode/blob/c04c0b43470c3c743468a5e5e51f036123503452/extensions/typescript-language-features/src/languageFeatures/fileConfigurationManager.ts#L133 | ||
export function intoFormatting( | ||
config: WorkspaceConfiguration | ||
): ts.server.protocol.FormatCodeSettings { | ||
let editorOptions = window.activeTextEditor?.options; | ||
let tabSize = typeof editorOptions?.tabSize === 'string' ? undefined : editorOptions?.tabSize; | ||
let insertSpaces = | ||
typeof editorOptions?.insertSpaces === 'string' ? undefined : editorOptions?.insertSpaces; | ||
|
||
return { | ||
tabSize, | ||
indentSize: tabSize, | ||
convertTabsToSpaces: insertSpaces, | ||
// We can use \n here since the editor normalizes later on to its line endings. | ||
newLineCharacter: '\n', | ||
insertSpaceAfterCommaDelimiter: config.get<boolean>('insertSpaceAfterCommaDelimiter'), | ||
insertSpaceAfterConstructor: config.get<boolean>('insertSpaceAfterConstructor'), | ||
insertSpaceAfterSemicolonInForStatements: config.get<boolean>( | ||
'insertSpaceAfterSemicolonInForStatements' | ||
), | ||
insertSpaceBeforeAndAfterBinaryOperators: config.get<boolean>( | ||
'insertSpaceBeforeAndAfterBinaryOperators' | ||
), | ||
insertSpaceAfterKeywordsInControlFlowStatements: config.get<boolean>( | ||
'insertSpaceAfterKeywordsInControlFlowStatements' | ||
), | ||
insertSpaceAfterFunctionKeywordForAnonymousFunctions: config.get<boolean>( | ||
'insertSpaceAfterFunctionKeywordForAnonymousFunctions' | ||
), | ||
insertSpaceBeforeFunctionParenthesis: config.get<boolean>( | ||
'insertSpaceBeforeFunctionParenthesis' | ||
), | ||
insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis: config.get<boolean>( | ||
'insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis' | ||
), | ||
insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets: config.get<boolean>( | ||
'insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets' | ||
), | ||
insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces: config.get<boolean>( | ||
'insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces' | ||
), | ||
insertSpaceAfterOpeningAndBeforeClosingEmptyBraces: config.get<boolean>( | ||
'insertSpaceAfterOpeningAndBeforeClosingEmptyBraces' | ||
), | ||
insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces: config.get<boolean>( | ||
'insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces' | ||
), | ||
insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces: config.get<boolean>( | ||
'insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces' | ||
), | ||
insertSpaceAfterTypeAssertion: config.get<boolean>('insertSpaceAfterTypeAssertion'), | ||
placeOpenBraceOnNewLineForFunctions: config.get<boolean>('placeOpenBraceOnNewLineForFunctions'), | ||
placeOpenBraceOnNewLineForControlBlocks: config.get<boolean>( | ||
'placeOpenBraceOnNewLineForControlBlocks' | ||
), | ||
semicolons: config.get<ts.server.protocol.SemicolonPreference>('semicolons'), | ||
}; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Nice!