Skip to content

Commit

Permalink
Modularize root.ts, originally manager.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
James-Yu committed Nov 20, 2023
1 parent a0481ce commit bdc00a8
Show file tree
Hide file tree
Showing 30 changed files with 377 additions and 501 deletions.
18 changes: 9 additions & 9 deletions src/compile/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,10 @@ export class Builder {
return
}
const configuration = vscode.workspace.getConfiguration('latex-workshop', vscode.Uri.file(file))
if (!bibChanged && lw.manager.localRootFile && configuration.get('latex.rootFile.useSubFile')) {
return lw.commands.build(true, lw.manager.localRootFile, lw.manager.rootFileLanguageId)
if (!bibChanged && lw.root.subfiles.path && configuration.get('latex.rootFile.useSubFile')) {
return lw.commands.build(true, lw.root.subfiles.path, lw.root.file.langId)
} else {
return lw.commands.build(true, lw.manager.rootFile, lw.manager.rootFileLanguageId)
return lw.commands.build(true, lw.root.file.path, lw.root.file.langId)
}
}

Expand Down Expand Up @@ -136,7 +136,7 @@ export class Builder {
const workspaceFolder = vscode.workspace.workspaceFolders?.[0]
const cwd = workspaceFolder?.uri.fsPath || pwd
if (rootFile !== undefined) {
args = args.map(replaceArgumentPlaceholders(rootFile, lw.manager.tmpDir))
args = args.map(replaceArgumentPlaceholders(rootFile, lw.file.tmpDirPath))
}
const tool: Tool = { name: command, command, args }

Expand Down Expand Up @@ -193,7 +193,7 @@ export class Builder {
* @returns Whether auto build can be triggered now.
*/
canAutoBuild() {
const configuration = vscode.workspace.getConfiguration('latex-workshop', lw.manager.rootFile ? vscode.Uri.file(lw.manager.rootFile) : undefined)
const configuration = vscode.workspace.getConfiguration('latex-workshop', lw.root.file.path ? vscode.Uri.file(lw.root.file.path) : undefined)
if (Date.now() - this.lastBuild < (configuration.get('latex.autoBuild.interval', 1000) as number)) {
return false
}
Expand Down Expand Up @@ -279,8 +279,8 @@ export class Builder {
}
} else if (!step.isExternal) {
let cwd = path.dirname(step.rootFile)
if (step.command === 'latexmk' && step.rootFile === lw.manager.localRootFile && lw.manager.rootDir) {
cwd = lw.manager.rootDir
if (step.command === 'latexmk' && step.rootFile === lw.root.subfiles.path && lw.root.dir.path) {
cwd = lw.root.dir.path
}
logger.log(`cwd: ${cwd}`)
this.process = cs.spawn(step.command, step.args, {cwd, env})
Expand Down Expand Up @@ -518,10 +518,10 @@ export class Builder {
break
}
}
tool.args = tool.args?.map(replaceArgumentPlaceholders(rootFile, lw.manager.tmpDir))
tool.args = tool.args?.map(replaceArgumentPlaceholders(rootFile, lw.file.tmpDirPath))
const env = tool.env ?? {}
Object.entries(env).forEach(([key, value]) => {
env[key] = value && replaceArgumentPlaceholders(rootFile, lw.manager.tmpDir)(value)
env[key] = value && replaceArgumentPlaceholders(rootFile, lw.file.tmpDirPath)(value)
})
if (configuration.get('latex.option.maxPrintLine.enabled')) {
tool.args = tool.args ?? []
Expand Down
4 changes: 2 additions & 2 deletions src/completion/completer/citation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ export class Citation implements IProvider {
const label = configuration.get('intellisense.citation.label') as string
const fields = readCitationFormat(configuration)
const range: vscode.Range | undefined = computeFilteringRange(line, position)
return this.updateAll(this.getIncludedBibs(lw.manager.rootFile)).map(item => {
return this.updateAll(this.getIncludedBibs(lw.root.file.path)).map(item => {
// Compile the completion item label
switch(label) {
case 'bibtex key':
Expand Down Expand Up @@ -180,7 +180,7 @@ export class Citation implements IProvider {
const configuration = vscode.workspace.getConfiguration('latex-workshop', args?.uri)
const label = configuration.get('intellisense.citation.label') as string
const fields = readCitationFormat(configuration, label)
void vscode.window.showQuickPick(this.updateAll(this.getIncludedBibs(lw.manager.rootFile)).map(item => {
void vscode.window.showQuickPick(this.updateAll(this.getIncludedBibs(lw.root.file.path)).map(item => {
return {
label: item.fields.title ? trimMultiLineString(item.fields.title) : '',
description: item.key,
Expand Down
6 changes: 3 additions & 3 deletions src/completion/completer/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,12 @@ export class Input extends InputAbstract {

getBaseDir(currentFile: string, _importFromDir: string, command: string): string[] {
let baseDir: string[] = []
if (lw.manager.rootDir === undefined) {
logger.log(`No root dir can be found. The current root file should be undefined, is ${lw.manager.rootFile}. How did you get here?`)
if (lw.root.dir.path === undefined) {
logger.log(`No root dir can be found. The current root file should be undefined, is ${lw.root.file.path}. How did you get here?`)
return []
}
// If there is no root, 'root relative' and 'both' should fall back to 'file relative'
const rootDir = lw.manager.rootDir
const rootDir = lw.root.dir.path
if (['includegraphics', 'includesvg'].includes(command) && this.graphicsPath.size > 0) {
baseDir = Array.from(this.graphicsPath).map(dir => path.join(rootDir, dir))
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/completion/completer/reference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,12 @@ export class Reference implements IProvider {
}

private updateAll(line?: string, position?: vscode.Position) {
if (!lw.manager.rootFile) {
if (!lw.root.file.path) {
this.suggestions.clear()
return
}

const included: Set<string> = new Set([lw.manager.rootFile])
const included: Set<string> = new Set([lw.root.file.path])
// Included files may originate from \input or `xr`. If the latter, a
// prefix may be used to ref to the file. The following obj holds them.
const prefixes: {[filePath: string]: string} = {}
Expand Down
14 changes: 7 additions & 7 deletions src/core/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ async function refreshCache(filePath: string, rootPath?: string): Promise<void |
bibfiles: new Set(),
external: {}}
caches.set(filePath, cache)
rootPath = rootPath || lw.manager.rootFile
rootPath = rootPath || lw.root.file.path
updateChildren(cache, rootPath)

promises.set(
Expand Down Expand Up @@ -233,8 +233,8 @@ function refreshCacheAggressive(filePath: string) {
clearTimeout(updateCompleter)
}
updateCompleter = setTimeout(async () => {
await refreshCache(filePath, lw.manager.rootFile)
await loadFlsFile(lw.manager.rootFile || filePath)
await refreshCache(filePath, lw.root.file.path)
await loadFlsFile(lw.root.file.path || filePath)
}, configuration.get('intellisense.update.delay', 1000))
}
}
Expand Down Expand Up @@ -543,8 +543,8 @@ function parseAuxFile(filePath: string, srcDir: string) {
for (const bib of bibs) {
const bibPaths = lw.file.getBibPath(bib, srcDir)
for (const bibPath of bibPaths) {
if (lw.manager.rootFile && !get(lw.manager.rootFile)?.bibfiles.has(bibPath)) {
get(lw.manager.rootFile)?.bibfiles.add(bibPath)
if (lw.root.file.path && !get(lw.root.file.path)?.bibfiles.has(bibPath)) {
get(lw.root.file.path)?.bibfiles.add(bibPath)
logger.log(`Found .bib ${bibPath} from .aux ${filePath} .`)
}
if (!lw.watcher.bib.has(bibPath)) {
Expand All @@ -571,7 +571,7 @@ function parseAuxFile(filePath: string, srcDir: string) {
* @returns {string[]} - An array of included bibliography files.
*/
function getIncludedBib(filePath?: string, includedBib: string[] = []): string[] {
filePath = filePath ?? lw.manager.rootFile
filePath = filePath ?? lw.root.file.path
if (filePath === undefined) {
return []
}
Expand Down Expand Up @@ -610,7 +610,7 @@ function getIncludedBib(filePath?: string, includedBib: string[] = []): string[]
* @returns {string[]} - An array of included LaTeX files.
*/
function getIncludedTeX(filePath?: string, includedTeX: string[] = [], cachedOnly: boolean = true): string[] {
filePath = filePath ?? lw.manager.rootFile
filePath = filePath ?? lw.root.file.path
if (filePath === undefined) {
return []
}
Expand Down
43 changes: 23 additions & 20 deletions src/core/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ export async function build(skipSelection: boolean = false, rootFile: string | u
const externalBuildCommand = configuration.get('latex.external.build.command') as string
const externalBuildArgs = configuration.get('latex.external.build.args') as string[]
if (rootFile === undefined && lw.file.hasTexLangId(vscode.window.activeTextEditor.document.languageId)) {
rootFile = await lw.manager.findRoot()
languageId = lw.manager.rootFileLanguageId
await lw.root.find()
rootFile = lw.root.file.path
languageId = lw.root.file.langId
}
if (externalBuildCommand) {
const pwd = path.dirname(rootFile ? rootFile : vscode.window.activeTextEditor.document.fileName)
Expand All @@ -33,9 +34,9 @@ export async function build(skipSelection: boolean = false, rootFile: string | u
return
}
let pickedRootFile: string | undefined = rootFile
if (!skipSelection && lw.manager.localRootFile) {
if (!skipSelection && lw.root.subfiles.path) {
// We are using the subfile package
pickedRootFile = await quickPickRootFile(rootFile, lw.manager.localRootFile, 'build')
pickedRootFile = await quickPickRootFile(rootFile, lw.root.subfiles.path, 'build')
if (! pickedRootFile) {
return
}
Expand All @@ -48,7 +49,7 @@ export async function revealOutputDir() {
let outDir = lw.file.getOutDir()
if (!path.isAbsolute(outDir)) {
const workspaceFolder = vscode.workspace.workspaceFolders?.[0]
const rootDir = lw.manager.rootDir || workspaceFolder?.uri.fsPath
const rootDir = lw.root.dir.path || workspaceFolder?.uri.fsPath
if (rootDir === undefined) {
logger.log(`Cannot reveal ${vscode.Uri.file(outDir)}: no root dir can be identified.`)
return
Expand All @@ -61,7 +62,7 @@ export async function revealOutputDir() {

export function recipes(recipe?: string) {
logger.log('RECIPES command invoked.')
const configuration = vscode.workspace.getConfiguration('latex-workshop', lw.manager.getWorkspaceFolderRootDir())
const configuration = vscode.workspace.getConfiguration('latex-workshop', lw.root.getWorkspace())
const candidates = configuration.get('latex.recipes') as {name: string}[]
if (!candidates) {
return
Expand Down Expand Up @@ -93,15 +94,16 @@ export async function view(mode?: 'tab' | 'browser' | 'external' | vscode.Uri) {
logger.log('Active document is not a TeX file.')
return
}
const rootFile = await lw.manager.findRoot()
await lw.root.find()
const rootFile = lw.root.file.path
if (rootFile === undefined) {
logger.log('Cannot find LaTeX root PDF to view.')
return
}
let pickedRootFile: string | undefined = rootFile
if (lw.manager.localRootFile) {
if (lw.root.subfiles.path) {
// We are using the subfile package
pickedRootFile = await quickPickRootFile(rootFile, lw.manager.localRootFile, 'view')
pickedRootFile = await quickPickRootFile(rootFile, lw.root.subfiles.path, 'view')
}
if (!pickedRootFile) {
return
Expand All @@ -125,12 +127,12 @@ export function synctex() {
logger.log('Cannot start SyncTeX. The active editor is undefined, or the document is not a TeX document.')
return
}
const configuration = vscode.workspace.getConfiguration('latex-workshop', lw.manager.getWorkspaceFolderRootDir())
const configuration = vscode.workspace.getConfiguration('latex-workshop', lw.root.getWorkspace())
let pdfFile: string | undefined = undefined
if (lw.manager.localRootFile && configuration.get('latex.rootFile.useSubFile')) {
pdfFile = lw.file.getPdfPath(lw.manager.localRootFile)
} else if (lw.manager.rootFile !== undefined) {
pdfFile = lw.file.getPdfPath(lw.manager.rootFile)
if (lw.root.subfiles.path && configuration.get('latex.rootFile.useSubFile')) {
pdfFile = lw.file.getPdfPath(lw.root.subfiles.path)
} else if (lw.root.file.path !== undefined) {
pdfFile = lw.file.getPdfPath(lw.root.file.path)
}
lw.locator.syncTeX(undefined, undefined, pdfFile)
}
Expand All @@ -146,15 +148,16 @@ export function synctexonref(line: number, filePath: string) {

export async function clean(): Promise<void> {
logger.log('CLEAN command invoked.')
const rootFile = await lw.manager.findRoot()
await lw.root.find()
const rootFile = lw.root.file.path
if (rootFile === undefined) {
logger.log('Cannot find LaTeX root file to clean.')
return
}
let pickedRootFile: string | undefined = rootFile
if (lw.manager.localRootFile) {
if (lw.root.subfiles.path) {
// We are using the subfile package
pickedRootFile = await quickPickRootFile(rootFile, lw.manager.localRootFile, 'clean')
pickedRootFile = await quickPickRootFile(rootFile, lw.root.subfiles.path, 'clean')
if (! pickedRootFile) {
return
}
Expand All @@ -178,9 +181,9 @@ export function citation() {
export function wordcount() {
logger.log('WORDCOUNT command invoked.')
if (!vscode.window.activeTextEditor || !lw.file.hasTexLangId(vscode.window.activeTextEditor.document.languageId) ||
lw.manager.rootFile === vscode.window.activeTextEditor.document.fileName) {
if (lw.manager.rootFile) {
lw.counter.count(lw.manager.rootFile)
lw.root.file.path === vscode.window.activeTextEditor.document.fileName) {
if (lw.root.file.path) {
lw.counter.count(lw.root.file.path)
} else {
logger.log('WORDCOUNT: No rootFile defined.')
}
Expand Down
8 changes: 4 additions & 4 deletions src/core/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ function hasDtxLangId(langId: string): boolean {
* @returns {string} - The output directory path.
*/
function getOutDir(texPath?: string): string {
texPath = texPath ?? lw.manager.rootFile
texPath = texPath ?? lw.root.file.path
// rootFile is also undefined
if (texPath === undefined) {
return './'
Expand Down Expand Up @@ -215,7 +215,7 @@ function kpsewhich(args: string[]): string | undefined {
logger.log(`Calling ${command} to resolve ${args.join(' ')} .`)

try {
const kpsewhichReturn = cs.sync(command, args, {cwd: lw.manager.rootDir || vscode.workspace.workspaceFolders?.[0].uri.path})
const kpsewhichReturn = cs.sync(command, args, {cwd: lw.root.dir.path || vscode.workspace.workspaceFolders?.[0].uri.path})
if (kpsewhichReturn.status === 0) {
const output = kpsewhichReturn.stdout.toString().replace(/\r?\n/, '')
return output !== '' ? output : undefined
Expand Down Expand Up @@ -251,8 +251,8 @@ function getBibPath(bib: string, baseDir: string): string[] {
let searchDirs: string[] = [baseDir, ...bibDirs]
// chapterbib requires to load the .bib file in every chapter using
// the path relative to the rootDir
if (lw.manager.rootDir) {
searchDirs = [lw.manager.rootDir, ...searchDirs]
if (lw.root.dir.path) {
searchDirs = [lw.root.dir.path, ...searchDirs]
}
const bibPath = bib.includes('*') ? utils.resolveFileGlob(searchDirs, bib, '.bib') : utils.resolveFile(searchDirs, bib, '.bib')

Expand Down
Loading

0 comments on commit bdc00a8

Please sign in to comment.