Skip to content

Commit

Permalink
Modularize extra features
Browse files Browse the repository at this point in the history
  • Loading branch information
James-Yu committed Dec 12, 2023
1 parent eb2cd88 commit 4b955e9
Show file tree
Hide file tree
Showing 16 changed files with 858 additions and 903 deletions.
6 changes: 3 additions & 3 deletions src/compile/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ function handleRetryError(step: RecipeStep) {
logger.log('Cleaning auxiliary files and retrying build after toolchain error.')

queue.prepend(step)
void lw.cleaner.clean(step.rootFile).then(() => lw.event.fire(lw.event.AutoCleaned))
void lw.extra.clean(step.rootFile).then(() => lw.event.fire(lw.event.AutoCleaned))
}

/**
Expand All @@ -365,7 +365,7 @@ function handleRetryError(step: RecipeStep) {
function handleNoRetryError(configuration: vscode.WorkspaceConfiguration, step: RecipeStep) {
logger.refreshStatus('x', 'errorForeground')
if (['onFailed', 'onBuilt'].includes(configuration.get('latex.autoClean.run') as string)) {
void lw.cleaner.clean(step.rootFile).then(() => lw.event.fire(lw.event.AutoCleaned))
void lw.extra.clean(step.rootFile).then(() => lw.event.fire(lw.event.AutoCleaned))
}
void logger.showErrorMessageWithCompilerLogButton('Recipe terminated with error.')
queue.clear()
Expand Down Expand Up @@ -424,7 +424,7 @@ async function afterSuccessfulBuilt(lastStep: Step, skipped: boolean) {
}
if (['onSucceeded', 'onBuilt'].includes(configuration.get('latex.autoClean.run') as string)) {
logger.log('Auto Clean invoked.')
await lw.cleaner.clean(lastStep.rootFile)
await lw.extra.clean(lastStep.rootFile)
lw.event.fire(lw.event.AutoCleaned)
}
}
22 changes: 11 additions & 11 deletions src/core/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,15 @@ export async function clean(): Promise<void> {
return
}
}
return lw.cleaner.clean(pickedRootFile)
return lw.extra.clean(pickedRootFile)
}

export function addTexRoot() {
logger.log('ADDTEXROOT command invoked.')
if (!vscode.window.activeTextEditor || !lw.file.hasTexLangId(vscode.window.activeTextEditor.document.languageId)) {
return
}
lw.texMagician.addTexRoot()
lw.extra.texroot()
}

export function citation() {
Expand All @@ -148,12 +148,12 @@ export function wordcount() {
if (!vscode.window.activeTextEditor || !lw.file.hasTexLangId(vscode.window.activeTextEditor.document.languageId) ||
lw.root.file.path === vscode.window.activeTextEditor.document.fileName) {
if (lw.root.file.path) {
lw.counter.count(lw.root.file.path)
lw.extra.count(lw.root.file.path, true, true)
} else {
logger.log('WORDCOUNT: No rootFile defined.')
}
} else {
lw.counter.count(vscode.window.activeTextEditor.document.fileName, false)
lw.extra.count(vscode.window.activeTextEditor.document.fileName, false, true)
}
}

Expand Down Expand Up @@ -399,11 +399,11 @@ export async function toggleSelectedKeyword(keyword: string) {
* @param change
*/
export function shiftSectioningLevel(change: 'promote' | 'demote') {
lw.section.shiftSectioningLevel(change)
lw.extra.section(change)
}

export function selectSection() {
lw.section.selectSection()
lw.extra.section('select')
}

