Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modularize #4065

Closed
wants to merge 13 commits into from
2 changes: 1 addition & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
dev/*.js
src/lib/**/*.*
src/**/*.*
types/**/*.ts
resources/**/*.js
out
Expand Down
7 changes: 7 additions & 0 deletions .madgerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"detectiveOptions": {
"ts": {
"skipTypeImports": true
}
}
}
5 changes: 1 addition & 4 deletions .vscodeignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ out/resources/
out/snippets/
out/test/
test/
src/*.*
src/components/
src/lib/**/*.ts
src/providers/
src/**/*.ts
samples/
types/
viewer/**/*.ts
Expand Down
2 changes: 1 addition & 1 deletion dev/editviewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
line.replace('''<title>PDF.js viewer</title>''', '''<meta http-equiv="Content-Security-Policy" content="default-src 'self'; base-uri 'none'; connect-src 'self' ws://127.0.0.1:*; style-src 'self' 'unsafe-inline'; img-src 'self' data: blob:;">\n <title>PDF.js viewer</title>''')
.replace('''<link rel="stylesheet" href="viewer.css">''', '''<link rel="stylesheet" href="viewer.css">\n <link rel="stylesheet" href="latexworkshop.css">''')
.replace('''<script src="../build/pdf.mjs" type="module"></script>''', '''<script src="build/pdf.mjs" type="module"></script>''')
.replace('''<script src="viewer.mjs" type="module"></script>''', '''<script src="out/viewer/latexworkshop.js" type="module"></script>''')
.replace('''<script src="viewer.mjs" type="module"></script>''', '''<script src="out/viewer/latexworkshop.js" type="module"></script>\n <script src="viewer/viewer.mjs" type="module"></script>''')
)

