Skip to content

Commit fb8a429

Browse files
Angeartterales
andauthored
fix(react-i18next): show sub completion items when using scoped namespace (#941)
* fix(react-i18next): show sub completion items when using scoped namespace * Empty commit to trigger CI --------- Co-authored-by: Alex Terehov <[email protected]>
1 parent ae6972a commit fb8a429

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/core/KeyDetector.ts

+10
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,16 @@ export class KeyDetector {
5555
return keyRange?.key
5656
}
5757

58+
static getScopedKey(document: TextDocument, position: Position)
59+
{
60+
const scopes = Global.enabledFrameworks.flatMap(f => f.getScopeRange(document) || [])
61+
if (scopes.length > 0)
62+
{
63+
const offset = document.offsetAt(position)
64+
return scopes.filter(s => s.start < offset && offset < s.end).map(s => s.namespace).join('.')
65+
}
66+
}
67+
5868
static getKeyAndRange(document: TextDocument, position: Position, dotEnding?: boolean) {
5969
const { range, key } = KeyDetector.getKeyRange(document, position, dotEnding) || {}
6070
if (!range || !key)

src/editor/completion.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,18 @@ class CompletionProvider implements CompletionItemProvider {
1616
if (key === undefined)
1717
return
1818

19+
const scopedKey = KeyDetector.getScopedKey(document, position)
20+
1921
if (!key) {
2022
return Object
2123
.values(CurrentFile.loader.keys)
2224
.map((key) => {
23-
const item = new CompletionItem(key, CompletionItemKind.Text)
25+
let resolvedKey = key
26+
if (scopedKey)
27+
{
28+
resolvedKey = key.replace(`${scopedKey}.`, "")
29+
}
30+
const item = new CompletionItem(resolvedKey, CompletionItemKind.Text)
2431
item.detail = loader.getValueByKey(key)
2532
return item
2633
})
@@ -35,6 +42,9 @@ class CompletionProvider implements CompletionItemProvider {
3542

3643
let node: LocaleTree | LocaleNode | undefined
3744

45+
if (scopedKey && key)
46+
node = loader.getTreeNodeByKey([scopedKey, key].join('.'))
47+
3848
if (!key)
3949
node = loader.root
4050

0 commit comments

Comments
 (0)