提交 340110ff 编写于 作者: A Alex Dima

Review cursor position/selection change listeners (#26730)

上级 00018cc6
......@@ -765,6 +765,7 @@ export interface IValidatedEditorOptions {
readonly dragAndDrop: boolean;
readonly emptySelectionClipboard: boolean;
readonly useTabStops: boolean;
readonly performanceCritical: boolean;
readonly viewInfo: InternalEditorViewOptions;
readonly contribInfo: EditorContribOptions;
......@@ -1344,8 +1345,9 @@ export class EditorOptionsValidator {
wordWrap = _stringSet<'off' | 'on' | 'wordWrapColumn' | 'bounded'>(wordWrap, defaults.wordWrap, ['off', 'on', 'wordWrapColumn', 'bounded']);
}
const performanceCritical = false;
const viewInfo = this._sanitizeViewInfo(opts, defaults.viewInfo);
const contribInfo = this._sanitizeContribInfo(opts, defaults.contribInfo);
const contribInfo = this._sanitizeContribInfo(opts, defaults.contribInfo, performanceCritical);
return {
inDiffEditor: _boolean(opts.inDiffEditor, defaults.inDiffEditor),
......@@ -1367,6 +1369,7 @@ export class EditorOptionsValidator {
dragAndDrop: _boolean(opts.dragAndDrop, defaults.dragAndDrop),
emptySelectionClipboard: _boolean(opts.emptySelectionClipboard, defaults.emptySelectionClipboard),
useTabStops: _boolean(opts.useTabStops, defaults.useTabStops),
performanceCritical: performanceCritical,
viewInfo: viewInfo,
contribInfo: contribInfo,
};
......@@ -1514,7 +1517,7 @@ export class EditorOptionsValidator {
};
}
private static _sanitizeContribInfo(opts: IEditorOptions, defaults: EditorContribOptions): EditorContribOptions {
private static _sanitizeContribInfo(opts: IEditorOptions, defaults: EditorContribOptions, performanceCritical: boolean): EditorContribOptions {
let quickSuggestions: boolean | { other: boolean, comments: boolean, strings: boolean };
if (typeof opts.quickSuggestions === 'object') {
quickSuggestions = { other: true, ...opts.quickSuggestions };
......@@ -1538,12 +1541,12 @@ export class EditorOptionsValidator {
wordBasedSuggestions: _boolean(opts.wordBasedSuggestions, defaults.wordBasedSuggestions),
suggestFontSize: _clampedInt(opts.suggestFontSize, defaults.suggestFontSize, 0, 1000),
suggestLineHeight: _clampedInt(opts.suggestLineHeight, defaults.suggestLineHeight, 0, 1000),
selectionHighlight: _boolean(opts.selectionHighlight, defaults.selectionHighlight),
occurrencesHighlight: _boolean(opts.occurrencesHighlight, defaults.occurrencesHighlight),
codeLens: _boolean(opts.codeLens, defaults.codeLens) && _boolean(opts.referenceInfos, true),
folding: _boolean(opts.folding, defaults.folding),
selectionHighlight: !performanceCritical && _boolean(opts.selectionHighlight, defaults.selectionHighlight),
occurrencesHighlight: !performanceCritical && _boolean(opts.occurrencesHighlight, defaults.occurrencesHighlight),
codeLens: !performanceCritical && _boolean(opts.codeLens, defaults.codeLens) && _boolean(opts.referenceInfos, true),
folding: !performanceCritical && _boolean(opts.folding, defaults.folding),
showFoldingControls: _stringSet<'always' | 'mouseover'>(opts.showFoldingControls, defaults.showFoldingControls, ['always', 'mouseover']),
matchBrackets: _boolean(opts.matchBrackets, defaults.matchBrackets),
matchBrackets: !performanceCritical && _boolean(opts.matchBrackets, defaults.matchBrackets),
};
}
}
......@@ -1879,6 +1882,7 @@ export const EDITOR_DEFAULTS: IValidatedEditorOptions = {
dragAndDrop: false,
emptySelectionClipboard: true,
useTabStops: true,
performanceCritical: false,
viewInfo: {
extraEditorClassName: '',
......
......@@ -82,7 +82,16 @@ export class BracketMatchingController extends Disposable implements editorCommo
this._matchBrackets = this._editor.getConfiguration().contribInfo.matchBrackets;
this._updateBracketsSoon.schedule();
this._register(editor.onDidChangeCursorPosition((e) => this._updateBracketsSoon.schedule()));
this._register(editor.onDidChangeCursorPosition((e) => {
if (!this._matchBrackets) {
// Early exit if nothing needs to be done!
// Leave some form of early exit check here if you wish to continue being a cursor position change listener ;)
return;
}
this._updateBracketsSoon.schedule();
}));
this._register(editor.onDidChangeModel((e) => { this._decorations = []; this._updateBracketsSoon.schedule(); }));
this._register(editor.onDidChangeConfiguration((e) => {
this._matchBrackets = this._editor.getConfiguration().contribInfo.matchBrackets;
......
......@@ -928,6 +928,7 @@ export class SelectionHighlighter extends Disposable implements editorCommon.IEd
private static ID = 'editor.contrib.selectionHighlighter';
private editor: editorCommon.ICommonCodeEditor;
private _isEnabled: boolean;
private decorations: string[];
private updateSoon: RunOnceScheduler;
private state: SelectionHighlighterState;
......@@ -935,11 +936,22 @@ export class SelectionHighlighter extends Disposable implements editorCommon.IEd
constructor(editor: editorCommon.ICommonCodeEditor) {
super();
this.editor = editor;
this._isEnabled = editor.getConfiguration().contribInfo.selectionHighlight;
this.decorations = [];
this.updateSoon = this._register(new RunOnceScheduler(() => this._update(), 300));
this.state = null;
this._register(editor.onDidChangeConfiguration((e) => {
this._isEnabled = editor.getConfiguration().contribInfo.selectionHighlight;
}));
this._register(editor.onDidChangeCursorSelection((e: ICursorSelectionChangedEvent) => {
if (!this._isEnabled) {
// Early exit if nothing needs to be done!
// Leave some form of early exit check here if you wish to continue being a cursor position change listener ;)
return;
}
if (e.selection.isEmpty()) {
if (e.reason === CursorChangeReason.Explicit) {
if (this.state && (!this.state.lastWordUnderCursor || !this.state.lastWordUnderCursor.containsPosition(e.selection.getStartPosition()))) {
......@@ -968,10 +980,10 @@ export class SelectionHighlighter extends Disposable implements editorCommon.IEd
}
private _update(): void {
this._setState(SelectionHighlighter._createState(this.editor));
this._setState(SelectionHighlighter._createState(this._isEnabled, this.editor));
}
private static _createState(editor: editorCommon.ICommonCodeEditor): SelectionHighlighterState {
private static _createState(isEnabled: boolean, editor: editorCommon.ICommonCodeEditor): SelectionHighlighterState {
const model = editor.getModel();
if (!model) {
return null;
......@@ -980,7 +992,7 @@ export class SelectionHighlighter extends Disposable implements editorCommon.IEd
const config = editor.getConfiguration();
let lastWordUnderCursor: Selection = null;
if (!config.contribInfo.selectionHighlight) {
if (!isEnabled) {
return null;
}
......
......@@ -219,7 +219,16 @@ export class FoldingController implements IFoldingController {
this.localToDispose.push(this.cursorChangedScheduler);
this.localToDispose.push(this.editor.onDidChangeModelContent(e => this.contentChangedScheduler.schedule()));
this.localToDispose.push(this.editor.onDidChangeCursorPosition(e => this.cursorChangedScheduler.schedule()));
this.localToDispose.push(this.editor.onDidChangeCursorPosition((e) => {
if (!this._isEnabled) {
// Early exit if nothing needs to be done!
// Leave some form of early exit check here if you wish to continue being a cursor position change listener ;)
return;
}
this.cursorChangedScheduler.schedule();
}));
this.localToDispose.push(this.editor.onMouseDown(e => this.onEditorMouseDown(e)));
this.localToDispose.push(this.editor.onMouseUp(e => this.onEditorMouseUp(e)));
......
......@@ -248,6 +248,12 @@ export class SuggestModel implements IDisposable {
|| e.source !== 'keyboard'
|| e.reason !== CursorChangeReason.NotSet) {
if (this._state === State.Idle) {
// Early exit if nothing needs to be done!
// Leave some form of early exit check here if you wish to continue being a cursor position change listener ;)
return;
}
this.cancel();
return;
}
......
......@@ -78,6 +78,13 @@ class WordHighlighter {
this.model = this.editor.getModel();
this.toUnhook = [];
this.toUnhook.push(editor.onDidChangeCursorPosition((e: ICursorPositionChangedEvent) => {
if (!this.occurrencesHighlight) {
// Early exit if nothing needs to be done!
// Leave some form of early exit check here if you wish to continue being a cursor position change listener ;)
return;
}
this._onPositionChanged(e);
}));
this.toUnhook.push(editor.onDidChangeModel((e) => {
......
......@@ -61,10 +61,10 @@ export class EditorState {
return true; // always let API source win (e.g. "Go to definition" should add a history entry)
}
const liftedSelection = Selection.liftSelection(this._selection);
const liftedOtherSelection = Selection.liftSelection(other._selection);
const myLineNumber = Math.min(this._selection.selectionStartLineNumber, this._selection.positionLineNumber);
const otherLineNumber = Math.min(other._selection.selectionStartLineNumber, other._selection.positionLineNumber);
if (Math.abs(liftedSelection.getStartPosition().lineNumber - liftedOtherSelection.getStartPosition().lineNumber) < EditorState.EDITOR_SELECTION_THRESHOLD) {
if (Math.abs(myLineNumber - otherLineNumber) < EditorState.EDITOR_SELECTION_THRESHOLD) {
return false; // ignore selection changes in the range of EditorState.EDITOR_SELECTION_THRESHOLD lines
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册