@@ -5,9 +5,11 @@ import { checkExistKeyCommand } from './commands/checkKey';
5
5
import { renameKeyCommand } from './commands/renameKey' ;
6
6
import { removeKeyCommand } from './commands/removeKey' ;
7
7
import { updateKeyCommand } from './commands/updateKey' ;
8
- import { getHoverTranslation , getKeysValues } from './utils/jsonUtils' ;
8
+ import { getHoverTranslation , loadKeys } from './utils/jsonUtils' ;
9
+ import { JsonI18nKeySettings } from './models/JsonI18nKeySettings' ;
9
10
10
11
let outputChannel : vscode . OutputChannel | undefined ;
12
+ let keyCache : string [ ] = [ ] ;
11
13
12
14
/**
13
15
* This method is called when the extension is activated.
@@ -49,23 +51,56 @@ export function activate(context: vscode.ExtensionContext): void {
49
51
}
50
52
51
53
let fullKeyPath = document . getText ( range ) ;
54
+ // if (context.triggerKind !== vscode.CompletionTriggerKind.TriggerCharacter) {
55
+ const textBeforeCursor = document . getText ( new vscode . Range ( range . start , position ) ) ;
56
+ fullKeyPath = textBeforeCursor ;
57
+ // }
52
58
// Remove surrounding quotes (single or double quotes) if they exist
53
59
fullKeyPath = fullKeyPath . replace ( / ^ [ ' " ] | [ ' " ] $ / g, '' ) ;
54
60
55
- const results = getKeysValues ( fullKeyPath ) ;
56
- if ( results . length === 0 ) {
57
- return ;
58
- }
59
-
60
- return results . map ( ( obj :any ) => {
61
- const completionItem = new vscode . CompletionItem ( obj . parentProperty , vscode . CompletionItemKind . Field ) ;
62
- completionItem . documentation = new vscode . MarkdownString ( obj . value ) ;
61
+ const uniqueKeys = Array . from (
62
+ new Set ( keyCache
63
+ . filter ( key => key . startsWith ( fullKeyPath ) )
64
+ . map ( ( key : string ) => {
65
+ const str = key . slice ( fullKeyPath . lastIndexOf ( '.' ) + 1 , key . length ) ;
66
+ if ( str . indexOf ( '.' ) === - 1 ) {
67
+ return str ;
68
+ }
69
+ const nextKey = str . slice ( 0 , str . indexOf ( '.' ) ) ;
70
+ return nextKey ;
71
+ } )
72
+ )
73
+ ) ;
74
+ return uniqueKeys . map ( ( key : string ) => {
75
+ const completionItem = new vscode . CompletionItem ( key , vscode . CompletionItemKind . Field ) ;
76
+ completionItem . documentation = new vscode . MarkdownString ( key ) ;
77
+ completionItem . detail = "i18n key" ;
63
78
return completionItem ;
64
79
} ) ;
65
- }
66
- }
80
+ } ,
81
+ } ,
82
+ '.' // Trigger on `.`
67
83
) ;
68
84
85
+
86
+ // Set up file watcher for en.json
87
+ const watcher = vscode . workspace . createFileSystemWatcher ( JsonI18nKeySettings . instance . enJsonFilePath ) ;
88
+ watcher . onDidChange ( ( ) => {
89
+ console . log ( 'en.json changed, reloading keys...' ) ;
90
+ keyCache = loadKeys ( ) ;
91
+ } ) ;
92
+ watcher . onDidCreate ( ( ) => {
93
+ console . log ( 'en.json created, loading keys...' ) ;
94
+ keyCache = loadKeys ( ) ;
95
+ } ) ;
96
+ watcher . onDidDelete ( ( ) => {
97
+ console . log ( 'en.json deleted, clearing cache...' ) ;
98
+ keyCache = [ ] ;
99
+ } ) ;
100
+
101
+ // Load keys initially
102
+ keyCache = loadKeys ( ) ;
103
+
69
104
// Register commands and other providers
70
105
context . subscriptions . push (
71
106
vscode . commands . registerCommand ( 'json-i18n-key.findKey' , findKeyCommand ) ,
@@ -75,7 +110,8 @@ export function activate(context: vscode.ExtensionContext): void {
75
110
vscode . commands . registerCommand ( 'json-i18n-key.updateKey' , updateKeyCommand ) ,
76
111
vscode . commands . registerCommand ( 'json-i18n-key.addKey' , addKeyCommand ) ,
77
112
hoverProvider ,
78
- completionProvider
113
+ completionProvider ,
114
+ watcher ,
79
115
) ;
80
116
}
81
117
0 commit comments