From 9d46f70827b4d0aab73ce94380cacdc08a857c13 Mon Sep 17 00:00:00 2001 From: rebornix Date: Wed, 28 Jun 2017 18:22:14 -0700 Subject: [PATCH] Fix #29755. Adjust cursor position when pressing enter inside leading spaces. --- .../common/controller/cursorTypeOperations.ts | 31 ++++++++++++++++--- .../modes/languageConfigurationRegistry.ts | 2 +- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/src/vs/editor/common/controller/cursorTypeOperations.ts b/src/vs/editor/common/controller/cursorTypeOperations.ts index f7fde973b4e..708a5e83909 100644 --- a/src/vs/editor/common/controller/cursorTypeOperations.ts +++ b/src/vs/editor/common/controller/cursorTypeOperations.ts @@ -310,13 +310,34 @@ export class TypeOperations { let lineText = model.getLineContent(range.startLineNumber); let indentation = strings.getLeadingWhitespace(lineText).substring(0, range.startColumn - 1); + if (ir) { - if (/^\s+$/.test(lineText) || indentation === config.normalizeIndentation(ir.beforeEnter)) { - return TypeOperations._typeCommand(range, '\n' + config.normalizeIndentation(ir.afterEnter), keepPosition); + let beforeText = '\n'; + let isSelectionEmpty = range.isEmpty(); + if (indentation !== config.normalizeIndentation(ir.beforeEnter)) { + beforeText = config.normalizeIndentation(ir.beforeEnter) + lineText.substring(indentation.length, range.startColumn - 1) + '\n'; + range = new Range(range.startLineNumber, 1, range.endLineNumber, range.endColumn); + } + + let newLineContent = model.getLineContent(range.endLineNumber); + let oldEndColumn = CursorColumns.visibleColumnFromColumn2(config, model, range.getEndPosition()); + if (!config.insertSpaces) { + oldEndColumn = Math.ceil(oldEndColumn / config.tabSize) + 1; + } + let firstNonWhitespace = strings.firstNonWhitespaceIndex(newLineContent); + if (firstNonWhitespace >= 0) { + range = range.setEndPosition(range.endLineNumber, Math.max(range.endColumn, firstNonWhitespace + 1)); + } else { + range = range.setEndPosition(range.endLineNumber, model.getLineMaxColumn(range.endLineNumber)); } - let beforeText = config.normalizeIndentation(ir.beforeEnter) + lineText.substring(indentation.length, range.startColumn - 1); - range = new Range(range.startLineNumber, 1, range.endLineNumber, range.endColumn); - return TypeOperations._typeCommand(range, beforeText + '\n' + config.normalizeIndentation(ir.afterEnter), keepPosition); + + if (keepPosition) { + return new ReplaceCommandWithoutChangingPosition(range, beforeText + config.normalizeIndentation(ir.afterEnter), true); + } else { + let offset = isSelectionEmpty ? oldEndColumn - config.normalizeIndentation(ir.afterEnter).length - 1 : 0; + return new ReplaceCommandWithOffsetCursorState(range, beforeText + config.normalizeIndentation(ir.afterEnter), 0, offset, true); + } + } else { return TypeOperations._typeCommand(range, '\n' + config.normalizeIndentation(indentation), keepPosition); } diff --git a/src/vs/editor/common/modes/languageConfigurationRegistry.ts b/src/vs/editor/common/modes/languageConfigurationRegistry.ts index b80e5bb60aa..3fa864931d4 100644 --- a/src/vs/editor/common/modes/languageConfigurationRegistry.ts +++ b/src/vs/editor/common/modes/languageConfigurationRegistry.ts @@ -294,7 +294,7 @@ export class LanguageConfigurationRegistryImpl { return resultLineNumber; } let text = model.getLineContent(lastLineNumber); - if (indentRulesSupport.shouldIgnore(text) || text === '') { + if (indentRulesSupport.shouldIgnore(text) || /^\s+$/.test(text) || text === '') { resultLineNumber = lastLineNumber; continue; } -- GitLab