Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/platform/languageServer/common/languageContextService.ts
13401 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 type * as vscode from 'vscode';
7
import { createServiceIdentifier } from '../../../util/common/services';
8
9
export const ILanguageContextService = createServiceIdentifier<ILanguageContextService>('ILanguageContextService');
10
11
export enum ContextKind {
12
Snippet = 'snippet',
13
Trait = 'trait',
14
DiagnosticBag = 'diagnosticBag'
15
}
16
17
/**
18
* A context item represents a piece of code usually taken
19
* from a source file.
20
*/
21
export interface SnippetContext {
22
/**
23
* The kind of the context.
24
*/
25
kind: ContextKind.Snippet;
26
27
/**
28
* A unique ID for the context item, used to provide
29
* detailed statistics about the item's usage. If an ID
30
* is not provided, it will be generated randomly.
31
*/
32
id?: string;
33
34
/**
35
* The priority of the snippet. Value range is [0, 1].
36
*/
37
priority: number;
38
39
/**
40
* The main source the snippet is extracted from.
41
*/
42
uri: vscode.Uri;
43
44
/**
45
* Additional sources if available.
46
*/
47
additionalUris?: vscode.Uri[];
48
49
/**
50
* The actual snippet value.
51
*/
52
value: string;
53
}
54
55
export interface TraitContext {
56
/**
57
* The kind of the context.
58
*/
59
kind: ContextKind.Trait;
60
61
/**
62
* A unique ID for the context item, used to provide
63
* detailed statistics about the item's usage. If an ID
64
* is not provided, it will be generated randomly.
65
*/
66
id?: string;
67
68
/**
69
* The priority of the context.
70
*/
71
priority: number;
72
73
/**
74
* The name of the trait.
75
*/
76
name: string;
77
78
/**
79
* The value of the trait.
80
*/
81
value: string;
82
}
83
84
export interface DiagnosticBagContext {
85
/**
86
* The kind of the context.
87
*/
88
kind: ContextKind.DiagnosticBag;
89
90
/**
91
* A unique ID for the context item, used to provide
92
* detailed statistics about the item's usage. If an ID
93
* is not provided, it will be generated randomly.
94
*/
95
id?: string;
96
97
/**
98
* The priority of the context.
99
*/
100
priority: number;
101
102
/**
103
* The resource the diagnostics are associated with.
104
*/
105
uri: vscode.Uri;
106
107
/**
108
* The diagnostics.
109
*/
110
values: vscode.Diagnostic[];
111
}
112
113
export type ContextItem = SnippetContext | TraitContext | DiagnosticBagContext;
114
115
export enum KnownSources {
116
unknown = 'unknown',
117
sideCar = 'sideCar',
118
completion = 'completion',
119
populateCache = 'populateCache',
120
nes = 'nes',
121
chat = 'chat',
122
fix = 'fix'
123
}
124
125
export enum TriggerKind {
126
unknown = 'unknown',
127
selection = 'selection',
128
completion = 'completion',
129
}
130
131
export type RequestContext = {
132
/**
133
* A unique request id.
134
*/
135
requestId: string;
136
137
/**
138
* The opportunity ID provided by VS Code core.
139
*/
140
opportunityId?: string;
141
142
/**
143
* The time budget in milliseconds to compute the context.
144
*/
145
timeBudget?: number;
146
147
/**
148
* The token budget to compute the context.
149
*/
150
tokenBudget?: number;
151
152
/**
153
* The source of the request.
154
*/
155
source?: KnownSources | string;
156
157
/**
158
* The
159
*/
160
trigger?: TriggerKind | undefined;
161
162
/**
163
* A list of proposed edits that should be applied before computing the context.
164
*/
165
proposedEdits?: { edit: vscode.TextEdit; source?: 'selectedCompletionInfo' }[];
166
167
/**
168
* If provided the telemetry will be sampled. A value of 1 will log every request, a value of
169
* 5 will log every 5th request, a value of 10 will log every 10th request, etc. If not provided
170
* all telemetry will be logged.
171
*/
172
sampleTelemetry?: number;
173
};
174
175
export interface ILanguageContextService {
176
readonly _serviceBrand: undefined;
177
178
/**
179
* Checks whether is language server context is activated for the
180
* given text document or language.
181
*/
182
isActivated(documentOrLanguageId: vscode.TextDocument | string): Promise<boolean>;
183
184
/**
185
* Populates the cache with context information for the given document and position.
186
*
187
* @param document The document to populate the cache for.
188
* @param position The position in the document to populate the cache for.
189
* @param context The context for the request.
190
*/
191
populateCache(document: vscode.TextDocument, position: vscode.Position, context: RequestContext): Promise<void>;
192
193
/**
194
* Retrieves the context for the given document and position.
195
*
196
* @param document The document to retrieve the context for.
197
* @param position The position in the document to retrieve the context for.
198
* @param context The context for the request.
199
* @param token A cancellation token.
200
* @returns A promise that resolves to an array of context items.
201
*/
202
getContext(document: vscode.TextDocument, position: vscode.Position, context: RequestContext, token: vscode.CancellationToken): AsyncIterable<ContextItem>;
203
204
/**
205
* Retrieves the context for the given document and position when a request timeout is reached.
206
* This method is called when the `getContext` request takes too long to complete. Items
207
* returned by this method are usually coming from a cache and might be not a 100% accurate.
208
*
209
* This method is optional and should complete execution quickly, ideally without any
210
* block or long-running operations.
211
*
212
* @param document The document to retrieve the context for.
213
* @param position The position in the document to retrieve the context for.
214
* @param context The context for the request.
215
* @returns An array of `ContextItem` or `undefined`.
216
*/
217
getContextOnTimeout(document: vscode.TextDocument, position: vscode.Position, context: RequestContext): readonly ContextItem[] | undefined;
218
}
219
220
class EmptyAsyncIterable<T> implements AsyncIterable<T> {
221
public async *[Symbol.asyncIterator](): AsyncIterator<T> {
222
}
223
}
224
export const NullLanguageContextService: ILanguageContextService = {
225
_serviceBrand: undefined,
226
isActivated: async () => false,
227
populateCache: async () => { },
228
getContext: () => new EmptyAsyncIterable<ContextItem>(),
229
getContextOnTimeout: () => [],
230
};
231