Skip to content

Commit

Permalink
Modularize mathjax (a bit)
Browse files Browse the repository at this point in the history
  • Loading branch information
James-Yu committed Dec 12, 2023
1 parent 923dc2e commit eb2cd88
Show file tree
Hide file tree
Showing 14 changed files with 306 additions and 343 deletions.
6 changes: 3 additions & 3 deletions src/completion/latex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,9 @@ export class Completer implements vscode.CompletionItemProvider {
item.documentation = data.documentation
return item
}
const tex = lw.mathPreview.findHoverOnRef(sug, data.key)
const tex = lw.preview.math.findRef(sug, data.key)
if (tex) {
const svgDataUrl = await lw.mathPreview.renderSvgOnRef(tex, data, token)
const svgDataUrl = await lw.preview.math.renderSvgOnRef(tex, data, token)
item.documentation = new vscode.MarkdownString(`![equation](${svgDataUrl})`)
return item
} else {
Expand All @@ -209,7 +209,7 @@ export class Completer implements vscode.CompletionItemProvider {
if (typeof filePath !== 'string') {
return item
}
const md = await lw.graphicsPreview.renderGraphicsAsMarkdownString(filePath, { height: 190, width: 300 })
const md = await lw.preview.asMD(filePath, { height: 190, width: 300 })
if (md === undefined) {
return item
}
Expand Down
16 changes: 6 additions & 10 deletions src/extras/math-preview-panel.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as vscode from 'vscode'
import * as path from 'path'
import type { TexMathEnv } from '../preview/math/mathpreview'
import type { TeXMathEnv } from '../types'
import { moveWebviewPanel } from '../utils/webview'
import { lw } from '../lw'

Expand Down Expand Up @@ -49,10 +49,6 @@ export class MathPreviewPanel {
})
}

private get mathPreview() {
return lw.mathPreview
}

