Skip to content

Commit

Permalink
Modularize linting features
Browse files Browse the repository at this point in the history
  • Loading branch information
James-Yu committed Dec 12, 2023
1 parent be733a3 commit c3bc8b7
Show file tree
Hide file tree
Showing 17 changed files with 273 additions and 250 deletions.
2 changes: 1 addition & 1 deletion src/core/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ async function refreshCache(filePath: string, rootPath?: string): Promise<Promis
updateAST(fileCache).then(() => {
updateElements(fileCache)
}).finally(() => {
lw.dupLabelDetector.run()
lw.lint.label.check()
cachingFilesCount--
promises.delete(filePath)
lw.event.fire(lw.event.FileParsed, filePath)
Expand Down
2 changes: 1 addition & 1 deletion src/core/root.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ async function find(): Promise<undefined> {

// We also clean the completions from the old project
lw.completer.input.reset()
lw.dupLabelDetector.reset()
lw.lint.label.reset()
lw.cache.reset()
lw.cache.add(rootFilePath)
void lw.cache.refreshCache(rootFilePath).then(async () => {
Expand Down
22 changes: 14 additions & 8 deletions src/lint/bibtex-formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,22 @@ import * as vscode from 'vscode'
import { bibtexParser } from 'latex-utensils'
import { performance } from 'perf_hooks'
import { lw } from '../lw'
import * as BibtexUtils from './bibtexformatterlib/bibtexutils'
import { bibtexFormat, bibtexSort, getBibtexFormatConfig } from './bibtex-formatter/utils'
import type { BibtexEntry } from './bibtex-formatter/utils'

import { parser } from '../parse/parser'

const logger = lw.log('Format', 'Bib')

export {
format,
formattingProvider as formatter
}

const duplicatesDiagnostics: vscode.DiagnosticCollection = vscode.languages.createDiagnosticCollection('BibTeX')
const diags: vscode.Diagnostic[] = []

export async function bibtexFormat(sort: boolean, align: boolean) {
async function format(sort: boolean, align: boolean) {
if (!vscode.window.activeTextEditor) {
logger.log('Exit formatting. The active textEditor is undefined.')
return
Expand Down Expand Up @@ -47,7 +53,7 @@ export async function bibtexFormat(sort: boolean, align: boolean) {

async function formatDocument(document: vscode.TextDocument, sort: boolean, align: boolean, range?: vscode.Range): Promise<vscode.TextEdit[]> {
// Get configuration
const formatConfig = BibtexUtils.getBibtexFormatConfig(document.uri)
const formatConfig = getBibtexFormatConfig(document.uri)
const config = vscode.workspace.getConfiguration('latex-workshop', document)
const handleDuplicates = config.get('bibtex-format.handleDuplicates') as 'Ignore Duplicates' | 'Highlight Duplicates' | 'Comment Duplicates'
const lineOffset = range ? range.start.line : 0
Expand All @@ -60,7 +66,7 @@ async function formatDocument(document: vscode.TextDocument, sort: boolean, alig
}
logger.log(`Parsed ${ast.content.length} AST items.`)
// Create an array of entries and of their starting locations
const entries: BibtexUtils.BibtexEntry[] = []
const entries: BibtexEntry[] = []
const entryLocations: vscode.Range[] = []
ast.content.forEach(item => {
if (bibtexParser.isEntry(item) || bibtexParser.isStringEntry(item)) {
Expand All @@ -78,7 +84,7 @@ async function formatDocument(document: vscode.TextDocument, sort: boolean, alig
let sortedEntryLocations: vscode.Range[] = []
const duplicates = new Set<bibtexParser.Entry>()
if (sort) {
entries.sort(BibtexUtils.bibtexSort(duplicates, formatConfig)).forEach(entry => {
entries.sort(bibtexSort(duplicates, formatConfig)).forEach(entry => {
sortedEntryLocations.push((new vscode.Range(
entry.location.start.line - 1,
entry.location.start.column - 1,
Expand All @@ -99,7 +105,7 @@ async function formatDocument(document: vscode.TextDocument, sort: boolean, alig
for (let i = 0; i < entries.length; i++) {
if (align && bibtexParser.isEntry(entries[i])) {
const entry: bibtexParser.Entry = entries[i] as bibtexParser.Entry
text = BibtexUtils.bibtexFormat(entry, formatConfig)
text = bibtexFormat(entry, formatConfig)
} else {
text = document.getText(sortedEntryLocations[i])
}
Expand Down Expand Up @@ -139,7 +145,7 @@ async function formatDocument(document: vscode.TextDocument, sort: boolean, alig
return edits
}

class BibtexFormatterProvider implements vscode.DocumentFormattingEditProvider, vscode.DocumentRangeFormattingEditProvider {
class FormattingProvider implements vscode.DocumentFormattingEditProvider, vscode.DocumentRangeFormattingEditProvider {
public provideDocumentFormattingEdits(document: vscode.TextDocument, _options: vscode.FormattingOptions, _token: vscode.CancellationToken): vscode.ProviderResult<vscode.TextEdit[]> {
const sort = vscode.workspace.getConfiguration('latex-workshop', document).get('bibtex-format.sort.enabled') as boolean
logger.log('Start bibtex formatting on behalf of VSCode\'s formatter.')
Expand All @@ -153,4 +159,4 @@ class BibtexFormatterProvider implements vscode.DocumentFormattingEditProvider,
}
}

export const bibtexFormatterProvider = new BibtexFormatterProvider()
const formattingProvider = new FormattingProvider()
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function getBibtexFormatTab(tab: string): string | undefined {
}
}

export type BibtexFormatConfig = {
type BibtexFormatConfig = {
tab: string,
left: string,
right: string,
Expand Down
4 changes: 2 additions & 2 deletions src/lint/duplicate-label.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { lw } from '../lw'
const duplicatedLabelsDiagnostics = vscode.languages.createDiagnosticCollection('Duplicate Labels')

export const dupLabelDetector = {
run,
check,
reset
}

Expand Down Expand Up @@ -40,7 +40,7 @@ function computeDuplicates(): string[] {
return duplicates
}

function run() {
function check() {
const configuration = vscode.workspace.getConfiguration('latex-workshop')
if (!configuration.get('check.duplicatedLabels.enabled')) {
return
Expand Down
19 changes: 19 additions & 0 deletions src/lint/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { lint as latexLinter } from './latex-linter'
import { formatter as latexFormatter } from './latex-formatter'
import { provider as latexActionProvider, action as latexAction } from './latex-code-actions'
import { format as bibtexFormat, formatter as bibtexFormatter } from './bibtex-formatter'
import { dupLabelDetector } from './duplicate-label'

export const lint = {
latex: {
formatter: latexFormatter,
actionprovider: latexActionProvider,
action: latexAction,
...latexLinter
},
bibtex: {
format: bibtexFormat,
formatter: bibtexFormatter
},
label: dupLabelDetector
}
Loading

0 comments on commit c3bc8b7

Please sign in to comment.