Update environment name selection

The former select-envname is renamed to multicursor-envname and sets a
multicursor to the beginning of the environment name in the 'begin' and
'end' parts.

The new function select-envname selects the environment name. This
function does not work with the Vim extension.

This closes #612.
This commit is contained in:
Jerome Lelong 2018-05-10 16:02:17 +02:00
parent 0be87bf2fe
commit 3751cc9610
5 changed files with 52 additions and 19 deletions

View File

@ -24,7 +24,8 @@ LaTeX Workshop is an extension for [Visual Studio Code](https://code.visualstudi
- LaTeX file formatting.
- Auto close LaTeX environments: just call _LaTeX Workshop: Close current environment_ from the **Command Palette**. You can easily assign a shortcut to this action through the **Keyboard Shortcuts** menu, search for `latex-workshop.close-env`.
- Navigate from `\begin/\end` to the corresponding `\end/\begin`: while on the `begin` or `end` keyword, call _LaTeX Workshop: Navigate to matching begin/end_ from the **Command Palette**. To define a shortcut, search for `latex-workshop.navigate-envpair` in the **Keyboard Shortcuts** menu.
- Add a multicursor to the current environment name: call _LaTeX Workshop: Multicursor selection of the current environment name_ from the **Command Palette**. For this command to work, the cursor must be strictly between `\begin{...}` and `\end{...}`. To define a shortcut, search for `latex-workshop.select-envname` in the **Keyboard Shortcuts** menu. Repeated calls result in selecting the outer environments.
- Select the current environment name: call _LaTeX Workshop: Select the current environment name_ from the **Command Palette**. For this command to work, the cursor must be strictly between `\begin{...}` and `\end{...}`. To define a shortcut, search for `latex-workshop.select-envname` in the **Keyboard Shortcuts** menu. Repeated calls result in selecting the outer environments. **Note**: this function _does not_ work with the [Vim](https://github.com/VSCodeVim/Vim) extension.
- Add a multicursor to the current environment name: call _LaTeX Workshop: Multicursor selection of the current environment name_ from the **Command Palette**. For this command to work, the cursor must be strictly between `\begin{...}` and `\end{...}`. To define a shortcut, search for `latex-workshop.multicursor-envname` in the **Keyboard Shortcuts** menu. Repeated calls result in selecting the outer environments.
## Requirements

View File

@ -144,7 +144,12 @@
},
{
"command": "latex-workshop.select-envname",
"title": "Multicursor selection of the current environment name",
"title": "Select the current environment name",
"category": "LaTeX Workshop"
},
{
"command": "latex-workshop.multicursor-envname",
"title": "Add a multicursor to the current environment name",
"category": "LaTeX Workshop"
},
{

View File

@ -238,7 +238,15 @@ export class Commander {
if (!vscode.window.activeTextEditor || !this.extension.manager.isTex(vscode.window.activeTextEditor.document.fileName)) {
return
}
this.extension.envPair.selectEnvName()
this.extension.envPair.selectEnvName('selection')
}
multiCursorEnvName() {
this.extension.logger.addLogMessage(`MutliCursorEnvName command invoked.`)
if (!vscode.window.activeTextEditor || !this.extension.manager.isTex(vscode.window.activeTextEditor.document.fileName)) {
return
}
this.extension.envPair.selectEnvName('cursor')
}
closeEnv() {

View File

@ -27,11 +27,6 @@ function regexpAllMatches(str: string, reg: RegExp) {
return res
}
interface MatchPair {
str: string
pos: vscode.Position
}
interface MatchEnv {
name: string
type: string // 'begin' or 'end'
@ -81,7 +76,7 @@ export class EnvPair {
return null
}
locateMatchingPair(pattern: string, dir: number, pos: vscode.Position, doc: vscode.TextDocument) : MatchPair | null {
locateMatchingPair(pattern: string, dir: number, pos: vscode.Position, doc: vscode.TextDocument) : MatchEnv | null {
const patRegexp = new RegExp(pattern, 'g')
let lineNumber = pos.line
let nested = 0
@ -105,8 +100,9 @@ export class EnvPair {
if ((m[1] === 'end' && dir === 1) || (m[1] === 'begin' && dir === -1)) {
if (nested === 0) {
const matchPos = new vscode.Position(lineNumber, m.index + 1)
const matchStr = m[0]
return {str: matchStr, pos: matchPos}
const matchName = m[2]
const matchType = m[1]
return {name: matchName, type: matchType, pos: matchPos}
}
nested -= 1
}
@ -120,6 +116,9 @@ export class EnvPair {
return null
}
/**
* While on a 'begin' or 'end' keyword, moves the cursor to the corresponding 'end/begin'
*/
gotoPair() {
const editor = vscode.window.activeTextEditor
if (!editor || editor.document.languageId !== 'latex') {
@ -143,7 +142,14 @@ export class EnvPair {
}
}
selectEnvName() {
/**
* Select or add a multicursor to an environment name
*
* @param selectionOrCursor can be
* - 'selection': the environment name is selected both in the begin and end part
* - 'cursor': a multicursor is added at the beginning of the environment name is selected both in the begin and end part
*/
selectEnvName(selectionOrCursor: string) {
const editor = vscode.window.activeTextEditor
if (!editor || editor.document.languageId !== 'latex') {
return
@ -151,7 +157,7 @@ export class EnvPair {
const curPos = editor.selection.active
const document = editor.document
const pattern = '\\\\(begin|end)\\{[^\\{\\}]*\\}'
const pattern = '\\\\(begin|end)\\{([^\\{\\}]*)\\}'
const dirUp = -1
const beginEnv = this.locateMatchingPair(pattern, dirUp, curPos, document)
if (!beginEnv) {
@ -163,10 +169,22 @@ export class EnvPair {
return
}
const envStartPos = beginEnv.pos.translate(0, 'begin{'.length)
const envEndPos = endEnv.pos.translate(0, 'end{'.length)
editor.selections = [new vscode.Selection(envStartPos, envStartPos), new vscode.Selection(envEndPos, envEndPos)]
editor.revealRange(new vscode.Range(envStartPos, envEndPos))
const beginEnvStartPos = beginEnv.pos.translate(0, 'begin{'.length)
const endEnvStartPos = endEnv.pos.translate(0, 'end{'.length)
switch (selectionOrCursor) {
case 'cursor':
editor.selections = [new vscode.Selection(beginEnvStartPos, beginEnvStartPos), new vscode.Selection(endEnvStartPos, endEnvStartPos)]
break
case 'selection':
const envNameLength = beginEnv.name.length
const beginEnvStopPos = beginEnvStartPos.translate(0, envNameLength)
const endEnvStopPos = endEnvStartPos.translate(0, envNameLength)
editor.selections = [new vscode.Selection(beginEnvStartPos, beginEnvStopPos), new vscode.Selection(endEnvStartPos, endEnvStopPos)]
break
default:
this.extension.logger.addLogMessage(`Error - while selecting environment name`)
}
// editor.revealRange(new vscode.Range(beginEnvStartPos, endEnvStartPos))
}
closeEnv() {
@ -177,11 +195,11 @@ export class EnvPair {
const document = editor.document
const curPos = editor.selection.active
const pattern = '\\\\(begin|end)\\{[^\\{\\}]*\\}'
const pattern = '\\\\(begin|end)\\{([^\\{\\}]*)\\}'
const dir = -1
const resMatchingPair = this.locateMatchingPair(pattern, dir, curPos, document)
if (resMatchingPair) {
const endEnv = resMatchingPair.str.replace('begin', 'end')
const endEnv = '\\end{' + resMatchingPair.name + '}'
const edits = [vscode.TextEdit.insert(curPos, endEnv)]
const uri = document.uri
const edit = new vscode.WorkspaceEdit()

View File

@ -144,6 +144,7 @@ export async function activate(context: vscode.ExtensionContext) {
vscode.commands.registerCommand('latex-workshop.goto-section', (filePath, lineNumber) => extension.commander.gotoSection(filePath, lineNumber))
vscode.commands.registerCommand('latex-workshop.navigate-envpair', () => extension.commander.navigateToEnvPair())
vscode.commands.registerCommand('latex-workshop.select-envname', () => extension.commander.selectEnvName())
vscode.commands.registerCommand('latex-workshop.multicursor-envname', () => extension.commander.multiCursorEnvName())
vscode.commands.registerCommand('latex-workshop.close-env', () => extension.commander.closeEnv())
context.subscriptions.push(vscode.workspace.onDidSaveTextDocument((e: vscode.TextDocument) => {