with open(args.web + '/viewer.mjs', 'rt', encoding='utf-8') as fin:
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 4 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@
"main": "./out/src/main.js",
"capabilities": {
"virtualWorkspaces": {
"supported": "limited",
"description": "Only a few features are supported."
"supported": false
},
"untrustedWorkspaces": {
"supported": false
Expand Down Expand Up @@ -359,8 +358,8 @@
"category": "LaTeX Workshop"
},
{
"command": "latex-workshop.kill",
"title": "Kill LaTeX compiler process",
"command": "latex-workshop.terminate",
"title": "Terminate LaTeX compiler process",
"category": "LaTeX Workshop",
"enablement": "!virtualWorkspace"
},
Expand Down Expand Up @@ -2576,7 +2575,7 @@
"latex-utensils": "6.2.0",
"mathjax-full": "3.2.2",
"micromatch": "4.0.5",
"pdfjs-dist": "^4.0.189",
"pdfjs-dist": "4.0.189",
"tmp": "0.2.1",
"workerpool": "8.0.0",
"ws": "8.14.2"
Expand Down
114 changes: 40 additions & 74 deletions src/commander.ts → src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,21 @@ import * as vscode from 'vscode'
import * as path from 'path'
import * as lw from './lw'
import { getSurroundingCommandRange, stripText } from './utils/utils'
import { getLogger } from './components/logger'
import { parser } from './components/parser'
import { parser } from './parse/parser'
import { extension } from './extension'

const logger = getLogger('Commander')
const logger = extension.log('Commander')

export async function build(skipSelection: boolean = false, rootFile: string | undefined = undefined, languageId: string | undefined = undefined, recipe: string | undefined = undefined) {
logger.log('BUILD command invoked.')
if (!vscode.window.activeTextEditor) {
logger.log('Cannot start to build because the active editor is undefined.')
return
}
logger.log(`The document of the active editor: ${vscode.window.activeTextEditor.document.uri.toString(true)}`)
logger.log(`The languageId of the document: ${vscode.window.activeTextEditor.document.languageId}`)
const workspace = rootFile ? vscode.Uri.file(rootFile) : vscode.window.activeTextEditor.document.uri
const configuration = vscode.workspace.getConfiguration('latex-workshop', workspace)
const externalBuildCommand = configuration.get('latex.external.build.command') as string
const externalBuildArgs = configuration.get('latex.external.build.args') as string[]
if (rootFile === undefined && lw.manager.hasTexId(vscode.window.activeTextEditor.document.languageId)) {
rootFile = await lw.manager.findRoot()
languageId = lw.manager.rootFileLanguageId
}
if (externalBuildCommand) {
const pwd = path.dirname(rootFile ? rootFile : vscode.window.activeTextEditor.document.fileName)
await lw.builder.buildExternal(externalBuildCommand, externalBuildArgs, pwd, rootFile)
return
}
if (rootFile === undefined || languageId === undefined) {
logger.log('Cannot find LaTeX root file. See https://github.com/James-Yu/LaTeX-Workshop/wiki/Compile#the-root-file')
return
}
let pickedRootFile: string | undefined = rootFile
if (!skipSelection && lw.manager.localRootFile) {
// We are using the subfile package
pickedRootFile = await quickPickRootFile(rootFile, lw.manager.localRootFile, 'build')
if (! pickedRootFile) {
return
}
}
logger.log(`Building root file: ${pickedRootFile}`)
await lw.builder.build(pickedRootFile, languageId, recipe)
await extension.compile.build(skipSelection, rootFile, languageId, recipe)
}

export async function revealOutputDir() {
let outDir = lw.manager.getOutDir()
let outDir = extension.file.getOutDir()
if (!path.isAbsolute(outDir)) {
const workspaceFolder = vscode.workspace.workspaceFolders?.[0]
const rootDir = lw.manager.rootDir || workspaceFolder?.uri.fsPath
const rootDir = extension.root.file.path || workspaceFolder?.uri.fsPath
if (rootDir === undefined) {
logger.log(`Cannot reveal ${vscode.Uri.file(outDir)}: no root dir can be identified.`)
return
Expand All @@ -61,7 +29,7 @@ export async function revealOutputDir() {

export function recipes(recipe?: string) {
logger.log('RECIPES command invoked.')
const configuration = vscode.workspace.getConfiguration('latex-workshop', lw.manager.getWorkspaceFolderRootDir())
const configuration = vscode.workspace.getConfiguration('latex-workshop', extension.root.getWorkspace())
const candidates = configuration.get('latex.recipes') as {name: string}[]
if (!candidates) {
return
Expand Down Expand Up @@ -89,55 +57,52 @@ export async function view(mode?: 'tab' | 'browser' | 'external' | vscode.Uri) {
logger.log('Cannot find active TextEditor.')
return
}
if (!lw.manager.hasTexId(vscode.window.activeTextEditor.document.languageId)) {
if (!extension.file.hasTexLangId(vscode.window.activeTextEditor.document.languageId)) {
logger.log('Active document is not a TeX file.')
return
}
const rootFile = await lw.manager.findRoot()
if (rootFile === undefined) {
await extension.root.find()
if (extension.root.file.path === undefined) {
logger.log('Cannot find LaTeX root PDF to view.')
return
}
let pickedRootFile: string | undefined = rootFile
if (lw.manager.localRootFile) {
// We are using the subfile package
pickedRootFile = await quickPickRootFile(rootFile, lw.manager.localRootFile, 'view')
}
if (!pickedRootFile) {
const filePath: string | undefined = extension.root.subfiles.path ? await quickPickRootFile(extension.root.file.path, extension.root.subfiles.path, 'view') : extension.root.file.path

if (filePath === undefined) {
return
}
return lw.viewer.open(lw.manager.tex2pdf(pickedRootFile), typeof mode === 'string' ? mode : undefined)
return lw.viewer.open(extension.file.getPdfPath(filePath), typeof mode === 'string' ? mode : undefined)
}

export function refresh() {
logger.log('REFRESH command invoked.')
lw.viewer.refreshExistingViewer()
}

export function kill() {
logger.log('KILL command invoked.')
lw.builder.kill()
export function terminate() {
logger.log('TERMINATE command invoked.')
extension.compile.terminate()
}

export function synctex() {
logger.log('SYNCTEX command invoked.')
if (!vscode.window.activeTextEditor || !lw.manager.hasTexId(vscode.window.activeTextEditor.document.languageId)) {
if (!vscode.window.activeTextEditor || !extension.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
}
const configuration = vscode.workspace.getConfiguration('latex-workshop', lw.manager.getWorkspaceFolderRootDir())
const configuration = vscode.workspace.getConfiguration('latex-workshop', extension.root.getWorkspace())
let pdfFile: string | undefined = undefined
if (lw.manager.localRootFile && configuration.get('latex.rootFile.useSubFile')) {
pdfFile = lw.manager.tex2pdf(lw.manager.localRootFile)
} else if (lw.manager.rootFile !== undefined) {
pdfFile = lw.manager.tex2pdf(lw.manager.rootFile)
if (extension.root.subfiles.path && configuration.get('latex.rootFile.useSubFile')) {
pdfFile = extension.file.getPdfPath(extension.root.subfiles.path)
} else if (extension.root.file.path !== undefined) {
pdfFile = extension.file.getPdfPath(extension.root.file.path)
}
lw.locator.syncTeX(undefined, undefined, pdfFile)
}

export function synctexonref(line: number, filePath: string) {
logger.log('SYNCTEX command invoked on a reference.')
if (!vscode.window.activeTextEditor || !lw.manager.hasTexId(vscode.window.activeTextEditor.document.languageId)) {
if (!vscode.window.activeTextEditor || !extension.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
}
Expand All @@ -146,15 +111,15 @@ export function synctexonref(line: number, filePath: string) {

export async function clean(): Promise<void> {
logger.log('CLEAN command invoked.')
const rootFile = await lw.manager.findRoot()
const rootFile = await extension.root.find()
if (rootFile === undefined) {
logger.log('Cannot find LaTeX root file to clean.')
return
}
let pickedRootFile: string | undefined = rootFile
if (lw.manager.localRootFile) {
if (extension.root.subfiles.path) {
// We are using the subfile package
pickedRootFile = await quickPickRootFile(rootFile, lw.manager.localRootFile, 'clean')
pickedRootFile = await quickPickRootFile(rootFile, extension.root.subfiles.path, 'clean')
if (! pickedRootFile) {
return
}
Expand All @@ -164,7 +129,7 @@ export async function clean(): Promise<void> {

export function addTexRoot() {
logger.log('ADDTEXROOT command invoked.')
if (!vscode.window.activeTextEditor || !lw.manager.hasTexId(vscode.window.activeTextEditor.document.languageId)) {
if (!vscode.window.activeTextEditor || !extension.file.hasTexLangId(vscode.window.activeTextEditor.document.languageId)) {
return
}
lw.texMagician.addTexRoot()
Expand All @@ -177,10 +142,10 @@ export function citation() {

export function wordcount() {
logger.log('WORDCOUNT command invoked.')
if (!vscode.window.activeTextEditor || !lw.manager.hasTexId(vscode.window.activeTextEditor.document.languageId) ||
lw.manager.rootFile === vscode.window.activeTextEditor.document.fileName) {
if (lw.manager.rootFile) {
lw.counter.count(lw.manager.rootFile)
if (!vscode.window.activeTextEditor || !extension.file.hasTexLangId(vscode.window.activeTextEditor.document.languageId) ||
extension.root.file.path === vscode.window.activeTextEditor.document.fileName) {
if (extension.root.file.path) {
lw.counter.count(extension.root.file.path)
} else {
logger.log('WORDCOUNT: No rootFile defined.')
}
Expand Down Expand Up @@ -216,47 +181,47 @@ export function gotoSection(filePath: string, lineNumber: number) {

export function navigateToEnvPair() {
logger.log('JumpToEnvPair command invoked.')
if (!vscode.window.activeTextEditor || !lw.manager.hasTexId(vscode.window.activeTextEditor.document.languageId)) {
if (!vscode.window.activeTextEditor || !extension.file.hasTexLangId(vscode.window.activeTextEditor.document.languageId)) {
return
}
void lw.envPair.gotoPair()
}

export function selectEnvContent(mode: 'content' | 'whole') {
logger.log('SelectEnv command invoked.')
if (!vscode.window.activeTextEditor || !lw.manager.hasTexId(vscode.window.activeTextEditor.document.languageId)) {
if (!vscode.window.activeTextEditor || !extension.file.hasTexLangId(vscode.window.activeTextEditor.document.languageId)) {
return
}
void lw.envPair.selectEnvContent(mode)
}

export function selectEnvName() {
logger.log('SelectEnvName command invoked.')
if (!vscode.window.activeTextEditor || !lw.manager.hasTexId(vscode.window.activeTextEditor.document.languageId)) {
if (!vscode.window.activeTextEditor || !extension.file.hasTexLangId(vscode.window.activeTextEditor.document.languageId)) {
return
}
void lw.envPair.envNameAction('selection')
}

export function multiCursorEnvName() {
logger.log('MutliCursorEnvName command invoked.')
if (!vscode.window.activeTextEditor || !lw.manager.hasTexId(vscode.window.activeTextEditor.document.languageId)) {
if (!vscode.window.activeTextEditor || !extension.file.hasTexLangId(vscode.window.activeTextEditor.document.languageId)) {
return
}
void lw.envPair.envNameAction('cursor')
}

export function toggleEquationEnv() {
logger.log('toggleEquationEnv command invoked.')
if (!vscode.window.activeTextEditor || !lw.manager.hasTexId(vscode.window.activeTextEditor.document.languageId)) {
if (!vscode.window.activeTextEditor || !extension.file.hasTexLangId(vscode.window.activeTextEditor.document.languageId)) {
return
}
void lw.envPair.envNameAction('equationToggle')
}

export function closeEnv() {
logger.log('CloseEnv command invoked.')
if (!vscode.window.activeTextEditor || !lw.manager.hasTexId(vscode.window.activeTextEditor.document.languageId)) {
if (!vscode.window.activeTextEditor || !extension.file.hasTexLangId(vscode.window.activeTextEditor.document.languageId)) {
return
}
void lw.envPair.closeEnv()
Expand Down Expand Up @@ -478,7 +443,8 @@ export function texdocUsepackages() {
}

export async function saveActive() {
await lw.builder.saveActive()
extension.compile.lastBuildTime = Date.now()
await vscode.window.activeTextEditor?.document.save()
}

export function openMathPreviewPanel() {
Expand Down
Loading
Loading