Skip to content

Commit 52c59db

Browse files
authored
Add CopilotRelated command (microsoft#59963)
1 parent da1fb07 commit 52c59db

17 files changed

+298
-1
lines changed

src/harness/client.ts

+1
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,7 @@ export class SessionClient implements LanguageService {
800800
}
801801

802802
mapCode: typeof notImplemented = notImplemented;
803+
getImports: typeof notImplemented = notImplemented;
803804

804805
private createFileLocationOrRangeRequestArgs(positionOrRange: number | TextRange, fileName: string): protocol.FileLocationOrRangeRequestArgs {
805806
return typeof positionOrRange === "number"

src/harness/fourslashImpl.ts

+22
Original file line numberDiff line numberDiff line change
@@ -4638,6 +4638,28 @@ ${changes.join("\n// ---\n")}
46384638
${after}`;
46394639
this.baseline("mapCode", baseline, ".mapCode.ts");
46404640
}
4641+
4642+
public verifyGetImports(fileName: string, expectedImports: string[]): void {
4643+
const actualImports = this.languageService.getImports(fileName);
4644+
if (actualImports.length !== expectedImports.length) {
4645+
throw new Error(`Expected ${expectedImports.length} imports for ${fileName}, got ${actualImports.length}
4646+
Expected:
4647+
${expectedImports}
4648+
Actual:
4649+
${actualImports}
4650+
`);
4651+
}
4652+
for (let i = 0; i < expectedImports.length; i++) {
4653+
if (actualImports[i] !== expectedImports[i]) {
4654+
throw new Error(`Expected at ${fileName} index ${i}: ${expectedImports[i]}, got ${actualImports[i]}
4655+
Expected:
4656+
${expectedImports}
4657+
Actual:
4658+
${actualImports}
4659+
`);
4660+
}
4661+
}
4662+
}
46414663
}
46424664

46434665
function updateTextRangeForTextChanges({ pos, end }: ts.TextRange, textChanges: readonly ts.TextChange[]): ts.TextRange {

src/harness/fourslashInterfaceImpl.ts

+9
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,10 @@ export class VerifyNegatable {
257257
public baselineMapCode(ranges: FourSlash.Range[][], changes: string[] = []): void {
258258
this.state.baselineMapCode(ranges, changes);
259259
}
260+
261+
public getImports(fileName: string, imports: string[]): void {
262+
return this.state.verifyGetImports(fileName, imports);
263+
}
260264
}
261265

262266
export interface CompletionsResult {
@@ -2047,3 +2051,8 @@ export interface RenameOptions {
20472051
readonly providePrefixAndSuffixTextForRename?: boolean;
20482052
readonly quotePreference?: "auto" | "double" | "single";
20492053
}
2054+
2055+
export interface VerifyGetImportsOptions {
2056+
fileName: string;
2057+
imports: string[];
2058+
}

src/server/protocol.ts

+13
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ export const enum CommandTypes {
202202
ProvideInlayHints = "provideInlayHints",
203203
WatchChange = "watchChange",
204204
MapCode = "mapCode",
205+
CopilotRelated = "copilotRelated",
205206
}
206207

207208
/**
@@ -2406,6 +2407,18 @@ export interface MapCodeResponse extends Response {
24062407
body: readonly FileCodeEdits[];
24072408
}
24082409

2410+
export interface CopilotRelatedRequest extends FileRequest {
2411+
command: CommandTypes.CopilotRelated;
2412+
arguments: FileRequestArgs;
2413+
}
2414+
2415+
export interface CopilotRelatedItems {
2416+
relatedFiles: readonly string[];
2417+
}
2418+
2419+
export interface CopilotRelatedResponse extends Response {
2420+
body: CopilotRelatedItems;
2421+
}
24092422
/**
24102423
* Synchronous request for semantic diagnostics of one file.
24112424
*/

src/server/session.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -937,6 +937,7 @@ const invalidPartialSemanticModeCommands: readonly protocol.CommandTypes[] = [
937937
protocol.CommandTypes.ProvideCallHierarchyIncomingCalls,
938938
protocol.CommandTypes.ProvideCallHierarchyOutgoingCalls,
939939
protocol.CommandTypes.GetPasteEdits,
940+
protocol.CommandTypes.CopilotRelated,
940941
];
941942

942943
const invalidSyntacticModeCommands: readonly protocol.CommandTypes[] = [
@@ -2030,7 +2031,6 @@ export class Session<TMessage = string> implements EventSender {
20302031
};
20312032
});
20322033
}
2033-
20342034
private mapCode(args: protocol.MapCodeRequestArgs): protocol.FileCodeEdits[] {
20352035
const formatOptions = this.getHostFormatOptions();
20362036
const preferences = this.getHostPreferences();
@@ -2051,6 +2051,14 @@ export class Session<TMessage = string> implements EventSender {
20512051
return this.mapTextChangesToCodeEdits(changes);
20522052
}
20532053

2054+
private getCopilotRelatedInfo(args: protocol.FileRequestArgs): protocol.CopilotRelatedItems {
2055+
const { file, project } = this.getFileAndProject(args);
2056+
2057+
return {
2058+
relatedFiles: project.getLanguageService().getImports(file),
2059+
};
2060+
}
2061+
20542062
private setCompilerOptionsForInferredProjects(args: protocol.SetCompilerOptionsForInferredProjectsArgs): void {
20552063
this.projectService.setCompilerOptionsForInferredProjects(args.options, args.projectRootPath);
20562064
}
@@ -3791,6 +3799,9 @@ export class Session<TMessage = string> implements EventSender {
37913799
[protocol.CommandTypes.MapCode]: (request: protocol.MapCodeRequest) => {
37923800
return this.requiredResponse(this.mapCode(request.arguments));
37933801
},
3802+
[protocol.CommandTypes.CopilotRelated]: (request: protocol.CopilotRelatedRequest) => {
3803+
return this.requiredResponse(this.getCopilotRelatedInfo(request.arguments));
3804+
},
37943805
}));
37953806

37963807
public addProtocolHandler(command: string, handler: (request: protocol.Request) => HandlerResponse): void {

src/services/services.ts

+16
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
__String,
33
ApplicableRefactorInfo,
44
ApplyCodeActionCommandResult,
5+
arrayFrom,
56
AssignmentDeclarationKind,
67
BaseType,
78
BinaryExpression,
@@ -233,6 +234,7 @@ import {
233234
Node,
234235
NodeArray,
235236
NodeFlags,
237+
nodeIsSynthesized,
236238
noop,
237239
normalizePath,
238240
normalizeSpans,
@@ -1602,6 +1604,7 @@ const invalidOperationsInPartialSemanticMode: readonly (keyof LanguageService)[]
16021604
"provideInlayHints",
16031605
"getSupportedCodeFixes",
16041606
"getPasteEdits",
1607+
"getImports",
16051608
];
16061609

16071610
const invalidOperationsInSyntacticMode: readonly (keyof LanguageService)[] = [
@@ -3364,6 +3367,18 @@ export function createLanguageService(
33643367
);
33653368
}
33663369

3370+
function getImports(fileName: string): readonly string[] {
3371+
synchronizeHostData();
3372+
const file = getValidSourceFile(fileName);
3373+
let imports: Set<string> | undefined;
3374+
for (const specifier of file.imports) {
3375+
if (nodeIsSynthesized(specifier)) continue;
3376+
const name = program.getResolvedModuleFromModuleSpecifier(specifier, file)?.resolvedModule?.resolvedFileName;
3377+
if (name) (imports ??= new Set()).add(name);
3378+
}
3379+
return imports ? arrayFrom(imports) : emptyArray;
3380+
}
3381+
33673382
const ls: LanguageService = {
33683383
dispose,
33693384
cleanupSemanticCache,
@@ -3438,6 +3453,7 @@ export function createLanguageService(
34383453
preparePasteEditsForFile,
34393454
getPasteEdits,
34403455
mapCode,
3456+
getImports,
34413457
};
34423458

34433459
switch (languageServiceMode) {

src/services/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,7 @@ export interface LanguageService {
697697
getSupportedCodeFixes(fileName?: string): readonly string[];
698698

699699
/** @internal */ mapCode(fileName: string, contents: string[], focusLocations: TextSpan[][] | undefined, formatOptions: FormatCodeSettings, preferences: UserPreferences): readonly FileTextChanges[];
700+
/** @internal */ getImports(fileName: string): readonly string[];
700701

701702
dispose(): void;
702703
preparePasteEditsForFile(fileName: string, copiedTextRanges: TextRange[]): boolean;

tests/baselines/reference/api/typescript.d.ts

+12
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ declare namespace ts {
123123
ProvideInlayHints = "provideInlayHints",
124124
WatchChange = "watchChange",
125125
MapCode = "mapCode",
126+
CopilotRelated = "copilotRelated",
126127
}
127128
/**
128129
* A TypeScript Server message
@@ -1830,6 +1831,16 @@ declare namespace ts {
18301831
export interface MapCodeResponse extends Response {
18311832
body: readonly FileCodeEdits[];
18321833
}
1834+
export interface CopilotRelatedRequest extends FileRequest {
1835+
command: CommandTypes.CopilotRelated;
1836+
arguments: FileRequestArgs;
1837+
}
1838+
export interface CopilotRelatedItems {
1839+
relatedFiles: readonly string[];
1840+
}
1841+
export interface CopilotRelatedResponse extends Response {
1842+
body: CopilotRelatedItems;
1843+
}
18331844
/**
18341845
* Synchronous request for semantic diagnostics of one file.
18351846
*/
@@ -3514,6 +3525,7 @@ declare namespace ts {
35143525
private getDocumentHighlights;
35153526
private provideInlayHints;
35163527
private mapCode;
3528+
private getCopilotRelatedInfo;
35173529
private setCompilerOptionsForInferredProjects;
35183530
private getProjectInfo;
35193531
private getProjectInfoWorker;

tests/cases/fourslash/fourslash.ts

+1
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,7 @@ declare namespace FourSlashInterface {
472472
}
473473
}): void;
474474
baselineMapCode(ranges: Range[][], changes: string[]): void;
475+
getImports(fileName: string, imports: string[]): void;
475476
}
476477
class edit {
477478
caretPosition(): Marker;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
///<reference path="fourslash.ts"/>
2+
3+
// @Filename: /first.ts
4+
//// export function foo() {
5+
//// return 1;
6+
//// }
7+
//// export function bar() {
8+
//// return 2;
9+
//// }
10+
11+
// @Filename: /index.ts
12+
//// import { foo } from "./first";
13+
//// import { bar } from './first';
14+
//// console.log(foo() + bar())
15+
16+
verify.getImports('/index.ts', ['/first.ts'])
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
///<reference path="fourslash.ts"/>
2+
3+
// @Filename: /first.ts
4+
//// export function foo() {
5+
//// return 1;
6+
//// }
7+
// @Filename: /index.ts
8+
//// let bar: typeof import('./first').foo = function bar() {
9+
//// return 2;
10+
//// }
11+
12+
verify.getImports('/index.ts', ['/first.ts'])
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
///<reference path="fourslash.ts"/>
2+
3+
// @strict: true
4+
// @jsx: react-jsx
5+
// @jsxImportSource: preact
6+
// @filename: /node_modules/preact/index.d.ts
7+
//// type Defaultize<Props, Defaults> =
8+
//// // Distribute over unions
9+
//// Props extends any // Make any properties included in Default optional
10+
//// ? Partial<Pick<Props, Extract<keyof Props, keyof Defaults>>> &
11+
//// // Include the remaining properties from Props
12+
//// Pick<Props, Exclude<keyof Props, keyof Defaults>>
13+
//// : never;
14+
//// export namespace JSXInternal {
15+
//// interface HTMLAttributes<T = {}> { }
16+
//// interface SVGAttributes<T = {}> { }
17+
//// type LibraryManagedAttributes<Component, Props> = Component extends {
18+
//// defaultProps: infer Defaults;
19+
//// }
20+
//// ? Defaultize<Props, Defaults>
21+
//// : Props;
22+
////
23+
//// interface IntrinsicAttributes {
24+
//// key?: any;
25+
//// }
26+
////
27+
//// interface Element extends VNode<any> { }
28+
////
29+
//// interface ElementClass extends Component<any, any> { }
30+
////
31+
//// interface ElementAttributesProperty {
32+
//// props: any;
33+
//// }
34+
////
35+
//// interface ElementChildrenAttribute {
36+
//// children: any;
37+
//// }
38+
////
39+
//// interface IntrinsicElements {
40+
//// div: HTMLAttributes;
41+
//// }
42+
//// }
43+
//// export const Fragment: unique symbol;
44+
//// export type ComponentType<T = {}> = {};
45+
//// export type ComponentChild = {};
46+
//// export type ComponentChildren = {};
47+
//// export type VNode<T = {}> = {};
48+
//// export type Attributes = {};
49+
//// export type Component<T = {}, U = {}> = {};
50+
// @filename: /node_modules/preact/jsx-runtime/index.d.ts
51+
//// export { Fragment } from '..';
52+
//// import {
53+
//// ComponentType,
54+
//// ComponentChild,
55+
//// ComponentChildren,
56+
//// VNode,
57+
//// Attributes
58+
//// } from '..';
59+
//// import { JSXInternal } from '..';
60+
////
61+
//// export function jsx(
62+
//// type: string,
63+
//// props: JSXInternal.HTMLAttributes &
64+
//// JSXInternal.SVGAttributes &
65+
//// Record<string, any> & { children?: ComponentChild },
66+
//// key?: string
67+
//// ): VNode<any>;
68+
//// export function jsx<P>(
69+
//// type: ComponentType<P>,
70+
//// props: Attributes & P & { children?: ComponentChild },
71+
//// key?: string
72+
//// ): VNode<any>;
73+
////
74+
////
75+
//// export function jsxs(
76+
//// type: string,
77+
//// props: JSXInternal.HTMLAttributes &
78+
//// JSXInternal.SVGAttributes &
79+
//// Record<string, any> & { children?: ComponentChild[] },
80+
//// key?: string
81+
//// ): VNode<any>;
82+
//// export function jsxs<P>(
83+
//// type: ComponentType<P>,
84+
//// props: Attributes & P & { children?: ComponentChild[] },
85+
//// key?: string
86+
//// ): VNode<any>;
87+
////
88+
////
89+
//// export function jsxDEV(
90+
//// type: string,
91+
//// props: JSXInternal.HTMLAttributes &
92+
//// JSXInternal.SVGAttributes &
93+
//// Record<string, any> & { children?: ComponentChildren },
94+
//// key?: string
95+
//// ): VNode<any>;
96+
//// export function jsxDEV<P>(
97+
//// type: ComponentType<P>,
98+
//// props: Attributes & P & { children?: ComponentChildren },
99+
//// key?: string
100+
//// ): VNode<any>;
101+
////
102+
//// export import JSX = JSXInternal;
103+
////
104+
// @filename: /index.tsx
105+
//// export const Comp = () => <div></div>;
106+
107+
verify.noErrors()
108+
verify.getImports('/index.tsx', [])
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
///<reference path="fourslash.ts"/>
2+
3+
// @Filename: /index.ts
4+
//// function foo() {
5+
//// return 1;
6+
//// }
7+
//// function bar() {
8+
//// return 2;
9+
//// }
10+
////
11+
12+
verify.getImports('/index.ts', [])
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
///<reference path="fourslash.ts"/>
2+
3+
// @Filename: /first.ts
4+
//// export function foo() {
5+
//// return 1;
6+
//// }
7+
// @Filename: /index.ts
8+
//// import { foo } from "./first";
9+
//// function bar() {
10+
//// return 2;
11+
//// }
12+
////
13+
14+
verify.getImports('/index.ts', ['/first.ts'])

0 commit comments

Comments
 (0)