Skip to content

Commit

Permalink
Fix #4233 Cache outdir/auxdir per root file
Browse files Browse the repository at this point in the history
  • Loading branch information
James-Yu committed Apr 15, 2024
1 parent 729fe8a commit eca259d
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 32 deletions.
2 changes: 1 addition & 1 deletion src/compile/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ async function build(skipSelection: boolean = false, rootFile: string | undefine
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(activeEditor.document.languageId)) {
if (rootFile === undefined && lw.file.hasTeXLangId(activeEditor.document.languageId)) {
await lw.root.find()
rootFile = lw.root.file.path
languageId = lw.root.file.langId
Expand Down
2 changes: 1 addition & 1 deletion src/compile/queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ function clear() {
* @param {Step} step - The Step to check.
* @returns {boolean} - True if the step is the last one; otherwise, false.
*/
function isLastStep(step: Step) {
function isLastStep(step: Step): boolean {
return stepQueue.steps.length === 0 || stepQueue.steps[0].timestamp !== step.timestamp
}

Expand Down
7 changes: 5 additions & 2 deletions src/compile/recipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,8 +330,11 @@ function populateTools(rootFile: string, buildTools: Tool[]): Tool[] {
}
}
tool.args = tool.args?.map(replaceArgumentPlaceholders(rootFile, lw.file.tmpDirPath))
tool.outdir = tool.args?.filter(arg => arg.startsWith('-out-directory') || arg.startsWith('-outdir'))[0]?.replace(/^-out-directory=|^-outdir=/, '')
tool.auxdir = tool.args?.filter(arg => arg.startsWith('-aux-directory') || arg.startsWith('-auxdir'))[0]?.replace(/^-aux-directory=|^-auxdir=/, '')
lw.file.setTeXDirs(
rootFile,
tool.args?.filter(arg => arg.startsWith('-out-directory') || arg.startsWith('-outdir'))[0]?.replace(/^-out-directory=|^-outdir=/, ''),
tool.args?.filter(arg => arg.startsWith('-aux-directory') || arg.startsWith('-auxdir'))[0]?.replace(/^-aux-directory=|^-auxdir=/, '')
)
const env = tool.env ?? {}
Object.entries(env).forEach(([key, value]) => {
env[key] = value && replaceArgumentPlaceholders(rootFile, lw.file.tmpDirPath)(value)
Expand Down
22 changes: 11 additions & 11 deletions src/core/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export async function view(mode?: 'tab' | 'browser' | 'external' | vscode.Uri) {
logger.log('Cannot find active TextEditor.')
return
}
if (!lw.file.hasTexLangId(vscode.window.activeTextEditor.document.languageId)) {
if (!lw.file.hasTeXLangId(vscode.window.activeTextEditor.document.languageId)) {
logger.log('Active document is not a TeX file.')
return
}
Expand Down Expand Up @@ -88,7 +88,7 @@ export function kill() {

export function synctex() {
logger.log('SYNCTEX command invoked.')
if (!vscode.window.activeTextEditor || !lw.file.hasTexLangId(vscode.window.activeTextEditor.document.languageId)) {
if (!vscode.window.activeTextEditor || !lw.file.hasTeXLangId(vscode.window.activeTextEditor.document.languageId)) {
logger.log('Cannot start SyncTeX. The active editor is undefined, or the document is not a TeX document.')
return
}
Expand All @@ -104,7 +104,7 @@ export function synctex() {

export function synctexonref(line: number, filePath: string) {
logger.log('SYNCTEX command invoked on a reference.')
if (!vscode.window.activeTextEditor || !lw.file.hasTexLangId(vscode.window.activeTextEditor.document.languageId)) {
if (!vscode.window.activeTextEditor || !lw.file.hasTeXLangId(vscode.window.activeTextEditor.document.languageId)) {
logger.log('Cannot start SyncTeX. The active editor is undefined, or the document is not a TeX document.')
return
}
Expand Down Expand Up @@ -132,7 +132,7 @@ export async function clean(): Promise<void> {

export function addTexRoot() {
logger.log('ADDTEXROOT command invoked.')
if (!vscode.window.activeTextEditor || !lw.file.hasTexLangId(vscode.window.activeTextEditor.document.languageId)) {
if (!vscode.window.activeTextEditor || !lw.file.hasTeXLangId(vscode.window.activeTextEditor.document.languageId)) {
return
}
lw.extra.texroot()
Expand All @@ -145,7 +145,7 @@ export function citation() {

export function wordcount() {
logger.log('WORDCOUNT command invoked.')
if (!vscode.window.activeTextEditor || !lw.file.hasTexLangId(vscode.window.activeTextEditor.document.languageId) ||
if (!vscode.window.activeTextEditor || !lw.file.hasTeXLangId(vscode.window.activeTextEditor.document.languageId) ||
lw.root.file.path === vscode.window.activeTextEditor.document.fileName) {
if (lw.root.file.path) {
lw.extra.count(lw.root.file.path, true, true)
Expand Down Expand Up @@ -183,47 +183,47 @@ export async function gotoSection(filePath: string, lineNumber: number) {

export function navigateToEnvPair() {
logger.log('JumpToEnvPair command invoked.')
if (!vscode.window.activeTextEditor || !lw.file.hasTexLangId(vscode.window.activeTextEditor.document.languageId)) {
if (!vscode.window.activeTextEditor || !lw.file.hasTeXLangId(vscode.window.activeTextEditor.document.languageId)) {
return
}
void lw.locate.pair.goto()
}

export function selectEnvContent(mode: 'content' | 'whole') {
logger.log('SelectEnv command invoked.')
if (!vscode.window.activeTextEditor || !lw.file.hasTexLangId(vscode.window.activeTextEditor.document.languageId)) {
if (!vscode.window.activeTextEditor || !lw.file.hasTeXLangId(vscode.window.activeTextEditor.document.languageId)) {
return
}
void lw.locate.pair.select(mode)
}

export function selectEnvName() {
logger.log('SelectEnvName command invoked.')
if (!vscode.window.activeTextEditor || !lw.file.hasTexLangId(vscode.window.activeTextEditor.document.languageId)) {
if (!vscode.window.activeTextEditor || !lw.file.hasTeXLangId(vscode.window.activeTextEditor.document.languageId)) {
return
}
void lw.locate.pair.name('selection')
}

export function multiCursorEnvName() {
logger.log('MutliCursorEnvName command invoked.')
if (!vscode.window.activeTextEditor || !lw.file.hasTexLangId(vscode.window.activeTextEditor.document.languageId)) {
if (!vscode.window.activeTextEditor || !lw.file.hasTeXLangId(vscode.window.activeTextEditor.document.languageId)) {
return
}
void lw.locate.pair.name('cursor')
}

export function toggleEquationEnv() {
logger.log('toggleEquationEnv command invoked.')
if (!vscode.window.activeTextEditor || !lw.file.hasTexLangId(vscode.window.activeTextEditor.document.languageId)) {
if (!vscode.window.activeTextEditor || !lw.file.hasTeXLangId(vscode.window.activeTextEditor.document.languageId)) {
return
}
void lw.locate.pair.name('equationToggle')
}

export function closeEnv() {
logger.log('CloseEnv command invoked.')
if (!vscode.window.activeTextEditor || !lw.file.hasTexLangId(vscode.window.activeTextEditor.document.languageId)) {
if (!vscode.window.activeTextEditor || !lw.file.hasTeXLangId(vscode.window.activeTextEditor.document.languageId)) {
return
}
void lw.locate.pair.close()
Expand Down
23 changes: 19 additions & 4 deletions src/core/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ export const file = {
getFlsPath,
hasBinaryExt,
hasTeXExt,
hasTexLangId,
hasTeXLangId,
hasBibLangId,
hasDtxLangId,
setTeXDirs,
exists,
read,
kpsewhich,
Expand Down Expand Up @@ -97,7 +98,7 @@ function hasBinaryExt(extname: string): boolean {
* @param {string} langId - The language identifier.
* @returns {boolean} - Indicates whether the language is supported.
*/
function hasTexLangId(langId: string): boolean {
function hasTeXLangId(langId: string): boolean {
return ['tex', 'latex', 'latex-expl3', 'doctex', 'pweave', 'jlweave', 'rsweave'].includes(langId)
}

Expand All @@ -121,6 +122,20 @@ function hasDtxLangId(langId: string): boolean {
return langId === 'doctex'
}

const texDirs: {[tex: string]: {out?: string, aux?: string}} = {}
/**
* Set the output and aux-files directory for a root file.
* @param tex - The path to a root TeX file.
* @param out - The corresponding outdir path.
* @param aux - The corresponding auxdir path.
*/
function setTeXDirs(tex: string, out?: string, aux?: string) {
if (!tex.endsWith('.tex')) {
tex += '.tex'
}
texDirs[tex] = {out, aux}
}

/**
* Returns the output directory developed according to the input tex path and
* 'latex.outDir' config. If `texPath` is `undefined`, the default root file is
Expand All @@ -141,7 +156,7 @@ function getOutDir(texPath?: string): string {
const outDir = configuration.get('latex.outDir') as string
const out = utils.replaceArgumentPlaceholders(texPath, file.tmpDirPath)(outDir)
if (outDir === '%DIR%' || outDir === '%DIR_W32%') {
return lw.compile.lastSteps.filter(step => step.outdir).slice(-1)[0]?.outdir ?? path.normalize(out).split(path.sep).join('/')
return texDirs[texPath]?.out ?? path.normalize(out).split(path.sep).join('/')
}
return path.normalize(out).split(path.sep).join('/')
}
Expand Down Expand Up @@ -206,7 +221,7 @@ function getFlsPath(texPath: string): string | undefined {
if (fs.existsSync(flsFile)) {
return flsFile
}
flsFile = path.resolve(rootDir, lw.compile.lastSteps.filter(step => step.auxdir).slice(-1)[0]?.auxdir ?? '', fileName)
flsFile = path.resolve(rootDir, texDirs[texPath]?.aux ?? '', fileName)
return fs.existsSync(flsFile) ? flsFile : undefined
}

Expand Down
2 changes: 1 addition & 1 deletion src/extras/counter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ loadConfigs()

lw.onConfigChange(['texcount', 'docker.enabled'], loadConfigs)
lw.onDispose(vscode.window.onDidChangeActiveTextEditor((e: vscode.TextEditor | undefined) => {
if (e && lw.file.hasTexLangId(e.document.languageId)) {
if (e && lw.file.hasTeXLangId(e.document.languageId)) {
loadConfigs(e.document.uri)
} else {
state.statusBar.hide()
Expand Down
2 changes: 1 addition & 1 deletion src/locate/synctex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ function toPDF(args?: {line: number, filePath: string}, forcedViewer: 'auto' | '

if (args === undefined) {
filePath = vscode.window.activeTextEditor.document.uri.fsPath
if (!lw.file.hasTexLangId(vscode.window.activeTextEditor.document.languageId)) {
if (!lw.file.hasTeXLangId(vscode.window.activeTextEditor.document.languageId)) {
logger.log(`${filePath} is not valid LaTeX.`)
return
}
Expand Down
14 changes: 7 additions & 7 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export function activate(extensionContext: vscode.ExtensionContext) {
if (e.uri.scheme !== 'file'){
return
}
if (lw.file.hasTexLangId(e.languageId) ||
if (lw.file.hasTeXLangId(e.languageId) ||
lw.cache.getIncludedTeX(lw.root.file.path, [], false).includes(e.fileName) ||
lw.cache.getIncludedBib().includes(e.fileName)) {
logger.log(`onDidSaveTextDocument triggered: ${e.uri.toString(true)}`)
Expand All @@ -91,7 +91,7 @@ export function activate(extensionContext: vscode.ExtensionContext) {
extensionContext.subscriptions.push(vscode.window.onDidChangeActiveTextEditor(async (e: vscode.TextEditor | undefined) => {
const configuration = vscode.workspace.getConfiguration('latex-workshop')

if (vscode.window.visibleTextEditors.filter(editor => lw.file.hasTexLangId(editor.document.languageId)).length > 0) {
if (vscode.window.visibleTextEditors.filter(editor => lw.file.hasTeXLangId(editor.document.languageId)).length > 0) {
logger.showStatus()
if (configuration.get('view.autoFocus.enabled') && !isLaTeXActive) {
void vscode.commands.executeCommand('workbench.view.lw.latex-workshop-activitybar').then(() => vscode.commands.executeCommand('workbench.action.focusActiveEditorGroup'))
Expand All @@ -103,15 +103,15 @@ export function activate(extensionContext: vscode.ExtensionContext) {
if (e && e.document.uri.scheme !== 'file'){
return
}
if (e && lw.file.hasTexLangId(e.document.languageId) && e.document.fileName !== prevTeXDocumentPath) {
if (e && lw.file.hasTeXLangId(e.document.languageId) && e.document.fileName !== prevTeXDocumentPath) {
prevTeXDocumentPath = e.document.fileName
await lw.root.find()
lw.lint.latex.root()
} else if (!e || !lw.file.hasBibLangId(e.document.languageId)) {
isLaTeXActive = false
}
if (e && (
lw.file.hasTexLangId(e.document.languageId)
lw.file.hasTeXLangId(e.document.languageId)
|| lw.file.hasBibLangId(e.document.languageId)
|| lw.file.hasDtxLangId(e.document.languageId))) {
void lw.outline.refresh()
Expand All @@ -122,7 +122,7 @@ export function activate(extensionContext: vscode.ExtensionContext) {
if (e.document.uri.scheme !== 'file'){
return
}
if (!lw.file.hasTexLangId(e.document.languageId) &&
if (!lw.file.hasTeXLangId(e.document.languageId) &&
!lw.file.hasBibLangId(e.document.languageId) &&
!lw.file.hasDtxLangId(e.document.languageId)) {
return
Expand All @@ -133,7 +133,7 @@ export function activate(extensionContext: vscode.ExtensionContext) {
}))

extensionContext.subscriptions.push(vscode.window.onDidChangeTextEditorSelection((e: vscode.TextEditorSelectionChangeEvent) => {
if (lw.file.hasTexLangId(e.textEditor.document.languageId) ||
if (lw.file.hasTeXLangId(e.textEditor.document.languageId) ||
lw.file.hasBibLangId(e.textEditor.document.languageId) ||
lw.file.hasDtxLangId(e.textEditor.document.languageId)) {
return lw.outline.reveal(e)
Expand All @@ -145,7 +145,7 @@ export function activate(extensionContext: vscode.ExtensionContext) {

void lw.root.find().then(() => {
lw.lint.latex.root()
if (lw.file.hasTexLangId(vscode.window.activeTextEditor?.document.languageId ?? '')) {
if (lw.file.hasTeXLangId(vscode.window.activeTextEditor?.document.languageId ?? '')) {
prevTeXDocumentPath = vscode.window.activeTextEditor?.document.fileName
}
})
Expand Down
2 changes: 1 addition & 1 deletion src/preview/math-preview-panel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ async function update(ev?: UpdateEvent) {
}
const editor = vscode.window.activeTextEditor
const document = editor?.document
if (!editor || !document?.languageId || !lw.file.hasTexLangId(document.languageId)) {
if (!editor || !document?.languageId || !lw.file.hasTeXLangId(document.languageId)) {
clearCache()
return
}
Expand Down
4 changes: 1 addition & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,7 @@ export type Tool = {
name: string,
command: string,
args?: string[],
env?: ProcessEnv,
outdir?: string,
auxdir?: string
env?: ProcessEnv
}

export type Recipe = {
Expand Down

0 comments on commit eca259d

Please sign in to comment.