|
| 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 { Position } from 'vs/editor/common/core/position'; |
| 8 | +import { CharCode } from 'vs/base/common/charCode'; |
| 9 | +import * as strings from 'vs/base/common/strings'; |
| 10 | +import { IModeConfiguration } from 'vs/editor/common/controller/oneCursor'; |
| 11 | +import { IConfigurationChangedEvent, TextModelResolvedOptions, IConfiguration } from 'vs/editor/common/editorCommon'; |
| 12 | +import { TextModel } from 'vs/editor/common/model/textModel'; |
| 13 | + |
| 14 | +export interface CharacterMap { |
| 15 | + [char: string]: string; |
| 16 | +} |
| 17 | + |
| 18 | +export class CursorConfiguration { |
| 19 | + _cursorMoveConfigurationBrand: void; |
| 20 | + |
| 21 | + public readonly tabSize: number; |
| 22 | + public readonly insertSpaces: boolean; |
| 23 | + public readonly oneIndent: string; |
| 24 | + public readonly pageSize: number; |
| 25 | + public readonly useTabStops: boolean; |
| 26 | + public readonly wordSeparators: string; |
| 27 | + public readonly autoClosingBrackets: boolean; |
| 28 | + public readonly autoClosingPairsOpen: CharacterMap; |
| 29 | + |
| 30 | + public static shouldRecreate(e: IConfigurationChangedEvent): boolean { |
| 31 | + return ( |
| 32 | + e.layoutInfo |
| 33 | + || e.wordSeparators |
| 34 | + || e.autoClosingBrackets |
| 35 | + || e.useTabStops |
| 36 | + ); |
| 37 | + } |
| 38 | + |
| 39 | + constructor( |
| 40 | + oneIndent: string, |
| 41 | + modelOptions: TextModelResolvedOptions, |
| 42 | + configuration: IConfiguration, |
| 43 | + modeConfiguration: IModeConfiguration |
| 44 | + ) { |
| 45 | + let c = configuration.editor; |
| 46 | + |
| 47 | + this.tabSize = modelOptions.tabSize; |
| 48 | + this.insertSpaces = modelOptions.insertSpaces; |
| 49 | + this.oneIndent = oneIndent; |
| 50 | + this.pageSize = Math.floor(c.layoutInfo.height / c.fontInfo.lineHeight) - 2; |
| 51 | + this.useTabStops = c.useTabStops; |
| 52 | + this.wordSeparators = c.wordSeparators; |
| 53 | + this.autoClosingBrackets = c.autoClosingBrackets; |
| 54 | + this.autoClosingPairsOpen = modeConfiguration.autoClosingPairsOpen; |
| 55 | + } |
| 56 | + |
| 57 | + public normalizeIndentation(str: string): string { |
| 58 | + return TextModel.normalizeIndentation(str, this.tabSize, this.insertSpaces); |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +export interface ICursorSimpleModel { |
| 63 | + getLineCount(): number; |
| 64 | + getLineContent(lineNumber: number): string; |
| 65 | + getLineMinColumn(lineNumber: number): number; |
| 66 | + getLineMaxColumn(lineNumber: number): number; |
| 67 | + getLineFirstNonWhitespaceColumn(lineNumber: number): number; |
| 68 | + getLineLastNonWhitespaceColumn(lineNumber: number): number; |
| 69 | +} |
| 70 | + |
| 71 | +/** |
| 72 | + * Common operations that work and make sense both on the model and on the view model. |
| 73 | + */ |
| 74 | +export class CursorColumns { |
| 75 | + |
| 76 | + public static isLowSurrogate(model: ICursorSimpleModel, lineNumber: number, charOffset: number): boolean { |
| 77 | + let lineContent = model.getLineContent(lineNumber); |
| 78 | + if (charOffset < 0 || charOffset >= lineContent.length) { |
| 79 | + return false; |
| 80 | + } |
| 81 | + return strings.isLowSurrogate(lineContent.charCodeAt(charOffset)); |
| 82 | + } |
| 83 | + |
| 84 | + public static isHighSurrogate(model: ICursorSimpleModel, lineNumber: number, charOffset: number): boolean { |
| 85 | + let lineContent = model.getLineContent(lineNumber); |
| 86 | + if (charOffset < 0 || charOffset >= lineContent.length) { |
| 87 | + return false; |
| 88 | + } |
| 89 | + return strings.isHighSurrogate(lineContent.charCodeAt(charOffset)); |
| 90 | + } |
| 91 | + |
| 92 | + public static isInsideSurrogatePair(model: ICursorSimpleModel, lineNumber: number, column: number): boolean { |
| 93 | + return this.isHighSurrogate(model, lineNumber, column - 2); |
| 94 | + } |
| 95 | + |
| 96 | + public static visibleColumnFromColumn(lineContent: string, column: number, tabSize: number): number { |
| 97 | + let endOffset = lineContent.length; |
| 98 | + if (endOffset > column - 1) { |
| 99 | + endOffset = column - 1; |
| 100 | + } |
| 101 | + |
| 102 | + let result = 0; |
| 103 | + for (let i = 0; i < endOffset; i++) { |
| 104 | + let charCode = lineContent.charCodeAt(i); |
| 105 | + if (charCode === CharCode.Tab) { |
| 106 | + result = this.nextTabStop(result, tabSize); |
| 107 | + } else { |
| 108 | + result = result + 1; |
| 109 | + } |
| 110 | + } |
| 111 | + return result; |
| 112 | + } |
| 113 | + |
| 114 | + public static visibleColumnFromColumn2(config: CursorConfiguration, model: ICursorSimpleModel, position: Position): number { |
| 115 | + return this.visibleColumnFromColumn(model.getLineContent(position.lineNumber), position.column, config.tabSize); |
| 116 | + } |
| 117 | + |
| 118 | + public static columnFromVisibleColumn(lineContent: string, visibleColumn: number, tabSize: number): number { |
| 119 | + if (visibleColumn <= 0) { |
| 120 | + return 1; |
| 121 | + } |
| 122 | + |
| 123 | + const lineLength = lineContent.length; |
| 124 | + |
| 125 | + let beforeVisibleColumn = 0; |
| 126 | + for (let i = 0; i < lineLength; i++) { |
| 127 | + let charCode = lineContent.charCodeAt(i); |
| 128 | + |
| 129 | + let afterVisibleColumn: number; |
| 130 | + if (charCode === CharCode.Tab) { |
| 131 | + afterVisibleColumn = this.nextTabStop(beforeVisibleColumn, tabSize); |
| 132 | + } else { |
| 133 | + afterVisibleColumn = beforeVisibleColumn + 1; |
| 134 | + } |
| 135 | + |
| 136 | + if (afterVisibleColumn >= visibleColumn) { |
| 137 | + let prevDelta = visibleColumn - beforeVisibleColumn; |
| 138 | + let afterDelta = afterVisibleColumn - visibleColumn; |
| 139 | + if (afterDelta < prevDelta) { |
| 140 | + return i + 2; |
| 141 | + } else { |
| 142 | + return i + 1; |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + beforeVisibleColumn = afterVisibleColumn; |
| 147 | + } |
| 148 | + |
| 149 | + // walked the entire string |
| 150 | + return lineLength + 1; |
| 151 | + } |
| 152 | + |
| 153 | + public static columnFromVisibleColumn2(config: CursorConfiguration, model: ICursorSimpleModel, lineNumber: number, visibleColumn: number): number { |
| 154 | + let result = this.columnFromVisibleColumn(model.getLineContent(lineNumber), visibleColumn, config.tabSize); |
| 155 | + |
| 156 | + let minColumn = model.getLineMinColumn(lineNumber); |
| 157 | + if (result < minColumn) { |
| 158 | + return minColumn; |
| 159 | + } |
| 160 | + |
| 161 | + let maxColumn = model.getLineMaxColumn(lineNumber); |
| 162 | + if (result > maxColumn) { |
| 163 | + return maxColumn; |
| 164 | + } |
| 165 | + |
| 166 | + return result; |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * ATTENTION: This works with 0-based columns (as oposed to the regular 1-based columns) |
| 171 | + */ |
| 172 | + public static nextTabStop(visibleColumn: number, tabSize: number): number { |
| 173 | + return visibleColumn + tabSize - visibleColumn % tabSize; |
| 174 | + } |
| 175 | + |
| 176 | + /** |
| 177 | + * ATTENTION: This works with 0-based columns (as oposed to the regular 1-based columns) |
| 178 | + */ |
| 179 | + public static prevTabStop(column: number, tabSize: number): number { |
| 180 | + return column - 1 - (column - 1) % tabSize; |
| 181 | + } |
| 182 | +} |
0 commit comments