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

feat: add check citations command to see unused cites #4384

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,11 @@
"title": "%command.bibalignsort%",
"category": "LaTeX Workshop"
},
{
"command": "latex-workshop.checkcitations",
"title": "%command.checkcitations%",
"category": "LaTeX Workshop"
},
{
"command": "latex-workshop.openMathPreviewPanel",
"title": "%command.openMathPreviewPanel%",
Expand Down
1 change: 1 addition & 0 deletions package.nls.de.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"command.bibsort": "BibTeX-Datei sortieren",
"command.bibalign": "BibTeX-Datei ausrichten",
"command.bibalignsort": "BibTeX-Datei sortieren und ausrichten",
"command.checkcitations": "Auf ungenutzte Zitate prüfen",
"command.openMathPreviewPanel": "Mathematisches Vorschaufenster öffnen",
"command.closeMathPreviewPanel": "Mathematisches Vorschaufenster schließen",
"command.toggleMathPreviewPanel": "Mathematisches Vorschaufenster umschalten",
Expand Down
1 change: 1 addition & 0 deletions package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"command.bibsort": "Sort BibTeX file",
"command.bibalign": "Align BibTeX file",
"command.bibalignsort": "Sort and align BibTeX file",
"command.checkcitations": "Check for unused citations",
"command.openMathPreviewPanel": "Open Math Preview Panel",
"command.closeMathPreviewPanel": "Close Math Preview Panel",
"command.toggleMathPreviewPanel": "Toggle Math Preview Panel",
Expand Down
34 changes: 33 additions & 1 deletion src/core/commands.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as vscode from 'vscode'
import * as path from 'path'
import * as vscode from 'vscode'
import { lw } from '../lw'
import { getSurroundingMacroRange, stripText } from '../utils/utils'

Expand Down Expand Up @@ -428,6 +428,38 @@ export async function devParseBib() {
return vscode.workspace.openTextDocument({content: JSON.stringify(ast, null, 2), language: 'json'}).then(doc => vscode.window.showTextDocument(doc))
}

export async function checkCitations() {
const unused = lw.extra.checkCitations()
if (unused.length === 0){
return
}
const selected = await vscode.window.showQuickPick(unused, {title: 'Unused citations'})
if (!selected){
return
}

const bibFiles = lw.cache.getIncludedBib(lw.root.file.path)
for(const bibFile of bibFiles){
const content = await lw.file.read(bibFile)
if (content && content.includes(selected)){
const doc = await vscode.workspace.openTextDocument(bibFile)

const {line, index} = content
.split(doc.eol === vscode.EndOfLine.LF ? '\n' : '\r\n')
.map((l, i) => ({line: l, index: i}))
.find(({line: l}) => l.includes(selected))!

const start = new vscode.Position(index, line.indexOf(selected))
const end = new vscode.Position(index, line.indexOf(selected) + selected.length)

await vscode.window.showTextDocument(doc, {
selection: new vscode.Range(start, end),
})
return
}
}
}

export async function devStripText() {
if (vscode.window.activeTextEditor === undefined) {
return
Expand Down
3 changes: 2 additions & 1 deletion src/extras/activity-bar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ async function buildCommandTree(): Promise<LaTeXCommand[]> {
node = buildNode(bibtexCommand, [
new LaTeXCommand(await lw.language.getLocaleString('command.bibalign'), {command: 'latex-workshop.bibalign'}),
new LaTeXCommand(await lw.language.getLocaleString('command.bibsort'), {command: 'latex-workshop.bibsort'}, 'sort-precedence'),
new LaTeXCommand(await lw.language.getLocaleString('command.bibalignsort'), {command: 'latex-workshop.bibalignsort'})
new LaTeXCommand(await lw.language.getLocaleString('command.bibalignsort'), {command: 'latex-workshop.bibalignsort'}),
new LaTeXCommand(await lw.language.getLocaleString('command.checkcitations'), {command: 'latex-workshop.checkcitations'})
])
commands.push(node)
return commands
Expand Down
31 changes: 31 additions & 0 deletions src/extras/checkcites.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { EOL } from 'os'
import { lw } from '../lw'

const logger = lw.log('Citations', 'Linter')

export function checkCitations() {
logger.log('Checking citations.')

const auxFile = lw.root.file.path?.replace(/\.tex$/, '.aux')
if (!auxFile) {
logger.log('No aux file found.')
return []
}

const {stdout, error} = lw.external.sync('checkcites', ['-u', auxFile], {
cwd: lw.root.dir.path,
})
if (error) {
logger.logError('Error checking citations.', error)
return []
}

const result = stdout
.toString()
.split(EOL)
.filter(l => l.startsWith('=>'))
.map(l => l.slice(2).trim())

logger.log(`Found ${result.length} unused citation(s).`)
return result
}
12 changes: 7 additions & 5 deletions src/extras/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { count } from './counter'
import * as commands from './activity-bar'
import { checkCitations } from './checkcites'
import { clean } from './cleaner'
import { texdoc } from './texdoc'
import { texroot } from './texroot'
import { count } from './counter'
import { section } from './section'
import * as commands from './activity-bar'
import * as snippet from './snippet-view'
import { texdoc } from './texdoc'
import { texroot } from './texroot'

export const extra = {
count,
checkCitations,
clean,
count,
texdoc,
texroot,
section,
Expand Down
1 change: 1 addition & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ function registerLatexWorkshopCommands(extensionContext: vscode.ExtensionContext
vscode.commands.registerCommand('latex-workshop.bibsort', () => lw.lint.bibtex.format(true, false)),
vscode.commands.registerCommand('latex-workshop.bibalign', () => lw.lint.bibtex.format(false, true)),
vscode.commands.registerCommand('latex-workshop.bibalignsort', () => lw.lint.bibtex.format(true, true)),
vscode.commands.registerCommand('latex-workshop.checkcitations', () => lw.commands.checkCitations()),

vscode.commands.registerCommand('latex-workshop.openMathPreviewPanel', () => lw.commands.openMathPreviewPanel()),
vscode.commands.registerCommand('latex-workshop.closeMathPreviewPanel', () => lw.commands.closeMathPreviewPanel()),
Expand Down