Skip to content

Commit

Permalink
Split continued line at cursor position
Browse files Browse the repository at this point in the history
Fixes #1137
  • Loading branch information
PEZ committed Apr 21, 2021
1 parent 746f341 commit 72f322e
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/edit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ import * as vscode from 'vscode';
import * as util from './utilities';
import * as docMirror from './doc-mirror/index';

// Relies on that `when` claus guards this from being called
// when the cursor is before the comment marker
export function continueCommentCommand() {
const document = util.getDocument({});
if (document && document.languageId === 'clojure') {
const editor = vscode.window.activeTextEditor;
const position = editor.selection.active;
const eolPosition = position.with(position.line, Infinity);
const cursor = docMirror.getDocument(document).getTokenCursor();
if (!(cursor.getToken().type === 'comment')) {
if (cursor.getToken().type !== 'comment') {
if (cursor.getPrevToken().type === 'comment') {
cursor.previous();
} else {
Expand All @@ -20,17 +21,17 @@ export function continueCommentCommand() {
}
}
const commentOffset = cursor.rowCol[1];
const commentText = cursor.getToken().raw;
const commentText = cursor.getToken().raw.substring(0, commentOffset);
const [_1, startText, bullet, num] =
commentText.match(/^([;\s]+)([*-] +|(\d+)\. +)?/);
const newNum = num ? parseInt(num) + 1 : undefined;
const bulletText = newNum ? bullet.replace(/\d+/, '' + newNum) : bullet;
const pad = ' '.repeat(commentOffset);
const newText = `${pad}${startText}${bullet ? bulletText : ''}`;
editor.edit(edits => edits.insert(eolPosition, `\n${newText}`), { undoStopAfter: false, undoStopBefore: true }).then(fulfilled => {
editor.edit(edits => edits.insert(position, `\n${newText}`), { undoStopAfter: false, undoStopBefore: true }).then(fulfilled => {
if (fulfilled) {
const newEolPosition = eolPosition.with(eolPosition.line + 1, newText.length);
editor.selection = new vscode.Selection(newEolPosition, newEolPosition);
const newPosition = position.with(position.line + 1, newText.length);
editor.selection = new vscode.Selection(newPosition, newPosition);
}
});
}
Expand Down

0 comments on commit 72f322e

Please sign in to comment.