Skip to content

Commit

Permalink
fix(react-i18next): show sub completion items when using scoped names…
Browse files Browse the repository at this point in the history
…pace (#941)

* fix(react-i18next): show sub completion items when using scoped namespace

* Empty commit to trigger CI

---------

Co-authored-by: Alex Terehov <terales@users.noreply.github.com>
  • Loading branch information
Angeart and terales committed Aug 27, 2023
1 parent ae6972a commit fb8a429
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/core/KeyDetector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@ export class KeyDetector {
return keyRange?.key
}

static getScopedKey(document: TextDocument, position: Position)
{
const scopes = Global.enabledFrameworks.flatMap(f => f.getScopeRange(document) || [])
if (scopes.length > 0)
{
const offset = document.offsetAt(position)
return scopes.filter(s => s.start < offset && offset < s.end).map(s => s.namespace).join('.')
}
}

static getKeyAndRange(document: TextDocument, position: Position, dotEnding?: boolean) {
const { range, key } = KeyDetector.getKeyRange(document, position, dotEnding) || {}
if (!range || !key)
Expand Down
12 changes: 11 additions & 1 deletion src/editor/completion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,18 @@ class CompletionProvider implements CompletionItemProvider {
if (key === undefined)
return

const scopedKey = KeyDetector.getScopedKey(document, position)

if (!key) {
return Object
.values(CurrentFile.loader.keys)
.map((key) => {
const item = new CompletionItem(key, CompletionItemKind.Text)
let resolvedKey = key
if (scopedKey)
{
resolvedKey = key.replace(`${scopedKey}.`, "")
}
const item = new CompletionItem(resolvedKey, CompletionItemKind.Text)
item.detail = loader.getValueByKey(key)
return item
})
Expand All @@ -35,6 +42,9 @@ class CompletionProvider implements CompletionItemProvider {

let node: LocaleTree | LocaleNode | undefined

if (scopedKey && key)
node = loader.getTreeNodeByKey([scopedKey, key].join('.'))

if (!key)
node = loader.root

Expand Down

0 comments on commit fb8a429

Please sign in to comment.