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

Support code folding for DocTeX #4426

Merged
merged 6 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
34 changes: 29 additions & 5 deletions src/language/folding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import * as vscode from 'vscode'

export class FoldingProvider implements vscode.FoldingRangeProvider {
private readonly sectionRegex: RegExp[] = []
protected readonly envRegex = /\\(begin){(.*?)}|\\(begingroup)[%\s\\]|\\(end){(.*?)}|\\(endgroup)[%\s\\]|^%\s*#?([rR]egion)|^%\s*#?([eE]ndregion)|^%\s*<\*([|_()\-a-zA-Z0-9]+)>|^%\s*<\/([|_()\-a-zA-Z0-9]+)>|^%\s*\\iffalse\s*(meta-comment)|^%\s*\\(fi)/gm
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems you add new matches to this regexp. Are they only for DocTeX?


constructor() {
const sections = vscode.workspace.getConfiguration('latex-workshop').get('view.outline.sections') as string[]
this.sectionRegex = sections.map(section => RegExp(`\\\\(?:${section})(?:\\*)?(?:\\[[^\\[\\]\\{\\}]*\\])?{(.*)}`, 'm'))
this.sectionRegex = this.buildSectionRegex(sections)
}

public provideFoldingRanges(
Expand All @@ -16,6 +17,10 @@ export class FoldingProvider implements vscode.FoldingRangeProvider {
return [...this.getSectionFoldingRanges(document), ...this.getEnvironmentFoldingRanges(document)]
}

protected buildSectionRegex(sections: string[]) {
return sections.map(section => RegExp(`\\\\(?:${section})(?:\\*)?(?:\\[[^\\[\\]\\{\\}]*\\])?{(.*)}`, 'm'))
}

private getSectionFoldingRanges(document: vscode.TextDocument) {
const startingIndices: number[] = this.sectionRegex.map(_ => -1)
const lines = document.getText().split(/\r?\n/g)
Expand Down Expand Up @@ -82,10 +87,9 @@ export class FoldingProvider implements vscode.FoldingRangeProvider {
const ranges: vscode.FoldingRange[] = []
const opStack: { keyword: string, index: number }[] = []
const text: string = document.getText()
const envRegex = /\\(begin){(.*?)}|\\(begingroup)[%\s\\]|\\(end){(.*?)}|\\(endgroup)[%\s\\]|^%\s*#?([rR]egion)|^%\s*#?([eE]ndregion)/gm //to match one 'begin' OR 'end'

while (true) {
const match = envRegex.exec(text)
const match = this.envRegex.exec(text)
if (match === null) {
//TODO: if opStack still not empty
return ranges
Expand All @@ -96,6 +100,11 @@ export class FoldingProvider implements vscode.FoldingRangeProvider {
//for 'endgroup': match[6] contains 'endgroup', keyword is 'group'
//for '% region': match[7] contains 'region', keyword is 'region'
//for '% endregion': match[8] contains 'endregion', keyword is 'region'
//DocTeX folding support
//for '%<*abc>': match[9] contains '%<*abc>', keyword is '%<abc>'
//for '%</abc>': match[10] contains '%</abc>', keyword is '%<abc>'
//for '% \iffalse meta-comment': match[11] contains '% \iffalse meta-comment', keyword is '%\\iffalse meta-comment'
//for '% \fi': match[12] contains '% \fi', keyword is '%\\iffalse meta-comment'
let keyword: string = ''
if (match[1]) {
keyword = match[2]
Expand All @@ -105,6 +114,12 @@ export class FoldingProvider implements vscode.FoldingRangeProvider {
keyword = 'group'
} else if (match[7] || match[8]) {
keyword = 'region'
} else if (match[9]) {
keyword = '%<' + match[9] + '>'
} else if (match[10]) {
keyword = '%<' + match[10] + '>'
} else if (match[11] || match[12]) {
keyword = '%\\iffalse meta-comment'
Comment on lines +171 to +176
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They are only for DocTeX

}

const item = {
Expand All @@ -113,11 +128,12 @@ export class FoldingProvider implements vscode.FoldingRangeProvider {
}
const lastItem = opStack[opStack.length - 1]

if ((match[4] || match[6] || match[8]) && lastItem && lastItem.keyword === item.keyword) { // match 'end' with its 'begin'
if ((match[4] || match[6] || match[8] || match[10] || match[12]) && lastItem && lastItem.keyword === item.keyword) { // match 'end' with its 'begin'
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above

const lastLineTune: number = match[10] || match[12] ? 0 : -1
opStack.pop()
ranges.push(new vscode.FoldingRange(
document.positionAt(lastItem.index).line,
document.positionAt(item.index).line - 1
document.positionAt(item.index).line + lastLineTune
))
} else {
opStack.push(item)
Expand All @@ -126,6 +142,14 @@ export class FoldingProvider implements vscode.FoldingRangeProvider {
}
}

export class DoctexFoldingProvider extends FoldingProvider {
protected readonly envRegex: RegExp = /\\(begin){(.*?)}|\\(begingroup)[%\s\\]|\\(end){(.*?)}|\\(endgroup)[%\s\\]|^%\s*#?([rR]egion)|^%\s*#?([eE]ndregion)|^%\s*<\*([|_()\-a-zA-Z0-9]+)>|^%\s*<\/([|_()\-a-zA-Z0-9]+)>|^%\s*\\iffalse\s*(meta-comment)|^%\s*\\(fi)/gm

protected buildSectionRegex(sections: string[]) {
return sections.map(section => RegExp(`%\\s*\\\\(?:${section})(?:\\*)?(?:\\[[^\\[\\]\\{\\}]*\\])?{(.*)}`, 'm'))
}
}

export class WeaveFoldingProvider implements vscode.FoldingRangeProvider {
public provideFoldingRanges(
document: vscode.TextDocument,
Expand Down
3 changes: 2 additions & 1 deletion src/language/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { DocSymbolProvider } from './symbol-document'
import { ProjectSymbolProvider } from './symbol-project'
import { DefinitionProvider } from './definition'
import { FoldingProvider, WeaveFoldingProvider } from './folding'
import { FoldingProvider, DoctexFoldingProvider, WeaveFoldingProvider } from './folding'
import { SelectionRangeProvider } from './selection'
import { getLocaleString } from './l10n'

Expand All @@ -10,6 +10,7 @@ export const language = {
projectSymbol: new ProjectSymbolProvider(),
definition: new DefinitionProvider(),
folding: new FoldingProvider(),
doctexFolding: new DoctexFoldingProvider(),
weaveFolding: new WeaveFoldingProvider(),
selectionRage: new SelectionRangeProvider(),
getLocaleString
Expand Down
3 changes: 2 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,8 @@ function registerProviders(extensionContext: vscode.ExtensionContext) {
extensionContext.subscriptions.push(
vscode.languages.registerCodeActionsProvider(latexSelector, lw.lint.latex.actionprovider),
vscode.languages.registerFoldingRangeProvider(latexSelector, lw.language.folding),
vscode.languages.registerFoldingRangeProvider(weaveSelector, lw.language.weaveFolding)
vscode.languages.registerFoldingRangeProvider(weaveSelector, lw.language.weaveFolding),
vscode.languages.registerFoldingRangeProvider(latexDoctexSelector, lw.language.doctexFolding)
)

const selectionLatex = configuration.get('selection.smart.latex.enabled', true)
Expand Down