Skip to content

Commit c7c1717

Browse files
pingrenbpasero
andauthored
Write selection text to hidden textarea on macOS (microsoft#156717)
* Write selection text to hidden textarea on macOS Fixes microsoft#27799 * use `getValueLengthInRange` to avoid large allocations Co-authored-by: Benjamin Pasero <[email protected]>
1 parent f073778 commit c7c1717

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

src/vs/editor/browser/controller/textAreaHandler.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,9 @@ export class TextAreaHandler extends ViewPart {
195195
},
196196
getValueInRange: (range: Range, eol: EndOfLinePreference): string => {
197197
return this._context.viewModel.getValueInRange(range, eol);
198+
},
199+
getValueLengthInRange: (range: Range): number => {
200+
return this._context.viewModel.model.getValueLengthInRange(range);
198201
}
199202
};
200203

@@ -242,6 +245,15 @@ export class TextAreaHandler extends ViewPart {
242245
return new TextAreaState(textBefore, textBefore.length, textBefore.length, position, position);
243246
}
244247
}
248+
// on macOS, write current selection into textarea will allow system text services pick selected text,
249+
// but we still want to limit the amount of text given Chromium handles very poorly text even of a few
250+
// thousand chars
251+
// (https://github.com/microsoft/vscode/issues/27799)
252+
const LIMIT_CHARS = 500;
253+
if (platform.isMacintosh && !selection.isEmpty() && simpleModel.getValueLengthInRange(selection) < LIMIT_CHARS) {
254+
const text = simpleModel.getValueInRange(selection, EndOfLinePreference.TextDefined);
255+
return new TextAreaState(text, 0, text.length, selection.getStartPosition(), selection.getEndPosition());
256+
}
245257

246258
// on Safari, document.execCommand('cut') and document.execCommand('copy') will just not work
247259
// if the textarea has no content selected. So if there is an editor selection, ensure something

src/vs/editor/browser/controller/textAreaState.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export interface ISimpleModel {
2323
getLineCount(): number;
2424
getLineMaxColumn(lineNumber: number): number;
2525
getValueInRange(range: Range, eol: EndOfLinePreference): string;
26+
getValueLengthInRange(range: Range): number;
2627
}
2728

2829
export interface ITypeData {

src/vs/editor/test/browser/controller/imeTester.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ class SingleLineTestModel implements ISimpleModel {
3434
return this._line.substring(range.startColumn - 1, range.endColumn - 1);
3535
}
3636

37+
getValueLengthInRange(range: Range): number {
38+
return this.getValueInRange(range, EndOfLinePreference.TextDefined).length;
39+
}
40+
3741
getModelLineContent(lineNumber: number): string {
3842
return this._line;
3943
}

0 commit comments

Comments
 (0)