1
- import { commands , window , workspace , QuickPickItem , Range } from 'vscode'
1
+ import { commands , window , QuickPickItem , Range , TextDocument } from 'vscode'
2
2
import { trim } from 'lodash'
3
3
import { overrideConfirm } from './overrideConfirm'
4
4
import { keypathValidate } from './keypathValidate'
@@ -8,21 +8,20 @@ import { extractHardStrings, generateKeyFromText, Config, CurrentFile } from '~/
8
8
import i18n from '~/i18n'
9
9
import { Log , promptTemplates } from '~/utils'
10
10
import { parseHardString } from '~/extraction/parseHardString'
11
- import { DetectionResult } from '~/extraction '
11
+ import { DetectionResult } from '~/core/types '
12
12
13
13
interface QuickPickItemWithKey extends QuickPickItem {
14
14
keypath : string
15
15
type : 'tree' | 'node' | 'new' | 'existed'
16
16
}
17
17
18
18
export interface ExtractTextOptions {
19
- filepath : string
20
19
text : string
21
20
rawText ?: string
22
21
args ?: string [ ]
23
22
range : Range
24
23
isDynamic ?: boolean
25
- languageId ?: string
24
+ document : TextDocument
26
25
isInsert ?: boolean
27
26
}
28
27
@@ -35,16 +34,15 @@ async function ExtractOrInsertCommnad(options?: ExtractTextOptions, detection?:
35
34
if ( ! options ) {
36
35
// execute from command palette, get from active document
37
36
const editor = window . activeTextEditor
38
- const document = editor ?. document
39
- if ( ! editor || ! document )
37
+ const currentDoc = editor ?. document
38
+ if ( ! editor || ! currentDoc )
40
39
return
41
40
42
41
options = {
43
- filepath : document . uri . fsPath ,
44
42
text : '' ,
45
- rawText : trim ( document . getText ( editor . selection ) , '\'"` ' ) ,
43
+ rawText : trim ( currentDoc . getText ( editor . selection ) , '\'"` ' ) ,
46
44
range : editor . selection ,
47
- languageId : document . languageId ,
45
+ document : currentDoc ,
48
46
isInsert : editor . selection . start . isEqual ( editor . selection . end ) ,
49
47
}
50
48
}
@@ -53,12 +51,13 @@ async function ExtractOrInsertCommnad(options?: ExtractTextOptions, detection?:
53
51
const loader = CurrentFile . loader
54
52
55
53
if ( options . rawText && ! options . text ) {
56
- const result = parseHardString ( options . rawText , options . languageId , options . isDynamic )
54
+ const result = parseHardString ( options . rawText , options . document ?. languageId , options . isDynamic )
57
55
options . text = result ?. text || ''
58
56
options . args = result ?. args
59
57
}
60
58
61
- const { filepath, text, rawText, range, args, languageId, isInsert } = options
59
+ const { text, rawText, range, args, document, isInsert } = options
60
+ const filepath = document . uri . fsPath
62
61
63
62
const default_keypath = generateKeyFromText ( rawText || text , filepath )
64
63
@@ -147,15 +146,13 @@ async function ExtractOrInsertCommnad(options?: ExtractTextOptions, detection?:
147
146
return
148
147
}
149
148
150
- const replacer = await promptTemplates ( keypath , args , languageId , detection )
149
+ const replacer = await promptTemplates ( keypath , args , document , detection )
151
150
152
151
if ( ! replacer ) {
153
152
window . showWarningMessage ( i18n . t ( 'prompt.extraction_canceled' ) )
154
153
return
155
154
}
156
155
157
- const document = await workspace . openTextDocument ( filepath )
158
-
159
156
await extractHardStrings ( document , [ {
160
157
range,
161
158
replaceTo : replacer ,
0 commit comments