-
Notifications
You must be signed in to change notification settings - Fork 535
/
package.ts
248 lines (220 loc) · 8.86 KB
/
package.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import * as vscode from 'vscode'
import * as fs from 'fs'
import * as path from 'path'
import type * as Ast from '@unified-latex/unified-latex-types'
import { lw } from '../../lw'
import type { CompletionProvider, FileCache, Package } from '../../types'
import { argContentToStr } from '../../utils/parser'
import { replaceArgumentPlaceholders } from '../../utils/utils'
const logger = lw.log('Intelli', 'Package')
export const provider: CompletionProvider = { from }
export const usepackage = {
parse,
load,
getAll,
getDeps,
getOpts,
setDeps,
setOpts
}
const data = {
loaded: [] as string[],
suggestions: [] as vscode.CompletionItem[],
packageDeps: Object.create(null) as { [packageName: string]: { [key: string]: string[] } },
packageOptions: Object.create(null) as { [packageName: string]: string[] }
}
type PackageItemEntry = {
command: string,
detail: string,
documentation: string
}
function load(packageName: string) {
if (data.loaded.includes(packageName)) {
return
}
const filePath: string | undefined = resolvePackageFile(packageName)
if (filePath === undefined) {
data.loaded.push(packageName)
return
}
try {
const packageData = JSON.parse(fs.readFileSync(filePath).toString()) as Package
populatePackageData(packageData)
setDeps(packageName, packageData.includes)
setOpts(packageName, packageData.options)
lw.completion.environment.setPackageEnvs(packageName, packageData.envs)
lw.completion.macro.setPackageCmds(packageName, packageData.macros)
data.loaded.push(packageName)
} catch (e) {
logger.log(`Cannot parse intellisense file: ${filePath}`)
}
}
function resolvePackageFile(packageName: string): string | undefined {
const defaultDir = `${lw.extensionRoot}/data/packages/`
const rawDirs = vscode.workspace.getConfiguration('latex-workshop').get('intellisense.package.dirs') as string[]
const dirs = rawDirs.map((dir) => {return replaceArgumentPlaceholders('', '')(dir)})
dirs.push(defaultDir)
for (const dir of dirs) {
const filePath = path.resolve(dir, `${packageName}.json`)
if (fs.existsSync(filePath)) {
return filePath
}
}
// Many package with names like toppackage-config.sty are just wrappers around
// the general package toppacke.sty and do not define macros on their own.
const indexDash = packageName.lastIndexOf('-')
if (indexDash > - 1) {
const generalPkg = packageName.substring(0, indexDash)
const filePath = path.resolve(defaultDir, `${generalPkg}.json`)
if (fs.existsSync(filePath)) {
return filePath
}
}
return
}
function populatePackageData(packageData: Package) {
Object.entries(packageData.macros).forEach(([key, cmd]) => {
cmd.macro = key
cmd.snippet = cmd.snippet || key
cmd.keyvals = packageData.keyvals[cmd.keyvalindex ?? -1]
})
Object.entries(packageData.envs).forEach(([key, env]) => {
env.detail = key
env.name = env.name || key
env.snippet = env.snippet || ''
env.keyvals = packageData.keyvals[env.keyvalindex ?? -1]
})
}
function initialize(defaultPackages: {[key: string]: PackageItemEntry}) {
Object.values(defaultPackages).forEach(item => {
const pack = new vscode.CompletionItem(item.command, vscode.CompletionItemKind.Module)
pack.detail = item.detail
pack.documentation = new vscode.MarkdownString(`[${item.documentation}](${item.documentation})`)
data.suggestions.push(pack)
})
}
function from(): vscode.CompletionItem[] {
if (data.suggestions.length === 0) {
const pkgs: {[key: string]: PackageItemEntry} = JSON.parse(fs.readFileSync(`${lw.extensionRoot}/data/packagenames.json`).toString()) as typeof import('../../../data/packagenames.json')
initialize(pkgs)
}
return data.suggestions
}
function setDeps(packageName: string, deps: {[key: string]: string[]}) {
data.packageDeps[packageName] = deps
}
function setOpts(packageName: string, options: string[]) {
data.packageOptions[packageName] = options
}
function getOpts(packageName: string) {
return data.packageOptions[packageName] || []
}
function getDeps(packageName: string): {[key: string]: string[]} {
return data.packageDeps[packageName] || {}
}
function getAll(languageId: string): {[packageName: string]: string[]} {
const packages: {[packageName: string]: string[]} = {}
const config = vscode.workspace.getConfiguration('latex-workshop')
const excluded = config.get('intellisense.package.exclude') as string[]
if (!excluded.includes('lw-default')) {
if (['latex', 'latex-expl3'].includes(languageId)) {
packages['latex-document'] = []
}
if (languageId === 'latex-expl3') {
packages['expl3'] = []
}
}
(config.get('intellisense.package.extra') as string[])
.filter(packageName => !excluded.includes(packageName))
.forEach(packageName => packages[packageName] = [])
lw.cache.getIncludedTeX().forEach(tex => {
const included = lw.cache.get(tex)?.elements.package
if (included === undefined) {
return
}
Object.entries(included)
.filter(([packageName, ]) => !excluded.includes(packageName))
.forEach(([packageName, options]) => packages[packageName] = options)
})
while (true) {
let newPackageInserted = false
Object.entries(packages).forEach(([packageName, options]) => Object.keys(getDeps(packageName))
.filter(includeName => !excluded.includes(includeName))
.forEach(includeName => {
const dependOptions = getDeps(packageName)[includeName]
const hasOption = dependOptions.length === 0
|| options.filter(option => dependOptions.includes(option)).length > 0
if (packages[includeName] === undefined && hasOption) {
packages[includeName] = []
newPackageInserted = true
}
}
))
if (!newPackageInserted) {
break
}
}
return packages
}
function parse(cache: FileCache) {
if (cache.ast !== undefined) {
cache.elements.package = parseAst(cache.ast)
} else {
cache.elements.package = parseContent(cache.content)
}
}
function parseAst(node: Ast.Node): {[pkgName: string]: string[]} {
const packages = {}
if (node.type === 'macro' && ['usepackage', 'documentclass'].includes(node.content)) {
const options: string[] = argContentToStr(node.args?.[0]?.content || [])
.split(',')
.map(arg => arg.trim())
const optionsNoTrue = options
.filter(option => option.includes('=true'))
.map(option => option.replace('=true', ''))
argContentToStr(node.args?.[1]?.content || [])
.split(',')
.map(packageName => toPackageObj(packageName.trim(), [...options, ...optionsNoTrue], node))
.forEach(packageObj => Object.assign(packages, packageObj))
} else if ('content' in node && typeof node.content !== 'string') {
for (const subNode of node.content) {
Object.assign(packages, parseAst(subNode))
}
}
return packages
}
function parseContent(content: string): {[pkgName: string]: string[]} {
const packages = {}
const pkgReg = /\\(?:usepackage|RequirePackage)(\[[^[\]{}]*\])?{(.*?)}/gs
while (true) {
const result = pkgReg.exec(content)
if (result === null) {
break
}
const packageNames = result[2].split(',').map(packageName => packageName.trim())
const options = (result[1] || '[]').slice(1,-1).replace(/\s*=\s*/g,'=').split(',').map(option => option.trim())
const optionsNoTrue = options.filter(option => option.includes('=true')).map(option => option.replace('=true', ''))
packageNames
.map(packageName => toPackageObj(packageName, [...options, ...optionsNoTrue]))
.forEach(packageObj => Object.assign(packages, packageObj))
}
return packages
}
function toPackageObj(packageName: string, options: string[], node?: Ast.Node): {[pkgName: string]: string[]} {
packageName = packageName.trim()
if (packageName === '') {
return {}
}
let pkgObj: {[pkgName: string]: string[]} = {}
if (node?.type === 'macro' && node.content === 'documentclass') {
if (vscode.workspace.getConfiguration('latex-workshop').get('kpsewhich.class.enabled') as boolean) {
const clsPath = lw.file.kpsewhich(`${packageName}.cls`)
if (clsPath && fs.existsSync(clsPath)) {
pkgObj = parseContent(fs.readFileSync(clsPath).toString())
}
}
packageName = 'class-' + packageName
}
pkgObj[packageName] = options
return pkgObj
}