Skip to content

Commit

Permalink
Merge pull request #4466 from James-Yu/4457_extra_ext
Browse files Browse the repository at this point in the history
Handle LaTeX files with non `.tex` extension
  • Loading branch information
jlelong authored Nov 25, 2024
2 parents 40406ff + 88a4bf0 commit f819eea
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 7 deletions.
9 changes: 9 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
23 changes: 17 additions & 6 deletions src/core/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { lw } from '../lw'

const logger = lw.log('File')

let extraTeXExts: string[]
export const file = {
tmpDirPath: '',
getOutDir,
Expand All @@ -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.
*
Expand Down Expand Up @@ -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').
Expand All @@ -97,6 +105,7 @@ function handleTmpDirError(error: Error) {
*/
function hasTeXExt(extname: string): boolean {
return [
...extraTeXExts,
...lw.constant.TEX_EXT,
...lw.constant.RSWEAVE_EXT,
...lw.constant.JLWEAVE_EXT,
Expand All @@ -111,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').
Expand All @@ -122,6 +132,7 @@ function hasTeXExt(extname: string): boolean {
*/
function hasBinaryExt(extname: string): boolean {
return ![
...extraTeXExts,
...lw.constant.TEX_EXT,
...lw.constant.TEX_NOCACHE_EXT,
...lw.constant.RSWEAVE_EXT,
Expand Down Expand Up @@ -265,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'
Expand Down
2 changes: 1 addition & 1 deletion src/lw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,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
Expand Down
44 changes: 44 additions & 0 deletions test/units/01_core_file.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -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', () => {
Expand Down

0 comments on commit f819eea

Please sign in to comment.