forked from ember-tooling/ember-language-server
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: hover provider for intl addon (translations) (#340)
* translations hover for intl * move `focusPath` logic out of addon into hover providers entrypoint * test for translation autocomplete basing on translation text instead of key
- Loading branch information
Showing
7 changed files
with
225 additions
and
19 deletions.
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,41 @@ | ||
import { ASTv1 } from '@glimmer/syntax'; | ||
import { Hover } from 'vscode-languageserver'; | ||
import { Server } from '../..'; | ||
import { nodeLoc } from '../../glimmer-utils'; | ||
import { HoverFunctionParams } from '../../utils/addon-api'; | ||
import { isLocalizationHelperTranslataionName } from '../../utils/ast-helpers'; | ||
import { getTranslations } from './intl-utils'; | ||
|
||
export default class IntlHoverProvider { | ||
server: Server; | ||
onInit(server: Server) { | ||
this.server = server; | ||
} | ||
|
||
async onHover(root: string, params: HoverFunctionParams): Promise<Hover[]> { | ||
const { results, focusPath, type } = params; | ||
|
||
if (isLocalizationHelperTranslataionName(focusPath, type)) { | ||
const node = focusPath.node as ASTv1.StringLiteral; | ||
const key = node.value; | ||
const translations = await getTranslations(root, this.server); | ||
const location = nodeLoc(node); | ||
|
||
Object.keys(translations).forEach((tr) => { | ||
if (tr === key) { | ||
const detail = translations[tr].map((t) => `${t.locale} : ${t.text}`).join('\n'); | ||
|
||
results.push({ | ||
contents: { kind: 'plaintext', value: detail }, | ||
range: { | ||
start: { line: location.start.line - 1, character: location.start.column }, | ||
end: { line: location.end.line - 1, character: location.end.column }, | ||
}, | ||
}); | ||
} | ||
}); | ||
} | ||
|
||
return results; | ||
} | ||
} |
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
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
Oops, something went wrong.