From d9991b25a4079694b55080bc494488a372a5bee7 Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Thu, 25 May 2017 20:48:48 -0700 Subject: [PATCH] Support wide char double click --- src/SelectionManager.ts | 37 ++++++++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/src/SelectionManager.ts b/src/SelectionManager.ts index d7c8b952..11b370dc 100644 --- a/src/SelectionManager.ts +++ b/src/SelectionManager.ts @@ -355,7 +355,7 @@ export class SelectionManager extends EventEmitter { private _convertViewportColToCharacterIndex(coords: [number, number]): number { const line = this._buffer.get(coords[1]); let charIndex = coords[0]; - for (let i = 0; charIndex >= i; i++) { + for (let i = 0; coords[0] >= i; i++) { const char = line[i]; if (char[LINE_DATA_WIDTH_INDEX] === 0) { charIndex--; @@ -378,7 +378,8 @@ export class SelectionManager extends EventEmitter { // Get actual index, taking into consideration wide characters let endIndex = this._convertViewportColToCharacterIndex(coords); let startIndex = endIndex; - let wideCharCount = 0; + let leftWideCharCount = 0; + let rightWideCharCount = 0; console.log('line string: ', line); console.log('initial startIndex: ', startIndex); @@ -400,26 +401,44 @@ export class SelectionManager extends EventEmitter { } // TODO: Expand to all whitespace in block if it's whitespace } else { + let startCol = coords[0]; + let endCol = coords[0]; + // Consider the initial position, skip it and increment the wide char + // variable + if (this._buffer.get(coords[1])[startCol][LINE_DATA_WIDTH_INDEX] === 0) { + leftWideCharCount++; + startCol--; + } + if (this._buffer.get(coords[1])[endCol][LINE_DATA_WIDTH_INDEX] === 2) { + rightWideCharCount++; + endCol++; + } // Expand the string in both directions until a space is hit while (startIndex > 0 && line.charAt(startIndex - 1) !== ' ') { - if (this._buffer.get(coords[1])[startIndex][LINE_DATA_WIDTH_INDEX] === 2) { - wideCharCount++; + if (this._buffer.get(coords[1])[startCol - 1][LINE_DATA_WIDTH_INDEX] === 0) { + leftWideCharCount++; + startCol--; } startIndex--; + startCol--; } while (endIndex < line.length && line.charAt(endIndex + 1) !== ' ') { - if (this._buffer.get(coords[1])[endIndex][LINE_DATA_WIDTH_INDEX] === 2) { - wideCharCount++; + if (this._buffer.get(coords[1])[endCol + 1][LINE_DATA_WIDTH_INDEX] === 2) { + rightWideCharCount++; + endCol++; } endIndex++; + endCol++; } } console.log('charOffset', charOffset); console.log('startIndex', startIndex); console.log('endIndex', endIndex); - console.log('wideCharCount', wideCharCount); - this._model.selectionStart = [startIndex + charOffset, coords[1]]; - this._model.selectionStartLength = endIndex - startIndex + wideCharCount + 1/*include endIndex char*/; + console.log('leftWideCharCount', leftWideCharCount); + console.log('rightWideCharCount', rightWideCharCount); + this._model.selectionStart = [startIndex + charOffset - leftWideCharCount, coords[1]]; + // this._model.selectionStartLength = endIndex - startIndex + wideCharCount + 1/*include endIndex char*/; + this._model.selectionStartLength = endIndex - startIndex + leftWideCharCount + rightWideCharCount + 1/*include endIndex char*/; } private _selectLineAt(line: number): void {