Skip to content

Commit

Permalink
Merge branch 'tweak_onenter'
Browse files Browse the repository at this point in the history
  • Loading branch information
tamuratak committed Oct 6, 2024
2 parents 7f2b329 + 9c278e2 commit 4df40a3
Show file tree
Hide file tree
Showing 4 changed files with 2 additions and 77 deletions.
4 changes: 2 additions & 2 deletions data/environments.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
},
"enumerate": {
"name": "enumerate",
"snippet": "\n\t\\item $0"
"snippet": "\n\t\\item $0\n\t\\item \n\t\\item "
},
"itemize": {
"name": "itemize",
"snippet": "\n\t\\item $0"
"snippet": "\n\t\\item $0\n\t\\item \n\t\\item "
},
"math": {
"name": "math"
Expand Down
16 changes: 0 additions & 16 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -352,16 +352,6 @@
"mac": "cmd+alt+j",
"command": "latex-toybox.synctex",
"when": "editorTextFocus && editorLangId == 'latex' && !config.latex-toybox.bind.altKeymap.enabled && !virtualWorkspace"
},
{
"key": "enter",
"command": "latex-toybox.onEnterKey",
"when": "latex-toybox:enabled && config.latex-toybox.bind.enter.key && editorTextFocus && (acceptSuggestionOnEnter && !suggestWidgetVisible || !acceptSuggestionOnEnter) && !editorReadonly && editorLangId =~ /^latex$|^latex-expl3$/ && (vim.active && vim.mode == 'Insert' || !vim.active)"
},
{
"key": "alt+enter",
"command": "latex-toybox.onAltEnterKey",
"when": "latex-toybox:enabled && config.latex-toybox.bind.enter.key && editorTextFocus && (acceptSuggestionOnEnter && !suggestWidgetVisible || !acceptSuggestionOnEnter) && !editorReadonly && editorLangId =~ /^latex$|^latex-expl3$/"
}
],
"configurationDefaults": {
Expand Down Expand Up @@ -1253,12 +1243,6 @@
"default": false,
"markdownDescription": "Enable the LaTeX contextual menu. This menu is deactivated as it is available through the new LaTeX badge. Just set this variable to `true` to recover the menu."
},
"latex-toybox.bind.enter.key": {
"scope": "window",
"type": "boolean",
"default": true,
"markdownDescription": "Enable the automatic insertion of `\\item` on a newline when pressing `Enter` in a line starting in `\\item`."
},
"latex-toybox.bind.altKeymap.enabled": {
"scope": "window",
"type": "boolean",
Expand Down
57 changes: 0 additions & 57 deletions src/commander.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,63 +272,6 @@ export class Commander {
return this.extension.envPair.gotoPair()
}

/**
* If the current line starts with \item or \item[], do the same for
* the new line when hitting enter.
* Note that hitting enter on a line containing only \item or \item[]
* actually deletes the content of the line.
*/
onEnterKey(modifiers?: string) {
const editor = vscode.window.activeTextEditor
if (!editor) {
return
}
const configuration = vscode.workspace.getConfiguration('latex-toybox')
if (!configuration.get('bind.enter.key')) {
return vscode.commands.executeCommand('type', { source: 'keyboard', text: '\n' })
}
if (modifiers === 'alt') {
return vscode.commands.executeCommand('editor.action.insertLineAfter')
}

// Test if every cursor is at the end of a line starting with \item
const allCursorsOnItem = editor.selections.every((selection: vscode.Selection) => {
const cursorPos = selection.active
const line = editor.document.lineAt(cursorPos.line)
return /^\s*\\item/.test(line.text) && (line.text.substring(cursorPos.character).trim().length === 0)
})
if (!allCursorsOnItem) {
return vscode.commands.executeCommand('type', { source: 'keyboard', text: '\n' })
}

return editor.edit(editBuilder => {
// If we arrive here, all the cursors are at the end of a line starting with `\s*\\item`.
// Yet, we keep the conditions for the sake of maintenance.
for (const selection of editor.selections) {
const cursorPos = selection.active
const line = editor.document.lineAt(cursorPos.line)
const indentation = line.text.substring(0, line.firstNonWhitespaceCharacterIndex)

if (/^\s*\\item(\[\s*\])?\s*$/.test(line.text)) {
// The line is an empty \item or \item[]
const rangeToDelete = line.range.with(cursorPos.with(line.lineNumber, line.firstNonWhitespaceCharacterIndex), line.range.end)
editBuilder.delete(rangeToDelete)
} else if(/^\s*\\item\[[^[\]]*\]/.test(line.text)) {
// The line starts with \item[blabla] or \item[] blabla
const itemString = `\n${indentation}\\item[] `
editBuilder.insert(cursorPos, itemString)
} else if(/^\s*\\item\s*[^\s]+.*$/.test(line.text)) {
// The line starts with \item blabla
const itemString = `\n${indentation}\\item `
editBuilder.insert(cursorPos, itemString)
} else {
// If we do not know what to do, insert a newline and indent using the current indentation
editBuilder.insert(cursorPos, `\n${indentation}`)
}
}
})
}

/**
* Shift the level sectioning in the selection by one (up or down)
* @param change
Expand Down
2 changes: 0 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,6 @@ function registerLatexToyboxCommands(
vscode.commands.registerCommand('latex-toybox.compilerlog', () => extension.commander.log('compiler')),
vscode.commands.registerCommand('latex-toybox.goto-section', (filePath: string, lineNumber: number) => extension.commander.gotoSection(filePath, lineNumber)),
vscode.commands.registerCommand('latex-toybox.navigate-envpair', () => extension.commander.navigateToEnvPair()),
vscode.commands.registerCommand('latex-toybox.onEnterKey', () => extension.commander.onEnterKey()),
vscode.commands.registerCommand('latex-toybox.onAltEnterKey', () => extension.commander.onEnterKey('alt')),
vscode.commands.registerCommand('latex-toybox.revealOutputDir', () => extension.commander.revealOutputDir()),

vscode.commands.registerCommand('latex-toybox.promote-sectioning', () => extension.commander.shiftSectioningLevel('promote')),
Expand Down

0 comments on commit 4df40a3

Please sign in to comment.