|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | +'use strict'; |
| 6 | + |
| 7 | +import { ReplaceCommand } from 'vs/editor/common/commands/replaceCommand'; |
| 8 | +import { CursorMove, CursorMoveConfiguration, ICursorMoveHelperModel } from 'vs/editor/common/controller/cursorMoveHelper'; |
| 9 | +import { Range } from 'vs/editor/common/core/range'; |
| 10 | +import { CursorChangeReason } from 'vs/editor/common/editorCommon'; |
| 11 | +import { CursorModelState } from 'vs/editor/common/controller/oneCursor'; |
| 12 | +import * as strings from 'vs/base/common/strings'; |
| 13 | +import { EditOperationResult } from 'vs/editor/common/controller/cursorDeleteOperations'; |
| 14 | +import { ShiftCommand } from 'vs/editor/common/commands/shiftCommand'; |
| 15 | +import { Selection } from 'vs/editor/common/core/selection'; |
| 16 | +import { LanguageConfigurationRegistry } from 'vs/editor/common/modes/languageConfigurationRegistry'; |
| 17 | +import { ITokenizedModel } from 'vs/editor/common/editorCommon'; |
| 18 | +import { IndentAction } from 'vs/editor/common/modes/languageConfiguration'; |
| 19 | + |
| 20 | +export class TypeOperations { |
| 21 | + |
| 22 | + public static indent(config: CursorMoveConfiguration, model: ICursorMoveHelperModel, cursor: CursorModelState): EditOperationResult { |
| 23 | + return new EditOperationResult( |
| 24 | + new ShiftCommand(cursor.selection, { |
| 25 | + isUnshift: false, |
| 26 | + tabSize: config.tabSize, |
| 27 | + oneIndent: config.oneIndent |
| 28 | + }), { |
| 29 | + shouldPushStackElementBefore: true, |
| 30 | + shouldPushStackElementAfter: true, |
| 31 | + shouldRevealHorizontal: false |
| 32 | + } |
| 33 | + ); |
| 34 | + } |
| 35 | + |
| 36 | + public static outdent(config: CursorMoveConfiguration, model: ICursorMoveHelperModel, cursor: CursorModelState): EditOperationResult { |
| 37 | + return new EditOperationResult( |
| 38 | + new ShiftCommand(cursor.selection, { |
| 39 | + isUnshift: true, |
| 40 | + tabSize: config.tabSize, |
| 41 | + oneIndent: config.oneIndent |
| 42 | + }), { |
| 43 | + shouldPushStackElementBefore: true, |
| 44 | + shouldPushStackElementAfter: true, |
| 45 | + shouldRevealHorizontal: false |
| 46 | + } |
| 47 | + ); |
| 48 | + } |
| 49 | + |
| 50 | + public static paste(config: CursorMoveConfiguration, model: ICursorMoveHelperModel, cursor: CursorModelState, text: string, pasteOnNewLine: boolean): EditOperationResult { |
| 51 | + let position = cursor.position; |
| 52 | + let selection = cursor.selection; |
| 53 | + |
| 54 | + if (pasteOnNewLine && text.indexOf('\n') !== text.length - 1) { |
| 55 | + pasteOnNewLine = false; |
| 56 | + } |
| 57 | + if (pasteOnNewLine && selection.startLineNumber !== selection.endLineNumber) { |
| 58 | + pasteOnNewLine = false; |
| 59 | + } |
| 60 | + if (pasteOnNewLine && selection.startColumn === model.getLineMinColumn(selection.startLineNumber) && selection.endColumn === model.getLineMaxColumn(selection.startLineNumber)) { |
| 61 | + pasteOnNewLine = false; |
| 62 | + } |
| 63 | + |
| 64 | + if (pasteOnNewLine) { |
| 65 | + // Paste entire line at the beginning of line |
| 66 | + |
| 67 | + let typeSelection = new Range(position.lineNumber, 1, position.lineNumber, 1); |
| 68 | + return new EditOperationResult(new ReplaceCommand(typeSelection, text), { |
| 69 | + shouldPushStackElementBefore: true, |
| 70 | + shouldPushStackElementAfter: true, |
| 71 | + cursorPositionChangeReason: CursorChangeReason.Paste |
| 72 | + }); |
| 73 | + } |
| 74 | + |
| 75 | + return new EditOperationResult(new ReplaceCommand(selection, text), { |
| 76 | + shouldPushStackElementBefore: true, |
| 77 | + shouldPushStackElementAfter: true, |
| 78 | + cursorPositionChangeReason: CursorChangeReason.Paste |
| 79 | + }); |
| 80 | + } |
| 81 | + |
| 82 | + private static _goodIndentForLine(config: CursorMoveConfiguration, model: ITokenizedModel, lineNumber: number): string { |
| 83 | + let lastLineNumber = lineNumber - 1; |
| 84 | + |
| 85 | + for (lastLineNumber = lineNumber - 1; lastLineNumber >= 1; lastLineNumber--) { |
| 86 | + let lineText = model.getLineContent(lastLineNumber); |
| 87 | + let nonWhitespaceIdx = strings.lastNonWhitespaceIndex(lineText); |
| 88 | + if (nonWhitespaceIdx >= 0) { |
| 89 | + break; |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + if (lastLineNumber < 1) { |
| 94 | + // No previous line with content found |
| 95 | + return '\t'; |
| 96 | + } |
| 97 | + |
| 98 | + let r = LanguageConfigurationRegistry.getEnterActionAtPosition(model, lastLineNumber, model.getLineMaxColumn(lastLineNumber)); |
| 99 | + |
| 100 | + let indentation: string; |
| 101 | + if (r.enterAction.indentAction === IndentAction.Outdent) { |
| 102 | + let desiredIndentCount = ShiftCommand.unshiftIndentCount(r.indentation, r.indentation.length, config.tabSize); |
| 103 | + indentation = ''; |
| 104 | + for (let i = 0; i < desiredIndentCount; i++) { |
| 105 | + indentation += '\t'; |
| 106 | + } |
| 107 | + indentation = config.normalizeIndentation(indentation); |
| 108 | + } else { |
| 109 | + indentation = r.indentation; |
| 110 | + } |
| 111 | + |
| 112 | + let result = indentation + r.enterAction.appendText; |
| 113 | + if (result.length === 0) { |
| 114 | + // good position is at column 1, but we gotta do something... |
| 115 | + return '\t'; |
| 116 | + } |
| 117 | + return result; |
| 118 | + } |
| 119 | + |
| 120 | + private static _replaceJumpToNextIndent(config: CursorMoveConfiguration, model: ICursorMoveHelperModel, selection: Selection): ReplaceCommand { |
| 121 | + let typeText = ''; |
| 122 | + |
| 123 | + let position = selection.getStartPosition(); |
| 124 | + if (config.insertSpaces) { |
| 125 | + let visibleColumnFromColumn = CursorMove.visibleColumnFromColumn2(config, model, position); |
| 126 | + let tabSize = config.tabSize; |
| 127 | + let spacesCnt = tabSize - (visibleColumnFromColumn % tabSize); |
| 128 | + for (let i = 0; i < spacesCnt; i++) { |
| 129 | + typeText += ' '; |
| 130 | + } |
| 131 | + } else { |
| 132 | + typeText = '\t'; |
| 133 | + } |
| 134 | + |
| 135 | + return new ReplaceCommand(selection, typeText); |
| 136 | + } |
| 137 | + |
| 138 | + public static tab(config: CursorMoveConfiguration, model: ITokenizedModel, cursor: CursorModelState): EditOperationResult { |
| 139 | + let selection = cursor.selection; |
| 140 | + |
| 141 | + if (selection.isEmpty()) { |
| 142 | + |
| 143 | + let lineText = model.getLineContent(selection.startLineNumber); |
| 144 | + |
| 145 | + if (/^\s*$/.test(lineText)) { |
| 146 | + let possibleTypeText = config.normalizeIndentation(this._goodIndentForLine(config, model, selection.startLineNumber)); |
| 147 | + if (!strings.startsWith(lineText, possibleTypeText)) { |
| 148 | + let command = new ReplaceCommand(new Range(selection.startLineNumber, 1, selection.startLineNumber, lineText.length + 1), possibleTypeText); |
| 149 | + return new EditOperationResult(command, { |
| 150 | + shouldPushStackElementBefore: false, |
| 151 | + shouldPushStackElementAfter: false, |
| 152 | + isAutoWhitespaceCommand: true |
| 153 | + }); |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + return new EditOperationResult(this._replaceJumpToNextIndent(config, model, selection), { |
| 158 | + shouldPushStackElementBefore: false, |
| 159 | + shouldPushStackElementAfter: false, |
| 160 | + isAutoWhitespaceCommand: true |
| 161 | + }); |
| 162 | + } else { |
| 163 | + if (selection.startLineNumber === selection.endLineNumber) { |
| 164 | + let lineMaxColumn = model.getLineMaxColumn(selection.startLineNumber); |
| 165 | + if (selection.startColumn !== 1 || selection.endColumn !== lineMaxColumn) { |
| 166 | + // This is a single line selection that is not the entire line |
| 167 | + return new EditOperationResult(this._replaceJumpToNextIndent(config, model, selection)); |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + return this.indent(config, model, cursor); |
| 172 | + } |
| 173 | + } |
| 174 | +} |
0 commit comments