Skip to content

Commit

Permalink
Add a selectSection command
Browse files Browse the repository at this point in the history
to select the content of the current section level.
  • Loading branch information
jlelong committed Jul 24, 2020
1 parent 620e28f commit 19ab221
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 0 deletions.
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,11 @@
"title": "Demote all the section levels used in the selection",
"category": "LaTeX Workshop"
},
{
"command": "latex-workshop.select-section",
"title": "Select the current section",
"category": "LaTeX Workshop"
},
{
"command": "latex-workshop.showCompilationPanel",
"title": "Show LaTeX Compilation Info",
Expand Down
4 changes: 4 additions & 0 deletions src/commander.ts
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,10 @@ export class Commander {
this.extension.section.shiftSectioningLevel(change)
}

selectSection() {
this.extension.section.selectSection()
}

devParseLog() {
if (vscode.window.activeTextEditor === undefined) {
return
Expand Down
58 changes: 58 additions & 0 deletions src/components/section.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import * as vscode from 'vscode'
import { Extension } from '../main'
import { stripCommentsAndVerbatim } from '../utils/utils'

interface MatchSection {
level: string,
pos: vscode.Position
}

export class Section {
private readonly extension: Extension
Expand Down Expand Up @@ -108,4 +114,56 @@ export class Section {
})
}

private searchLevelUp(levels: string[], pos: vscode.Position, doc: vscode.TextDocument): MatchSection | undefined{

const range = new vscode.Range(new vscode.Position(0, 0), pos.translate(-1, 0))
// range = new vscode.Range(pos, new vscode.Position(doc.lineCount, 0))
const content = stripCommentsAndVerbatim(doc.getText(range)).split('\n')
const pattern = '\\\\(' + levels.join('|') + ')\\*?(?:\\[.+?\\])?\\{.*?\\}'
const regex = new RegExp(pattern)
for (let i = pos.line - 1; i >= 0; i -= 1) {
const line = content[i]
const res = line.match(regex)
if (res) {
return {level: res[1], pos: new vscode.Position(i, 0)}
}
}
return undefined
}


private searchLevelDown(levels: string[], pos: vscode.Position, doc: vscode.TextDocument): vscode.Position {

const range = new vscode.Range(pos, new vscode.Position(doc.lineCount, 0))
const content = stripCommentsAndVerbatim(doc.getText(range)).split('\n')
const pattern = '\\\\(?:(' + levels.join('|') + ')\\*?(?:\\[.+?\\])?\\{.*?\\})|appendix|\\\\end{document}'
const regex = new RegExp(pattern)
for (let i = 0; i < content.length; i += 1) {
const line = content[i]
const res = line.match(regex)
if (res) {
return new vscode.Position(i + pos.line - 1, Math.max(content[i-1].length - 1, 0))
}
}
return new vscode.Position(doc.lineCount - 1, doc.lineAt(doc.lineCount - 1).text.length - 1)
}

selectSection() {
this.extension.logger.addLogMessage('Calling selectSection.')

const editor = vscode.window.activeTextEditor
if (editor === undefined) {
return
}
const beginLevel = this.searchLevelUp(this.levels, editor.selection.anchor, editor.document)
if (!beginLevel) {
this.extension.logger.addLogMessage('Cannot find any section command above current line.')
return
}
const levelIndex = this.levels.indexOf(beginLevel.level)
const levels = this.levels.slice(0, levelIndex + 1)
const endPosition = this.searchLevelDown(levels, editor.selection.end, editor.document)
editor.selection = new vscode.Selection(beginLevel.pos, endPosition)
}

}
1 change: 1 addition & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ export function activate(context: vscode.ExtensionContext) {

vscode.commands.registerCommand('latex-workshop.promote-sectioning', () => extension.commander.shiftSectioningLevel('promote'))
vscode.commands.registerCommand('latex-workshop.demote-sectioning', () => extension.commander.shiftSectioningLevel('demote'))
vscode.commands.registerCommand('latex-workshop.select-section', () => extension.commander.selectSection())

vscode.commands.registerCommand('latex-workshop.showCompilationPanel', () => extension.buildInfo.showPanel())
vscode.commands.registerCommand('latex-workshop.showSnippetPanel', () => extension.snippetPanel.showPanel())
Expand Down

0 comments on commit 19ab221

Please sign in to comment.