-
Notifications
You must be signed in to change notification settings - Fork 535
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Duplabel check run only once, wording change
- Loading branch information
Showing
7 changed files
with
100 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters