Skip to content

Commit

Permalink
Add CopilotRelated command (#59963)
Browse files Browse the repository at this point in the history
  • Loading branch information
sandersn authored Sep 26, 2024
1 parent da1fb07 commit 52c59db
Show file tree
Hide file tree
Showing 17 changed files with 298 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/harness/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -800,6 +800,7 @@ export class SessionClient implements LanguageService {
}

mapCode: typeof notImplemented = notImplemented;
getImports: typeof notImplemented = notImplemented;

private createFileLocationOrRangeRequestArgs(positionOrRange: number | TextRange, fileName: string): protocol.FileLocationOrRangeRequestArgs {
return typeof positionOrRange === "number"
Expand Down
22 changes: 22 additions & 0 deletions src/harness/fourslashImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4638,6 +4638,28 @@ ${changes.join("\n// ---\n")}
${after}`;
this.baseline("mapCode", baseline, ".mapCode.ts");
}

public verifyGetImports(fileName: string, expectedImports: string[]): void {
const actualImports = this.languageService.getImports(fileName);
if (actualImports.length !== expectedImports.length) {
throw new Error(`Expected ${expectedImports.length} imports for ${fileName}, got ${actualImports.length}
Expected:
${expectedImports}
Actual:
${actualImports}
`);
}
for (let i = 0; i < expectedImports.length; i++) {
if (actualImports[i] !== expectedImports[i]) {
throw new Error(`Expected at ${fileName} index ${i}: ${expectedImports[i]}, got ${actualImports[i]}
Expected:
${expectedImports}
Actual:
${actualImports}
`);
}
}
}
}

function updateTextRangeForTextChanges({ pos, end }: ts.TextRange, textChanges: readonly ts.TextChange[]): ts.TextRange {
Expand Down
9 changes: 9 additions & 0 deletions src/harness/fourslashInterfaceImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,10 @@ export class VerifyNegatable {
public baselineMapCode(ranges: FourSlash.Range[][], changes: string[] = []): void {
this.state.baselineMapCode(ranges, changes);
}

public getImports(fileName: string, imports: string[]): void {
return this.state.verifyGetImports(fileName, imports);
}
}

export interface CompletionsResult {
Expand Down Expand Up @@ -2047,3 +2051,8 @@ export interface RenameOptions {
readonly providePrefixAndSuffixTextForRename?: boolean;
readonly quotePreference?: "auto" | "double" | "single";
}

export interface VerifyGetImportsOptions {
fileName: string;
imports: string[];
}
13 changes: 13 additions & 0 deletions src/server/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ export const enum CommandTypes {
ProvideInlayHints = "provideInlayHints",
WatchChange = "watchChange",
MapCode = "mapCode",
CopilotRelated = "copilotRelated",
}

/**
Expand Down Expand Up @@ -2406,6 +2407,18 @@ export interface MapCodeResponse extends Response {
body: readonly FileCodeEdits[];
}

export interface CopilotRelatedRequest extends FileRequest {
command: CommandTypes.CopilotRelated;
arguments: FileRequestArgs;
}

export interface CopilotRelatedItems {
relatedFiles: readonly string[];
}

export interface CopilotRelatedResponse extends Response {
body: CopilotRelatedItems;
}
/**
* Synchronous request for semantic diagnostics of one file.
*/
Expand Down
13 changes: 12 additions & 1 deletion src/server/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -937,6 +937,7 @@ const invalidPartialSemanticModeCommands: readonly protocol.CommandTypes[] = [
protocol.CommandTypes.ProvideCallHierarchyIncomingCalls,
protocol.CommandTypes.ProvideCallHierarchyOutgoingCalls,
protocol.CommandTypes.GetPasteEdits,
protocol.CommandTypes.CopilotRelated,
];

const invalidSyntacticModeCommands: readonly protocol.CommandTypes[] = [
Expand Down Expand Up @@ -2030,7 +2031,6 @@ export class Session<TMessage = string> implements EventSender {
};
});
}

private mapCode(args: protocol.MapCodeRequestArgs): protocol.FileCodeEdits[] {
const formatOptions = this.getHostFormatOptions();
const preferences = this.getHostPreferences();
Expand All @@ -2051,6 +2051,14 @@ export class Session<TMessage = string> implements EventSender {
return this.mapTextChangesToCodeEdits(changes);
}

private getCopilotRelatedInfo(args: protocol.FileRequestArgs): protocol.CopilotRelatedItems {
const { file, project } = this.getFileAndProject(args);

return {
relatedFiles: project.getLanguageService().getImports(file),
};
}

private setCompilerOptionsForInferredProjects(args: protocol.SetCompilerOptionsForInferredProjectsArgs): void {
this.projectService.setCompilerOptionsForInferredProjects(args.options, args.projectRootPath);
}
Expand Down Expand Up @@ -3791,6 +3799,9 @@ export class Session<TMessage = string> implements EventSender {
[protocol.CommandTypes.MapCode]: (request: protocol.MapCodeRequest) => {
return this.requiredResponse(this.mapCode(request.arguments));
},
[protocol.CommandTypes.CopilotRelated]: (request: protocol.CopilotRelatedRequest) => {
return this.requiredResponse(this.getCopilotRelatedInfo(request.arguments));
},
}));

public addProtocolHandler(command: string, handler: (request: protocol.Request) => HandlerResponse): void {
Expand Down
16 changes: 16 additions & 0 deletions src/services/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
__String,
ApplicableRefactorInfo,
ApplyCodeActionCommandResult,
arrayFrom,
AssignmentDeclarationKind,
BaseType,
BinaryExpression,
Expand Down Expand Up @@ -233,6 +234,7 @@ import {
Node,
NodeArray,
NodeFlags,
nodeIsSynthesized,
noop,
normalizePath,
normalizeSpans,
Expand Down Expand Up @@ -1602,6 +1604,7 @@ const invalidOperationsInPartialSemanticMode: readonly (keyof LanguageService)[]
"provideInlayHints",
"getSupportedCodeFixes",
"getPasteEdits",
"getImports",
];

const invalidOperationsInSyntacticMode: readonly (keyof LanguageService)[] = [
Expand Down Expand Up @@ -3364,6 +3367,18 @@ export function createLanguageService(
);
}

function getImports(fileName: string): readonly string[] {
synchronizeHostData();
const file = getValidSourceFile(fileName);
let imports: Set<string> | undefined;
for (const specifier of file.imports) {
if (nodeIsSynthesized(specifier)) continue;
const name = program.getResolvedModuleFromModuleSpecifier(specifier, file)?.resolvedModule?.resolvedFileName;
if (name) (imports ??= new Set()).add(name);
}
return imports ? arrayFrom(imports) : emptyArray;
}

const ls: LanguageService = {
dispose,
cleanupSemanticCache,
Expand Down Expand Up @@ -3438,6 +3453,7 @@ export function createLanguageService(
preparePasteEditsForFile,
getPasteEdits,
mapCode,
getImports,
};

switch (languageServiceMode) {
Expand Down
1 change: 1 addition & 0 deletions src/services/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,7 @@ export interface LanguageService {
getSupportedCodeFixes(fileName?: string): readonly string[];

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

dispose(): void;
preparePasteEditsForFile(fileName: string, copiedTextRanges: TextRange[]): boolean;
Expand Down
12 changes: 12 additions & 0 deletions tests/baselines/reference/api/typescript.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ declare namespace ts {
ProvideInlayHints = "provideInlayHints",
WatchChange = "watchChange",
MapCode = "mapCode",
CopilotRelated = "copilotRelated",
}
/**
* A TypeScript Server message
Expand Down Expand Up @@ -1830,6 +1831,16 @@ declare namespace ts {
export interface MapCodeResponse extends Response {
body: readonly FileCodeEdits[];
}
export interface CopilotRelatedRequest extends FileRequest {
command: CommandTypes.CopilotRelated;
arguments: FileRequestArgs;
}
export interface CopilotRelatedItems {
relatedFiles: readonly string[];
}
export interface CopilotRelatedResponse extends Response {
body: CopilotRelatedItems;
}
/**
* Synchronous request for semantic diagnostics of one file.
*/
Expand Down Expand Up @@ -3514,6 +3525,7 @@ declare namespace ts {
private getDocumentHighlights;
private provideInlayHints;
private mapCode;
private getCopilotRelatedInfo;
private setCompilerOptionsForInferredProjects;
private getProjectInfo;
private getProjectInfoWorker;
Expand Down
1 change: 1 addition & 0 deletions tests/cases/fourslash/fourslash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,7 @@ declare namespace FourSlashInterface {
}
}): void;
baselineMapCode(ranges: Range[][], changes: string[]): void;
getImports(fileName: string, imports: string[]): void;
}
class edit {
caretPosition(): Marker;
Expand Down
16 changes: 16 additions & 0 deletions tests/cases/fourslash/getImportsDuplicate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
///<reference path="fourslash.ts"/>

// @Filename: /first.ts
//// export function foo() {
//// return 1;
//// }
//// export function bar() {
//// return 2;
//// }

// @Filename: /index.ts
//// import { foo } from "./first";
//// import { bar } from './first';
//// console.log(foo() + bar())

verify.getImports('/index.ts', ['/first.ts'])
12 changes: 12 additions & 0 deletions tests/cases/fourslash/getImportsDynamic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
///<reference path="fourslash.ts"/>

// @Filename: /first.ts
//// export function foo() {
//// return 1;
//// }
// @Filename: /index.ts
//// let bar: typeof import('./first').foo = function bar() {
//// return 2;
//// }

verify.getImports('/index.ts', ['/first.ts'])
108 changes: 108 additions & 0 deletions tests/cases/fourslash/getImportsJSXFactory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
///<reference path="fourslash.ts"/>

// @strict: true
// @jsx: react-jsx
// @jsxImportSource: preact
// @filename: /node_modules/preact/index.d.ts
//// type Defaultize<Props, Defaults> =
//// // Distribute over unions
//// Props extends any // Make any properties included in Default optional
//// ? Partial<Pick<Props, Extract<keyof Props, keyof Defaults>>> &
//// // Include the remaining properties from Props
//// Pick<Props, Exclude<keyof Props, keyof Defaults>>
//// : never;
//// export namespace JSXInternal {
//// interface HTMLAttributes<T = {}> { }
//// interface SVGAttributes<T = {}> { }
//// type LibraryManagedAttributes<Component, Props> = Component extends {
//// defaultProps: infer Defaults;
//// }
//// ? Defaultize<Props, Defaults>
//// : Props;
////
//// interface IntrinsicAttributes {
//// key?: any;
//// }
////
//// interface Element extends VNode<any> { }
////
//// interface ElementClass extends Component<any, any> { }
////
//// interface ElementAttributesProperty {
//// props: any;
//// }
////
//// interface ElementChildrenAttribute {
//// children: any;
//// }
////
//// interface IntrinsicElements {
//// div: HTMLAttributes;
//// }
//// }
//// export const Fragment: unique symbol;
//// export type ComponentType<T = {}> = {};
//// export type ComponentChild = {};
//// export type ComponentChildren = {};
//// export type VNode<T = {}> = {};
//// export type Attributes = {};
//// export type Component<T = {}, U = {}> = {};
// @filename: /node_modules/preact/jsx-runtime/index.d.ts
//// export { Fragment } from '..';
//// import {
//// ComponentType,
//// ComponentChild,
//// ComponentChildren,
//// VNode,
//// Attributes
//// } from '..';
//// import { JSXInternal } from '..';
////
//// export function jsx(
//// type: string,
//// props: JSXInternal.HTMLAttributes &
//// JSXInternal.SVGAttributes &
//// Record<string, any> & { children?: ComponentChild },
//// key?: string
//// ): VNode<any>;
//// export function jsx<P>(
//// type: ComponentType<P>,
//// props: Attributes & P & { children?: ComponentChild },
//// key?: string
//// ): VNode<any>;
////
////
//// export function jsxs(
//// type: string,
//// props: JSXInternal.HTMLAttributes &
//// JSXInternal.SVGAttributes &
//// Record<string, any> & { children?: ComponentChild[] },
//// key?: string
//// ): VNode<any>;
//// export function jsxs<P>(
//// type: ComponentType<P>,
//// props: Attributes & P & { children?: ComponentChild[] },
//// key?: string
//// ): VNode<any>;
////
////
//// export function jsxDEV(
//// type: string,
//// props: JSXInternal.HTMLAttributes &
//// JSXInternal.SVGAttributes &
//// Record<string, any> & { children?: ComponentChildren },
//// key?: string
//// ): VNode<any>;
//// export function jsxDEV<P>(
//// type: ComponentType<P>,
//// props: Attributes & P & { children?: ComponentChildren },
//// key?: string
//// ): VNode<any>;
////
//// export import JSX = JSXInternal;
////
// @filename: /index.tsx
//// export const Comp = () => <div></div>;

verify.noErrors()
verify.getImports('/index.tsx', [])
12 changes: 12 additions & 0 deletions tests/cases/fourslash/getImportsNone.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
///<reference path="fourslash.ts"/>

// @Filename: /index.ts
//// function foo() {
//// return 1;
//// }
//// function bar() {
//// return 2;
//// }
////

verify.getImports('/index.ts', [])
14 changes: 14 additions & 0 deletions tests/cases/fourslash/getImportsOne.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
///<reference path="fourslash.ts"/>

// @Filename: /first.ts
//// export function foo() {
//// return 1;
//// }
// @Filename: /index.ts
//// import { foo } from "./first";
//// function bar() {
//// return 2;
//// }
////

verify.getImports('/index.ts', ['/first.ts'])
Loading

0 comments on commit 52c59db

Please sign in to comment.