Path: blob/main/src/vs/editor/contrib/suggest/browser/wordContextKey.ts
4797 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { IDisposable } from '../../../../base/common/lifecycle.js';6import { ICodeEditor } from '../../../browser/editorBrowser.js';7import { EditorOption } from '../../../common/config/editorOptions.js';8import { IContextKey, IContextKeyService, RawContextKey } from '../../../../platform/contextkey/common/contextkey.js';9import { localize } from '../../../../nls.js';1011export class WordContextKey {1213static readonly AtEnd = new RawContextKey<boolean>('atEndOfWord', false, { type: 'boolean', description: localize('desc', "A context key that is true when at the end of a word. Note that this is only defined when tab-completions are enabled") });1415private readonly _ckAtEnd: IContextKey<boolean>;16private readonly _configListener: IDisposable;1718private _enabled: boolean = false;19private _selectionListener?: IDisposable;2021constructor(22private readonly _editor: ICodeEditor,23@IContextKeyService contextKeyService: IContextKeyService,24) {2526this._ckAtEnd = WordContextKey.AtEnd.bindTo(contextKeyService);27this._configListener = this._editor.onDidChangeConfiguration(e => e.hasChanged(EditorOption.tabCompletion) && this._update());28this._update();29}3031dispose(): void {32this._configListener.dispose();33this._selectionListener?.dispose();34this._ckAtEnd.reset();35}3637private _update(): void {38// only update this when tab completions are enabled39const enabled = this._editor.getOption(EditorOption.tabCompletion) === 'on';40if (this._enabled === enabled) {41return;42}43this._enabled = enabled;4445if (this._enabled) {46const checkForWordEnd = () => {47if (!this._editor.hasModel()) {48this._ckAtEnd.set(false);49return;50}51const model = this._editor.getModel();52const selection = this._editor.getSelection();53const word = model.getWordAtPosition(selection.getStartPosition());54if (!word) {55this._ckAtEnd.set(false);56return;57}58this._ckAtEnd.set(word.endColumn === selection.getStartPosition().column && selection.getStartPosition().lineNumber === selection.getEndPosition().lineNumber);59};60this._selectionListener = this._editor.onDidChangeCursorSelection(checkForWordEnd);61checkForWordEnd();6263} else if (this._selectionListener) {64this._ckAtEnd.reset();65this._selectionListener.dispose();66this._selectionListener = undefined;67}68}69}707172