Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/editor/contrib/suggest/browser/wordContextKey.ts
4797 views
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
6
import { IDisposable } from '../../../../base/common/lifecycle.js';
7
import { ICodeEditor } from '../../../browser/editorBrowser.js';
8
import { EditorOption } from '../../../common/config/editorOptions.js';
9
import { IContextKey, IContextKeyService, RawContextKey } from '../../../../platform/contextkey/common/contextkey.js';
10
import { localize } from '../../../../nls.js';
11
12
export class WordContextKey {
13
14
static 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") });
15
16
private readonly _ckAtEnd: IContextKey<boolean>;
17
private readonly _configListener: IDisposable;
18
19
private _enabled: boolean = false;
20
private _selectionListener?: IDisposable;
21
22
constructor(
23
private readonly _editor: ICodeEditor,
24
@IContextKeyService contextKeyService: IContextKeyService,
25
) {
26
27
this._ckAtEnd = WordContextKey.AtEnd.bindTo(contextKeyService);
28
this._configListener = this._editor.onDidChangeConfiguration(e => e.hasChanged(EditorOption.tabCompletion) && this._update());
29
this._update();
30
}
31
32
dispose(): void {
33
this._configListener.dispose();
34
this._selectionListener?.dispose();
35
this._ckAtEnd.reset();
36
}
37
38
private _update(): void {
39
// only update this when tab completions are enabled
40
const enabled = this._editor.getOption(EditorOption.tabCompletion) === 'on';
41
if (this._enabled === enabled) {
42
return;
43
}
44
this._enabled = enabled;
45
46
if (this._enabled) {
47
const checkForWordEnd = () => {
48
if (!this._editor.hasModel()) {
49
this._ckAtEnd.set(false);
50
return;
51
}
52
const model = this._editor.getModel();
53
const selection = this._editor.getSelection();
54
const word = model.getWordAtPosition(selection.getStartPosition());
55
if (!word) {
56
this._ckAtEnd.set(false);
57
return;
58
}
59
this._ckAtEnd.set(word.endColumn === selection.getStartPosition().column && selection.getStartPosition().lineNumber === selection.getEndPosition().lineNumber);
60
};
61
this._selectionListener = this._editor.onDidChangeCursorSelection(checkForWordEnd);
62
checkForWordEnd();
63
64
} else if (this._selectionListener) {
65
this._ckAtEnd.reset();
66
this._selectionListener.dispose();
67
this._selectionListener = undefined;
68
}
69
}
70
}
71
72