-
Notifications
You must be signed in to change notification settings - Fork 537
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
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
79146f7
support code folding for DocTeX
huangyxi 9e1b5f8
DoctexFoldingProvider extends FoldingProvider
huangyxi e04da9a
support meta-comment folding
huangyxi 6bc3420
avoid potential keyword collision
huangyxi b86048d
finetune folding range
huangyxi c66dc1a
fork getEnvironmentFoldingRanges
huangyxi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
||
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( | ||
|
@@ -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) | ||
|
@@ -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 | ||
|
@@ -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] | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. They are only for DocTeX |
||
} | ||
|
||
const item = { | ||
|
@@ -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' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
@@ -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, | ||
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?