forked from lokalise/i18n-ally
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase.ts
61 lines (49 loc) · 1.65 KB
/
base.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/* eslint-disable @typescript-eslint/no-unused-vars */
import { TextDocument } from 'vscode'
import { KeyStyle, ParserOptions, KeyInDocument, Config } from '~/core'
import { File } from '~/utils'
export abstract class Parser {
abstract readonly id: string
private supportedExtsRegex: RegExp
readonly readonly: boolean = false
constructor(
public readonly languageIds: string[],
public readonly supportedExts: string,
public options: ParserOptions = {
get indent() {
return Config.indent
},
get tab() {
return Config.tabStyle
},
},
) {
this.supportedExtsRegex = new RegExp(`.?(${this.supportedExts})$`)
}
supports(ext: string) {
return !!ext.toLowerCase().match(this.supportedExtsRegex)
}
async load(filepath: string): Promise<object> {
const raw = await File.read(filepath)
if (!raw)
return {}
return await this.parse(raw)
}
async save(filepath: string, object: object, sort: boolean, compare: ((x: string, y: string) => number) | undefined) {
const text = await this.dump(object, sort, compare)
await File.write(filepath, text)
}
abstract parse(text: string): Promise<object>
abstract dump(object: object, sort: boolean, compare: ((x: string, y: string) => number) | undefined): Promise<string>
parseAST(text: string): KeyInDocument[] {
return []
}
navigateToKey(text: string, keypath: string, keystyle: KeyStyle) {
return this.parseAST(text).find(k => k.key === keypath)
}
annotationSupported = false
annotationLanguageIds: string[] = []
annotationGetKeys(document: TextDocument) {
return this.parseAST(document.getText())
}
}