Skip to content

Commit

Permalink
Duplabel check run only once, wording change
Browse files Browse the repository at this point in the history
  • Loading branch information
James-Yu committed Oct 4, 2023
1 parent 9b529ba commit 1b2e886
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 111 deletions.
4 changes: 2 additions & 2 deletions src/components/cacher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ export class Cacher {
this.promises[filePath] = this.updateAST(cache).then(() => {
this.updateElements(cache)
}).finally(() => {
lw.dupLabelDetector.run()
this.caching--
delete this.promises[filePath]
lw.eventBus.fire(eventbus.FileParsed, filePath)
Expand Down Expand Up @@ -241,14 +242,13 @@ export class Cacher {

private updateElements(cache: Cache) {
const start = performance.now()
lw.completer.citation.update(cache.filePath, cache.content)
lw.completer.citation.parse(cache)
// Package parsing must be before command and environment.
lw.completer.package.parse(cache)
lw.completer.reference.parse(cache)
lw.completer.glossary.parse(cache)
lw.completer.environment.parse(cache)
lw.completer.command.parse(cache)
lw.duplicateLabels.run(cache.filePath)
this.updateBibfiles(cache)
const elapsed = performance.now() - start
logger.log(`Updated elements in ${elapsed.toFixed(2)} ms: ${cache.filePath} .`)
Expand Down
89 changes: 89 additions & 0 deletions src/components/duplabeldetector.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import * as vscode from 'vscode'
import * as path from 'path'
import * as lw from '../lw'

const duplicatedLabelsDiagnostics = vscode.languages.createDiagnosticCollection('Duplicate Labels')

export const dupLabelDetector = {
run,
reset
}

/**
* Compute the dictionary of labels
*/
function computeDuplicates(): string[] {
const labelsCount = new Map<string, number>()
lw.cacher.getIncludedTeX().forEach(cachedFile => {
const cachedRefs = lw.cacher.get(cachedFile)?.elements.reference
if (cachedRefs === undefined) {
return
}
cachedRefs.forEach(ref => {
if (ref.range === undefined) {
return
}
let count = labelsCount.get(ref.label)
if (count === undefined) {
count = 0
}
count += 1
labelsCount.set(ref.label, count)
})
})
const duplicates = []
for (const [label, count] of labelsCount) {
if (count > 1) {
duplicates.push(label)
}
}
return duplicates
}

function run() {
const configuration = vscode.workspace.getConfiguration('latex-workshop')
if (!configuration.get('check.duplicatedLabels.enabled')) {
return
}
const duplicates = computeDuplicates()
showDiagnostics(duplicates)
}

function showDiagnostics(duplicates: string[]) {
duplicatedLabelsDiagnostics.clear()
if (duplicates.length === 0) {
return
}
const diagsCollection = Object.create(null) as { [key: string]: vscode.Diagnostic[] }

lw.cacher.getIncludedTeX().forEach(cachedFile => {
const cachedRefs = lw.cacher.get(cachedFile)?.elements.reference
if (cachedRefs === undefined) {
return
}
cachedRefs.forEach(ref => {
if (ref.range === undefined) {
return
}
if (duplicates.includes(ref.label)) {
if (! (cachedFile in diagsCollection)) {
diagsCollection[cachedFile] = []
}
const range = ref.range instanceof vscode.Range ? ref.range : ref.range.inserting
const diag = new vscode.Diagnostic(range, `Duplicate label ${ref.label}`, vscode.DiagnosticSeverity.Warning)
diag.source = 'DuplicateLabels'
diagsCollection[cachedFile].push(diag)
}
})
})

for (const file in diagsCollection) {
if (path.extname(file) === '.tex') {
duplicatedLabelsDiagnostics.set(vscode.Uri.file(file), diagsCollection[file])
}
}
}

function reset() {
duplicatedLabelsDiagnostics.clear()
}
94 changes: 0 additions & 94 deletions src/components/duplicatelabels.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/components/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ export class Manager {

// We also clean the completions from the old project
lw.completer.input.reset()
lw.duplicateLabels.reset()
lw.dupLabelDetector.reset()
lw.cacher.src.reset()
lw.cacher.add(rootFile)
void lw.cacher.refreshCache(rootFile).then(async () => {
Expand Down
3 changes: 1 addition & 2 deletions src/lw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Cleaner } from './components/cleaner'
import { LaTeXCommanderTreeView } from './components/commander'
import { Configuration } from './components/configuration'
import { Counter } from './components/counter'
import { DuplicateLabels } from './components/duplicatelabels'
export { dupLabelDetector } from './components/duplabeldetector'
import { EnvPair } from './components/envpair'
import { EventBus } from './components/eventbus'
import { Linter } from './components/linter'
Expand Down Expand Up @@ -55,7 +55,6 @@ export const server = new Server()
export const locator = new Locator()
export const completer = new Completer()
export const atSuggestionCompleter = new AtSuggestionCompleter()
export const duplicateLabels = new DuplicateLabels()
export const linter = new Linter()
export const cleaner = new Cleaner()
export const counter = new Counter()
Expand Down
17 changes: 6 additions & 11 deletions src/providers/completer/citation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {computeFilteringRange} from './completerutils'
import type { IProvider, ICompletionItem, IProviderArgs } from '../completion'
import { getLogger } from '../../components/logger'
import { parser } from '../../components/parser'
import { Cache } from '../../components/cacher'

const logger = getLogger('Intelli', 'Citation')

Expand Down Expand Up @@ -342,18 +343,12 @@ export class Citation implements IProvider {
}

/**
* Updates the Manager cache for bibitems defined in `file`.
* `content` is parsed with regular expressions,
* and the result is used to update the cache.
*
* @param file The path of a LaTeX file.
* @param content The content of a LaTeX file.
* Updates the Manager cache for bibitems with Cache.
* Cache `content` is parsed with regular expressions,
* and the result is used to update the cache bibitem element.
*/
update(file: string, content: string) {
const cache = lw.cacher.get(file)
if (cache !== undefined) {
cache.elements.bibitem = this.parseContent(file, content)
}
parse(cache: Cache) {
cache.elements.bibitem = this.parseContent(cache.filePath, cache.content)
}

private parseContent(file: string, content: string): CiteSuggestion[] {
Expand Down
2 changes: 1 addition & 1 deletion test/suites/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export async function reset() {
lw.manager.rootFile = undefined
lw.manager.localRootFile = undefined
lw.completer.input.reset()
lw.duplicateLabels.reset()
lw.dupLabelDetector.reset()
lw.cacher.reset()
glob.sync('**/{**.tex,**.pdf,**.bib}', { cwd: getFixture() }).forEach(file => { try {fs.unlinkSync(path.resolve(getFixture(), file))} catch {} })
}
Expand Down

0 comments on commit 1b2e886

Please sign in to comment.