diff --git a/src/compile/build.ts b/src/compile/build.ts index db6aee741..a973b964c 100644 --- a/src/compile/build.ts +++ b/src/compile/build.ts @@ -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 diff --git a/src/compile/queue.ts b/src/compile/queue.ts index be8071260..c028dd01f 100644 --- a/src/compile/queue.ts +++ b/src/compile/queue.ts @@ -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 } diff --git a/src/compile/recipe.ts b/src/compile/recipe.ts index f60bcb338..8160a63e9 100644 --- a/src/compile/recipe.ts +++ b/src/compile/recipe.ts @@ -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) diff --git a/src/core/commands.ts b/src/core/commands.ts index 80986f4d0..82e6231bb 100644 --- a/src/core/commands.ts +++ b/src/core/commands.ts @@ -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 } @@ -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 } @@ -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 } @@ -132,7 +132,7 @@ export async function clean(): Promise { 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() @@ -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) @@ -183,7 +183,7 @@ 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() @@ -191,7 +191,7 @@ export function navigateToEnvPair() { 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) @@ -199,7 +199,7 @@ export function selectEnvContent(mode: 'content' | 'whole') { 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') @@ -207,7 +207,7 @@ export function selectEnvName() { 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') @@ -215,7 +215,7 @@ export function multiCursorEnvName() { 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') @@ -223,7 +223,7 @@ export function toggleEquationEnv() { 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() diff --git a/src/core/file.ts b/src/core/file.ts index f2fe7ea6a..a3881c7a7 100644 --- a/src/core/file.ts +++ b/src/core/file.ts @@ -19,9 +19,10 @@ export const file = { getFlsPath, hasBinaryExt, hasTeXExt, - hasTexLangId, + hasTeXLangId, hasBibLangId, hasDtxLangId, + setTeXDirs, exists, read, kpsewhich, @@ -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) } @@ -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 @@ -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('/') } @@ -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 } diff --git a/src/extras/counter.ts b/src/extras/counter.ts index 36a578296..6fbaf84c2 100644 --- a/src/extras/counter.ts +++ b/src/extras/counter.ts @@ -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() diff --git a/src/locate/synctex.ts b/src/locate/synctex.ts index 59dc6fa8c..7135a9876 100644 --- a/src/locate/synctex.ts +++ b/src/locate/synctex.ts @@ -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 } diff --git a/src/main.ts b/src/main.ts index cf484f437..4b27c98b2 100644 --- a/src/main.ts +++ b/src/main.ts @@ -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)}`) @@ -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')) @@ -103,7 +103,7 @@ 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() @@ -111,7 +111,7 @@ export function activate(extensionContext: vscode.ExtensionContext) { 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() @@ -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 @@ -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) @@ -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 } }) diff --git a/src/preview/math-preview-panel.ts b/src/preview/math-preview-panel.ts index 9188e9cd9..393aadbe4 100644 --- a/src/preview/math-preview-panel.ts +++ b/src/preview/math-preview-panel.ts @@ -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 } diff --git a/src/types.ts b/src/types.ts index 21603b076..ce1d09008 100644 --- a/src/types.ts +++ b/src/types.ts @@ -64,9 +64,7 @@ export type Tool = { name: string, command: string, args?: string[], - env?: ProcessEnv, - outdir?: string, - auxdir?: string + env?: ProcessEnv } export type Recipe = {