-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconfig.ts
118 lines (102 loc) · 3.5 KB
/
config.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import * as vscode from 'vscode'
import { Config, GenerationConfig, TagSuggestionConfig, TagSuggestion, ValueSuggestionConfig } from './types'
let currentConfig: Config = {
tagSuggestion: {},
valueSuggestion: {},
generation: {
template: '',
},
}
let workspaceSettings: vscode.WorkspaceConfiguration
let tagSuggestionChangeCallback: (() => void) | undefined
let valueSuggestionChangeCallback: (() => void) | undefined
function init(): vscode.Disposable {
loadConfig()
return vscode.workspace.onDidChangeConfiguration(() => {
const prevConfig = JSON.parse(JSON.stringify(currentConfig)) as Config
loadConfig()
if (tagSuggestionChangeCallback && JSON.stringify(currentConfig.tagSuggestion) !== JSON.stringify(prevConfig.tagSuggestion)) {
tagSuggestionChangeCallback()
}
if (valueSuggestionChangeCallback && JSON.stringify(currentConfig.valueSuggestion) !== JSON.stringify(prevConfig.valueSuggestion)) {
valueSuggestionChangeCallback()
}
})
}
function loadConfig() {
workspaceSettings = vscode.workspace.getConfiguration('goStructTagAutogen')
var err: string[] = []
const tagSuggestion = workspaceSettings.get<any>('tagSuggestion')
if (tagSuggestion && typeof(tagSuggestion) === 'object') {
Object.keys(tagSuggestion).forEach(k => {
if (!tagSuggestion[k].cases && !tagSuggestion[k].options) {
err.push(k)
return
}
if (tagSuggestion[k].cases && !Array.isArray(tagSuggestion[k].cases)) {
err.push(k)
return
}
if (tagSuggestion[k].options && !Array.isArray(tagSuggestion[k].options)) {
err.push(k)
return
}
currentConfig.tagSuggestion[k] = tagSuggestion[k] as TagSuggestion
})
}
const valueSuggestion = workspaceSettings.get<any>('valueSuggestion')
if (valueSuggestion && typeof(valueSuggestion) === 'object') {
Object.keys(valueSuggestion).forEach(k => {
if (!Array.isArray(valueSuggestion[k])) {
err.push(k)
return
}
const list = (valueSuggestion[k] as any[]).filter(v => typeof(v) === 'string') as string[]
currentConfig.valueSuggestion[k] = list
})
}
const generation = workspaceSettings.get<any>('generation')
if (generation && generation.template && typeof(generation.template) === 'string') {
currentConfig.generation.template = generation.template
} else {
err.push('template')
}
if (err.length > 0) {
vscode.window
.showInformationMessage(`GoStructTagAutogen: Please check the formatting of these configs — ${err.map(e => `"${e}"`).join(',')}`, 'Open Config')
.then(option => {
if (option === 'Open Config') {
vscode.commands.executeCommand('workbench.action.openSettingsJson');
}
})
}
}
function onTagSuggestionConfigChange(callback: () => void) {
tagSuggestionChangeCallback = callback
}
function onValueSuggestionConfigChange(callback: () => void) {
valueSuggestionChangeCallback = callback
}
function getTagSuggestionSupportedTags(): string[] {
return Object.keys(currentConfig.tagSuggestion)
}
function getTagSuggestionConfig(tag: string): (TagSuggestion | undefined) {
const supportedTags = Object.keys(currentConfig.tagSuggestion)
if (!supportedTags.includes(tag)) return undefined
return currentConfig.tagSuggestion[tag]
}
function getValueSuggestionConfig(): (ValueSuggestionConfig) {
return currentConfig.valueSuggestion
}
function getGenerationConfig(): GenerationConfig {
return currentConfig.generation
}
export default {
init,
onTagSuggestionConfigChange,
onValueSuggestionConfigChange,
getTagSuggestionSupportedTags,
getTagSuggestionConfig,
getValueSuggestionConfig,
getGenerationConfig,
}