export function devParseLog() {
Expand Down Expand Up @@ -438,11 +438,11 @@ export async function devStripText() {
}

export function texdoc(packageName?: string) {
lw.texdoc.texdoc(packageName)
lw.extra.texdoc(packageName)
}

export function texdocUsepackages() {
lw.texdoc.texdocUsepackages()
lw.extra.texdoc(undefined, true)
}

export async function saveActive() {
Expand All @@ -451,15 +451,15 @@ export async function saveActive() {
}

export function openMathPreviewPanel() {
return lw.mathPreviewPanel.open()
lw.extra.mathpreview.toggle('open')
}

export function closeMathPreviewPanel() {
lw.mathPreviewPanel.close()
lw.extra.mathpreview.toggle('close')
}

export function toggleMathPreviewPanel() {
lw.mathPreviewPanel.toggle()
lw.extra.mathpreview.toggle()
}

async function quickPickRootFile(rootFile: string, localRootFile: string, verb: string): Promise<string | undefined> {
Expand Down
187 changes: 84 additions & 103 deletions src/extras/activity-bar.ts
Original file line number Diff line number Diff line change
@@ -1,115 +1,89 @@
import * as vscode from 'vscode'
import { lw } from '../lw'

export class LaTeXCommanderTreeView {
private readonly latexCommanderProvider: LaTeXCommanderProvider

constructor() {
this.latexCommanderProvider = new LaTeXCommanderProvider()
vscode.window.createTreeView(
'latex-workshop-commands',
{
treeDataProvider: this.latexCommanderProvider,
showCollapseAll: true
})
}
lw.onConfigChange('latex.recipes', update)

update() {
this.latexCommanderProvider.update()
function buildNode(parent: LaTeXCommand, children: LaTeXCommand[]) {
if (children.length > 0) {
parent.collapsibleState = vscode.TreeItemCollapsibleState.Collapsed
parent.children = children
children.forEach((c) => c.parent = parent)
}
return parent
}

class LaTeXCommanderProvider implements vscode.TreeDataProvider<LaTeXCommand> {
private readonly _onDidChangeTreeData: vscode.EventEmitter<LaTeXCommand | undefined> = new vscode.EventEmitter<LaTeXCommand | undefined>()
readonly onDidChangeTreeData: vscode.Event<LaTeXCommand | undefined>
private commands: LaTeXCommand[] = []

constructor() {
this.onDidChangeTreeData = this._onDidChangeTreeData.event
vscode.workspace.onDidChangeConfiguration((ev: vscode.ConfigurationChangeEvent) => {
if (ev.affectsConfiguration('latex-workshop.latex.recipes', lw.root.getWorkspace())) {
this.update()
}
})
this.commands = this.buildCommandTree()
}
function buildCommandTree(): LaTeXCommand[] {
const commands: LaTeXCommand[] = []
const configuration = vscode.workspace.getConfiguration('latex-workshop', lw.root.getWorkspace())

const buildCommand = new LaTeXCommand('Build LaTeX project', {command: 'latex-workshop.build'}, 'debug-start')
const recipes = configuration.get('latex.recipes', []) as {name: string}[]
const recipeCommands = recipes.map(recipe => new LaTeXCommand(`Recipe: ${recipe.name}`, {command: 'latex-workshop.recipes', arguments: [recipe.name]}, 'debug-start'))
let node: LaTeXCommand
node = buildNode(buildCommand, [
new LaTeXCommand('Clean up auxiliary files', {command: 'latex-workshop.clean'}, 'clear-all'),
new LaTeXCommand('Terminate current compilation', {command: 'latex-workshop.kill'}, 'debug-stop'),
...recipeCommands
])
commands.push(node)

const viewCommand = new LaTeXCommand('View LaTeX PDF', {command: 'latex-workshop.view'}, 'open-preview')
node = buildNode(viewCommand, [
new LaTeXCommand('View in VSCode tab', {command: 'latex-workshop.view', arguments: ['tab']}, 'open-preview'),
new LaTeXCommand('View in web browser', {command: 'latex-workshop.view', arguments: ['browser']}, 'browser'),
new LaTeXCommand('View in external viewer', {command: 'latex-workshop.view', arguments: ['external']}, 'preview'),
new LaTeXCommand('Refresh all viewers', {command: 'latex-workshop.refresh-viewer'}, 'refresh')
])
commands.push(node)

const logCommand = new LaTeXCommand('View Log messages', {command: 'latex-workshop.log'}, 'output')
const compilerLog = new LaTeXCommand('View LaTeX compiler log', {command: 'latex-workshop.compilerlog'}, 'output')
const latexWorkshopLog = new LaTeXCommand('View LaTeX Workshop extension log', {command: 'latex-workshop.log'}, 'output')
node = buildNode(logCommand, [
latexWorkshopLog,
compilerLog
])
commands.push(node)

const navCommand = new LaTeXCommand('Navigate, select, and edit', undefined, 'edit')
node= buildNode(navCommand, [
new LaTeXCommand('SyncTeX from cursor', {command: 'latex-workshop.synctex'}, 'go-to-file'),
new LaTeXCommand('Navigate to matching begin/end', {command: 'latex-workshop.navigate-envpair'}),
new LaTeXCommand('Select current environment content', {command: 'latex-workshop.select-envcontent'}),
new LaTeXCommand('Select current environment name', {command: 'latex-workshop.select-envname'}),
new LaTeXCommand('Close current environment', {command: 'latex-workshop.close-env'}),
new LaTeXCommand('Surround with begin{}...\\end{}', {command: 'latex-workshop.wrap-env'}),
new LaTeXCommand('Insert %!TeX root magic comment', {command: 'latex-workshop.addtexroot'})
])
commands.push(node)

const miscCommand = new LaTeXCommand('Miscellaneous', undefined, 'menu')
node = buildNode(miscCommand, [
new LaTeXCommand('Open citation browser', {command: 'latex-workshop.citation'}),
new LaTeXCommand('Count words in LaTeX project', {command: 'latex-workshop.wordcount'}),
new LaTeXCommand('Reveal output folder in OS', {command: 'latex-workshop.revealOutputDir'}, 'folder-opened')
])
commands.push(node)

const bibtexCommand = new LaTeXCommand('BibTeX actions', undefined, 'references')
node = buildNode(bibtexCommand, [
new LaTeXCommand('Align bibliography', {command: 'latex-workshop.bibalign'}),
new LaTeXCommand('Sort bibliography', {command: 'latex-workshop.bibsort'}, 'sort-precedence'),
new LaTeXCommand('Align and sort bibliography', {command: 'latex-workshop.bibalignsort'})
])
commands.push(node)
return commands
}

update() {
this.commands = this.buildCommandTree()
this._onDidChangeTreeData.fire(undefined)
}

private buildNode(parent: LaTeXCommand, children: LaTeXCommand[]) {
if (children.length > 0) {
parent.collapsibleState = vscode.TreeItemCollapsibleState.Collapsed
parent.children = children
children.forEach((c) => c.parent = parent)
}
return parent
}
function update() {
state.commands = buildCommandTree()
state.treeDataProvider._onDidChangeTreeData.fire(undefined)
}

private buildCommandTree(): LaTeXCommand[] {
const commands: LaTeXCommand[] = []
const configuration = vscode.workspace.getConfiguration('latex-workshop', lw.root.getWorkspace())

const buildCommand = new LaTeXCommand('Build LaTeX project', {command: 'latex-workshop.build'}, 'debug-start')
const recipes = configuration.get('latex.recipes', []) as {name: string}[]
const recipeCommands = recipes.map(recipe => new LaTeXCommand(`Recipe: ${recipe.name}`, {command: 'latex-workshop.recipes', arguments: [recipe.name]}, 'debug-start'))
let node: LaTeXCommand
node = this.buildNode(buildCommand, [
new LaTeXCommand('Clean up auxiliary files', {command: 'latex-workshop.clean'}, 'clear-all'),
new LaTeXCommand('Terminate current compilation', {command: 'latex-workshop.kill'}, 'debug-stop'),
...recipeCommands
])
commands.push(node)

const viewCommand = new LaTeXCommand('View LaTeX PDF', {command: 'latex-workshop.view'}, 'open-preview')
node = this.buildNode(viewCommand, [
new LaTeXCommand('View in VSCode tab', {command: 'latex-workshop.view', arguments: ['tab']}, 'open-preview'),
new LaTeXCommand('View in web browser', {command: 'latex-workshop.view', arguments: ['browser']}, 'browser'),
new LaTeXCommand('View in external viewer', {command: 'latex-workshop.view', arguments: ['external']}, 'preview'),
new LaTeXCommand('Refresh all viewers', {command: 'latex-workshop.refresh-viewer'}, 'refresh')
])
commands.push(node)

const logCommand = new LaTeXCommand('View Log messages', {command: 'latex-workshop.log'}, 'output')
const compilerLog = new LaTeXCommand('View LaTeX compiler log', {command: 'latex-workshop.compilerlog'}, 'output')
const latexWorkshopLog = new LaTeXCommand('View LaTeX Workshop extension log', {command: 'latex-workshop.log'}, 'output')
node = this.buildNode(logCommand, [
latexWorkshopLog,
compilerLog
])
commands.push(node)

const navCommand = new LaTeXCommand('Navigate, select, and edit', undefined, 'edit')
node= this.buildNode(navCommand, [
new LaTeXCommand('SyncTeX from cursor', {command: 'latex-workshop.synctex'}, 'go-to-file'),
new LaTeXCommand('Navigate to matching begin/end', {command: 'latex-workshop.navigate-envpair'}),
new LaTeXCommand('Select current environment content', {command: 'latex-workshop.select-envcontent'}),
new LaTeXCommand('Select current environment name', {command: 'latex-workshop.select-envname'}),
new LaTeXCommand('Close current environment', {command: 'latex-workshop.close-env'}),
new LaTeXCommand('Surround with begin{}...\\end{}', {command: 'latex-workshop.wrap-env'}),
new LaTeXCommand('Insert %!TeX root magic comment', {command: 'latex-workshop.addtexroot'})
])
commands.push(node)

const miscCommand = new LaTeXCommand('Miscellaneous', undefined, 'menu')
node = this.buildNode(miscCommand, [
new LaTeXCommand('Open citation browser', {command: 'latex-workshop.citation'}),
new LaTeXCommand('Count words in LaTeX project', {command: 'latex-workshop.wordcount'}),
new LaTeXCommand('Reveal output folder in OS', {command: 'latex-workshop.revealOutputDir'}, 'folder-opened')
])
commands.push(node)

const bibtexCommand = new LaTeXCommand('BibTeX actions', undefined, 'references')
node = this.buildNode(bibtexCommand, [
new LaTeXCommand('Align bibliography', {command: 'latex-workshop.bibalign'}),
new LaTeXCommand('Sort bibliography', {command: 'latex-workshop.bibsort'}, 'sort-precedence'),
new LaTeXCommand('Align and sort bibliography', {command: 'latex-workshop.bibalignsort'})
])
commands.push(node)
return commands
}
class CommandProvider implements vscode.TreeDataProvider<LaTeXCommand> {
readonly _onDidChangeTreeData: vscode.EventEmitter<LaTeXCommand | undefined> = new vscode.EventEmitter<LaTeXCommand | undefined>()
readonly onDidChangeTreeData: vscode.Event<LaTeXCommand | undefined> = this._onDidChangeTreeData.event

getTreeItem(element: LaTeXCommand): vscode.TreeItem {
const treeItem: vscode.TreeItem = new vscode.TreeItem(element.label, element.collapsibleState)
Expand All @@ -120,7 +94,7 @@ class LaTeXCommanderProvider implements vscode.TreeDataProvider<LaTeXCommand> {

getChildren(element?: LaTeXCommand): LaTeXCommand[] {
if (!element) {
return this.commands
return state.commands
}

return element.children
Expand All @@ -147,3 +121,10 @@ class LaTeXCommand {
}
}
}

const treeDataProvider = new CommandProvider()
const state = {
commands: buildCommandTree(),
view: vscode.window.createTreeView('latex-workshop-commands', { treeDataProvider, showCollapseAll: true }),
treeDataProvider
}
Loading

0 comments on commit 4b955e9

Please sign in to comment.