open() {
const activeDocument = vscode.window.activeTextEditor?.document
if (this.panel) {
Expand All @@ -61,7 +57,7 @@ export class MathPreviewPanel {
}
return
}
this.mathPreview.getColor()
lw.preview.math.getColor()
const panel = vscode.window.createWebviewPanel(
'latex-workshop-mathpreview',
'Math Preview',
Expand Down Expand Up @@ -193,7 +189,7 @@ export class MathPreviewPanel {
if (this.needCursor) {
await this.renderCursor(document, texMath)
}
const result = await this.mathPreview.generateSVG(texMath, cachedCommands).catch(() => undefined)
const result = await lw.preview.math.generateSVG(texMath, cachedCommands).catch(() => undefined)
if (!result) {
return
}
Expand All @@ -204,7 +200,7 @@ export class MathPreviewPanel {
}

private getTexMath(document: vscode.TextDocument, position: vscode.Position) {
const texMath = this.mathPreview.findMathEnvIncludingPosition(document, position)
const texMath = lw.preview.math.findMath(document, position)
if (texMath) {
if (texMath.envname !== '$') {
return texMath
Expand All @@ -216,8 +212,8 @@ export class MathPreviewPanel {
return
}

async renderCursor(document: vscode.TextDocument, tex: TexMathEnv) {
const s = await this.mathPreview.renderCursor(document, tex)
async renderCursor(document: vscode.TextDocument, tex: TeXMathEnv) {
const s = await lw.preview.math.renderCursor(document, tex)
tex.texString = s
}

Expand Down
23 changes: 14 additions & 9 deletions src/lw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type { watcher } from './core/watcher'
import type { cache } from './core/cache'
import type { root } from './core/root'
import type { compile } from './compile'
import type { server, viewer } from './preview'
import type { preview, server, viewer } from './preview'
import type { locate } from './locate'
import type { lint } from './lint'
import type { outline } from './outline'
Expand All @@ -20,8 +20,6 @@ import type { Section } from './extras/section'
import type { SnippetView } from './extras/snippet-view'
import type { TeXMagician } from './extras/texroot'
import type { AtSuggestionCompleter, Completer } from './completion/latex'
import type { GraphicsPreview } from './preview/graphics'
import type { MathPreview } from './preview/math/mathpreview'
import type { TeXDoc } from './extras/texdoc'
import type * as commands from './core/commands'

Expand All @@ -39,6 +37,7 @@ export const lw = {
compile: {} as typeof compile,
viewer: {} as typeof viewer,
server: {} as typeof server,
preview: {} as typeof preview,
locate: {} as typeof locate,
completer: Object.create(null) as Completer,
atSuggestionCompleter: Object.create(null) as AtSuggestionCompleter,
Expand All @@ -51,8 +50,6 @@ export const lw = {
section: Object.create(null) as Section,
latexCommanderTreeView: Object.create(null) as LaTeXCommanderTreeView,
snippetView: Object.create(null) as SnippetView,
graphicsPreview: Object.create(null) as GraphicsPreview,
mathPreview: Object.create(null) as MathPreview,
mathPreviewPanel: Object.create(null) as MathPreviewPanel,
commands: Object.create(null) as typeof commands,
onConfigChange,
Expand All @@ -77,7 +74,13 @@ const constant = {
* See https://stackoverflow.com/questions/695438/safe-characters-for-friendly-url
* See https://tools.ietf.org/html/rfc3986#section-2.3
*/
PDF_PREFIX: 'pdf..'
PDF_PREFIX: 'pdf..',
MATHJAX_EXT: [
'amscd', 'bbox', 'boldsymbol', 'braket', 'bussproofs', 'cancel',
'cases', 'centernot', 'colortbl', 'empheq', 'enclose', 'extpfeil',
'gensymb', 'html', 'mathtools', 'mhchem', 'physics', 'textcomp',
'textmacros', 'unicode', 'upgreek', 'verb'
]
}
lw.constant = constant

Expand All @@ -87,8 +90,9 @@ const tempDisposables: vscode.Disposable[] = []
* Handle configuration changes and invoke the specified callback function when
* relevant configurations are updated.
*
* @param {string[]} [configs] - Optional. An array of configuration keys to
* monitor for changes.
* @param {string | string[]} [configs] - Optional. A string or an array of
* configuration keys to monitor for changes. The leading `latex-workshop.`
* should be omitted. A '*' can also be passed here for wildcard.
* @param {Function} [callback] - Optional. The callback function to be executed
* when relevant configurations change.
* @param {vscode.ConfigurationScope} [scope] - Optional. The configuration
Expand All @@ -97,7 +101,8 @@ const tempDisposables: vscode.Disposable[] = []
function onConfigChange(configs?: string | string[], callback?: () => void, scope?: vscode.ConfigurationScope) {
const disposable = vscode.workspace.onDidChangeConfiguration((e: vscode.ConfigurationChangeEvent) => {
if (configs && callback &&
[ configs ].flat().some(config => e.affectsConfiguration(`latex-workshop.${config}`, scope))) {
([ configs ].flat().some(config => e.affectsConfiguration(`latex-workshop.${config}`, scope))
|| configs === '*')) {
callback()
}
})
Expand Down
19 changes: 4 additions & 15 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ import { parse } from './parse'
lw.parse = parse
import { compile } from './compile'
lw.compile = compile
import { server, viewer } from './preview'
import { preview, server, viewer } from './preview'
lw.server = server
lw.viewer = viewer
lw.preview = preview
import { locate } from './locate'
lw.locate = locate
import { lint } from './lint'
Expand All @@ -30,7 +31,6 @@ lw.outline = outline

import { MathPreviewPanelSerializer } from './extras/math-preview-panel'
import { BibtexCompleter } from './completion/bibtex'
import { HoverProvider } from './preview/hover'
import { DocSymbolProvider } from './language/symbol-document'
import { ProjectSymbolProvider } from './language/symbol-project'
import { DefinitionProvider } from './language/definition'
Expand All @@ -45,11 +45,8 @@ import { Section } from './extras/section'
import { SnippetView } from './extras/snippet-view'
import { TeXMagician } from './extras/texroot'
import { AtSuggestionCompleter, Completer } from './completion/latex'
import { GraphicsPreview } from './preview/graphics'
import { MathPreview } from './preview/math/mathpreview'
import { TeXDoc } from './extras/texdoc'
import { parser } from './parse/parser'
import { MathJaxPool } from './preview/math/mathjaxpool'
import * as commander from './core/commands'

const logger = lw.log('Extension')
Expand All @@ -65,8 +62,6 @@ function initialize(extensionContext: vscode.ExtensionContext) {
lw.section = new Section()
lw.latexCommanderTreeView = new LaTeXCommanderTreeView()
lw.snippetView = new SnippetView()
lw.graphicsPreview = new GraphicsPreview()
lw.mathPreview = new MathPreview()
lw.mathPreviewPanel = new MathPreviewPanel()
lw.commands = commander

Expand All @@ -87,18 +82,12 @@ function initialize(extensionContext: vscode.ExtensionContext) {
log.logConfig()
log.logDeprecatedConfig()
logger.log('LaTeX Workshop initialized.')
return {
dispose: async () => {
await parser.dispose()
MathJaxPool.dispose()
}
}
}

export function activate(extensionContext: vscode.ExtensionContext) {
void vscode.commands.executeCommand('setContext', 'latex-workshop:enabled', true)

extensionContext.subscriptions.push(initialize(extensionContext))
initialize(extensionContext)

registerLatexWorkshopCommands(extensionContext)

Expand Down Expand Up @@ -295,7 +284,7 @@ function registerProviders(extensionContext: vscode.ExtensionContext) {
)

extensionContext.subscriptions.push(
vscode.languages.registerHoverProvider(latexSelector, new HoverProvider()),
vscode.languages.registerHoverProvider(latexSelector, lw.preview.provider),
vscode.languages.registerDefinitionProvider(latexSelector, new DefinitionProvider()),
vscode.languages.registerDocumentSymbolProvider(latexSelector, new DocSymbolProvider()),
vscode.languages.registerDocumentSymbolProvider(bibtexSelector, new DocSymbolProvider()),
Expand Down
8 changes: 3 additions & 5 deletions src/parse/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as path from 'path'
import * as workerpool from 'workerpool'
import type * as Ast from '@unified-latex/unified-latex-types'
import type { bibtexParser } from 'latex-utensils'
import { lw } from '../lw'
import type { Worker } from './parser/unified'
import { getEnvDefs, getMacroDefs } from './parser/unified-defs'
import { bibtexLogParser } from './parser/bibtexlog'
Expand All @@ -13,8 +14,7 @@ export const parser = {
log,
tex,
args,
reset,
dispose
reset
}

const pool = workerpool.pool(
Expand All @@ -23,9 +23,7 @@ const pool = workerpool.pool(
)
const proxy = pool.proxy<Worker>()

async function dispose() {
await pool.terminate(true)
}
lw.onDispose({ dispose: async () => { await pool.terminate(true) } })

async function tex(content: string): Promise<Ast.Root> {
return (await proxy).parseLaTeX(content)
Expand Down
Loading

0 comments on commit eb2cd88

Please sign in to comment.