From 5c8216566e83759c19a2d35f9cb6cda9d0bdc4e1 Mon Sep 17 00:00:00 2001 From: Jerome Lelong Date: Thu, 21 Nov 2024 12:10:39 +0100 Subject: [PATCH 1/4] Add new config latex-workshop.latex.extraExts This variable lists all the file extensions that LW will consider as LaTeX files. --- package.json | 9 +++++++++ src/core/file.ts | 2 ++ src/lw.ts | 3 ++- 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 654bec5ed..18f1350c5 100644 --- a/package.json +++ b/package.json @@ -1100,6 +1100,15 @@ "default": "%DIR%", "markdownDescription": "The directory where the extension tries to find project files (e.g., PDF and SyncTeX files) are located. Both relative and absolute paths are supported. Relative path start from the root file location, so beware if it is located in sub-directory. The path must not contain a trailing slash. The LaTeX toolchain should output files to this path. For a list of supported placeholders, please visit https://github.com/James-Yu/LaTeX-Workshop/wiki/Compile#placeholders. Note that if this config is set to %DIR% (default value) or %DIR_W32%, the extension will try to parse the last LaTeX tools used and look for `-out-directory=` and `-outdir=`, and automatically determine the output directory. This means that you can safely ignore this config if you use `latexmk` and do not manually `mv` the output files in your recipe." }, + "latex-workshop.latex.extraExts": { + "scope": "resource", + "type": "array", + "items": { + "type": "string" + }, + "default": [], + "markdownDescription": "The list of extra file extensions to be considered as LaTeX files. The extension will parse these files for references and to provide intellisense." + }, "latex-workshop.latex.jobname": { "scope": "resource", "type": "string", diff --git a/src/core/file.ts b/src/core/file.ts index 04b9ff892..dd0834efd 100644 --- a/src/core/file.ts +++ b/src/core/file.ts @@ -98,6 +98,7 @@ function handleTmpDirError(error: Error) { function hasTeXExt(extname: string): boolean { return [ ...lw.constant.TEX_EXT, + ...lw.constant.EXTRA_TEX_EXT, ...lw.constant.RSWEAVE_EXT, ...lw.constant.JLWEAVE_EXT, ...lw.constant.PWEAVE_EXT @@ -123,6 +124,7 @@ function hasTeXExt(extname: string): boolean { function hasBinaryExt(extname: string): boolean { return ![ ...lw.constant.TEX_EXT, + ...lw.constant.EXTRA_TEX_EXT, ...lw.constant.TEX_NOCACHE_EXT, ...lw.constant.RSWEAVE_EXT, ...lw.constant.JLWEAVE_EXT, diff --git a/src/lw.ts b/src/lw.ts index 83dde85ea..cde5d0c4a 100644 --- a/src/lw.ts +++ b/src/lw.ts @@ -60,6 +60,7 @@ export const lw = { const constant = { TEX_EXT: ['.tex', '.bib'], + EXTRA_TEX_EXT: vscode.workspace.getConfiguration('latex-workshop').get('latex.extraExts', []) as string[], TEX_NOCACHE_EXT: ['.cls', '.sty', '.bst', '.bbx', '.cbx', '.def', '.cfg'], RSWEAVE_EXT: ['.rnw', '.Rnw', '.rtex', '.Rtex', '.snw', '.Snw'], JLWEAVE_EXT: ['.jnw', '.jtexw'], @@ -69,7 +70,7 @@ const constant = { MAGIC_PROGRAM_ARGS_SUFFIX: '_WITH_ARGS', MAX_PRINT_LINE: '10000', /** - * Prefix that server.ts uses to distiguish requests on pdf files from + * Prefix that server.ts uses to distinguish requests on pdf files from * others. We use '.' because it is not converted by encodeURIComponent and * other functions. * See https://stackoverflow.com/questions/695438/safe-characters-for-friendly-url From b06e909e1445cccd729c2df34615f2edcde3dffc Mon Sep 17 00:00:00 2001 From: Jerome Lelong Date: Sat, 23 Nov 2024 07:31:26 +0100 Subject: [PATCH 2/4] Refactor latex.extraExts config to allow live changes --- src/core/file.ts | 23 ++++++++++++++++------- src/lw.ts | 1 - 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/core/file.ts b/src/core/file.ts index dd0834efd..a8cfe79d9 100644 --- a/src/core/file.ts +++ b/src/core/file.ts @@ -7,6 +7,7 @@ import { lw } from '../lw' const logger = lw.log('File') +let extraTeXExts: string[] export const file = { tmpDirPath: '', getOutDir, @@ -31,6 +32,12 @@ export function initialize() { file.tmpDirPath = createTmpDir() } +setExtraTeXExts() +lw.onConfigChange('latex.extraExts', setExtraTeXExts) +function setExtraTeXExts() { + extraTeXExts = vscode.workspace.getConfiguration('latex-workshop').get('latex.extraExts', []) as string[] +} + /** * Creates a temporary directory and returns its path as a string. * @@ -87,8 +94,9 @@ function handleTmpDirError(error: Error) { * This function verifies whether a provided file extension string matches any * of the TeX-related extensions defined in several constant arrays. It * consolidates these arrays into a single collection and checks if the given - * extension exists within this collection. The arrays include TeX extensions, R - * Sweave extensions, Julia Weave extensions, and Python Weave extensions. + * extension exists within this collection. The arrays include TeX extensions + * (including those defined by the user ), R Sweave extensions, Julia Weave extensions, + * and Python Weave extensions. * * @param {string} extname - The file extension to be checked including the dot * (e.g., '.tex'). @@ -97,8 +105,8 @@ function handleTmpDirError(error: Error) { */ function hasTeXExt(extname: string): boolean { return [ + ...extraTeXExts, ...lw.constant.TEX_EXT, - ...lw.constant.EXTRA_TEX_EXT, ...lw.constant.RSWEAVE_EXT, ...lw.constant.JLWEAVE_EXT, ...lw.constant.PWEAVE_EXT @@ -112,9 +120,10 @@ function hasTeXExt(extname: string): boolean { * This function evaluates the given file extension and checks it against a * predefined list of TeX source extensions such as `.tex`, `.ltx`, `.sty`, * `.cls`, `.fd`, `.aux`, `.bbl`, `.blg`, `.brf`, `.log`, `.out`, and R Sweave - * extensions, Julia Weave extensions, and Python Weave extensions. It returns - * `true` if the extension is not found in this list, and `false` otherwise. - * This is useful for filtering out non-TeX files from a collection of files. + * extensions, Julia Weave extensions, Python Weave extensions and user defined + * tex extensions. It returns `true` if the extension is not found in this list, + * and `false` otherwise. This is useful for filtering out non-TeX files from a + * collection of files. * * @param {string} extname - The file extension to be checked including the dot * (e.g., '.tex'). @@ -123,8 +132,8 @@ function hasTeXExt(extname: string): boolean { */ function hasBinaryExt(extname: string): boolean { return ![ + ...extraTeXExts, ...lw.constant.TEX_EXT, - ...lw.constant.EXTRA_TEX_EXT, ...lw.constant.TEX_NOCACHE_EXT, ...lw.constant.RSWEAVE_EXT, ...lw.constant.JLWEAVE_EXT, diff --git a/src/lw.ts b/src/lw.ts index cde5d0c4a..9f1e73700 100644 --- a/src/lw.ts +++ b/src/lw.ts @@ -60,7 +60,6 @@ export const lw = { const constant = { TEX_EXT: ['.tex', '.bib'], - EXTRA_TEX_EXT: vscode.workspace.getConfiguration('latex-workshop').get('latex.extraExts', []) as string[], TEX_NOCACHE_EXT: ['.cls', '.sty', '.bst', '.bbx', '.cbx', '.def', '.cfg'], RSWEAVE_EXT: ['.rnw', '.Rnw', '.rtex', '.Rtex', '.snw', '.Snw'], JLWEAVE_EXT: ['.jnw', '.jtexw'], From f2f7b567a597d152504001c59f7ace1c8669ee36 Mon Sep 17 00:00:00 2001 From: James Yu Date: Sat, 23 Nov 2024 19:53:13 +0800 Subject: [PATCH 3/4] Add tests to `latex.extraExts` config item --- test/units/01_core_file.test.ts | 44 +++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/test/units/01_core_file.test.ts b/test/units/01_core_file.test.ts index 95f9662dd..bb336ea5b 100644 --- a/test/units/01_core_file.test.ts +++ b/test/units/01_core_file.test.ts @@ -395,6 +395,28 @@ describe(path.basename(__filename).split('.')[0] + ':', () => { assert.ok(!lw.file.hasTeXExt('.sty')) assert.ok(!lw.file.hasTeXExt('.txt')) }) + + it('should return true for extensions defined in `latex.extraExts`', async () => { + await set.codeConfig('latex.extraExts', ['.txt']) + assert.ok(lw.file.hasTeXExt('.txt')) + }) + + it('should respond to changes to `latex.extraExts` on-the-fly', async () => { + assert.ok(!lw.file.hasTeXExt('.txt')) + assert.ok(!lw.file.hasTeXExt('.md')) + + await set.codeConfig('latex.extraExts', ['.txt']) + assert.ok(lw.file.hasTeXExt('.txt')) + assert.ok(!lw.file.hasTeXExt('.md')) + + await set.codeConfig('latex.extraExts', ['.txt', '.md']) + assert.ok(lw.file.hasTeXExt('.txt')) + assert.ok(lw.file.hasTeXExt('.md')) + + await set.codeConfig('latex.extraExts', []) + assert.ok(!lw.file.hasTeXExt('.txt')) + assert.ok(!lw.file.hasTeXExt('.md')) + }) }) describe('lw.file.hasBinaryExt', () => { @@ -411,6 +433,28 @@ describe(path.basename(__filename).split('.')[0] + ':', () => { assert.ok(!lw.file.hasBinaryExt('.jnw')) assert.ok(!lw.file.hasBinaryExt('.pnw')) }) + + it('should return false for extensions defined in `latex.extraExts`', async () => { + await set.codeConfig('latex.extraExts', ['.txt']) + assert.ok(!lw.file.hasBinaryExt('.txt')) + }) + + it('should respond to changes to `latex.extraExts` on-the-fly', async () => { + assert.ok(lw.file.hasBinaryExt('.txt')) + assert.ok(lw.file.hasBinaryExt('.md')) + + await set.codeConfig('latex.extraExts', ['.txt']) + assert.ok(!lw.file.hasBinaryExt('.txt')) + assert.ok(lw.file.hasBinaryExt('.md')) + + await set.codeConfig('latex.extraExts', ['.txt', '.md']) + assert.ok(!lw.file.hasBinaryExt('.txt')) + assert.ok(!lw.file.hasBinaryExt('.md')) + + await set.codeConfig('latex.extraExts', []) + assert.ok(lw.file.hasBinaryExt('.txt')) + assert.ok(lw.file.hasBinaryExt('.md')) + }) }) describe('lw.file.hasTeXLangId', () => { From 88a4bf056fa2d7fdcea07d8c778a4a4a1ee0b2ef Mon Sep 17 00:00:00 2001 From: Jerome Lelong Date: Sat, 23 Nov 2024 17:29:08 +0100 Subject: [PATCH 4/4] Associate extension in extraExts to latex langId This is required to enable the recipe mechanism --- src/core/file.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/file.ts b/src/core/file.ts index a8cfe79d9..e6931f366 100644 --- a/src/core/file.ts +++ b/src/core/file.ts @@ -276,7 +276,7 @@ function getOutDir(texPath?: string): string { */ function getLangId(filename: string): string | undefined { const ext = path.extname(filename).toLocaleLowerCase() - if (ext === '.tex') { + if (ext === '.tex' || extraTeXExts.includes(ext)) { return 'latex' } else if (lw.constant.PWEAVE_EXT.includes(ext)) { return 'pweave'