diff --git a/src/compile/build.ts b/src/compile/build.ts index 4d31e37c4..48c07f577 100644 --- a/src/compile/build.ts +++ b/src/compile/build.ts @@ -2,8 +2,6 @@ import * as vscode from 'vscode' import * as path from 'path' import * as cs from 'cross-spawn' import { pickRootPath } from '../utils/quick-pick' -import { parser } from '../parse/parser' - import { lw } from '../lw' import type { ProcessEnv, RecipeStep, Step } from '../types' import { build as buildRecipe } from './recipe' @@ -261,7 +259,7 @@ async function monitorProcess(step: Step, env: ProcessEnv): Promise { }) lw.compile.process.on('exit', (code, signal) => { - const isSkipped = parser.parseLog(stdout, step.rootFile) + const isSkipped = lw.parse.log(stdout, step.rootFile) if (!step.isExternal) { step.isSkipped = isSkipped } diff --git a/src/completion/completer/citation.ts b/src/completion/completer/citation.ts index a4891ce37..a94cd30c5 100644 --- a/src/completion/completer/citation.ts +++ b/src/completion/completer/citation.ts @@ -8,8 +8,6 @@ import {trimMultiLineString} from '../../utils/utils' import {computeFilteringRange} from './completerutils' import type { IProvider, ICompletionItem, IProviderArgs } from '../latex' -import { parser } from '../../parse/parser' - const logger = lw.log('Intelli', 'Citation') class Fields extends Map { @@ -305,7 +303,7 @@ export class Citation implements IProvider { const newEntry: CiteSuggestion[] = [] const bibtex = fs.readFileSync(fileName).toString() logger.log(`Parse BibTeX AST from ${fileName} .`) - const ast = await parser.parseBibTeX(bibtex) + const ast = await lw.parse.bib(bibtex) if (ast === undefined) { logger.log(`Parsed 0 bib entries from ${fileName}.`) lw.event.fire(lw.event.FileParsed, fileName) diff --git a/src/core/cache.ts b/src/core/cache.ts index f1ab4c273..173d23a4f 100644 --- a/src/core/cache.ts +++ b/src/core/cache.ts @@ -10,7 +10,6 @@ import type { FileCache } from '../types' import * as utils from '../utils/utils' import { InputFileRegExp } from '../utils/inputfilepath' -import { parser } from '../parse/parser' const logger = lw.log('Cacher') @@ -248,7 +247,7 @@ function refreshCacheAggressive(filePath: string) { */ async function updateAST(fileCache: FileCache) { logger.log(`Parse LaTeX AST: ${fileCache.filePath} .`) - fileCache.ast = await parser.parseLaTeX(fileCache.content) + fileCache.ast = await lw.parse.tex(fileCache.content) logger.log(`Parsed LaTeX AST: ${fileCache.filePath} .`) } diff --git a/src/core/commands.ts b/src/core/commands.ts index 415cd8af4..06198df7b 100644 --- a/src/core/commands.ts +++ b/src/core/commands.ts @@ -3,8 +3,6 @@ import * as path from 'path' import { lw } from '../lw' import { getSurroundingCommandRange, stripText } from '../utils/utils' -import { parser } from '../parse/parser' - const logger = lw.log('Commander') export async function build(skipSelection: boolean = false, rootFile: string | undefined = undefined, languageId: string | undefined = undefined, recipe: string | undefined = undefined) { @@ -412,14 +410,14 @@ export function devParseLog() { if (vscode.window.activeTextEditor === undefined) { return } - parser.parseLog(vscode.window.activeTextEditor.document.getText()) + lw.parse.log(vscode.window.activeTextEditor.document.getText()) } export async function devParseTeX() { if (vscode.window.activeTextEditor === undefined) { return } - const ast = await parser.parseLaTeX(vscode.window.activeTextEditor.document.getText()) + const ast = await lw.parse.tex(vscode.window.activeTextEditor.document.getText()) return vscode.workspace.openTextDocument({content: JSON.stringify(ast, null, 2), language: 'json'}).then(doc => vscode.window.showTextDocument(doc)) } @@ -427,7 +425,7 @@ export async function devParseBib() { if (vscode.window.activeTextEditor === undefined) { return } - const ast = await parser.parseBibTeX(vscode.window.activeTextEditor.document.getText()) + const ast = await lw.parse.bib(vscode.window.activeTextEditor.document.getText()) return vscode.workspace.openTextDocument({content: JSON.stringify(ast, null, 2), language: 'json'}).then(doc => vscode.window.showTextDocument(doc)) } diff --git a/src/lint/bibtex-formatter.ts b/src/lint/bibtex-formatter.ts index 8772c33c3..81f95948a 100644 --- a/src/lint/bibtex-formatter.ts +++ b/src/lint/bibtex-formatter.ts @@ -5,8 +5,6 @@ import { lw } from '../lw' import { bibtexFormat, bibtexSort, getBibtexFormatConfig } from './bibtex-formatter/utils' import type { BibtexEntry } from './bibtex-formatter/utils' -import { parser } from '../parse/parser' - const logger = lw.log('Format', 'Bib') export { @@ -60,7 +58,7 @@ async function formatDocument(document: vscode.TextDocument, sort: boolean, alig const columnOffset = range ? range.start.character : 0 logger.log('Parse active BibTeX document for AST.') - const ast = await parser.parseBibTeX(document.getText(range)) + const ast = await lw.parse.bib(document.getText(range)) if (ast === undefined) { return [] } diff --git a/src/locate/pair.ts b/src/locate/pair.ts index bb920092d..173b5561b 100644 --- a/src/locate/pair.ts +++ b/src/locate/pair.ts @@ -3,7 +3,6 @@ import * as vscode from 'vscode' import type * as Ast from '@unified-latex/unified-latex-types' import { lw } from '../lw' import { argContentToStr } from '../utils/parser' -import { parser } from '../parse/parser' const logger = lw.log('EnvPair') @@ -82,7 +81,7 @@ class CommandPair { * document. */ async function build(document: vscode.TextDocument): Promise { - const ast = await parser.parseLaTeX(document.getText()) + const ast = await lw.parse.tex(document.getText()) if (!ast) { logger.log('Error parsing current document as AST.') return [] diff --git a/src/lw.ts b/src/lw.ts index f45976c04..e6bd362ad 100644 --- a/src/lw.ts +++ b/src/lw.ts @@ -10,6 +10,7 @@ import type { server, viewer } from './preview' import type { locate } from './locate' import type { lint } from './lint' import type { outline } from './outline' +import type { parse } from './parse' import type { Cleaner } from './extras/cleaner' import type { LaTeXCommanderTreeView } from './extras/activity-bar' @@ -34,6 +35,7 @@ export const lw = { watcher: {} as typeof watcher, cache: {} as typeof cache, root: {} as typeof root, + parse: {} as typeof parse, compile: {} as typeof compile, viewer: {} as typeof viewer, server: {} as typeof server, diff --git a/src/main.ts b/src/main.ts index 97e545589..bba909615 100644 --- a/src/main.ts +++ b/src/main.ts @@ -14,6 +14,8 @@ import { cache } from './core/cache' lw.cache = cache import { root } from './core/root' lw.root = root +import { parse } from './parse' +lw.parse = parse import { compile } from './compile' lw.compile = compile import { server, viewer } from './preview' diff --git a/src/outline/structure.ts b/src/outline/structure.ts index 614b42e66..e14a91241 100644 --- a/src/outline/structure.ts +++ b/src/outline/structure.ts @@ -4,7 +4,6 @@ import type { TeXElement } from '../types' import { construct as constructLaTeX } from './structure/latex' import { buildBibTeX } from './structure/bibtex' import { construct as constructDocTeX } from './structure/doctex' -import { parser } from '../parse/parser' const logger = lw.log('Structure') @@ -16,11 +15,11 @@ export const outline = { } lw.onConfigChange(['view.outline.sections', 'view.outline.commands'], async () => { - await parser.reset() + await lw.parse.reset() lw.cache.paths().forEach(async filePath => { const ast = lw.cache.get(filePath)?.ast if (ast) { - await parser.parseArgs(ast) + await lw.parse.args(ast) } }) void reconstruct() diff --git a/src/outline/structure/bibtex.ts b/src/outline/structure/bibtex.ts index 0d40e120a..d7db9b735 100644 --- a/src/outline/structure/bibtex.ts +++ b/src/outline/structure/bibtex.ts @@ -2,7 +2,6 @@ import * as vscode from 'vscode' import { bibtexParser } from 'latex-utensils' import { lw } from '../../lw' import { type TeXElement, TeXElementType } from '../../types' -import { parser } from '../../parse/parser' import { bibTools } from '../../completion/completer/citation' @@ -29,7 +28,7 @@ export async function buildBibTeX(document: vscode.TextDocument): Promise { +async function tex(content: string): Promise { return (await proxy).parseLaTeX(content) } -async function parseArgs(ast: Ast.Root): Promise { +async function args(ast: Ast.Root): Promise { return (await proxy).parseArgs(ast, getMacroDefs()) } @@ -39,7 +39,7 @@ async function reset() { return (await proxy).reset(getMacroDefs(), getEnvDefs()) } -async function parseBibTeX(s: string, options?: bibtexParser.ParserOptions): Promise { +async function bib(s: string, options?: bibtexParser.ParserOptions): Promise { return (await proxy).parseBibTeX(s, options) } @@ -62,52 +62,52 @@ const bibtexPattern = /^This is BibTeX, Version.*$/m const biberPattern = /^INFO - This is Biber .*$/m /** - * @param log The log message to parse. + * @param msg The log message to parse. * @param rootFile The current root file. * @returns whether the current compilation is indeed a skipped one in latexmk. */ -function parseLog(log: string, rootFile?: string): boolean { +function log(msg: string, rootFile?: string): boolean { let isLaTeXmkSkipped = false // Canonicalize line-endings - log = log.replace(/(\r\n)|\r/g, '\n') + msg = msg.replace(/(\r\n)|\r/g, '\n') - if (log.match(bibtexPattern)) { - bibtexLogParser.parse(log.match(latexmkPattern) ? trimLaTeXmkBibTeX(log) : log, rootFile) + if (msg.match(bibtexPattern)) { + bibtexLogParser.parse(msg.match(latexmkPattern) ? trimLaTeXmkBibTeX(msg) : msg, rootFile) bibtexLogParser.showLog() - } else if (log.match(biberPattern)) { - biberLogParser.parse(log.match(latexmkPattern) ? trimLaTeXmkBiber(log) : log, rootFile) + } else if (msg.match(biberPattern)) { + biberLogParser.parse(msg.match(latexmkPattern) ? trimLaTeXmkBiber(msg) : msg, rootFile) biberLogParser.showLog() } - if (log.match(latexmkPattern)) { - log = trimLaTeXmk(log) - } else if (log.match(texifyPattern)) { - log = trimTexify(log) + if (msg.match(latexmkPattern)) { + msg = trimLaTeXmk(msg) + } else if (msg.match(texifyPattern)) { + msg = trimTexify(msg) } - if (log.match(latexPattern) || log.match(latexFatalPattern)) { - latexLogParser.parse(log, rootFile) + if (msg.match(latexPattern) || msg.match(latexFatalPattern)) { + latexLogParser.parse(msg, rootFile) latexLogParser.showLog() - } else if (latexmkSkipped(log)) { + } else if (latexmkSkipped(msg)) { isLaTeXmkSkipped = true } return isLaTeXmkSkipped } -function trimLaTeXmk(log: string): string { - return trimPattern(log, latexmkLogLatex, latexmkLog) +function trimLaTeXmk(msg: string): string { + return trimPattern(msg, latexmkLogLatex, latexmkLog) } -function trimLaTeXmkBibTeX(log: string): string { - return trimPattern(log, bibtexPattern, latexmkLogLatex) +function trimLaTeXmkBibTeX(msg: string): string { + return trimPattern(msg, bibtexPattern, latexmkLogLatex) } -function trimLaTeXmkBiber(log: string): string { - return trimPattern(log, biberPattern, latexmkLogLatex) +function trimLaTeXmkBiber(msg: string): string { + return trimPattern(msg, biberPattern, latexmkLogLatex) } -function trimTexify(log: string): string { - return trimPattern(log, texifyLogLatex, texifyLog) +function trimTexify(msg: string): string { + return trimPattern(msg, texifyLogLatex, texifyLog) } @@ -116,8 +116,8 @@ function trimTexify(log: string): string { * If `endPattern` is not found, the lines from the last occurrence of * `beginPattern` up to the end is returned. */ -function trimPattern(log: string, beginPattern: RegExp, endPattern: RegExp): string { - const lines = log.split('\n') +function trimPattern(msg: string, beginPattern: RegExp, endPattern: RegExp): string { + const lines = msg.split('\n') let startLine = -1 let finalLine = -1 for (let index = 0; index < lines.length; index++) { @@ -139,8 +139,8 @@ function trimPattern(log: string, beginPattern: RegExp, endPattern: RegExp): str } -function latexmkSkipped(log: string): boolean { - if (log.match(latexmkUpToDate) && !log.match(latexmkPattern)) { +function latexmkSkipped(msg: string): boolean { + if (msg.match(latexmkUpToDate) && !msg.match(latexmkPattern)) { latexLogParser.showLog() bibtexLogParser.showLog() biberLogParser.showLog() diff --git a/src/parse/parserlib/biberlog.ts b/src/parse/parser/biberlog.ts similarity index 100% rename from src/parse/parserlib/biberlog.ts rename to src/parse/parser/biberlog.ts diff --git a/src/parse/parserlib/bibtexlog.ts b/src/parse/parser/bibtexlog.ts similarity index 100% rename from src/parse/parserlib/bibtexlog.ts rename to src/parse/parser/bibtexlog.ts diff --git a/src/parse/parserlib/latexlog.ts b/src/parse/parser/latexlog.ts similarity index 100% rename from src/parse/parserlib/latexlog.ts rename to src/parse/parser/latexlog.ts diff --git a/src/parse/parserlib/parserutils.ts b/src/parse/parser/parserutils.ts similarity index 100% rename from src/parse/parserlib/parserutils.ts rename to src/parse/parser/parserutils.ts diff --git a/src/parse/parserlib/unified-defs.ts b/src/parse/parser/unified-defs.ts similarity index 100% rename from src/parse/parserlib/unified-defs.ts rename to src/parse/parser/unified-defs.ts diff --git a/src/parse/parserlib/unified.ts b/src/parse/parser/unified.ts similarity index 100% rename from src/parse/parserlib/unified.ts rename to src/parse/parser/unified.ts diff --git a/src/preview/math/mathpreviewlib/cursorrenderer.ts b/src/preview/math/mathpreviewlib/cursorrenderer.ts index 2feead9db..a4f0564d2 100644 --- a/src/preview/math/mathpreviewlib/cursorrenderer.ts +++ b/src/preview/math/mathpreviewlib/cursorrenderer.ts @@ -3,7 +3,6 @@ import type * as Ast from '@unified-latex/unified-latex-types' import { lw } from '../../../lw' import { TexMathEnv } from './texmathenvfinder' import type { ITextDocumentLike } from './textdocumentlike' -import { parser } from '../../../parse/parser' import { findNode } from '../../../language/selection' const logger = lw.log('Preview', 'Math', 'Cursor') @@ -58,7 +57,7 @@ async function findNodeAt(texMath: TexMathEnv, cursorPos: vscode.Position) { ast = cache.ast } else { logger.log(`Parse LaTeX AST from ${texMath.texString} .`) - ast = await parser.parseLaTeX(texMath.texString) + ast = await lw.parse.tex(texMath.texString) cache.ast = ast cache.texString = texMath.texString }